我正在拦截使用netty的http请求。
但是,现在我想拦截一个显然使用分块传输编码的webservice请求。
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代码中的一些管道中,但没有用。

最佳答案

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

10-08 02:05