diff options
Diffstat (limited to 'deps/npm/node_modules/request/main.js')
-rw-r--r-- | deps/npm/node_modules/request/main.js | 121 |
1 files changed, 101 insertions, 20 deletions
diff --git a/deps/npm/node_modules/request/main.js b/deps/npm/node_modules/request/main.js index f651202740..5a6bd9eb04 100644 --- a/deps/npm/node_modules/request/main.js +++ b/deps/npm/node_modules/request/main.js @@ -137,12 +137,12 @@ Request.prototype.init = function (options) { // do the HTTP CONNECT dance using koichik/node-tunnel if (http.globalAgent && self.uri.protocol === "https:") { - self.tunnel = true var tunnelFn = self.proxy.protocol === "http:" ? tunnel.httpsOverHttp : tunnel.httpsOverHttps var tunnelOptions = { proxy: { host: self.proxy.hostname - , port: +self.proxy.port } + , port: +self.proxy.port + , proxyAuth: self.proxy.auth } , ca: this.ca } self.agent = tunnelFn(tunnelOptions) @@ -170,7 +170,7 @@ Request.prototype.init = function (options) { self.setHost = true } - self.jar(options.jar) + self.jar(self._jar || options.jar) if (!self.uri.pathname) {self.uri.pathname = '/'} if (!self.uri.port) { @@ -348,6 +348,70 @@ Request.prototype.init = function (options) { }) } +// Must call this when following a redirect from https to http or vice versa +// Attempts to keep everything as identical as possible, but update the +// httpModule, Tunneling agent, and/or Forever Agent in use. +Request.prototype._updateProtocol = function () { + var self = this + var protocol = self.uri.protocol + + if (protocol === 'https:') { + // previously was doing http, now doing https + // if it's https, then we might need to tunnel now. + if (self.proxy) { + self.tunnel = true + var tunnelFn = self.proxy.protocol === 'http:' + ? tunnel.httpsOverHttp : tunnel.httpsOverHttps + var tunnelOptions = { proxy: { host: self.proxy.hostname + , post: +self.proxy.port + , proxyAuth: self.proxy.auth } + , ca: self.ca } + self.agent = tunnelFn(tunnelOptions) + return + } + + self.httpModule = https + switch (self.agentClass) { + case ForeverAgent: + self.agentClass = ForeverAgent.SSL + break + case http.Agent: + self.agentClass = https.Agent + break + default: + // nothing we can do. Just hope for the best. + return + } + + // if there's an agent, we need to get a new one. + if (self.agent) self.agent = self.getAgent() + + } else { + if (log) log('previously https, now http') + // previously was doing https, now doing http + // stop any tunneling. + if (self.tunnel) self.tunnel = false + self.httpModule = http + switch (self.agentClass) { + case ForeverAgent.SSL: + self.agentClass = ForeverAgent + break + case https.Agent: + self.agentClass = http.Agent + break + default: + // nothing we can do. just hope for the best + return + } + + // if there's an agent, then get a new one. + if (self.agent) { + self.agent = null + self.agent = self.getAgent() + } + } +} + Request.prototype.getAgent = function () { var Agent = this.agentClass var options = {} @@ -373,7 +437,11 @@ Request.prototype.getAgent = function () { poolKey += this.host + ':' + this.port } - if (options.ca) { + // ca option is only relevant if proxy or destination are https + var proxy = this.proxy + if (typeof proxy === 'string') proxy = url.parse(proxy) + var caRelevant = (proxy && proxy.protocol === 'https:') || this.uri.protocol === 'https:' + if (options.ca && caRelevant) { if (poolKey) poolKey += ':' poolKey += options.ca } @@ -383,6 +451,9 @@ Request.prototype.getAgent = function () { return this.httpModule.globalAgent } + // we're using a stored agent. Make sure it's protocol-specific + poolKey = this.uri.protocol + poolKey + // already generated an agent for this setting if (this.pool[poolKey]) return this.pool[poolKey] @@ -439,7 +510,15 @@ Request.prototype.start = function () { if (!isUrl.test(response.headers.location)) { response.headers.location = url.resolve(self.uri.href, response.headers.location) } - self.uri = response.headers.location + + var uriPrev = self.uri + self.uri = url.parse(response.headers.location) + + // handle the case where we change protocol from https to http or vice versa + if (self.uri.protocol !== uriPrev.protocol) { + self._updateProtocol() + } + self.redirects.push( { statusCode : response.statusCode , redirectUri: response.headers.location @@ -449,6 +528,7 @@ Request.prototype.start = function () { delete self.req delete self.agent delete self._started + delete self.body if (self.headers) { delete self.headers.host } @@ -598,17 +678,17 @@ Request.prototype.setHeaders = function (headers) { return this } Request.prototype.qs = function (q, clobber) { - 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)) - + var base + if (!clobber && this.uri.query) base = qs.parse(this.uri.query) + else base = {} + + for (var i in q) { + base[i] = q[i] + } + + this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base)) + this.url = this.uri + return this } Request.prototype.form = function (form) { @@ -787,6 +867,7 @@ function initParams(uri, options, callback) { } function request (uri, options, callback) { + if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.') if ((typeof options === 'function') && !callback) callback = options; if (typeof options === 'object') { options.uri = uri; @@ -841,12 +922,12 @@ request.get = request request.post = function (uri, options, callback) { var params = initParams(uri, options, callback); params.options.method = 'POST'; - return request(params.uri, params.options, params.callback) + return request(params.uri || null, params.options, params.callback) } request.put = function (uri, options, callback) { var params = initParams(uri, options, callback); params.options.method = 'PUT' - return request(params.uri, params.options, params.callback) + return request(params.uri || null, params.options, params.callback) } request.head = function (uri, options, callback) { var params = initParams(uri, options, callback); @@ -857,12 +938,12 @@ request.head = function (uri, options, callback) { params.options.multipart) { throw new Error("HTTP HEAD requests MUST NOT include a request body.") } - return request(params.uri, params.options, params.callback) + return request(params.uri || null, params.options, params.callback) } request.del = function (uri, options, callback) { var params = initParams(uri, options, callback); params.options.method = 'DELETE' - return request(params.uri, params.options, params.callback) + return request(params.uri || null, params.options, params.callback) } request.jar = function () { return new CookieJar |