mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
http: do not leak error listeners
PR-URL: https://github.com/nodejs/node/pull/43587 Fixes: https://github.com/nodejs/node/issues/43548 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
This commit is contained in:
parent
56c15f1c95
commit
736a7d8d60
@ -763,7 +763,10 @@ const requestHeaderFieldsTooLargeResponse = Buffer.from(
|
||||
function socketOnError(e) {
|
||||
// Ignore further errors
|
||||
this.removeListener('error', socketOnError);
|
||||
this.on('error', noop);
|
||||
|
||||
if (this.listenerCount('error') === 0) {
|
||||
this.on('error', noop);
|
||||
}
|
||||
|
||||
if (!this.server.emit('clientError', e, this)) {
|
||||
if (this.writable && this.bytesWritten === 0) {
|
||||
|
44
test/parallel/test-http-socket-listeners.js
Normal file
44
test/parallel/test-http-socket-listeners.js
Normal file
@ -0,0 +1,44 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const http = require('http');
|
||||
const net = require('net');
|
||||
|
||||
// This test sends an invalid character to a HTTP server and purposely
|
||||
// does not handle clientError (even if it sets an event handler).
|
||||
//
|
||||
// The idea is to let the server emit multiple errors on the socket,
|
||||
// mostly due to parsing error, and make sure they don't result
|
||||
// in leaking event listeners.
|
||||
|
||||
let i = 0;
|
||||
let socket;
|
||||
|
||||
process.on('warning', common.mustNotCall());
|
||||
|
||||
const server = http.createServer(common.mustNotCall());
|
||||
|
||||
server.on('clientError', common.mustCallAtLeast((err) => {
|
||||
assert.strictEqual(err.code, 'HPE_INVALID_METHOD');
|
||||
assert.strictEqual(err.rawPacket.toString(), '*');
|
||||
|
||||
if (i === 20) {
|
||||
socket.end();
|
||||
} else {
|
||||
socket.write('*');
|
||||
i++;
|
||||
}
|
||||
}, 1));
|
||||
|
||||
server.listen(0, () => {
|
||||
socket = net.createConnection({ port: server.address().port });
|
||||
|
||||
socket.on('connect', () => {
|
||||
socket.write('*');
|
||||
});
|
||||
|
||||
socket.on('close', () => {
|
||||
server.close();
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user