From b716d0950e364e20740eb07df2ab090db8c87e18 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Tue, 9 Sep 2025 15:48:45 -0300 Subject: [PATCH] fixup! http: add optimizeEmptyRequests server option --- doc/api/http.md | 7 +++---- lib/_http_incoming.js | 9 +++++++++ lib/_http_server.js | 14 ++------------ .../test-http-server-optimize-empty-requests.js | 3 +++ 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index b9c740562ad..ec2b62ea15b 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -3636,10 +3636,9 @@ changes: when writing to an HTTP response which does not have a body. **Default:** `false`. * `optimizeEmptyRequests` {boolean} If set to `true`, requests without `Content-Length` - or `Transfer-Encoding` headers (indicating no body) will have their lifecycle events - immediately emitted. For `HEAD` and `GET` requests, this optimization is applied - regardless of headers. This improves performance for requests without bodies - across all HTTP methods. + or `Transfer-Encoding` headers (indicating no body) will be initialized with an + already-ended body stream, so they will never emit any stream events + (like `data` or `end`). You can use `req.readableEnded` to detect this case. **Default:** `false`. * `requestListener` {Function} diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index c3e901e53e8..6cda3a84cee 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -423,6 +423,15 @@ function _addHeaderLineDistinct(field, value, dest) { } } +IncomingMessage.prototype._dumpAndCloseReadable = function _dumpAndCloseReadable() { + this._dumped = true; + this._readableState.ended = true; + this._readableState.endEmitted = true; + this._readableState.destroyed = true; + this._readableState.closed = true; + this._readableState.closeEmitted = true; +}; + // Call this instead of resume() if we want to just // dump all the data to /dev/null diff --git a/lib/_http_server.js b/lib/_http_server.js index e0bf0ed02ab..9dbae86642a 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -1111,24 +1111,14 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { // Check if we should optimize empty requests (those without Content-Length or Transfer-Encoding headers) const hasBodyHeaders = ('content-length' in req.headers) || ('transfer-encoding' in req.headers); - const isHeadOrGet = (req.method === 'HEAD' || req.method === 'GET'); - - // Apply optimization for all methods when optimizeEmptyRequests is enabled and there are no body headers - // For HEAD and GET methods, we optimize regardless of body headers for backward compatibility - const shouldOptimize = server[kOptimizeEmptyRequests] === true && (isHeadOrGet || !hasBodyHeaders); + const shouldOptimize = server[kOptimizeEmptyRequests] === true && !hasBodyHeaders; if (shouldOptimize) { // Fast processing where request "has" already emitted all lifecycle events. // This avoids a lot of unnecessary overhead otherwise introduced by // stream.Readable life cycle rules. The downside is that this will // break some servers that read bodies for methods that don't have body headers. - req._dumped = true; - req._readableState.ended = true; - req._readableState.endEmitted = true; - req._readableState.destroyed = true; - req._readableState.closed = true; - req._readableState.closeEmitted = true; - + req._dumpAndCloseReadable(); req._read(); } diff --git a/test/parallel/test-http-server-optimize-empty-requests.js b/test/parallel/test-http-server-optimize-empty-requests.js index 6683accb485..c1c070fc12f 100644 --- a/test/parallel/test-http-server-optimize-empty-requests.js +++ b/test/parallel/test-http-server-optimize-empty-requests.js @@ -10,6 +10,9 @@ const server = http.createServer({ optimizeEmptyRequests: true }, (req, res) => { reqs++; + req.on('data', common.mustNotCall()); + req.on('end', common.mustNotCall()); + assert.strictEqual(req._dumped, true); assert.strictEqual(req._readableState.ended, true); assert.strictEqual(req._readableState.endEmitted, true);