2013-03-25 35 views
0

我正在攔截一個使用Netty的LittleProxy的HTTP請求。 但是,現在我想攔截顯然使用分塊傳輸編碼的web服務請求。Netty - 分塊HTTP請求的訪問內容(LittleProxy)

的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 

如何訪問這些內容?我已經嘗試在littleproxy代碼中的某個管道中添加httpChunkAggregator,但沒有用。

+0

我想這是相當有關,而不是網狀到littleproxy。如何用'littleproxy'標記你的問題並召喚Adam Fisk? – trustin 2013-04-10 07:34:45

回答

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,語義將有所不同。

+0

今天我試圖做到這一點,當分塊時請求顯示爲DefaultHttpContent和DefaultLastHttpContent對象。這些似乎有一個完全不同的API比常規HttpRequest對象,我不知道如何修改它們(IE:更改URI以重定向流量)。任何指針? – 2015-05-05 01:26:19

2

您需要在HttpFiltersSourceAdapter中重寫這兩個方法。返回一個非零的緩衝區大小。 LittleProxy會自動將httpRequest和httpContent聚合在一起幷包裝到一個AggregatedFullHttpRequest中,該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; 
}