Skip to content

Commit 069b6e1

Browse files
ShogunPandarichardlau
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 5dab101 commit 069b6e1

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

doc/api/http.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ added: v0.1.90
943943

944944
Stops the server from accepting new connections. See [`net.Server.close()`][].
945945

946-
### server.listen()
946+
### `server.listen()`
947947

948948
Starts the HTTP server listening for connections.
949949
This method is identical to [`server.listen()`][] from [`net.Server`][].
@@ -984,6 +984,8 @@ event is emitted on the server object, and (by default) the socket is destroyed.
984984
See [server.timeout][] for more information on how timeout behaviour can be
985985
customised.
986986

987+
A value of `0` will disable the HTTP headers timeout check.
988+
987989
### server.setTimeout([msecs][, callback])
988990
<!-- YAML
989991
added: v0.9.12

lib/_http_server.js

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

502502
// If we have not parsed the headers, destroy the socket
503503
// after server.headersTimeout to protect from DoS attacks.
504-
// start === 0 means that we have parsed headers.
505-
if (start !== 0 && nowDate() - start > server.headersTimeout) {
504+
// start === 0 means that we have parsed headers, while
505+
// server.headersTimeout === 0 means user disabled this check.
506+
if (
507+
start !== 0 && server.headersTimeout &&
508+
nowDate() - start > server.headersTimeout
509+
) {
506510
const serverTimeout = server.emit('timeout', socket);
507511

508512
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)