Search in sources :

Example 1 with MessageConstraints

use of org.apache.http.config.MessageConstraints in project indy by Commonjava.

the class ProxyRequestReader method handleEvent.

// TODO: May need to tune this to preserve request body.
// TODO: NONE of the request headers (except authorization) are passed through!
@Override
public void handleEvent(final ConduitStreamSourceChannel channel) {
    boolean sendResponse = false;
    try {
        final int read = doRead(channel);
        logger.debug("Request in progress is:\n\n'{}'", new String(req.toByteArray()));
        if (read < 0 || headDone) {
            logger.debug("request done. parsing.");
            MessageConstraints mc = MessageConstraints.DEFAULT;
            SessionInputBufferImpl inbuf = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), 1024);
            HttpRequestFactory requestFactory = new DefaultHttpRequestFactory();
            LineParser lp = new BasicLineParser();
            DefaultHttpRequestParser requestParser = new DefaultHttpRequestParser(inbuf, lp, requestFactory, mc);
            inbuf.bind(new ByteArrayInputStream(req.toByteArray()));
            try {
                logger.debug("Passing parsed http request off to response writer.");
                HttpRequest request = requestParser.parse();
                logger.debug("Request contains {} header: '{}'", ApplicationHeader.authorization.key(), request.getHeaders(ApplicationHeader.authorization.key()));
                writer.setHttpRequest(request);
                sendResponse = true;
            } catch (ConnectionClosedException e) {
                logger.warn("Client closed connection. Aborting proxy request.");
                sendResponse = false;
                channel.resumeReads();
            } catch (HttpException e) {
                logger.error("Failed to parse http request: " + e.getMessage(), e);
                writer.setError(e);
            }
        } else {
            logger.debug("request not finished. Pausing until more reads are available.'");
            channel.resumeReads();
        }
    } catch (final IOException e) {
        writer.setError(e);
        sendResponse = true;
    }
    if (sendResponse) {
        sinkChannel.resumeWrites();
        try {
            channel.shutdownReads();
        } catch (final IOException e) {
            logger.debug("failed to shutdown proxy request reads.", e);
        }
    //            IOUtils.closeQuietly( channel );
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) DefaultHttpRequestFactory(org.apache.http.impl.DefaultHttpRequestFactory) HttpRequestFactory(org.apache.http.HttpRequestFactory) ConnectionClosedException(org.apache.http.ConnectionClosedException) BasicLineParser(org.apache.http.message.BasicLineParser) IOException(java.io.IOException) DefaultHttpRequestFactory(org.apache.http.impl.DefaultHttpRequestFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) MessageConstraints(org.apache.http.config.MessageConstraints) HttpTransportMetricsImpl(org.apache.http.impl.io.HttpTransportMetricsImpl) BasicLineParser(org.apache.http.message.BasicLineParser) LineParser(org.apache.http.message.LineParser) HttpException(org.apache.http.HttpException) SessionInputBufferImpl(org.apache.http.impl.io.SessionInputBufferImpl) DefaultHttpRequestParser(org.apache.http.impl.io.DefaultHttpRequestParser)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1 HttpException (org.apache.http.HttpException)1 HttpRequest (org.apache.http.HttpRequest)1 HttpRequestFactory (org.apache.http.HttpRequestFactory)1 MessageConstraints (org.apache.http.config.MessageConstraints)1 DefaultHttpRequestFactory (org.apache.http.impl.DefaultHttpRequestFactory)1 DefaultHttpRequestParser (org.apache.http.impl.io.DefaultHttpRequestParser)1 HttpTransportMetricsImpl (org.apache.http.impl.io.HttpTransportMetricsImpl)1 SessionInputBufferImpl (org.apache.http.impl.io.SessionInputBufferImpl)1 BasicLineParser (org.apache.http.message.BasicLineParser)1 LineParser (org.apache.http.message.LineParser)1