我正在拦截使用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代码中的某个管道中,但没有用。
答案 0 :(得分: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;
}
答案 1 :(得分: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,语义也会有所不同。