1

I'm intercepting an HTTP request with LittleProxy which uses Netty. However, now I want to intercept a webservice request that apparently uses chunked transfer encoding.

The HTTP header looks like this

Content-Type -> text/xml; charset=UTF-8
Host -> 192.168.56.1:7897
SOAPAction -> "getSymbols"
Transfer-Encoding -> chunked
User-Agent -> Axis2
Via -> 1.1.tvmbp

How can I access the content? I've tried adding httpChunkAggregator to some pipeline in the littleproxy code, but to no use.

Thomas
  • 2,070
  • 3
  • 16
  • 21
  • I guess this is rather related to littleproxy than to netty. How about tagging your question with 'littleproxy' and summon Adam Fisk? – trustin Apr 10 '13 at 07:34

2 Answers2

3

You need to override this two methods in a HttpFiltersSourceAdapter. Return a non-zero buffer size. LittleProxy will automatically aggregate httpRequest and httpContent and wrapped into an AggregatedFullHttpRequest, which allow to cast to httpContent.

@Override
public int getMaximumRequestBufferSizeInBytes() {
    return 1024 * 1024;
}

@Override
public int getMaximumResponseBufferSizeInBytes() {
    return 1024 * 1024 * 2;
}

Then you can clone and read content in HTTP package:

String cloneAndExtractContent(HttpObject httpObject, Charset charset){
    List<Byte> bytes = new ArrayList<Byte>();
    HttpContent httpContent = (HttpContent) httpObject;
    ByteBuf buf = httpContent.content();
    byte[] buffer = new byte[buf.readableBytes()];
    if(buf.readableBytes() > 0) {
        int readerIndex = buf.readerIndex();
        buf.getBytes(readerIndex, buffer);
    }
    for(byte b : buffer){
        bytes.add(b);
    }
    return new String(Bytes.toArray(bytes), charset);
}


@Override
public HttpResponse clientToProxyRequest(HttpObject httpObject) {
     System.out.println("clientToProxyRequest - to -> "+getRequestUrl());
     System.out.println(cloneAndExtractContent(httpObject, StandardCharsets.UTF_8));

     return null;
}


@Override
public HttpObject serverToProxyResponse(HttpObject httpObject)
{
      System.out.println("serverToProxyResponse <- from - "+getRequestUrl());
      System.out.println(cloneAndExtractContent(httpObject, StandardCharsets.UTF_8));

      return httpObject;
}
Roger Chan
  • 1,293
  • 2
  • 11
  • 24
0

You can use an HttpRequestFilter, as follows:

    final HttpProxyServer plain = 
        new DefaultHttpProxyServer(8888, new HttpRequestFilter() {
            @Override
            public void filter(HttpRequest httpRequest) {
                System.out.println("Request went through proxy: "+httpRequest);
            }
        },
        new HttpResponseFilters() {
            public HttpFilter getFilter(String hostAndPort) {
                return null;
            }
        });

That's with LittleProxy 0.5.3. GitHub master is updated to use Netty 4, and the semantics will be a bit different.

adamfisk
  • 578
  • 4
  • 10
  • I'm trying to do this today and the requests are showing up as DefaultHttpContent and DefaultLastHttpContent objects when chunked. These seem to have a completely different API than the regular HttpRequest objects and I'm not sure how to modify them (IE: change the URI in order to redirect traffic). Any pointers? – Alex Jansen May 05 '15 at 01:26