Browse Source

Add 'close' and 'aborted' events to Agent responses

Closes GH-722.
v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
ea9f5b110a
  1. 11
      lib/http.js
  2. 46
      test/simple/test-http-abort-client.js

11
lib/http.js

@ -1235,11 +1235,16 @@ Agent.prototype._establishNewConnection = function() {
// has a request but no response and it never emitted an error event: // has a request but no response and it never emitted an error event:
// THEN we need to trigger it manually. // THEN we need to trigger it manually.
// There must be a better way to do this. // There must be a better way to do this.
if (socket._httpMessage && if (socket._httpMessage) {
!socket._httpMessage.res && if (socket._httpMessage.res) {
!socket._httpMessage._hadError) { socket._httpMessage.res.emit('aborted');
socket._httpMessage.res.emit('close');
} else {
if (!socket._httpMessage._hadError) {
socket._httpMessage.emit('error', new Error('socket hang up')); socket._httpMessage.emit('error', new Error('socket hang up'));
} }
}
}
self._removeSocket(socket); self._removeSocket(socket);
// unref the parser for easy gc // unref the parser for easy gc

46
test/simple/test-http-abort-client.js

@ -0,0 +1,46 @@
var common = require("../common");
var http = require("http");
var assert = require("assert");
var server = http.Server(function (req, res) {
console.log("Server accepted request.");
res.writeHead(200);
res.write("Part of my res.");
res.destroy();
});
var responseClose = false
server.listen(common.PORT, function() {
var client = http.get({
port: common.PORT,
headers: { connection: "keep-alive" }
}, function(res) {
server.close();
console.log("Got res: " + res.statusCode);
console.dir(res.headers);
res.on("data", function (chunk) {
console.log("Read " + chunk.length + " bytes");
console.log(chunk.toString());
});
res.on("end", function () {
console.log("Response ended.");
});
// it would be nice if this worked:
res.on("close", function () {
console.log("Response aborted");
responseClose = true;
});
});
});
process.on('exit', function() {
assert.ok(responseClose);
});
Loading…
Cancel
Save