Skip to content

Commit 7370226

Browse files
authored
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.
1 parent 2a00da2 commit 7370226

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

lib/response.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,19 @@ res.send = function send(body) {
209209
// freshness
210210
if (req.fresh) this.statusCode = 304;
211211

212-
// strip irrelevant headers
213-
if (204 === this.statusCode || 304 === this.statusCode) {
214-
this.removeHeader('Content-Type');
215-
this.removeHeader('Content-Length');
216-
this.removeHeader('Transfer-Encoding');
217-
chunk = '';
218-
}
219-
220212
// alter headers for 205
221213
if (this.statusCode === 205) {
222214
this.set('Content-Length', '0')
223215
this.removeHeader('Transfer-Encoding')
224216
chunk = ''
225217
}
226218

227-
if (req.method === 'HEAD') {
228-
// skip body for HEAD
219+
if (
220+
req.method === 'HEAD' ||
221+
204 === this.statusCode ||
222+
304 === this.statusCode
223+
) {
224+
// skip body
229225
this.end();
230226
} else {
231227
// respond

0 commit comments

Comments
 (0)