Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/mikeal/request
Browse files Browse the repository at this point in the history
  • Loading branch information
itay committed Feb 24, 2012
2 parents b693ce6 + 97386b5 commit 1330eef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
25 changes: 16 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ var isUrl = /^https?:/
var globalPool = {}

function Request (options) {
var self = this
stream.Stream.call(self)
self.readable = true
self.writable = true
stream.Stream.call(this)
this.readable = true
this.writable = true

if (typeof options === 'string') {
options = {uri:options}
}

var reserved = Object.keys(Request.prototype)
for (var i in options) {
if (reserved.indexOf(i) === -1) {
self[i] = options[i]
this[i] = options[i]
} else {
if (typeof options[i] === 'function') {
delete options[i]
Expand All @@ -99,6 +98,14 @@ function Request (options) {
}
options = copy(options)

this.init(options)
}
util.inherits(Request, stream.Stream)
Request.prototype.init = function (options) {
var self = this

if (!options) options = {}

if (!self.pool) self.pool = globalPool
self.dests = []
self.__isRequestRequest = true
Expand Down Expand Up @@ -315,7 +322,6 @@ function Request (options) {
self.ntick = true
})
}
util.inherits(Request, stream.Stream)
Request.prototype.getAgent = function (host, port) {
if (!this.pool[host+':'+port]) {
this.pool[host+':'+port] = new this.httpModule.Agent({host:host, port:port})
Expand Down Expand Up @@ -389,7 +395,7 @@ Request.prototype.start = function () {
delete self.headers.host
}
if (log) log('Redirect to %uri', self)
request(self, self.callback)
self.init()
return // Ignore the rest of the response
} else {
self._redirectsFollowed = self._redirectsFollowed || 0
Expand Down Expand Up @@ -454,7 +460,7 @@ Request.prototype.start = function () {
response.body = buffer.join('')
}

if (self.json) {
if (self._json) {
try {
response.body = JSON.parse(response.body)
} catch (e) {}
Expand Down Expand Up @@ -583,6 +589,7 @@ Request.prototype.multipart = function (multipart) {
Request.prototype.json = function (val) {
this.setHeader('content-type', 'application/json')
this.setHeader('accept', 'application/json')
this._json = true
if (typeof val === 'boolean') {
if (typeof this.body === 'object') this.body = JSON.stringify(this.body)
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ "name" : "request"
, "description" : "Simplified HTTP request client."
, "tags" : ["http", "simple", "util", "utility"]
, "version" : "2.9.150"
, "version" : "2.9.151"
, "author" : "Mikeal Rogers <mikeal.rogers@gmail.com>"
, "repository" :
{ "type" : "git"
Expand Down
2 changes: 1 addition & 1 deletion tests/test-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ s.listen(s.port, function () {
});

// test .del(string, function)
request.defaults({headers:{foo:"bar"}}).del(s.url + '/del', function (e, r, b){
request.defaults({headers:{foo:"bar"}, json:true}).del(s.url + '/del', function (e, r, b){
if (e) throw e;
assert.deepEqual('bar', b.foo);
counter += 1;
Expand Down

0 comments on commit 1330eef

Please sign in to comment.