views:

140

answers:

2

I am decoding http packets. And I faced a problem that chunk problem. When I get a http packet it has a header and body. When transefer-encoding is chunked I don't know what to do ?

Is there a useful API or class for dechunk the data in JAVA ?

And if someone , experienced about http decoding , please show me a way how to do this ?

+1  A: 

Apache HttpComponents

Oh, and if we are talking about the client side, HttpUrlConnection does this as well.

Maurice Perry
I will offline http decoding( already captured packets ) and I have just a header and a body.So I need an api that gets just header and body and gives me decoded data. Is there any api like this?
CodingForever
+2  A: 

Use a fullworthy HTTP client like Apache HttpComponents Client or just the Java SE provided java.net.URLConnection (mini tutorial here). Both handles it fully transparently and gives you a "normal" InputStream back. HttpClient in turn also comes with a ChunkedInputStream which you just have to decorate your InputStream with.

If you really insist in homegrowing a library for this, then I'd suggest to create a class like ChunkedInputStream extends InputStream and write logic accordingly. You can find more detail how to parse it in this Wikipedia article.

BalusC
Actually I make offline http decoding and I have just header and body of http packet. And I will decode the packet. But I need an api that gets header and body and gives me decoded data. Is there so api ?
CodingForever
The Wikipedia article contains detail how a chunk look like. You can basically just split on CRLF (\r\n). It are the bytes 10 and 13. The first part is then the header which represents the chunk length in hex. The second part is then the chunk data itself. You just collect and concatenate all those chunks. The `ChunkedInputStream` does exactly that.
BalusC
because your answer is right.
CodingForever
Sorry , there are two ChunkedInputStream classfirst: http://jigsaw.w3.org/Doc/Programmer/api/org/w3c/www/http/ChunkedInputStream.htmlsecond: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/ChunkedInputStream.htmlWhich one is right for this ?And Do you have any information about how to use ChunkedInputStream ?
CodingForever
The `ChunkedInputStream` part in my answer is clickable (as all other blueish parts). It's an `InputStream`, you can just decorate another `InputStream` with it. E.g. `InputStream input = new ChunkedInputStream(originalInput);`.
BalusC
Firstly thanks for your answers , they indeed help me. But I have an another question ChunkedInputStream constructor gets SessionInputBuffer( interface )How I will convert the string( chunked body ) to this format ?
CodingForever
Hmm, it has changed as per HttpClient 4.x. Well, either pick the HttpClient 3.x one which takes an `InputStream` or homebrew one. Detemining a chunked body is pretty trivial. Just split on CRLF and do the logic math.
BalusC
ok it is working httpclient 3.x thanks for everything.
CodingForever