Skip to content

Commit ebd9090

Browse files
ShogunPandatargos
authored andcommitted
http: disable headersTimeout check when set to zero
PR-URL: #33307 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 5c0232a commit ebd9090

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

doc/api/http.md

+2
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ event is emitted on the server object, and (by default) the socket is destroyed.
11221122
See [`server.timeout`][] for more information on how timeout behavior can be
11231123
customized.
11241124

1125+
A value of `0` will disable the HTTP headers timeout check.
1126+
11251127
### `server.listen()`
11261128

11271129
Starts the HTTP server listening for connections.

lib/_http_server.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,12 @@ function onParserExecute(server, socket, parser, state, ret) {
571571

572572
// If we have not parsed the headers, destroy the socket
573573
// after server.headersTimeout to protect from DoS attacks.
574-
// start === 0 means that we have parsed headers.
575-
if (start !== 0 && nowDate() - start > server.headersTimeout) {
574+
// start === 0 means that we have parsed headers, while
575+
// server.headersTimeout === 0 means user disabled this check.
576+
if (
577+
start !== 0 && server.headersTimeout &&
578+
nowDate() - start > server.headersTimeout
579+
) {
576580
const serverTimeout = server.emit('timeout', socket);
577581

578582
if (!serverTimeout)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { createServer } = require('http');
6+
const { connect } = require('net');
7+
8+
// This test verifies that it is possible to disable
9+
// headersTimeout by setting it to zero.
10+
11+
const server = createServer(common.mustCall((req, res) => {
12+
res.writeHead(200);
13+
res.end('OK');
14+
}));
15+
16+
server.headersTimeout = 0;
17+
18+
server.once('timeout', common.mustNotCall((socket) => {
19+
socket.destroy();
20+
}));
21+
22+
server.listen(0, common.mustCall(() => {
23+
const client = connect(server.address().port);
24+
let response = '';
25+
26+
client.resume();
27+
client.write('GET / HTTP/1.1\r\nConnection: close\r\n');
28+
29+
// All the timeouts below must be greater than a second, otherwise
30+
// headersTimeout won't be triggered anyway as the current date is cached
31+
// for a second in HTTP internals.
32+
setTimeout(() => {
33+
client.write('X-Crash: Ab: 456\r\n');
34+
}, common.platformTimeout(1100)).unref();
35+
36+
setTimeout(() => {
37+
client.write('\r\n');
38+
}, common.platformTimeout(1200)).unref();
39+
40+
client.on('data', (chunk) => {
41+
response += chunk.toString('utf-8');
42+
});
43+
44+
client.on('end', common.mustCall(() => {
45+
assert.strictEqual(response.split('\r\n').shift(), 'HTTP/1.1 200 OK');
46+
client.end();
47+
server.close();
48+
}));
49+
}));

0 commit comments

Comments
 (0)