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: free the parser before emitting 'upgrade' #18209

Closed
wants to merge 1 commit into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ function socketOnData(d) {
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
parser.finish();
freeParser(parser, req, socket);

var bodyHead = d.slice(bytesParsed, d.length);

Expand All @@ -440,7 +441,6 @@ function socketOnData(d) {
// Got Upgrade header or CONNECT method, but have no handler.
socket.destroy();
}
freeParser(parser, req, socket);
} else if (parser.incoming && parser.incoming.complete &&
// When the status code is 100 (Continue), the server will
// send a final response after this client sends a request
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-http-parser-freed-before-upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const http = require('http');

const server = http.createServer();

server.on('upgrade', common.mustCall((request, socket) => {
assert.strictEqual(socket.parser, null);
socket.write([
'HTTP/1.1 101 Switching Protocols',
'Connection: Upgrade',
'Upgrade: WebSocket',
'\r\n'
].join('\r\n'));
}));

server.listen(common.mustCall(() => {
const request = http.get({
port: server.address().port,
headers: {
Connection: 'Upgrade',
Upgrade: 'WebSocket'
}
});

request.on('upgrade', common.mustCall((response, socket) => {
assert.strictEqual(socket.parser, null);
socket.destroy();
server.close();
}));
}));