summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/request/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/request/index.js')
-rwxr-xr-xdeps/npm/node_modules/request/index.js165
1 files changed, 101 insertions, 64 deletions
diff --git a/deps/npm/node_modules/request/index.js b/deps/npm/node_modules/request/index.js
index 29284a1bb..22707be70 100755
--- a/deps/npm/node_modules/request/index.js
+++ b/deps/npm/node_modules/request/index.js
@@ -21,10 +21,11 @@ var http = require('http')
, qs = require('qs')
, querystring = require('querystring')
, crypto = require('crypto')
-
+
, oauth = require('oauth-sign')
, hawk = require('hawk')
, aws = require('aws-sign')
+ , httpSignature = require('http-signature')
, uuid = require('node-uuid')
, mime = require('mime')
, tunnel = require('tunnel-agent')
@@ -32,7 +33,7 @@ var http = require('http')
, ForeverAgent = require('forever-agent')
, FormData = require('form-data')
-
+
, Cookie = require('cookie-jar')
, CookieJar = Cookie.Jar
, cookieJar = new CookieJar
@@ -104,7 +105,7 @@ function Request (options) {
if (typeof options === 'string') {
options = {uri:options}
}
-
+
var reserved = Object.keys(Request.prototype)
for (var i in options) {
if (reserved.indexOf(i) === -1) {
@@ -115,7 +116,11 @@ function Request (options) {
}
}
}
-
+
+ if (options.method) {
+ this.explicitMethod = true
+ }
+
this.init(options)
}
util.inherits(Request, stream.Stream)
@@ -125,14 +130,15 @@ Request.prototype.init = function (options) {
// this function is called from both the constructor and on redirect.
var self = this
if (!options) options = {}
-
- self.method = options.method || 'GET'
-
+
+ if (!self.method) self.method = options.method || 'GET'
+ self.localAddress = options.localAddress
+
debug(options)
if (!self.pool && self.pool !== false) self.pool = globalPool
self.dests = self.dests || []
self.__isRequestRequest = true
-
+
// Protect against double callback
if (!self._callback && self.callback) {
self._callback = self.callback
@@ -157,7 +163,11 @@ Request.prototype.init = function (options) {
} else {
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
}
-
+
+ if (self.strictSSL === false) {
+ self.rejectUnauthorized = false
+ }
+
if (self.proxy) {
if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
@@ -169,8 +179,9 @@ Request.prototype.init = function (options) {
var tunnelOptions = { proxy: { host: self.proxy.hostname
, port: +self.proxy.port
, proxyAuth: self.proxy.auth
- , headers: { Host: self.uri.hostname + ':' +
+ , headers: { Host: self.uri.hostname + ':' +
(self.uri.port || self.uri.protocol === 'https:' ? 443 : 80) }}
+ , rejectUnauthorized: self.rejectUnauthorized
, ca: this.ca }
self.agent = tunnelFn(tunnelOptions)
@@ -212,7 +223,7 @@ Request.prototype.init = function (options) {
}
self.setHost = true
}
-
+
self.jar(self._jar || options.jar)
if (!self.uri.pathname) {self.uri.pathname = '/'}
@@ -231,7 +242,7 @@ Request.prototype.init = function (options) {
self.clientErrorHandler = function (error) {
if (self._aborted) return
-
+
if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET'
&& self.agent.addRequestNoreuse) {
self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
@@ -261,7 +272,7 @@ Request.prototype.init = function (options) {
if (options.form) {
self.form(options.form)
}
-
+
if (options.qs) self.qs(options.qs)
if (self.uri.path) {
@@ -277,31 +288,35 @@ Request.prototype.init = function (options) {
if (options.oauth) {
self.oauth(options.oauth)
}
-
+
if (options.aws) {
self.aws(options.aws)
}
-
+
if (options.hawk) {
self.hawk(options.hawk)
}
+ if (options.httpSignature) {
+ self.httpSignature(options.httpSignature)
+ }
+
if (options.auth) {
self.auth(
- options.auth.user || options.auth.username,
+ (options.auth.user==="") ? options.auth.user : (options.auth.user || options.auth.username ),
options.auth.pass || options.auth.password,
options.auth.sendImmediately)
}
if (self.uri.auth && !self.headers.authorization) {
var authPieces = self.uri.auth.split(':').map(function(item){ return querystring.unescape(item) })
- self.auth(authPieces[0], authPieces[1], true)
+ self.auth(authPieces[0], authPieces.slice(1).join(':'), true)
}
if (self.proxy && self.proxy.auth && !self.headers['proxy-authorization'] && !self.tunnel) {
self.headers['proxy-authorization'] = "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return querystring.unescape(item)}).join(':'))
}
-
+
if (self.proxy && !self.tunnel) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
if (options.json) {
@@ -354,10 +369,6 @@ Request.prototype.init = function (options) {
self.agentClass = self.httpModule.Agent
}
}
-
- if (self.strictSSL === false) {
- self.rejectUnauthorized = false
- }
if (self.pool === false) {
self.agent = false
@@ -389,7 +400,7 @@ Request.prototype.init = function (options) {
}
if (self._json && !self.headers['content-type'] && !self.headers['Content-Type'])
self.headers['content-type'] = 'application/json'
- if (src.method && !self.method) {
+ if (src.method && !self.explicitMethod) {
self.method = src.method
}
}
@@ -401,7 +412,7 @@ Request.prototype.init = function (options) {
process.nextTick(function () {
if (self._aborted) return
-
+
if (self._form) {
self.setHeaders(self._form.getHeaders())
self._form.pipe(self)
@@ -445,6 +456,7 @@ Request.prototype._updateProtocol = function () {
var tunnelOptions = { proxy: { host: self.proxy.hostname
, port: +self.proxy.port
, proxyAuth: self.proxy.auth }
+ , rejectUnauthorized: self.rejectUnauthorized
, ca: self.ca }
self.agent = tunnelFn(tunnelOptions)
return
@@ -588,7 +600,7 @@ Request.prototype.start = function () {
e.code = "ETIMEDOUT"
self.emit("error", e)
}, self.timeout)
-
+
// Set additional timeout on socket - in case if remote
// server freeze after sending headers
if (self.req.setTimeout) { // only works on node 0.6+
@@ -602,7 +614,7 @@ Request.prototype.start = function () {
})
}
}
-
+
self.req.on('error', self.clientErrorHandler)
self.req.on('drain', function() {
self.emit('drain')
@@ -618,7 +630,7 @@ Request.prototype.onResponse = function (response) {
response.on('end', function() {
debug('response end', self.uri.href, response.statusCode, response.headers)
});
-
+
if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) {
response.connection.once('error', self._parserErrorHandler)
}
@@ -769,14 +781,19 @@ Request.prototype.onResponse = function (response) {
delete self.agent
delete self._started
if (response.statusCode != 401) {
+ // Remove parameters from the previous response, unless this is the second request
+ // for a server that requires digest authentication.
delete self.body
delete self._form
+ if (self.headers) {
+ delete self.headers.host
+ delete self.headers['content-type']
+ delete self.headers['content-length']
+ }
}
- if (self.headers) {
- delete self.headers.host
- delete self.headers['content-type']
- delete self.headers['content-length']
- }
+
+ self.emit('redirect');
+
self.init()
return // Ignore the rest of the response
} else {
@@ -795,6 +812,8 @@ Request.prototype.onResponse = function (response) {
}
}
+ self.emit('response', response)
+
self.dests.forEach(function (dest) {
self.pipeDest(dest)
})
@@ -809,8 +828,6 @@ Request.prototype.onResponse = function (response) {
})
response.on("close", function () {self.emit("close")})
- self.emit('response', response)
-
if (self.callback) {
var buffer = []
var bodyLen = 0
@@ -853,6 +870,9 @@ Request.prototype.onResponse = function (response) {
} catch (e) {}
}
debug('emitting complete', self.uri.href)
+ if(response.body == undefined && !self._json) {
+ response.body = "";
+ }
self.emit('complete', response, response.body)
})
}
@@ -862,14 +882,14 @@ Request.prototype.onResponse = function (response) {
Request.prototype.abort = function () {
this._aborted = true
-
+
if (this.req) {
this.req.abort()
}
else if (this.response) {
this.response.abort()
}
-
+
this.emit("abort")
}
@@ -906,7 +926,7 @@ Request.prototype.qs = function (q, clobber) {
var base
if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
else base = {}
-
+
for (var i in q) {
base[i] = q[i]
}
@@ -914,10 +934,11 @@ Request.prototype.qs = function (q, clobber) {
if (qs.stringify(base) === ''){
return this
}
-
+
this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
this.url = this.uri
-
+ this.path = this.uri.path
+
return this
}
Request.prototype.form = function (form) {
@@ -925,7 +946,7 @@ Request.prototype.form = function (form) {
this.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8'
this.body = qs.stringify(form).toString('utf8')
return this
- }
+ }
// create form-data object
this._form = new FormData()
return this._form
@@ -945,7 +966,7 @@ Request.prototype.multipart = function (multipart) {
if (self.preambleCRLF) {
self.body.push(new Buffer('\r\n'))
}
-
+
multipart.forEach(function (part) {
var body = part.body
if(body == null) throw Error('Body attribute missing in multipart.')
@@ -994,7 +1015,7 @@ function getHeader(name, headers) {
return result
}
Request.prototype.auth = function (user, pass, sendImmediately) {
- if (typeof user !== 'string' || typeof pass !== 'string') {
+ if (typeof user !== 'string' || (pass !== undefined && typeof pass !== 'string')) {
throw new Error('auth() received invalid user or password')
}
this._user = user
@@ -1033,7 +1054,23 @@ Request.prototype.aws = function (opts, now) {
}
auth.resource = aws.canonicalizeResource(auth.resource)
this.setHeader('authorization', aws.authorization(auth))
-
+
+ return this
+}
+Request.prototype.httpSignature = function (opts) {
+ var req = this
+ httpSignature.signRequest({
+ getHeader: function(header) {
+ return getHeader(header, req.headers)
+ },
+ setHeader: function(header, value) {
+ req.setHeader(header, value)
+ },
+ method: this.method,
+ path: this.path
+ }, opts)
+ debug('httpSignature authorization', getHeader('authorization', this.headers))
+
return this
}
@@ -1043,15 +1080,15 @@ Request.prototype.hawk = function (opts) {
Request.prototype.oauth = function (_oauth) {
var form
- if (this.headers['content-type'] &&
+ if (this.headers['content-type'] &&
this.headers['content-type'].slice(0, 'application/x-www-form-urlencoded'.length) ===
- 'application/x-www-form-urlencoded'
+ 'application/x-www-form-urlencoded'
) {
form = qs.parse(this.body)
}
if (this.uri.query) {
form = qs.parse(this.uri.query)
- }
+ }
if (!form) form = {}
var oa = {}
for (var i in form) oa[i] = form[i]
@@ -1059,9 +1096,9 @@ Request.prototype.oauth = function (_oauth) {
if (!oa.oauth_version) oa.oauth_version = '1.0'
if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString()
if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '')
-
+
oa.oauth_signature_method = 'HMAC-SHA1'
-
+
var consumer_secret = oa.oauth_consumer_secret
delete oa.oauth_consumer_secret
var token_secret = oa.oauth_token_secret
@@ -1070,11 +1107,11 @@ Request.prototype.oauth = function (_oauth) {
var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname
var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret)
-
+
// oa.oauth_signature = signature
for (var i in form) {
if ( i.slice(0, 'oauth_') in _oauth) {
- // skip
+ // skip
} else {
delete oa['oauth_'+i]
if (i !== 'x_auth_mode') delete oa[i]
@@ -1088,11 +1125,11 @@ Request.prototype.oauth = function (_oauth) {
}
Request.prototype.jar = function (jar) {
var cookies
-
+
if (this._redirectsFollowed === 0) {
this.originalCookieHeader = this.headers.cookie
}
-
+
if (jar === false) {
// disable cookies
cookies = false
@@ -1104,7 +1141,7 @@ Request.prototype.jar = function (jar) {
// fetch cookie from the global cookie jar
cookies = cookieJar.get({ url: this.uri.href })
}
-
+
if (cookies && cookies.length) {
var cookieString = cookies.map(function (c) {
return c.name + "=" + c.value
@@ -1261,9 +1298,9 @@ request.patch = function (uri, options, callback) {
request.head = function (uri, options, callback) {
var params = initParams(uri, options, callback)
params.options.method = 'HEAD'
- if (params.options.body ||
- params.options.requestBodyStream ||
- (params.options.json && typeof params.options.json !== 'boolean') ||
+ if (params.options.body ||
+ params.options.requestBodyStream ||
+ (params.options.json && typeof params.options.json !== 'boolean') ||
params.options.multipart) {
throw new Error("HTTP HEAD requests MUST NOT include a request body.")
}
@@ -1288,23 +1325,23 @@ request.cookie = function (str) {
// Safe toJSON
-function getSafe (self, uuid) {
+function getSafe (self, uuid) {
if (typeof self === 'object' || typeof self === 'function') var safe = {}
if (Array.isArray(self)) var safe = []
var recurse = []
-
+
Object.defineProperty(self, uuid, {})
-
+
var attrs = Object.keys(self).filter(function (i) {
- if (i === uuid) return false
+ if (i === uuid) return false
if ( (typeof self[i] !== 'object' && typeof self[i] !== 'function') || self[i] === null) return true
return !(Object.getOwnPropertyDescriptor(self[i], uuid))
})
-
-
+
+
for (var i=0;i<attrs.length;i++) {
- if ( (typeof self[attrs[i]] !== 'object' && typeof self[attrs[i]] !== 'function') ||
+ if ( (typeof self[attrs[i]] !== 'object' && typeof self[attrs[i]] !== 'function') ||
self[attrs[i]] === null
) {
safe[attrs[i]] = self[attrs[i]]
@@ -1317,7 +1354,7 @@ function getSafe (self, uuid) {
for (var i=0;i<recurse.length;i++) {
safe[recurse[i]] = getSafe(self[recurse[i]], uuid)
}
-
+
return safe
}