Skip to content

Commit 36ec4ee

Browse files
committed
http: optimize IncomingMessage._dump
1 parent 2ea31e5 commit 36ec4ee

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

doc/api/http.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,6 +3569,7 @@ changes:
35693569
- v8.12.0
35703570
pr-url: https://github.com/nodejs/node/pull/15752
35713571
description: The `options` argument is supported now.
3572+
- version: REPLACEME
35723573
-->
35733574
35743575
* `options` {Object}
@@ -3632,6 +3633,10 @@ changes:
36323633
* `rejectNonStandardBodyWrites` {boolean} If set to `true`, an error is thrown
36333634
when writing to an HTTP response which does not have a body.
36343635
**Default:** `false`.
3636+
* `dumpGETBody` {boolean} If set to `true`, request body for `HEAD` and `GET`
3637+
requests will be immediatly dumped and the `end` and `close` events for
3638+
`IncomingMessage` will be emitted before the server emits `'request'`. This
3639+
enables some optimizations that would otherwise not be possible.
36353640
36363641
* `requestListener` {Function}
36373642

lib/_http_server.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const onResponseFinishChannel = dc.channel('http.server.response.finish');
105105

106106
const kServerResponse = Symbol('ServerResponse');
107107
const kServerResponseStatistics = Symbol('ServerResponseStatistics');
108+
const kDumpGet = Symbol('kDumpGet');
108109

109110
const {
110111
hasObserver,
@@ -447,6 +448,8 @@ function storeHTTPOptions(options) {
447448
this[kIncomingMessage] = options.IncomingMessage || IncomingMessage;
448449
this[kServerResponse] = options.ServerResponse || ServerResponse;
449450

451+
this[kDumpGet] = Boolean(options.dumpGETBody ?? globalThis.IS_NODE_TEST);
452+
450453
const maxHeaderSize = options.maxHeaderSize;
451454
if (maxHeaderSize !== undefined)
452455
validateInteger(maxHeaderSize, 'maxHeaderSize', 0);
@@ -1102,6 +1105,26 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
11021105
});
11031106
}
11041107

1108+
if (req[kDumpGet]) {
1109+
if (req.method === 'HEAD' || req.method === 'GET') {
1110+
// Fast dump where request "has" already emitted all lifecycle events.
1111+
// This avoid a lot of unnecessary overhead otherwise introduced by
1112+
// stream.Readable life cycle rules.
1113+
1114+
req._dumped = true;
1115+
1116+
req._read();
1117+
1118+
req._readableState.ended = true;
1119+
req._readableState.endEmitted = true;
1120+
1121+
req._readableState.destroyed = true;
1122+
1123+
req._readableState.closed = true;
1124+
req._readableState.closeEmitted = true;
1125+
}
1126+
}
1127+
11051128
if (socket._httpMessage) {
11061129
// There are already pending outgoing res, append.
11071130
state.outgoing.push(res);

test/common/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const bits = ['arm64', 'loong64', 'mips', 'mipsel', 'ppc64', 'riscv64', 's390x',
3636
.includes(process.arch) ? 64 : 32;
3737
const hasIntl = !!process.config.variables.v8_enable_i18n_support;
3838

39+
globalThis.IS_NODE_TEST = true;
40+
3941
const {
4042
atob,
4143
btoa,

test/parallel/test-http-chunk-extensions-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const assert = require('assert');
124124
}));
125125

126126
sock.end('' +
127-
'GET / HTTP/1.1\r\n' +
127+
'PUT / HTTP/1.1\r\n' +
128128
`Host: localhost:${port}\r\n` +
129129
'Transfer-Encoding: chunked\r\n\r\n' +
130130
'2;' + 'A'.repeat(10000) + '=bar\r\nAA\r\n' +

test/parallel/test-http.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ const server = http.Server(common.mustCall((req, res) => {
5252
if (expectedRequests.length === 0)
5353
server.close();
5454

55-
req.on('end', () => {
55+
if (req.readableEnded) {
5656
res.writeHead(200, { 'Content-Type': 'text/plain' });
5757
res.write(`The path was ${url.parse(req.url).pathname}`);
5858
res.end();
59-
});
60-
req.resume();
59+
} else {
60+
req.on('end', () => {
61+
res.writeHead(200, { 'Content-Type': 'text/plain' });
62+
res.write(`The path was ${url.parse(req.url).pathname}`);
63+
res.end();
64+
});
65+
req.resume();
66+
}
6167
}, 3));
6268
server.listen(0);
6369

0 commit comments

Comments
 (0)