Skip to content

Commit 22725f5

Browse files
fatal10110targos
authored andcommitted
http: use 0 as default for requests limit
PR-URL: #40192 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Khaidi Chu <[email protected]>
1 parent 34f3021 commit 22725f5

4 files changed

+161
-5
lines changed

doc/api/http.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1353,12 +1353,12 @@ explicitly.
13531353
added: v16.10.0
13541354
-->
13551355

1356-
* {number} Requests per socket. **Default:** null (no limit)
1356+
* {number} Requests per socket. **Default:** 0 (no limit)
13571357

13581358
The maximum number of requests socket can handle
13591359
before closing keep alive connection.
13601360

1361-
A value of `null` will disable the limit.
1361+
A value of `0` will disable the limit.
13621362

13631363
When the limit is reached it will set the `Connection` header value to `close`,
13641364
but will not actually close the connection, subsequent requests sent

lib/_http_server.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ function Server(options, requestListener) {
396396
this.timeout = 0;
397397
this.keepAliveTimeout = 5000;
398398
this.maxHeadersCount = null;
399-
this.maxRequestsPerSocket = null;
399+
this.maxRequestsPerSocket = 0;
400400
this.headersTimeout = 60 * 1000; // 60 seconds
401401
this.requestTimeout = 0;
402402
}
@@ -908,13 +908,18 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
908908
let handled = false;
909909

910910
if (req.httpVersionMajor === 1 && req.httpVersionMinor === 1) {
911-
if (typeof server.maxRequestsPerSocket === 'number') {
911+
const isRequestsLimitSet = (
912+
typeof server.maxRequestsPerSocket === 'number' &&
913+
server.maxRequestsPerSocket > 0
914+
);
915+
916+
if (isRequestsLimitSet) {
912917
state.requestsCount++;
913918
res.maxRequestsOnConnectionReached = (
914919
server.maxRequestsPerSocket <= state.requestsCount);
915920
}
916921

917-
if (typeof server.maxRequestsPerSocket === 'number' &&
922+
if (isRequestsLimitSet &&
918923
(server.maxRequestsPerSocket < state.requestsCount)) {
919924
handled = true;
920925

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const bodySent = 'This is my request';
9+
10+
function assertResponse(headers, body, expectClosed) {
11+
assert.match(headers, /Connection: keep-alive\r\n/m);
12+
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
13+
assert.match(body, /Hello World!/m);
14+
}
15+
16+
function writeRequest(socket) {
17+
socket.write('POST / HTTP/1.1\r\n');
18+
socket.write('Connection: keep-alive\r\n');
19+
socket.write('Content-Type: text/plain\r\n');
20+
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`);
21+
socket.write(`${bodySent}\r\n`);
22+
socket.write('\r\n\r\n');
23+
}
24+
25+
const server = http.createServer((req, res) => {
26+
let body = '';
27+
req.on('data', (data) => {
28+
body += data;
29+
});
30+
31+
req.on('end', () => {
32+
if (req.method === 'POST') {
33+
assert.strictEqual(bodySent, body);
34+
}
35+
36+
res.writeHead(200, { 'Content-Type': 'text/plain' });
37+
res.write('Hello World!');
38+
res.end();
39+
});
40+
});
41+
42+
server.listen(0, common.mustCall((res) => {
43+
assert.strictEqual(server.maxRequestsPerSocket, 0);
44+
45+
const socket = new net.Socket();
46+
47+
socket.on('end', common.mustCall(() => {
48+
server.close();
49+
}));
50+
51+
socket.on('ready', common.mustCall(() => {
52+
writeRequest(socket);
53+
writeRequest(socket);
54+
writeRequest(socket);
55+
writeRequest(socket);
56+
}));
57+
58+
let buffer = '';
59+
60+
socket.on('data', (data) => {
61+
buffer += data;
62+
63+
const responseParts = buffer.trim().split('\r\n\r\n');
64+
65+
if (responseParts.length === 8) {
66+
assertResponse(responseParts[0], responseParts[1]);
67+
assertResponse(responseParts[2], responseParts[3]);
68+
assertResponse(responseParts[4], responseParts[5]);
69+
assertResponse(responseParts[6], responseParts[7]);
70+
71+
socket.end();
72+
}
73+
});
74+
75+
socket.connect({ port: server.address().port });
76+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const net = require('net');
5+
const http = require('http');
6+
const assert = require('assert');
7+
8+
const bodySent = 'This is my request';
9+
10+
function assertResponse(headers, body, expectClosed) {
11+
assert.match(headers, /Connection: keep-alive\r\n/m);
12+
assert.match(headers, /Keep-Alive: timeout=5\r\n/m);
13+
assert.match(body, /Hello World!/m);
14+
}
15+
16+
function writeRequest(socket) {
17+
socket.write('POST / HTTP/1.1\r\n');
18+
socket.write('Connection: keep-alive\r\n');
19+
socket.write('Content-Type: text/plain\r\n');
20+
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`);
21+
socket.write(`${bodySent}\r\n`);
22+
socket.write('\r\n\r\n');
23+
}
24+
25+
const server = http.createServer((req, res) => {
26+
let body = '';
27+
req.on('data', (data) => {
28+
body += data;
29+
});
30+
31+
req.on('end', () => {
32+
if (req.method === 'POST') {
33+
assert.strictEqual(bodySent, body);
34+
}
35+
36+
res.writeHead(200, { 'Content-Type': 'text/plain' });
37+
res.write('Hello World!');
38+
res.end();
39+
});
40+
});
41+
42+
server.maxRequestsPerSocket = null;
43+
server.listen(0, common.mustCall((res) => {
44+
const socket = new net.Socket();
45+
46+
socket.on('end', common.mustCall(() => {
47+
server.close();
48+
}));
49+
50+
socket.on('ready', common.mustCall(() => {
51+
writeRequest(socket);
52+
writeRequest(socket);
53+
writeRequest(socket);
54+
writeRequest(socket);
55+
}));
56+
57+
let buffer = '';
58+
59+
socket.on('data', (data) => {
60+
buffer += data;
61+
62+
const responseParts = buffer.trim().split('\r\n\r\n');
63+
64+
if (responseParts.length === 8) {
65+
assertResponse(responseParts[0], responseParts[1]);
66+
assertResponse(responseParts[2], responseParts[3]);
67+
assertResponse(responseParts[4], responseParts[5]);
68+
assertResponse(responseParts[6], responseParts[7]);
69+
70+
socket.end();
71+
}
72+
});
73+
74+
socket.connect({ port: server.address().port });
75+
}));

0 commit comments

Comments
 (0)