diff --git a/lib/_http_client.js b/lib/_http_client.js index 73a6409c411cc1..5e0c4011f5b2f1 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -409,7 +409,6 @@ function socketCloseListener() { // NOTE: It's important to get parser here, because it could be freed by // the `socketOnData`. - const parser = socket.parser; const res = req.res; if (res) { // Socket closed before we emitted 'end' below. @@ -443,10 +442,7 @@ function socketCloseListener() { if (req.outputData) req.outputData.length = 0; - if (parser) { - parser.finish(); - freeParser(parser, req, socket); - } + freeSocket(req, socket); } function socketErrorListener(err) { @@ -461,11 +457,7 @@ function socketErrorListener(err) { req.emit('error', err); } - const parser = socket.parser; - if (parser) { - parser.finish(); - freeParser(parser, req, socket); - } + freeSocket(req, socket); // Ensure that no further data will come out of the socket socket.removeListener('data', socketOnData); @@ -476,7 +468,6 @@ function socketErrorListener(err) { function socketOnEnd() { const socket = this; const req = this._httpMessage; - const parser = this.parser; if (!req.res && !req.socket._hadError) { // If we don't have a response then we know that the socket @@ -484,10 +475,7 @@ function socketOnEnd() { req.socket._hadError = true; req.emit('error', connResetException('socket hang up')); } - if (parser) { - parser.finish(); - freeParser(parser, req, socket); - } + freeSocket(req, this); socket.destroy(); } @@ -519,8 +507,7 @@ function socketOnData(d) { if (req.timeoutCb) socket.removeListener('timeout', req.timeoutCb); - parser.finish(); - freeParser(parser, req, socket); + freeSocket(req, this); const bodyHead = d.slice(bytesParsed, d.length); @@ -712,10 +699,19 @@ function emitFreeNT(req) { } if (req.socket) { + freeSocket(req, req.socket); req.socket.emit('free'); } } +function freeSocket(req, socket) { + const parser = socket.parser; + if (parser) { + parser.finish(); + freeParser(parser, req, socket); + } +} + function tickOnSocket(req, socket) { const parser = parsers.alloc(); req.socket = socket;