您需要在 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;
}