1

我正在使用使用 Netty的LittleProxy拦截 HTTP 请求。但是,现在我想拦截一个显然使用分块传输编码的 Web 服务请求。

HTTP 标头看起来像这样

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

如何访问内容?我尝试将 httpChunkAggregator 添加到 littleproxy 代码中的某个管道中,但没有用。

4

2 回答 2

3

您需要在 HttpFiltersSourceAdapter 中覆盖这两个方法。返回非零缓冲区大小。LittleProxy 会自动聚合 httpRequest 和 httpContent 并包装成一个 AggregatedFullHttpRequest,它允许转换为 httpContent。

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

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

然后你可以克隆和读取 HTTP 包中的内容:

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;
}
于 2015-12-18T02:15:21.843 回答
0

您可以使用 HttpRequestFilter,如下所示:

    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;
            }
        });

LittleProxy 0.5.3 就是这样。GitHub master 更新为使用 Netty 4,语义会有点不同。

于 2013-08-20T21:40:13.307 回答