Handle the chunked http requests.

The fix is based on Daniel Graña's commit which has not been merged into mainline.
It should fix issues 252 and 367.