Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: remove 'data' and 'end' listener if client parser error #40244

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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