grantila / fetch-h2

HTTP/1+2 Fetch API client for Node.js
MIT License
336 stars 16 forks source link

HEAD requests cause "unexpected end of file" from gzip decoder #70

Open colinbendell opened 4 years ago

colinbendell commented 4 years ago

It appears HEAD requests cause the gzip decoder to throw an 'unexpected end of file' error. This is likely because the HEAD has a Content-Length but no body frame.

grantila commented 4 years ago

Is this on HTTP 2?

colinbendell commented 4 years ago

yes. HEAD requests don't have a body, but it appears the use of stream.pipe() in the StreamResponse() constructor. However, when the h2stream is completed, the stream pipe is closed. However, because there is a content-length header from the head response, the Response.setBody() function assumes that there is more stream to decode thus tripping over zlib's internal problems of being able to handle the broken chunks without resorting to createGunzip({ finishFlush: zlib.Z_SYNC_FLUSH }). (see: https://github.com/nodejs/node/blob/e66a2acc4cb9fc09fc32d1833b89ae56468a0931/src/node_zlib.cc#L874 )

There are a few solutions as I see it:

With this in mind, I might suggest adding the createGunzip({ finishFlush: zlib.Z_SYNC_FLUSH }) since this has the least adverse effects and is probably more future proof.

pseudosavant commented 4 years ago

I am experiencing this issue as well. Was there any decision on the path forward for a fix? I'd gladly write it up and send a pull request but I want to make sure I don't go down a dead end.

colinbendell commented 4 years ago

I have a PR for this here: https://github.com/grantila/fetch-h2/pull/72