Skip to content

Commit

Permalink
Do not auto-remove content headers for body-less status codes
Browse files Browse the repository at this point in the history
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 but no body, imho responding to such a request with a 204 status code seems reasonable (if not ideal), thus we should not remove any headers but only ensure that no body is sent.
  • Loading branch information
LJ1102 committed Nov 16, 2023
1 parent 2a00da2 commit 7370226
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions lib/response.js
Expand Up @@ -209,23 +209,19 @@ res.send = function send(body) {
// freshness
if (req.fresh) this.statusCode = 304;

// strip irrelevant headers
if (204 === this.statusCode || 304 === this.statusCode) {
this.removeHeader('Content-Type');
this.removeHeader('Content-Length');
this.removeHeader('Transfer-Encoding');
chunk = '';
}

// alter headers for 205
if (this.statusCode === 205) {
this.set('Content-Length', '0')
this.removeHeader('Transfer-Encoding')
chunk = ''
}

if (req.method === 'HEAD') {
// skip body for HEAD
if (
req.method === 'HEAD' ||
204 === this.statusCode ||
304 === this.statusCode
) {
// skip body
this.end();
} else {
// respond
Expand Down

0 comments on commit 7370226

Please sign in to comment.