Skip to content

Commit

Permalink
Add abort support to the returned request
Browse files Browse the repository at this point in the history
  • Loading branch information
itay committed Feb 24, 2012
1 parent 00d0d9f commit 0f580e6
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ function Request (options) {
})

process.nextTick(function () {
if (self._wasAborted) {
self.emit("error", "abort");
return;
}

if (self.body) {
if (Array.isArray(self.body)) {
self.body.forEach(function(part) {
Expand All @@ -306,6 +311,10 @@ function Request (options) {
}
self.ntick = true
})

self.abort = function() {
self._wasAborted = true;
}
}
util.inherits(Request, stream.Stream)
Request.prototype.getAgent = function (host, port) {
Expand All @@ -316,6 +325,12 @@ Request.prototype.getAgent = function (host, port) {
}
Request.prototype.start = function () {
var self = this

if (self._wasAborted) {
self.emit("error", "abort");
return;
}

self._started = true
self.method = self.method || 'GET'
self.href = self.uri.href
Expand All @@ -324,6 +339,12 @@ Request.prototype.start = function () {
self.response = response
response.request = self

self.abort = function() {
self.req.abort();
self.emit("error", "abort");
return;
}

if (self.httpModule === https &&
self.strictSSL &&
!response.client.authorized) {
Expand Down Expand Up @@ -445,6 +466,12 @@ Request.prototype.start = function () {
}
})

self.abort = function() {
self.req.abort();
self.emit("error", "abort");
return;
}

if (self.timeout && !self.timeoutTimer) {
self.timeoutTimer = setTimeout(function() {
self.req.abort()
Expand Down

0 comments on commit 0f580e6

Please sign in to comment.