Skip to content

Commit

Permalink
http: emit abort event from ClientRequest
Browse files Browse the repository at this point in the history
ClientRequest will now emit an `abort` event to detect when `abort` is
called.
  • Loading branch information
evanlucas committed Feb 25, 2015
1 parent 8a1e22a commit ea0fb7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,12 @@ Emitted when the server sends a '100 Continue' HTTP response, usually because
the request contained 'Expect: 100-continue'. This is an instruction that
the client should send the request body.

### Event: 'abort'

`function () { }`

Emitted when the request has been aborted by the client.

### request.flush()

Flush the request headers.
Expand Down
6 changes: 6 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ ClientRequest.prototype._implicitHeader = function() {
};

ClientRequest.prototype.abort = function() {
var self = this;
if (this.aborted === undefined) {
process.nextTick(function() {
self.emit('abort');
});
}
// Mark as aborting so we can avoid sending queued request data
// This is used as a truthy flag elsewhere. The use of Date.now is for
// debugging purposes only.
Expand Down
28 changes: 28 additions & 0 deletions test/parallel/test-http-client-abort-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var assert = require('assert');
var http = require('http');
var common = require('../common');
var server = http.createServer(function(req, res) {
res.end();
});
var count = 0;
server.listen(common.PORT, function() {
var req = http.request({
port: common.PORT
}, function() {
assert(false, 'should not receive data');
});

req.on('abort', function() {
// should only be emitted once
count++;
server.close();
});

req.end();
req.abort();
req.abort();
});

process.on('exit', function() {
assert.equal(count, 1);
})

0 comments on commit ea0fb7c

Please sign in to comment.