Skip to content

Commit

Permalink
fixed redirection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
icodeforlove committed Apr 3, 2013
1 parent ca59b33 commit 586be48
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions lib/requester.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -295,7 +297,7 @@ var Requester = PromiseObject.create(EventsMixin, {
var self = this,
data = '',
gunzip = zlib.createGunzip();

response.setEncoding('binary');

gunzip = zlib.createGunzip();
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "requester",
"version": "0.1.16",
"version": "0.1.17",
"description": "swiss army knife for requests",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit 586be48

Please sign in to comment.