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_client: ensure empty socket on error #1103

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
9 changes: 8 additions & 1 deletion lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ function socketCloseListener() {

function socketErrorListener(err) {
var socket = this;
var parser = socket.parser;
var req = socket._httpMessage;
debug('SOCKET ERROR:', err.message, err.stack);

Expand All @@ -258,10 +257,18 @@ function socketErrorListener(err) {
req.socket._hadError = true;
}

// Handle any pending data
socket.read();

var parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}

// Ensure that no further data will come out of the socket
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.destroy();
}

Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-http-client-read-in-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var net = require('net');
var http = require('http');
var util = require('util');

function Agent() {
http.Agent.call(this);
}
util.inherits(Agent, http.Agent);

Agent.prototype.createConnection = function() {
var self = this;
var socket = new net.Socket();

socket.on('error', function() {
socket.push('HTTP/1.1 200\r\n\r\n');
});

socket.on('newListener', function onNewListener(name) {
if (name !== 'error')
return;
socket.removeListener('newListener', onNewListener);

// Let other listeners to be set up too
process.nextTick(function() {
self.breakSocket(socket);
});
});

return socket;
};

Agent.prototype.breakSocket = function breakSocket(socket) {
socket.emit('error', new Error('Intentional error'));
};

var agent = new Agent();

http.request({
agent: agent
}).once('error', function() {
console.log('ignore');
});