Permalink
Browse files

fixed redirection issue

  • Loading branch information...
1 parent ca59b33 commit 586be48e2c0a6620c4ceb1f2899027fcefd5e827 @icodeforlove committed Apr 3, 2013
Showing with 15 additions and 11 deletions.
  1. +14 −10 lib/requester.js
  2. +1 −1 package.json
View
@@ -119,13 +119,14 @@ var RequesterHandler = PromiseObject.create(EventsMixin, {
$options.signRequest = $options.signRequest || this._signRequest;
$options.processResponse = $options.processResponse || this._processResponse;
$options.dataType = $options.dataType || this._dataType;
-
+
if (this._cookiejar) $options.cookiejar = _.extend(this._cookiejar, $options.cookies);
$options.follow = _.isUndefined($options.follow) ? this._follow : $options.follow;
$options.followMax = $options.followMax || this._followMax;
$options.auth = $options.auth || this._auth;
$options.proxy = this._proxies.length && _.isUndefined($options.proxy) ? this._proxies[this._requestID % this._proxies.length] : $options.proxy;
$options.id = this._requestID;
+ $options.handler = this;
this._requestID++;
@@ -139,6 +140,7 @@ var Requester = PromiseObject.create(EventsMixin, {
this._retry = 0;
this._followCount = 0;
this._url = $url;
+ this._handler = $options.handler;
this._multipart = $options.multipart;
this._method = $options.method;
@@ -227,7 +229,7 @@ var Requester = PromiseObject.create(EventsMixin, {
_prepareRequestObject: function () {
var self = this;
-
+
// strip cookie if we dont have one
if (!this._headers.cookie) delete this._headers.cookie;
@@ -275,13 +277,13 @@ var Requester = PromiseObject.create(EventsMixin, {
_normalResponse: function (response) {
var self = this,
data = '';
-
+
response.setEncoding(this._encoding);
response.on('data', function (chunk) {
data += chunk;
});
-
+
response.on('end', function () {
response.parsedUrl = self._url;
response.proxy = self._proxy;
@@ -295,7 +297,7 @@ var Requester = PromiseObject.create(EventsMixin, {
var self = this,
data = '',
gunzip = zlib.createGunzip();
-
+
response.setEncoding('binary');
gunzip = zlib.createGunzip();
@@ -312,7 +314,7 @@ var Requester = PromiseObject.create(EventsMixin, {
response.on('data', function (chunk) {
gunzip.write(new Buffer(chunk, 'binary'));
});
-
+
response.on('end', function () {
response.parsedUrl = self._url;
response.proxy = self._proxy;
@@ -339,14 +341,16 @@ var Requester = PromiseObject.create(EventsMixin, {
if (this._cookiejar) this._cookiejar = _.extend(this._cookiejar, response.cookies);
if (this._follow && this._followCount <= this._followMax && response.statusCode >= 300 && response.statusCode < 400 && response.headers.location) {
- this._url = url.parse(response.headers.location);
- this._followCount++;
- this._attempt();
+ var location = url.parse(response.headers.location);
+ if (!location.hostname) location.hostname = this._url.hostname;
+ if (!location.port) location.port = this._url.port;
+ if (!location.protocol) location.protocol = this._url.protocol;
+ this._handler.get(url.format(location), this._callback);
return;
}
if (this._processResponse) response.body = this._processResponse.call(response, response.body);
-
+
if (this._dataType === 'JSON') {
var json;
try {
View
@@ -1,6 +1,6 @@
{
"name": "requester",
- "version": "0.1.16",
+ "version": "0.1.17",
"description": "swiss army knife for requests",
"main": "index.js",
"directories": {

0 comments on commit 586be48

Please sign in to comment.