Skip to content

Commit 2ca22aa

Browse files
evanlucascjihrig
authored andcommitted
http: emit abort event from ClientRequest
ClientRequest will now emit an abort event the first time abort() is called. Semver: Minor Fixes: nodejs/node-v0.x-archive#9278 PR-URL: #945 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Chris Dickinson <[email protected]>
1 parent 89e133a commit 2ca22aa

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

doc/api/http.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,13 @@ Emitted when the server sends a '100 Continue' HTTP response, usually because
859859
the request contained 'Expect: 100-continue'. This is an instruction that
860860
the client should send the request body.
861861

862+
### Event: 'abort'
863+
864+
`function () { }`
865+
866+
Emitted when the request has been aborted by the client. This event is only
867+
emitted on the first call to `abort()`.
868+
862869
### request.flush()
863870

864871
Flush the request headers.

lib/_http_client.js

+6
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ ClientRequest.prototype._implicitHeader = function() {
166166
};
167167

168168
ClientRequest.prototype.abort = function() {
169+
var self = this;
170+
if (this.aborted === undefined) {
171+
process.nextTick(function() {
172+
self.emit('abort');
173+
});
174+
}
169175
// Mark as aborting so we can avoid sending queued request data
170176
// This is used as a truthy flag elsewhere. The use of Date.now is for
171177
// debugging purposes only.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var assert = require('assert');
2+
var http = require('http');
3+
var common = require('../common');
4+
var server = http.createServer(function(req, res) {
5+
res.end();
6+
});
7+
var count = 0;
8+
server.listen(common.PORT, function() {
9+
var req = http.request({
10+
port: common.PORT
11+
}, function() {
12+
assert(false, 'should not receive data');
13+
});
14+
15+
req.on('abort', function() {
16+
// should only be emitted once
17+
count++;
18+
server.close();
19+
});
20+
21+
req.end();
22+
req.abort();
23+
req.abort();
24+
});
25+
26+
process.on('exit', function() {
27+
assert.equal(count, 1);
28+
})

0 commit comments

Comments
 (0)