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 <ronagy@icloud.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 383dbe9 commit 555af5b

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

‎lib/_http_client.js‎

Lines changed: 2 additions & 0 deletions
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‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
constcommon=require('../common');
2424
consthttp=require('http');
2525
constnet=require('net');
26+
constassert=require('assert');
2627
constCountdown=require('../common/countdown');
2728

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

3940
server.listen(0,common.mustCall(()=>{
4041
for(leti=0;i<2;i++){
41-
http.get({
42+
constreq=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)