Skip to content
Closed
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
30 changes: 13 additions & 17 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -476,18 +468,14 @@ 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
// ended prematurely and we need to emit an error on the request.
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
}
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
freeSocket(req, this);
socket.destroy();
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -712,10 +699,19 @@ function emitFreeNT(req) {
}

if (req.socket) {
freeSocket(req, req.socket);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the significant change.

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;
Expand Down