Skip to content

Commit

Permalink
http: free the parser before emitting 'upgrade'
Browse files Browse the repository at this point in the history
Ensure that the parser is freed before emitting the 'connect' or
'upgrade' event.

PR-URL: #18209
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
lpinca authored and BridgeAR committed Jan 21, 2018
1 parent 08ada72 commit 93f1d9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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();
}));
}));

0 comments on commit 93f1d9e

Please sign in to comment.