Skip to content

Commit dd9593c

Browse files
committed
http: fix ServerResponse does not emit 'close'
Refs #2453.
1 parent 78dbb4b commit dd9593c

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

doc/api/http.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,13 @@ authentication details.
247247
This object is created internally by a HTTP server--not by the user. It is
248248
passed as the second parameter to the `'request'` event. It is a `Writable Stream`.
249249

250+
### Event: 'close'
251+
252+
`function () { }`
253+
254+
Indicates that the underlaying connection was terminated before
255+
`response.end()` was called or able to flush.
256+
250257
### response.writeContinue()
251258

252259
Sends a HTTP/1.1 100 Continue message to the client, indicating that

lib/http.js

+20-16
Original file line numberDiff line numberDiff line change
@@ -343,22 +343,6 @@ util.inherits(OutgoingMessage, stream.Stream);
343343
exports.OutgoingMessage = OutgoingMessage;
344344

345345

346-
OutgoingMessage.prototype.assignSocket = function(socket) {
347-
assert(!socket._httpMessage);
348-
socket._httpMessage = this;
349-
this.socket = socket;
350-
this.connection = socket;
351-
this._flush();
352-
};
353-
354-
355-
OutgoingMessage.prototype.detachSocket = function(socket) {
356-
assert(socket._httpMessage == this);
357-
socket._httpMessage = null;
358-
this.socket = this.connection = null;
359-
};
360-
361-
362346
OutgoingMessage.prototype.destroy = function(error) {
363347
this.socket.destroy(error);
364348
};
@@ -792,6 +776,26 @@ exports.ServerResponse = ServerResponse;
792776

793777
ServerResponse.prototype.statusCode = 200;
794778

779+
function onServerResponseClose() {
780+
this._httpMessage.emit('close');
781+
}
782+
783+
ServerResponse.prototype.assignSocket = function(socket) {
784+
assert(!socket._httpMessage);
785+
socket._httpMessage = this;
786+
socket.on('close', onServerResponseClose);
787+
this.socket = socket;
788+
this.connection = socket;
789+
this._flush();
790+
};
791+
792+
ServerResponse.prototype.detachSocket = function(socket) {
793+
assert(socket._httpMessage == this);
794+
socket.removeListener('close', onServerResponseClose);
795+
socket._httpMessage = null;
796+
this.socket = this.connection = null;
797+
};
798+
795799
ServerResponse.prototype.writeContinue = function() {
796800
this._writeRaw('HTTP/1.1 100 Continue' + CRLF + CRLF, 'ascii');
797801
this._sent100 = true;

test/simple/test-http-response-close.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,20 @@ var common = require('../common');
2323
var assert = require('assert');
2424
var http = require('http');
2525

26-
var gotEnd = false;
26+
var requestGotEnd = false;
27+
var responseGotEnd = false;
2728

2829
var server = http.createServer(function(req, res) {
2930
res.writeHead(200);
3031
res.write('a');
3132

3233
req.on('close', function() {
33-
console.error('aborted');
34-
gotEnd = true;
34+
console.error('request aborted');
35+
requestGotEnd = true;
36+
});
37+
res.on('close', function() {
38+
console.error('response aborted');
39+
responseGotEnd = true;
3540
});
3641
});
3742
server.listen(common.PORT);
@@ -51,5 +56,6 @@ server.on('listening', function() {
5156
});
5257

5358
process.on('exit', function() {
54-
assert.ok(gotEnd);
59+
assert.ok(requestGotEnd);
60+
assert.ok(responseGotEnd);
5561
});

0 commit comments

Comments
 (0)