Skip to content

Commit 555af5b

Browse files
mcollinatargos
authored andcommitted
http: remove 'data' and 'end' listener if client parser error
There might be the case of some more data coming through after the parser has returned an error and we have destroyed the socket. We should also be removing the 'data' event handler. Fixes: #40242 PR-URL: #40244 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 383dbe9 commit 555af5b

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ function socketOnData(d) {
489489
prepareError(ret, parser, d);
490490
debug('parse error', ret);
491491
freeParser(parser, req, socket);
492+
socket.removeListener('data', socketOnData);
493+
socket.removeListener('end', socketOnEnd);
492494
socket.destroy();
493495
req.socket._hadError = true;
494496
req.emit('error', ret);

test/parallel/test-http-client-parse-error.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
const common = require('../common');
2424
const http = require('http');
2525
const net = require('net');
26+
const assert = require('assert');
2627
const Countdown = require('../common/countdown');
2728

2829
const countdown = new Countdown(2, () => server.close());
@@ -38,10 +39,12 @@ const server =
3839

3940
server.listen(0, common.mustCall(() => {
4041
for (let i = 0; i < 2; i++) {
41-
http.get({
42+
const req = http.get({
4243
port: server.address().port,
4344
path: '/'
4445
}).on('error', common.mustCall((e) => {
46+
assert.strictEqual(req.socket.listenerCount('data'), 0);
47+
assert.strictEqual(req.socket.listenerCount('end'), 1);
4548
common.expectsError({
4649
code: 'HPE_INVALID_CONSTANT',
4750
message: 'Parse Error: Expected HTTP/'

0 commit comments

Comments
 (0)