Skip to content

Commit c74652f

Browse files
committed
http: IncomingMessage destroyed state
IncomingMessage should implement _destroy() instead of overriding destroy() in order to behave properly like a stream.
1 parent ddcd235 commit c74652f

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

lib/_http_client.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,9 @@ ClientRequest.prototype.abort = function abort() {
318318

319319
// If we're aborting, we don't care about any more response data.
320320
if (this.res) {
321-
this.res._dump();
321+
// TODO(ronag): No more data events should occur after destroy.
322+
this.res.removeAllListeners('data');
323+
this.res.destroy();
322324
}
323325

324326
// In the event that we don't have a socket, we will pop out of
@@ -365,11 +367,11 @@ function socketCloseListener() {
365367
req.emit('close');
366368
if (res.readable) {
367369
res.on('end', function() {
368-
this.emit('close');
370+
res.destroy();
369371
});
370372
res.push(null);
371373
} else {
372-
res.emit('close');
374+
res.destroy();
373375
}
374376
} else {
375377
if (!req.socket._hadError) {

lib/_http_incoming.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ IncomingMessage.prototype._read = function _read(n) {
109109
// It's possible that the socket will be destroyed, and removed from
110110
// any messages, before ever calling this. In that case, just skip
111111
// it, since something else is destroying this connection anyway.
112-
IncomingMessage.prototype.destroy = function destroy(error) {
112+
IncomingMessage.prototype._destroy = function destroy(err, cb) {
113113
if (this.socket)
114-
this.socket.destroy(error);
114+
this.socket.destroy(err, cb);
115+
else
116+
cb(err);
115117
};
116118

117119

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
7+
{
8+
const server = http.Server(function(req, res) {
9+
req.destroy();
10+
assert.strictEqual(req.destroyed, true);
11+
});
12+
13+
server.listen(0, function() {
14+
http.get({
15+
port: this.address().port,
16+
}).on('error', common.mustCall(() => {
17+
server.close();
18+
}));
19+
});
20+
}
21+
22+
{
23+
const server = http.Server(function(req, res) {
24+
req.destroy(new Error('kaboom'));
25+
req.destroy(new Error('kaboom2'));
26+
assert.strictEqual(req.destroyed, true);
27+
req.on('error', common.mustCall((err) => {
28+
assert.strictEqual(err.message, 'kaboom');
29+
}));
30+
});
31+
32+
server.listen(0, function() {
33+
http.get({
34+
port: this.address().port,
35+
}).on('error', common.mustCall(() => {
36+
server.close();
37+
}));
38+
});
39+
}

0 commit comments

Comments
 (0)