Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

http: emit abort event from ClientRequest #9311

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,14 @@ 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.

### request.flushHeaders()
### Event: 'abort'

`function () { }`

Emitted when the request has been aborted by the client. This event is only
emitted on the first call to `abort()`.

### request.flush()

Flush the request headers.

Expand Down
9 changes: 9 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ ClientRequest.prototype._implicitHeader = function() {
};

ClientRequest.prototype.abort = function() {
if (this.aborted !== undefined)
return;

var self = this;

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/simple/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);
})