Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: Improved timeout defaults handling. #45778

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1515,9 +1515,13 @@ or waiting for a response.
added:
- v11.3.0
- v10.14.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/45778
description: The default is now set to the minimum between 60000 (60 seconds) or `requestTimeout`.
-->

* {number} **Default:** `60000`
* {number} **Default:** The minimum between [`server.requestTimeout`][] or `60000`.

Limit the amount of time the parser will wait to receive the complete HTTP
headers.
Expand Down
7 changes: 4 additions & 3 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
ArrayIsArray,
Error,
MathMin,
ObjectKeys,
ObjectSetPrototypeOf,
RegExpPrototypeExec,
Expand Down Expand Up @@ -451,11 +452,11 @@ function storeHTTPOptions(options) {
validateInteger(headersTimeout, 'headersTimeout', 0);
this.headersTimeout = headersTimeout;
} else {
this.headersTimeout = 60_000; // 60 seconds
this.headersTimeout = MathMin(60_000, this.requestTimeout); // Minimum between 60 seconds or requestTimeout
}

if (this.requestTimeout > 0 && this.headersTimeout > 0 && this.headersTimeout >= this.requestTimeout) {
throw new codes.ERR_OUT_OF_RANGE('headersTimeout', '< requestTimeout', headersTimeout);
if (this.requestTimeout > 0 && this.headersTimeout > 0 && this.headersTimeout > this.requestTimeout) {
throw new codes.ERR_OUT_OF_RANGE('headersTimeout', '<= requestTimeout', headersTimeout);
}

const keepAliveTimeout = options.keepAliveTimeout;
Expand Down
50 changes: 50 additions & 0 deletions test/parallel/test-http-server-timeouts-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

require('../common');
const assert = require('assert');
const { createServer } = require('http');

// This test validates that the HTTP server timeouts are properly validated and set.

{
const server = createServer();
assert.strictEqual(server.headersTimeout, 60000);
assert.strictEqual(server.requestTimeout, 300000);
}

{
const server = createServer({ headersTimeout: 10000, requestTimeout: 20000 });
assert.strictEqual(server.headersTimeout, 10000);
assert.strictEqual(server.requestTimeout, 20000);
}

{
const server = createServer({ headersTimeout: 10000, requestTimeout: 10000 });
assert.strictEqual(server.headersTimeout, 10000);
assert.strictEqual(server.requestTimeout, 10000);
}

{
const server = createServer({ headersTimeout: 10000 });
assert.strictEqual(server.headersTimeout, 10000);
assert.strictEqual(server.requestTimeout, 300000);
}

{
const server = createServer({ requestTimeout: 20000 });
assert.strictEqual(server.headersTimeout, 20000);
assert.strictEqual(server.requestTimeout, 20000);
}

{
const server = createServer({ requestTimeout: 100000 });
assert.strictEqual(server.headersTimeout, 60000);
assert.strictEqual(server.requestTimeout, 100000);
}

{
assert.throws(
() => createServer({ headersTimeout: 10000, requestTimeout: 1000 }),
{ code: 'ERR_OUT_OF_RANGE' }
);
}