Skip to content

Commit

Permalink
Modified how the qs function works, it now no longer tweaks the exist…
Browse files Browse the repository at this point in the history
…ing request uri, instead it recreates a new one. This allows me to revert all the other changes I had to make previously and gives a nice clean commit that is self contained.
  • Loading branch information
Chris Sainty committed Feb 19, 2012
1 parent becedaa commit 9b2bbf0
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function Request (options) {
if (!self.uri) {
throw new Error("options.uri is a required argument")
} else {
if (typeof self.uri == "string") self.uri = url.parse(self.uri, true)
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
}
if (self.proxy) {
if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
Expand Down Expand Up @@ -201,11 +201,15 @@ function Request (options) {
self.headers['proxy-authorization'] = "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
}

self.path = self.uri.pathname
if (options.qs) self.qs(options.qs)

if (self.path.length === 0) self.path = '/'
if (self.uri.path) {
self.path = self.uri.path
} else {
self.path = self.uri.pathname + (self.uri.search || "")
}

if (options.qs) self.qs(options.qs)
if (self.path.length === 0) self.path = '/'

if (self.proxy) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)

Expand Down Expand Up @@ -495,14 +499,16 @@ Request.prototype.setHeaders = function (headers) {
return this
}
Request.prototype.qs = function (q, clobber) {
if (clobber) this.uri.query = q
else for (i in q) this.uri.query[i] = q[i]

this.uri.search = qs.stringify(this.uri.query)
if (this.uri.search !== '') {
this.uri.search = '?' + this.uri.search
this.path += this.uri.search
}
var uri = {
protocol: this.uri.protocol,
host: this.uri.host,
pathname: this.uri.pathname,
query: clobber ? q : qs.parse(this.uri.query),
hash: this.uri.hash
};
if (!clobber) for (var i in q) uri.query[i] = q[i]

this.uri= url.parse(url.format(uri))

return this
}
Expand Down Expand Up @@ -555,8 +561,9 @@ Request.prototype.oauth = function (_oauth) {
'application/x-www-form-urlencoded'
) {
form = qs.parse(this.body)
} else if (this.uri.query) {
form = this.uri.query
}
if (this.uri.query) {
form = qs.parse(this.uri.query)
}
if (!form) form = {}
var oa = {}
Expand Down

0 comments on commit 9b2bbf0

Please sign in to comment.