expressjs / compression

Node.js compression middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HEAD request should get same headers as a GET request

ptpt opened this issue · comments

According to the definition of a HEAD method https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD

The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method.

However it seems that compression doesn't set Content-Encoding and doesn't remove Content-Length from the response if it is a HEAD request, which behaves differently from a "GET" request.

Hi @ptpt yes, they should be the same, however they do not have to. You can read the HTTP specification regarding how HEAD works in the spec directly https://tools.ietf.org/html/rfc7231#section-4.3.2 . Here is a quote:

The server SHOULD send the same header fields in response to a HEAD request as it would have sent if the request had been a GET, except that the payload header fields (Section 3.3) MAY be omitted.

The issue is that since a HEAD response is not required to send the same header fields as a GET, we use that escape hatch to provided consistent responses. Because the application of compression for this module is dependent on Content-Type and Content-Length, if a server doesn't provide those (since it can omit any payload header fields), then this module would thus have no idea if that would then have been compressed, so will simply never send the header along with HEAD requests to provide that consistency.

Make sense. Thanks for the explanation!