From 7370226d8dfcf022f72879c40a802f0897785837 Mon Sep 17 00:00:00 2001 From: Jan Scheurer Date: Thu, 16 Nov 2023 19:25:50 +0100 Subject: [PATCH] Do not auto-remove content headers for body-less status codes 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. --- lib/response.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/response.js b/lib/response.js index fede486c06..55b3a52c10 100644 --- a/lib/response.js +++ b/lib/response.js @@ -209,14 +209,6 @@ 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') @@ -224,8 +216,12 @@ res.send = function send(body) { 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