-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updates redirect handling #76
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f6e7a85
Updates redirect handling
plessbd a377e1a
put back in this.emit('redirect', res); and fixed some spacing
plessbd b3aff2f
Merge pull request #117 from TooTallNate/fix/stuff
tj 6c1ad80
Release 0.9.4
tj 876aeef
remove wtf isFunction() helper
tj 4aa89d6
refactor isObject()
tj c8d6d5b
add text "parser"
tj 3fe9fcd
Update lib/node/index.js
plessbd efc2d79
Updates redirect handling
plessbd 9087fb0
put back in this.emit('redirect', res); and fixed some spacing
plessbd 94330e6
Update lib/node/index.js
plessbd 4f212bc
rebased?
plessbd 0923e70
Update lib/node/index.js
plessbd e85d1e9
Update test/node/redirects.js
plessbd cfd9baf
Update lib/node/index.js
plessbd ded98e0
Update lib/node/index.js
plessbd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
/*! | ||
* superagent | ||
* Copyright (c) 2011 TJ Holowaychuk <[email protected]> | ||
|
@@ -126,6 +125,9 @@ function Request(method, url) { | |
this.writable = true; | ||
this._redirects = 0; | ||
this.redirects(5); | ||
this._redirectData = false; | ||
this._redirectAuth = false; | ||
this._buffer = true; | ||
this.attachments = []; | ||
this.cookies = ''; | ||
this._redirectList = []; | ||
|
@@ -173,6 +175,33 @@ Request.prototype.redirects = function(n){ | |
return this; | ||
}; | ||
|
||
/** | ||
* Set if redirects should forward data. | ||
* | ||
* @param {boolean} bool | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
|
||
Request.prototype.redirectData = function(bool){ | ||
this._redirectData = bool; | ||
return this; | ||
}; | ||
|
||
/** | ||
* Set the if redirects should forward Auth, | ||
* when redirected in the same domain. | ||
* | ||
* @param {boolean} bool | ||
* @return {Request} for chaining | ||
* @api public | ||
*/ | ||
|
||
Request.prototype.redirectAuth = function(bool){ | ||
this._redirectAuth = bool; | ||
return this; | ||
}; | ||
|
||
/** | ||
* Return a new `Part` for this request. | ||
* | ||
|
@@ -429,20 +458,34 @@ Request.prototype.clearTimeout = function(){ | |
|
||
Request.prototype.redirect = function(res){ | ||
var url = res.headers.location; | ||
var auth = this.req._headers.authorization; | ||
var reqHost = this.req._headers.host; | ||
var seeOther = 303 == res.statusCode; | ||
var idempotent = 'HEAD' == this.method || 'GET' == this.method; | ||
|
||
if (!~url.indexOf('://')) { | ||
if (0 != url.indexOf('//')) { | ||
url = '//' + this.host + url; | ||
} | ||
url = this.protocol + url; | ||
} | ||
|
||
delete this.req; | ||
this.method = 'HEAD' == this.method | ||
? this.method | ||
: 'GET'; | ||
this._data = null; | ||
this.url = url; | ||
|
||
delete this.req; | ||
|
||
if (this._redirectAuth && ~url.indexOf(reqHost)) { | ||
this.set('Authorization', auth); | ||
} | ||
|
||
if (!seeOther && !idempotent && this._redirectData) { | ||
this.send(this._data); | ||
} else { | ||
this.method = 'HEAD' == this.method | ||
? this.method | ||
: 'GET'; | ||
this._data = null; | ||
} | ||
|
||
this._redirectList.push(url); | ||
this.emit('redirect', res); | ||
this.end(this._callback); | ||
|
@@ -653,12 +696,10 @@ Request.prototype.end = function(fn){ | |
|
||
// buffered response | ||
if (buffer) { | ||
res.text = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk){ res.text += chunk; }); | ||
var parse = 'text' == type | ||
? exports.parse.text | ||
: exports.parse[utils.type(res.headers['content-type'] || '')]; | ||
|
||
// parser | ||
var parse = exports.parse[utils.type(res.headers['content-type'] || '')]; | ||
if (parse) { | ||
parse(res, function(err, obj){ | ||
// TODO: handle error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
|
||
exports['application/x-www-form-urlencoded'] = require('./urlencoded'); | ||
exports['application/json'] = require('./json'); | ||
exports.text = require('./text'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
module.exports = function(res, fn){ | ||
res.text = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', function(chunk){ res.text += chunk; }); | ||
res.on('end', fn); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just noticed this, we shouldn't ever do this I dont wan't to buffer data and this will not support streams etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and this patch should have been more focused, this is a completely separate thing, I just didn't read very well :D