Hi,
I have built an HTTP client in Go which connects to servers that I don't control. It makes a HEAD request and then conditionally a GET request as well. On some servers, the GET request results in the error: malformed HTTP response "0" coming from client.Do(req).
It turns out the server is responding with Transfer-Encoding: Chunked even when responding to HEAD requests. Chunked encoding uses an integer to specify the length of each chunk. In this case, the server is producing a single chunk of zero length, so it just provides a body of "0". Here's what it looks like on the wire:
HEAD /example HTTP/1.1
Accept: */*
HTTP/1.1 200 OK
Date: Tue, 03 May 2016 18:53:21 GMT
Server: Microsoft-IIS/6.0
X-Frame-Options: SAMEORIGIN
X-AspNet-Version: 2.0.50727
Transfer-Encoding: chunked
Cache-Control: private
Content-Type: application/pdf
0
It's that body containing "0" that stays in the channel and screws up the next request that reuses the channel. I'm convinced this is incorrect behaviour on the server's part based on the arguments at:
How should I deal with this? Right now my only way to cope is to disable KeepAlive on the http.Transport. The only other thing I can think of is to patch http/transport.go:1116 where it sets hasBody to false for all HEAD requests. Would such a patch that is more accepting of malformed HEAD responses be accepted? If not, is there any other workaround I can do such as drain the channel between requests or something?
Thanks!
Luke Cyca