From 8c4c5fcb61bd0b3bcbf6c8141ce2ea12db10fd01 Mon Sep 17 00:00:00 2001 From: qwerzl Date: Wed, 22 Jan 2025 00:06:26 +0800 Subject: [PATCH] http: return `Content-Length` header for `HEAD`s In the current http library, all responses without body will not return the Content-Length header. Fixes: https://github.com/nodejs/node/issues/56680 --- lib/_http_outgoing.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 23b850d1522c97..acc8ba08e7a577 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -547,7 +547,7 @@ function _storeHeader(firstLine, headers) { } if (!state.contLen && !state.te) { - if (!this._hasBody) { + if (!this._hasBody && this.req.method !== 'HEAD') { // Make sure we don't end the 0\r\n\r\n at the end of the message. this.chunkedEncoding = false; } else if (!this.useChunkedEncodingByDefault) { @@ -932,7 +932,7 @@ function strictContentLength(msg) { return ( msg.strictContentLength && msg._contentLength != null && - msg._hasBody && + (msg._hasBody || msg.req.method === 'HEAD') && !msg._removedContLen && !msg.chunkedEncoding && !msg.hasHeader('transfer-encoding')