Skip to content

Commit

Permalink
Adding better redirect handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Sep 20, 2010
1 parent f835b5f commit 91616c4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ function request (options, callback) {
}
}

options.followRedirect = options.followRedirect ? options.followRedirect : true;
options._redirectsFollowed = options._redirectsFollowed ? options._redirectsFollowed : 0;
options.maxRedirects = options.maxRedirects ? options.maxRedirects : 10;

options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true;
options.method = options.method ? options.method : 'GET';

options.headers = options.headers ? options.headers : {};
Expand Down Expand Up @@ -79,15 +82,16 @@ function request (options, callback) {
response.addListener("end", function () {
options.client.removeListener("error", clientErrorHandler);

if (response.statusCode > 299 && response.statusCode < 400 && options.followRedirect && response.headers.location) {
if (response.statusCode > 299 && response.statusCode < 400 && options.followRedirect && response.headers.location && (options._redirectsFollowed < options.maxRedirects) ) {
options._redirectsFollowed += 1
options.uri = response.headers.location;
delete options.client;
if (options.headers) {
delete options.headers.host;
}
request(options, callback);
return;
}
} else {options._redirectsFollowed = 0}

if (setHost) delete options.headers.host;
if (callback) callback(null, response, buffer);
Expand Down

0 comments on commit 91616c4

Please sign in to comment.