| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1353,12 +1353,12 @@ explicitly. | |||
| 1353 | 1353 | added: v16.10.0 | |
| 1354 | 1354 | --> | |
| 1355 | 1355 | ||
| 1356 | - * {number} Requests per socket. **Default:** null (no limit) | ||
| 1356 | + * {number} Requests per socket. **Default:** 0 (no limit) | ||
| 1357 | 1357 | ||
| 1358 | 1358 | The maximum number of requests socket can handle | |
| 1359 | 1359 | before closing keep alive connection. | |
| 1360 | 1360 | ||
| 1361 | - A value of `null` will disable the limit. | ||
| 1361 | + A value of `0` will disable the limit. | ||
| 1362 | 1362 | ||
| 1363 | 1363 | When the limit is reached it will set the `Connection` header value to `close`, | |
| 1364 | 1364 | but will not actually close the connection, subsequent requests sent | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -396,7 +396,7 @@ function Server(options, requestListener) { | |||
| 396 | 396 | this.timeout = 0; | |
| 397 | 397 | this.keepAliveTimeout = 5000; | |
| 398 | 398 | this.maxHeadersCount = null; | |
| 399 | - this.maxRequestsPerSocket = null; | ||
| 399 | + this.maxRequestsPerSocket = 0; | ||
| 400 | 400 | this.headersTimeout = 60 * 1000; // 60 seconds | |
| 401 | 401 | this.requestTimeout = 0; | |
| 402 | 402 | } | |
@@ -908,13 +908,18 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | |||
| 908 | 908 | let handled = false; | |
| 909 | 909 | ||
| 910 | 910 | 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) { | ||
| 912 | 917 | state.requestsCount++; | |
| 913 | 918 | res.maxRequestsOnConnectionReached = ( | |
| 914 | 919 | server.maxRequestsPerSocket <= state.requestsCount); | |
| 915 | 920 | } | |
| 916 | 921 | ||
| 917 | - if (typeof server.maxRequestsPerSocket === 'number' && | ||
| 922 | + if (isRequestsLimitSet && | ||
| 918 | 923 | (server.maxRequestsPerSocket < state.requestsCount)) { | |
| 919 | 924 | handled = true; | |
| 920 | 925 | ||
| Original file line number | Diff line number | Diff 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 number | Diff line number | Diff 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 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments