Search in sources :

Example 1 with DefaultHttpRequestParser

use of org.apache.http.impl.io.DefaultHttpRequestParser in project hadoop-pcap by RIPE-NCC.

the class HttpPcapReader method processPacketPayload.

@Override
protected void processPacketPayload(Packet packet, final byte[] payload) {
    String protocol = (String) packet.get(Packet.PROTOCOL);
    if (!PcapReader.PROTOCOL_TCP.equals(protocol))
        return;
    HttpPacket httpPacket = (HttpPacket) packet;
    Integer srcPort = (Integer) packet.get(Packet.SRC_PORT);
    Integer dstPort = (Integer) packet.get(Packet.DST_PORT);
    if ((HTTP_PORT == srcPort || HTTP_PORT == dstPort) && packet.containsKey(Packet.REASSEMBLED_TCP_FRAGMENTS)) {
        final SessionInputBuffer inBuf = new AbstractSessionInputBuffer() {

            {
                init(new ByteArrayInputStream(payload), 1024, params);
            }

            @Override
            public boolean isDataAvailable(int timeout) throws IOException {
                return true;
            }
        };
        final SessionOutputBuffer outBuf = new AbstractSessionOutputBuffer() {
        };
        if (HTTP_PORT == srcPort) {
            HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inBuf, null, respFactory, params);
            HttpClientConnection conn = new DefaultClientConnection() {

                {
                    init(inBuf, outBuf, params);
                }

                @Override
                protected void assertNotOpen() {
                }

                @Override
                protected void assertOpen() {
                }
            };
            try {
                HttpResponse response = parser.parse();
                conn.receiveResponseEntity(response);
                propagateHeaders(httpPacket, response.getAllHeaders());
            } catch (IOException e) {
                LOG.error("IOException when decoding HTTP response", e);
            } catch (HttpException e) {
                LOG.error("HttpException when decoding HTTP response", e);
            }
        } else if (HTTP_PORT == dstPort) {
            HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inBuf, null, reqFactory, params);
            try {
                HttpRequest request = parser.parse();
                propagateHeaders(httpPacket, request.getAllHeaders());
            } catch (IOException e) {
                LOG.error("IOException when decoding HTTP request", e);
            } catch (HttpException e) {
                LOG.error("HttpException when decoding HTTP request", e);
            }
        }
    }
}
Also used : HttpRequest(org.apache.http.HttpRequest) SessionInputBuffer(org.apache.http.io.SessionInputBuffer) AbstractSessionInputBuffer(org.apache.http.impl.io.AbstractSessionInputBuffer) DefaultClientConnection(org.apache.http.impl.conn.DefaultClientConnection) HttpPacket(net.ripe.hadoop.pcap.packet.HttpPacket) HttpClientConnection(org.apache.http.HttpClientConnection) HttpMessageParser(org.apache.http.io.HttpMessageParser) HttpResponse(org.apache.http.HttpResponse) AbstractSessionInputBuffer(org.apache.http.impl.io.AbstractSessionInputBuffer) IOException(java.io.IOException) SessionOutputBuffer(org.apache.http.io.SessionOutputBuffer) AbstractSessionOutputBuffer(org.apache.http.impl.io.AbstractSessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpResponseParser(org.apache.http.impl.io.DefaultHttpResponseParser) AbstractSessionOutputBuffer(org.apache.http.impl.io.AbstractSessionOutputBuffer) HttpException(org.apache.http.HttpException) DefaultHttpRequestParser(org.apache.http.impl.io.DefaultHttpRequestParser)

Example 2 with DefaultHttpRequestParser

use of org.apache.http.impl.io.DefaultHttpRequestParser 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 sourceChannel) {
    boolean sendResponse = false;
    try {
        final int read = doRead(sourceChannel);
        if (read <= 0) {
            logger.debug("Reads: {} ", read);
            return;
        }
        byte[] bytes = bReq.toByteArray();
        if (sslTunnel != null) {
            logger.debug("Send to ssl tunnel, {}, bytes:\n\n {}\n", new String(bytes), Hex.encodeHexString(bytes));
            directTo(sslTunnel);
            return;
        }
        logger.debug("Request in progress is:\n\n{}", new String(bytes));
        if (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(bytes));
            try {
                logger.debug("Passing parsed http request off to response writer.");
                HttpRequest request = requestParser.parse();
                Optional<SpanAdapter> span;
                if (traceManager.isPresent())
                    span = traceManager.get().startServiceRootSpan("httprox-get", contextExtractor(request));
                else
                    span = Optional.empty();
                logger.debug("Request contains {} header: '{}'", ApplicationHeader.authorization.key(), request.getHeaders(ApplicationHeader.authorization.key()));
                writer.setHttpRequest(request);
                writer.setSpan(span);
                sendResponse = true;
            } catch (ConnectionClosedException e) {
                logger.warn("Client closed connection. Aborting proxy request.");
                sendResponse = false;
                sourceChannel.shutdownReads();
            } 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.");
            sourceChannel.resumeReads();
        }
    } catch (final IOException e) {
        writer.setError(e);
        sendResponse = true;
    }
    if (sendResponse) {
        sinkChannel.resumeWrites();
    }
}
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) InterruptedIOException(java.io.InterruptedIOException) 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) SpanAdapter(org.commonjava.o11yphant.trace.spi.adapter.SpanAdapter) HttpException(org.apache.http.HttpException) SessionInputBufferImpl(org.apache.http.impl.io.SessionInputBufferImpl) DefaultHttpRequestParser(org.apache.http.impl.io.DefaultHttpRequestParser)

Example 3 with DefaultHttpRequestParser

use of org.apache.http.impl.io.DefaultHttpRequestParser in project timbuctoo by HuygensING.

the class RequestCommand method parseRequest.

private HttpRequest parseRequest(Element element, Evaluator evaluator, ResultRecorder resultRecorder) {
    String contents = getTextAndRemoveIndent(element);
    contents = replaceVariableReferences(evaluator, contents, resultRecorder);
    SessionInputBufferImpl buffer = new SessionInputBufferImpl(new HttpTransportMetricsImpl(), contents.length());
    buffer.bind(new ByteArrayInputStream(contents.getBytes(StandardCharsets.UTF_8)));
    DefaultHttpRequestParser defaultHttpRequestParser = new DefaultHttpRequestParser(buffer);
    LinkedListMultimap<String, String> queryParameters = LinkedListMultimap.create();
    String method = "";
    String url = "";
    LinkedListMultimap<String, String> headers = LinkedListMultimap.create();
    String body = null;
    String server = null;
    try {
        org.apache.http.HttpRequest httpRequest = defaultHttpRequestParser.parse();
        method = httpRequest.getRequestLine().getMethod();
        url = httpRequest.getRequestLine().getUri();
        if (url.startsWith("#")) {
            url = "" + evaluator.evaluate(url);
        }
        Matcher matcher = Pattern.compile("(https?://[^/]+)(/.*)").matcher(url);
        if (matcher.matches()) {
            server = matcher.group(1);
            url = matcher.group(2);
        }
        if (url.contains("?")) {
            String[] urlAndQueryParameters = url.split("\\?");
            url = urlAndQueryParameters[0];
            for (String queryParameter : urlAndQueryParameters[1].split("&")) {
                String[] parameter = queryParameter.split("=");
                queryParameters.put(parameter[0], parameter[1]);
            }
        }
        for (Header header : httpRequest.getAllHeaders()) {
            headers.put(header.getName(), header.getValue());
        }
        if (buffer.hasBufferedData()) {
            body = "";
            while (buffer.hasBufferedData()) {
                body += (char) buffer.read();
            }
        }
    } catch (IOException | HttpException e) {
        e.printStackTrace();
    }
    return new HttpRequest(method, url, headers, body, server, queryParameters);
}
Also used : Matcher(java.util.regex.Matcher) IOException(java.io.IOException) Header(org.apache.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpTransportMetricsImpl(org.apache.http.impl.io.HttpTransportMetricsImpl) HttpException(org.apache.http.HttpException) SessionInputBufferImpl(org.apache.http.impl.io.SessionInputBufferImpl) DefaultHttpRequestParser(org.apache.http.impl.io.DefaultHttpRequestParser)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 HttpException (org.apache.http.HttpException)3 DefaultHttpRequestParser (org.apache.http.impl.io.DefaultHttpRequestParser)3 HttpRequest (org.apache.http.HttpRequest)2 HttpTransportMetricsImpl (org.apache.http.impl.io.HttpTransportMetricsImpl)2 SessionInputBufferImpl (org.apache.http.impl.io.SessionInputBufferImpl)2 InterruptedIOException (java.io.InterruptedIOException)1 Matcher (java.util.regex.Matcher)1 HttpPacket (net.ripe.hadoop.pcap.packet.HttpPacket)1 ConnectionClosedException (org.apache.http.ConnectionClosedException)1 Header (org.apache.http.Header)1 HttpClientConnection (org.apache.http.HttpClientConnection)1 HttpRequestFactory (org.apache.http.HttpRequestFactory)1 HttpResponse (org.apache.http.HttpResponse)1 MessageConstraints (org.apache.http.config.MessageConstraints)1 DefaultHttpRequestFactory (org.apache.http.impl.DefaultHttpRequestFactory)1 DefaultClientConnection (org.apache.http.impl.conn.DefaultClientConnection)1 AbstractSessionInputBuffer (org.apache.http.impl.io.AbstractSessionInputBuffer)1 AbstractSessionOutputBuffer (org.apache.http.impl.io.AbstractSessionOutputBuffer)1