Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

I believe this closes the issues regarding HTTP codes for temp redirects/found in 1.0/1.1 #30

Closed
wants to merge 1 commit into from

2 participants

@EvanCarroll

Minor code cleanup and fixed #29 with regards to redirecting HTTP 302/303/307 and POST, added myself to contributors in package.json

@EvanCarroll EvanCarroll Minor code cleanup and fixed #29 with regards to redirecting HTTP 302…
…/303/307 and POST, added myself to contributors in package.json
f033f20
@mikeal
Owner

this no longer merges cleanly.

@mikeal mikeal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 5, 2011
  1. @EvanCarroll

    Minor code cleanup and fixed #29 with regards to redirecting HTTP 302…

    EvanCarroll authored
    …/303/307 and POST, added myself to contributors in package.json
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 8 deletions.
  1. +27 −8 main.js
  2. +6 −0 package.json
View
35 main.js
@@ -94,8 +94,10 @@ Request.prototype.request = function () {
}
options._redirectsFollowed = options._redirectsFollowed || 0;
+ options._http302ashttp303 = true;
options.maxRedirects = (options.maxRedirects !== undefined) ? options.maxRedirects : 10;
options.followRedirect = (options.followRedirect !== undefined) ? options.followRedirect : true;
+ options.approveRedirect = (options.approveRedirect !== undefined) ? options.approveRedirect : true;
options.method = options.method || 'GET';
options.headers = options.headers || {};
@@ -209,27 +211,44 @@ Request.prototype.request = function () {
options.response = response;
if (setHost) delete options.headers.host;
- if (response.statusCode >= 300 &&
- response.statusCode < 400 &&
- options.followRedirect &&
- options.method !== 'PUT' &&
- options.method !== 'POST' &&
- response.headers.location) {
- if (options._redirectsFollowed >= options.maxRedirects) {
+ if (
+ options.followRedirect &&
+ response.headers.location &&
+ ( response.statusCode >= 300 && response.statusCode < 400 ) &&
+ ( options.approveRedirect || options.method === 'GET' || options.method === 'HEAD' )
+ ) {
+
+ options._redirectsFollowed += 1;
+ if (options._redirectsFollowed > options.maxRedirects) {
options.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."));
}
- options._redirectsFollowed += 1;
+
if (!isUrl.test(response.headers.location)) {
response.headers.location = url.resolve(options.uri.href, response.headers.location);
}
+
+ if (
+ options.method == 'POST' &&
+ (
+ response.statusCode === 303 ||
+ ( response.statusCode === 302 && options._http302ashttp303 )
+ )
+ ) {
+ options.method = 'GET';
+ }
+
options.uri = response.headers.location;
+
delete options.req;
delete options.agent;
+
if (options.headers) {
delete options.headers.host;
}
+
request(options, options.callback);
return; // Ignore the rest of the response
+
} else {
options._redirectsFollowed = 0;
// Be a good stream and emit end when the response is finished.
View
6 package.json
@@ -11,4 +11,10 @@
{ "web" : "http://github.com/mikeal/request/issues" }
, "engines" : ["node >= 0.3.6"]
, "main" : "./main"
+, "contributors" : [
+ { "name" : "Evan Carroll",
+ "url" : "http://www.evancarroll.com",
+ "email" : "me@evancarroll.com"
+ }
+ ]
}
Something went wrong with that request. Please try again.