Skip to content

Commit

Permalink
Enhance redirect handling: (1) response._redirectsFollowed reports th…
Browse files Browse the repository at this point in the history
…e total number of redirects followed instead of being reset to 0; (2) add response.redirects, an array of the response.statusCode and response.headers.location for each redirect.
  • Loading branch information
danmactough committed Sep 20, 2011
1 parent 731b32b commit 9c897df
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Request.prototype.request = function () {
self._redirectsFollowed = self._redirectsFollowed || 0
self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10
self.followRedirect = (self.followRedirect !== undefined) ? self.followRedirect : true
if (self.followRedirect)
self.redirects = self.redirects || []

self.headers = self.headers ? copy(self.headers) : {}

Expand Down Expand Up @@ -259,6 +261,8 @@ Request.prototype.request = function () {
response.headers.location = url.resolve(self.uri.href, response.headers.location)
}
self.uri = response.headers.location
self.redirects.push( { statusCode : response.statusCode,
redirectUri: response.headers.location })
delete self.req
delete self.agent
delete self._started
Expand All @@ -268,7 +272,7 @@ Request.prototype.request = function () {
request(self, self.callback)
return // Ignore the rest of the response
} else {
self._redirectsFollowed = 0
self._redirectsFollowed = self._redirectsFollowed || 0
// Be a good stream and emit end when the response is finished.
// Hack to emit end on close because of a core bug that never fires end
response.on('close', function () {
Expand Down

0 comments on commit 9c897df

Please sign in to comment.