Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

LJ1102
Copy link

@LJ1102 LJ1102 commented Nov 16, 2023

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.

@LJ1102
Copy link
Author

LJ1102 commented Nov 17, 2023

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.

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"
@TahsinAyman
Copy link

write clean code

@dougwilson dougwilson added the pr label Jan 24, 2024
Copy link

@Bishal07-glitch Bishal07-glitch left a 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
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants