| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1404,6 +1404,20 @@ This event is guaranteed to be passed an instance of the {net.Socket} class, | |||
| 1404 | 1404 | a subclass of {stream.Duplex}, unless the user specifies a socket | |
| 1405 | 1405 | type other than {net.Socket}. | |
| 1406 | 1406 | ||
| 1407 | + ### Event: `'dropRequest'` | ||
| 1408 | + | ||
| 1409 | + <!-- YAML | ||
| 1410 | + added: REPLACEME | ||
| 1411 | + --> | ||
| 1412 | + | ||
| 1413 | + * `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in | ||
| 1414 | + the [`'request'`][] event | ||
| 1415 | + * `socket` {stream.Duplex} Network socket between the server and client | ||
| 1416 | + | ||
| 1417 | + When the number of requests on a socket reaches the threshold of | ||
| 1418 | + `server.maxRequestsPerSocket`, the server will drop new requests | ||
| 1419 | + and emit `'dropRequest'` event instead, then send `503` to client. | ||
| 1420 | + | ||
| 1407 | 1421 | ### Event: `'request'` | |
| 1408 | 1422 | ||
| 1409 | 1423 | <!-- YAML | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -985,7 +985,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | |||
| 985 | 985 | if (isRequestsLimitSet && | |
| 986 | 986 | (server.maxRequestsPerSocket < state.requestsCount)) { | |
| 987 | 987 | handled = true; | |
| 988 | - | ||
| 988 | + server.emit('dropRequest', req, socket); | ||
| 989 | 989 | res.writeHead(503); | |
| 990 | 990 | res.end(); | |
| 991 | 991 | } else if (req.headers.expect !== undefined) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + const http = require('http'); | ||
| 5 | + const net = require('net'); | ||
| 6 | + const assert = require('assert'); | ||
| 7 | + | ||
| 8 | + function request(socket) { | ||
| 9 | + socket.write('GET / HTTP/1.1\r\n'); | ||
| 10 | + socket.write('Connection: keep-alive\r\n'); | ||
| 11 | + socket.write('\r\n\r\n'); | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + const server = http.createServer(common.mustCall((req, res) => { | ||
| 15 | + res.end('ok'); | ||
| 16 | + })); | ||
| 17 | + | ||
| 18 | + server.on('dropRequest', common.mustCall((request, socket) => { | ||
| 19 | + assert.strictEqual(request instanceof http.IncomingMessage, true); | ||
| 20 | + assert.strictEqual(socket instanceof net.Socket, true); | ||
| 21 | + server.close(); | ||
| 22 | + })); | ||
| 23 | + | ||
| 24 | + server.listen(0, common.mustCall(() => { | ||
| 25 | + const socket = net.connect(server.address().port); | ||
| 26 | + socket.on('connect', common.mustCall(() => { | ||
| 27 | + request(socket); | ||
| 28 | + request(socket); | ||
| 29 | + })); | ||
| 30 | + socket.on('data', common.mustCallAtLeast()); | ||
| 31 | + socket.on('close', common.mustCall()); | ||
| 32 | + })); | ||
| 33 | + | ||
| 34 | + server.maxRequestsPerSocket = 1; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + | ||
| 5 | + if (!common.hasCrypto) | ||
| 6 | + common.skip('missing crypto'); | ||
| 7 | + | ||
| 8 | + const https = require('https'); | ||
| 9 | + const http = require('http'); | ||
| 10 | + const net = require('net'); | ||
| 11 | + const assert = require('assert'); | ||
| 12 | + const tls = require('tls'); | ||
| 13 | + const { readKey } = require('../common/fixtures'); | ||
| 14 | + | ||
| 15 | + function request(socket) { | ||
| 16 | + socket.write('GET / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n\r\n'); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + // https options | ||
| 20 | + const httpsOptions = { | ||
| 21 | + key: readKey('agent1-key.pem'), | ||
| 22 | + cert: readKey('agent1-cert.pem') | ||
| 23 | + }; | ||
| 24 | + | ||
| 25 | + const server = https.createServer(httpsOptions, common.mustCall((req, res) => { | ||
| 26 | + res.end('ok'); | ||
| 27 | + })); | ||
| 28 | + | ||
| 29 | + server.on('dropRequest', common.mustCall((request, socket) => { | ||
| 30 | + assert.strictEqual(request instanceof http.IncomingMessage, true); | ||
| 31 | + assert.strictEqual(socket instanceof net.Socket, true); | ||
| 32 | + server.close(); | ||
| 33 | + })); | ||
| 34 | + | ||
| 35 | + server.listen(0, common.mustCall(() => { | ||
| 36 | + const socket = tls.connect( | ||
| 37 | + server.address().port, | ||
| 38 | + { | ||
| 39 | + rejectUnauthorized: false | ||
| 40 | + }, | ||
| 41 | + common.mustCall(() => { | ||
| 42 | + request(socket); | ||
| 43 | + request(socket); | ||
| 44 | + socket.on('error', common.mustNotCall()); | ||
| 45 | + socket.on('data', common.mustCallAtLeast()); | ||
| 46 | + socket.on('close', common.mustCall()); | ||
| 47 | + }) | ||
| 48 | + ); | ||
| 49 | + })); | ||
| 50 | + | ||
| 51 | + server.maxRequestsPerSocket = 1; | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments