express
express copied to clipboard
Do not auto-remove content headers for body-less status codes
According to RFC 2616 (HTTP/1.1 spec) a HEAD
request is supposed to return exactly the same entity-headers as a GET
request but no body, ~~imho responding to such a request with a 204 status code seems reasonable, thus we should not remove any headers but only ensure that no body is sent.~~
I did some more digging and in fact RFC 7230 specifies the following:
Transfer-Encoding MAY be sent in a response to a HEAD request or in a 304 (Not Modified) response
A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request
A server MUST NOT send a Content-Length header field in any response with a status code of 1xx (Informational) or 204 (No Content).
So I altered my changes (and adjusted the unit tests) to be compliant to that, so that with a 304 we leave the content-headers untouched, but on 204 we remove them.
write clean code