diff --git a/doc/api/http.markdown b/doc/api/http.markdown index 57f13a48ad..2c9342ff83 100644 --- a/doc/api/http.markdown +++ b/doc/api/http.markdown @@ -247,6 +247,13 @@ authentication details. This object is created internally by a HTTP server--not by the user. It is passed as the second parameter to the `'request'` event. It is a `Writable Stream`. +### Event: 'close' + +`function () { }` + +Indicates that the underlaying connection was terminated before +`response.end()` was called or able to flush. + ### response.writeContinue() Sends a HTTP/1.1 100 Continue message to the client, indicating that diff --git a/lib/http.js b/lib/http.js index 89005b3869..c4e0d54f77 100644 --- a/lib/http.js +++ b/lib/http.js @@ -343,22 +343,6 @@ util.inherits(OutgoingMessage, stream.Stream); exports.OutgoingMessage = OutgoingMessage; -OutgoingMessage.prototype.assignSocket = function(socket) { - assert(!socket._httpMessage); - socket._httpMessage = this; - this.socket = socket; - this.connection = socket; - this._flush(); -}; - - -OutgoingMessage.prototype.detachSocket = function(socket) { - assert(socket._httpMessage == this); - socket._httpMessage = null; - this.socket = this.connection = null; -}; - - OutgoingMessage.prototype.destroy = function(error) { this.socket.destroy(error); }; @@ -792,6 +776,26 @@ exports.ServerResponse = ServerResponse; ServerResponse.prototype.statusCode = 200; +function onServerResponseClose() { + this._httpMessage.emit('close'); +} + +ServerResponse.prototype.assignSocket = function(socket) { + assert(!socket._httpMessage); + socket._httpMessage = this; + socket.on('close', onServerResponseClose); + this.socket = socket; + this.connection = socket; + this._flush(); +}; + +ServerResponse.prototype.detachSocket = function(socket) { + assert(socket._httpMessage == this); + socket.removeListener('close', onServerResponseClose); + socket._httpMessage = null; + this.socket = this.connection = null; +}; + ServerResponse.prototype.writeContinue = function() { this._writeRaw('HTTP/1.1 100 Continue' + CRLF + CRLF, 'ascii'); this._sent100 = true; diff --git a/test/simple/test-http-response-close.js b/test/simple/test-http-response-close.js index a9955d0a45..be89682a68 100644 --- a/test/simple/test-http-response-close.js +++ b/test/simple/test-http-response-close.js @@ -23,15 +23,20 @@ var common = require('../common'); var assert = require('assert'); var http = require('http'); -var gotEnd = false; +var requestGotEnd = false; +var responseGotEnd = false; var server = http.createServer(function(req, res) { res.writeHead(200); res.write('a'); req.on('close', function() { - console.error('aborted'); - gotEnd = true; + console.error('request aborted'); + requestGotEnd = true; + }); + res.on('close', function() { + console.error('response aborted'); + responseGotEnd = true; }); }); server.listen(common.PORT); @@ -51,5 +56,6 @@ server.on('listening', function() { }); process.on('exit', function() { - assert.ok(gotEnd); + assert.ok(requestGotEnd); + assert.ok(responseGotEnd); });