-
-
Notifications
You must be signed in to change notification settings - Fork 16.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not auto-remove content headers for body-less status codes #5320
base: master
Are you sure you want to change the base?
Conversation
I did some more digging and in fact RFC 7230 specifies the following:
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. |
According to [RFC 2616 (HTTP/1.1 spec)](https://datatracker.ietf.org/doc/html/rfc2616#page-54) a `HEAD` request is supposed to return *exactly* the same entity-headers as a `GET` request would but without body. [RFC 7230](https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.2) further specifies that: "Transfer-Encoding MAY be sent in a response to a HEAD request or in a 304 (Not Modified) response" and "A server MAY send a Content-Length header field in a 304 (Not Modified) response to a conditional GET request"
This comment was marked as off-topic.
This comment was marked as off-topic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// freshness
if (req.fresh) this.statusCode = 304;
// strip irrelevant headers
if (204 === this.statusCode || 304 === this.statusCode) {
// remove content headers for 204
if (this.statusCode === 204) {
this.removeHeader('Content-Type');
this.removeHeader('Content-Length');
this.removeHeader('Transfer-Encoding');
chunk = ''; // Clear the response body
}
// alter headers for 205
if (this.statusCode === 205) {
this.set('Content-Length', '0');
this.removeHeader('Transfer-Encoding');
chunk = ''; // Clear the response body
}
// Handling HEAD request
if (req.method === 'HEAD' || this.statusCode === 204 || this.statusCode === 205 || this.statusCode === 304) {
// Skip sending the body for HEAD request or certain status codes
this.end();
} else {
// Respond with the actual content
}
}
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
I might be misunderstanding, but even if it's allowed to send headers in (304) responses, we still shouldn't send these headers. Since the idea is to minimize transfer in a 304 response. |
According to RFC 2616 (HTTP/1.1 spec) a
HEAD
request is supposed to return exactly the same entity-headers as aGET
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.