Skip to content

Commit

Permalink
http: remove 'data' and 'end' listener if client parser error
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mcollina authored and targos committed Oct 4, 2021
1 parent 383dbe9 commit 555af5b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/_http_client.js
Expand Up @@ -489,6 +489,8 @@ function socketOnData(d) {
prepareError(ret, parser, d);
debug('parse error', ret);
freeParser(parser, req, socket);
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.destroy();
req.socket._hadError = true;
req.emit('error', ret);
Expand Down
5 changes: 4 additions & 1 deletion test/parallel/test-http-client-parse-error.js
Expand Up @@ -23,6 +23,7 @@
const common = require('../common');
const http = require('http');
const net = require('net');
const assert = require('assert');
const Countdown = require('../common/countdown');

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

server.listen(0, common.mustCall(() => {
for (let i = 0; i < 2; i++) {
http.get({
const req = http.get({
port: server.address().port,
path: '/'
}).on('error', common.mustCall((e) => {
assert.strictEqual(req.socket.listenerCount('data'), 0);
assert.strictEqual(req.socket.listenerCount('end'), 1);
common.expectsError({
code: 'HPE_INVALID_CONSTANT',
message: 'Parse Error: Expected HTTP/'
Expand Down

0 comments on commit 555af5b

Please sign in to comment.