Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Proper redirects and ip on host header #40

Merged
merged 4 commits into from

2 participants

@ma2rten

I noticed that the port is not set in the host header and that 302 and 303 redirects are not handled as they should (see http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection). This should fix it. Sorry that it took me 4 commits to do so, it's a little late.

Love your project by the way. Keep it up.

@chriso chriso merged commit 1a9bab5 into from
@chriso
Owner

This is perfect thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 20 deletions.
  1. +33 −20 lib/node.io/request.js
View
53 lib/node.io/request.js
@@ -233,25 +233,6 @@ Job.prototype.doRequest = function (method, resource, body, headers, callback, p
//Parse the URL into parts
url = urlparse(resource, false),
- //Determine the port
- port = url.port;
- if (!port) {
- switch (url.protocol) {
- case 'http:':
- port = 80;
- break;
- case 'https:':
- port = 443;
- secure = true;
- break;
- case 'ftp:':
- port = 21;
- break;
- default:
- port = 80;
- }
- }
-
//Get the request path
path = url.pathname || '/';
if (url.search) {
@@ -288,6 +269,27 @@ Job.prototype.doRequest = function (method, resource, body, headers, callback, p
headers['Content-Length'] = body.length;
}
+ //Determine the port and add it to the host header
+ port = url.port;
+ if (!port) {
+ switch (url.protocol) {
+ case 'http:':
+ port = 80;
+ break;
+ case 'https:':
+ port = 443;
+ secure = true;
+ break;
+ case 'ftp:':
+ port = 21;
+ break;
+ default:
+ port = 80;
+ }
+ } else {
+ headers.host += ':' + port;
+ }
+
method = method.toUpperCase();
//Debug request headers
@@ -354,7 +356,18 @@ Job.prototype.doRequest = function (method, resource, body, headers, callback, p
if (redirect.host) {
headers.host = redirect.host;
}
- self.doRequest(method, location, body, headers, callback, parse, ++redirects);
+ if (code === 302 || code === 303) {
+ // morph this post request into a get request
+ for (header in headers) {
+ h = header.toLowerCase();
+ if (h === 'content-length' || h === 'content-type') {
+ delete headers[header];
+ }
+ }
+ self.doRequest('GET', location, null, headers, callback, parse, ++redirects);
+ } else {
+ self.doRequest(method, location, body, headers, callback, parse, ++redirects);
+ }
}
return;
}
Something went wrong with that request. Please try again.