mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
fixup! http: add optimizeEmptyRequests server option
This commit is contained in:
4 files changed
+17
-16
No files matched your search
+3
-4
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
+2
-12
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user