Skip to content

Commit fc3f493

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

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const http= require('http');
2+
3+
http.createServer((req, res) => {
4+
res.end();
5+
}).listen(9000);

lib/_http_server.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,22 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
11021102
});
11031103
}
11041104

1105+
if (req.method === 'HEAD' || req.method === 'GET') {
1106+
// Fast dump where request "has" already emitted all lifecycle events.
1107+
// This avoid a lot of unnecessary overhead otherwise introduced by
1108+
// stream.Readable life cycle rules. The downside is that this will
1109+
// break some servers that read GET bodies.
1110+
1111+
req._dumped = true;
1112+
req._readableState.ended = true;
1113+
req._readableState.endEmitted = true;
1114+
req._readableState.destroyed = true;
1115+
req._readableState.closed = true;
1116+
req._readableState.closeEmitted = true;
1117+
1118+
req._read();
1119+
}
1120+
11051121
if (socket._httpMessage) {
11061122
// There are already pending outgoing res, append.
11071123
state.outgoing.push(res);

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)