diff --git a/lib/http.js b/lib/http.js index 1768e7648e..c60703b3f5 100644 --- a/lib/http.js +++ b/lib/http.js @@ -810,6 +810,7 @@ function connectionListener(socket) { var parser = parsers.alloc(); parser.reinitialize('request'); parser.socket = socket; + parser.incoming = null; socket.addListener('error', function(e) { self.emit('clientError', e); @@ -953,6 +954,20 @@ Agent.prototype._establishNewConnection = function() { var parser = parsers.alloc(); parser.reinitialize('response'); parser.socket = socket; + parser.incoming = null; + + socket.on('error', function(err) { + debug("AGENT SOCKET ERROR: " + err.message); + if (socket._httpMessage) { + socket._httpMessage.emit('error', err); + } else if (self.queue.length) { + var req = self.queue.shift(); + if (req) req.emit('error', err); + } else { + // No requests on queue? Where is the request + assert(0); + } + }); socket.ondata = function(d, start, end) { var ret = parser.execute(d, start, end - start); @@ -998,6 +1013,8 @@ Agent.prototype._establishNewConnection = function() { var req = socket._httpMessage; assert(req); + req.res = res; + // Responses to HEAD requests are AWFUL. Ask Ryan. // A major oversight in HTTP. Hence this nastiness. var isHeadResponse = req.method == 'HEAD'; @@ -1100,11 +1117,7 @@ exports.request = function(options, cb) { var agent = getAgent(options.host, options.port); var req = agent.appendMessage(options); - if (cb) { - req.once('response', function (res) { - cb(null, res); - }); - } + if (cb) req.once('response', cb); return req; }; @@ -1123,6 +1136,7 @@ function Client(port, host) { this.port = port; this.host = host; } +util.inherits(Client, EventEmitter); Client.prototype.request = function(method, path, headers) { @@ -1141,7 +1155,15 @@ Client.prototype.request = function(method, path, headers) { host: this.host }; - return exports.request(options); + var self = this; + var req = exports.request(options); + + // proxy error events from req to Client + req.on('error', function(err) { + self.emit('error', err); + }); + + return req; };