1 var optional = require('./lib/optional')
2 , http = require('http')
3 , https = optional('https')
4 , tls = optional('tls')
6 , util = require('util')
7 , stream = require('stream')
9 , querystring = require('querystring')
10 , crypto = require('crypto')
12 , oauth = optional('oauth-sign')
13 , hawk = optional('hawk')
14 , aws = optional('aws-sign')
15 , httpSignature = optional('http-signature')
16 , uuid = require('node-uuid')
17 , mime = require('mime')
18 , tunnel = optional('tunnel-agent')
19 , _safeStringify = require('json-stringify-safe')
21 , ForeverAgent = require('forever-agent')
22 , FormData = optional('form-data')
24 , Cookie = optional('tough-cookie')
25 , CookieJar = Cookie && Cookie.CookieJar
26 , cookieJar = CookieJar && new CookieJar
28 , copy = require('./lib/copy')
29 , debug = require('./lib/debug')
30 , getSafe = require('./lib/getSafe')
33 function safeStringify (obj) {
35 try { ret = JSON.stringify(obj) }
36 catch (e) { ret = _safeStringify(obj) }
41 var isUrl = /^https?:/i
44 // Hacky fix for pre-0.4.4 https
45 if (https && !https.Agent) {
46 https.Agent = function (options) {
47 http.Agent.call(this, options)
49 util.inherits(https.Agent, http.Agent)
50 https.Agent.prototype._getConnection = function (host, port, cb) {
51 var s = tls.connect(port, host, this.options, function () {
52 // do other checks here?
59 function isReadStream (rs) {
60 if (rs.readable && rs.path && rs.mode) {
65 function toBase64 (str) {
66 return (new Buffer(str || "", "ascii")).toString("base64")
70 return crypto.createHash('md5').update(str).digest('hex')
73 function Request (options) {
74 stream.Stream.call(this)
78 if (typeof options === 'string') {
79 options = {uri:options}
82 var reserved = Object.keys(Request.prototype)
83 for (var i in options) {
84 if (reserved.indexOf(i) === -1) {
87 if (typeof options[i] === 'function') {
94 this.explicitMethod = true
97 this.canTunnel = options.tunnel !== false && tunnel;
101 util.inherits(Request, stream.Stream)
102 Request.prototype.init = function (options) {
103 // init() contains all the code to setup the request object.
104 // the actual outgoing request is not started until start() is called
105 // this function is called from both the constructor and on redirect.
107 if (!options) options = {}
109 if (!self.method) self.method = options.method || 'GET'
110 self.localAddress = options.localAddress
113 if (!self.pool && self.pool !== false) self.pool = globalPool
114 self.dests = self.dests || []
115 self.__isRequestRequest = true
117 // Protect against double callback
118 if (!self._callback && self.callback) {
119 self._callback = self.callback
120 self.callback = function () {
121 if (self._callbackCalled) return // Print a warning maybe?
122 self._callbackCalled = true
123 self._callback.apply(self, arguments)
125 self.on('error', self.callback.bind())
126 self.on('complete', self.callback.bind(self, null))
129 if (self.url && !self.uri) {
130 // People use this property instead all the time so why not just support it.
136 // this will throw if unhandled but is handleable when in a redirect
137 return self.emit('error', new Error("options.uri is a required argument"))
139 if (typeof self.uri == "string") self.uri = url.parse(self.uri)
142 if (self.strictSSL === false) {
143 self.rejectUnauthorized = false
147 if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
149 // do the HTTP CONNECT dance using koichik/node-tunnel
150 if (http.globalAgent && self.uri.protocol === "https:" && self.canTunnel) {
151 var tunnelFn = self.proxy.protocol === "http:"
152 ? tunnel.httpsOverHttp : tunnel.httpsOverHttps
154 var tunnelOptions = { proxy: { host: self.proxy.hostname
155 , port: +self.proxy.port
156 , proxyAuth: self.proxy.auth
157 , headers: { Host: self.uri.hostname + ':' +
158 (self.uri.port || self.uri.protocol === 'https:' ? 443 : 80) }}
159 , rejectUnauthorized: self.rejectUnauthorized
162 self.agent = tunnelFn(tunnelOptions)
167 if (!self.uri.pathname) {self.uri.pathname = '/'}
169 if (!self.uri.host) {
170 // Invalid URI: it may generate lot of bad errors, like "TypeError: Cannot call method 'indexOf' of undefined" in CookieJar
171 // Detect and reject it as soon as possible
172 var faultyUri = url.format(self.uri)
173 var message = 'Invalid URI "' + faultyUri + '"'
174 if (Object.keys(options).length === 0) {
175 // No option ? This can be the sign of a redirect
176 // As this is a case where the user cannot do anything (they didn't call request directly with this URL)
177 // they should be warned that it can be caused by a redirection (can save some hair)
178 message += '. This can be caused by a crappy redirection.'
180 self.emit('error', new Error(message))
181 return // This error was fatal
184 self._redirectsFollowed = self._redirectsFollowed || 0
185 self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10
186 self.followRedirect = (self.followRedirect !== undefined) ? self.followRedirect : true
187 self.followAllRedirects = (self.followAllRedirects !== undefined) ? self.followAllRedirects : false
188 if (self.followRedirect || self.followAllRedirects)
189 self.redirects = self.redirects || []
191 self.headers = self.headers ? copy(self.headers) : {}
194 if (!self.hasHeader('host')) {
195 self.setHeader('host', self.uri.hostname)
197 if ( !(self.uri.port === 80 && self.uri.protocol === 'http:') &&
198 !(self.uri.port === 443 && self.uri.protocol === 'https:') )
199 self.setHeader('host', self.getHeader('host') + (':'+self.uri.port) )
204 self.jar(self._jar || options.jar)
206 if (!self.uri.port) {
207 if (self.uri.protocol == 'http:') {self.uri.port = 80}
208 else if (self.uri.protocol == 'https:') {self.uri.port = 443}
211 if (self.proxy && !self.tunnel) {
212 self.port = self.proxy.port
213 self.host = self.proxy.hostname
215 self.port = self.uri.port
216 self.host = self.uri.hostname
219 self.clientErrorHandler = function (error) {
220 if (self._aborted) return
221 if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET'
222 && self.agent.addRequestNoreuse) {
223 self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
228 if (self.timeout && self.timeoutTimer) {
229 clearTimeout(self.timeoutTimer)
230 self.timeoutTimer = null
232 self.emit('error', error)
235 self._parserErrorHandler = function (error) {
237 if (this.res.request) {
238 this.res.request.emit('error', error)
240 this.res.emit('error', error)
243 this._httpMessage.emit('error', error)
248 self.form(options.form)
251 if (options.qs) self.qs(options.qs)
254 self.path = self.uri.path
256 self.path = self.uri.pathname + (self.uri.search || "")
259 if (self.path.length === 0) self.path = '/'
262 // Auth must happen last in case signing is dependent on other headers
264 self.oauth(options.oauth)
268 self.aws(options.aws)
272 self.hawk(options.hawk)
275 if (options.httpSignature) {
276 self.httpSignature(options.httpSignature)
280 if (Object.prototype.hasOwnProperty.call(options.auth, 'username')) options.auth.user = options.auth.username
281 if (Object.prototype.hasOwnProperty.call(options.auth, 'password')) options.auth.pass = options.auth.password
286 options.auth.sendImmediately
290 if (self.uri.auth && !self.hasHeader('authorization')) {
291 var authPieces = self.uri.auth.split(':').map(function(item){ return querystring.unescape(item) })
292 self.auth(authPieces[0], authPieces.slice(1).join(':'), true)
294 if (self.proxy && self.proxy.auth && !self.hasHeader('proxy-authorization') && !self.tunnel) {
295 self.setHeader('proxy-authorization', "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return querystring.unescape(item)}).join(':')))
299 if (self.proxy && !self.tunnel) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
302 self.json(options.json)
303 } else if (options.multipart) {
304 self.boundary = uuid()
305 self.multipart(options.multipart)
310 if (!Buffer.isBuffer(self.body)) {
311 if (Array.isArray(self.body)) {
312 for (var i = 0; i < self.body.length; i++) {
313 length += self.body[i].length
316 self.body = new Buffer(self.body)
317 length = self.body.length
320 length = self.body.length
323 if (!self.hasHeader('content-length')) self.setHeader('content-length', length)
325 throw new Error('Argument error, options.body.')
329 var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
330 , defaultModules = {'http:':http, 'https:':https}
331 , httpModules = self.httpModules || {}
333 self.httpModule = httpModules[protocol] || defaultModules[protocol]
335 if (!self.httpModule) return this.emit('error', new Error("Invalid protocol"))
337 if (options.ca) self.ca = options.ca
340 if (options.agentOptions) self.agentOptions = options.agentOptions
342 if (options.agentClass) {
343 self.agentClass = options.agentClass
344 } else if (options.forever) {
345 self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
347 self.agentClass = self.httpModule.Agent
351 if (self.pool === false) {
354 self.agent = self.agent || self.getAgent()
355 if (self.maxSockets) {
356 // Don't use our pooling if node has the refactored client
357 self.agent.maxSockets = self.maxSockets
359 if (self.pool.maxSockets) {
360 // Don't use our pooling if node has the refactored client
361 self.agent.maxSockets = self.pool.maxSockets
365 self.on('pipe', function (src) {
366 if (self.ntick && self._started) throw new Error("You cannot pipe to this stream after the outbound request has started.")
368 if (isReadStream(src)) {
369 if (!self.hasHeader('content-type')) self.setHeader('content-type', mime.lookup(src.path))
372 for (var i in src.headers) {
373 if (!self.hasHeader(i)) {
374 self.setHeader(i, src.headers[i])
378 if (self._json && !self.hasHeader('content-type'))
379 self.setHeader('content-type', 'application/json')
380 if (src.method && !self.explicitMethod) {
381 self.method = src.method
385 // self.on('pipe', function () {
386 // console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
390 process.nextTick(function () {
391 if (self._aborted) return
394 self.setHeaders(self._form.getHeaders())
395 self._form.pipe(self)
398 if (Array.isArray(self.body)) {
399 self.body.forEach(function (part) {
403 self.write(self.body)
406 } else if (self.requestBodyStream) {
407 console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.")
408 self.requestBodyStream.pipe(self)
409 } else if (!self.src) {
410 if (self.method !== 'GET' && typeof self.method !== 'undefined') {
411 self.setHeader('content-length', 0)
419 // Must call this when following a redirect from https to http or vice versa
420 // Attempts to keep everything as identical as possible, but update the
421 // httpModule, Tunneling agent, and/or Forever Agent in use.
422 Request.prototype._updateProtocol = function () {
424 var protocol = self.uri.protocol
426 if (protocol === 'https:') {
427 // previously was doing http, now doing https
428 // if it's https, then we might need to tunnel now.
429 if (self.proxy && self.canTunnel) {
431 var tunnelFn = self.proxy.protocol === 'http:'
432 ? tunnel.httpsOverHttp : tunnel.httpsOverHttps
433 var tunnelOptions = { proxy: { host: self.proxy.hostname
434 , port: +self.proxy.port
435 , proxyAuth: self.proxy.auth }
436 , rejectUnauthorized: self.rejectUnauthorized
438 self.agent = tunnelFn(tunnelOptions)
442 self.httpModule = https
443 switch (self.agentClass) {
445 self.agentClass = ForeverAgent.SSL
448 self.agentClass = https.Agent
451 // nothing we can do. Just hope for the best.
455 // if there's an agent, we need to get a new one.
456 if (self.agent) self.agent = self.getAgent()
459 // previously was doing https, now doing http
460 // stop any tunneling.
461 if (self.tunnel) self.tunnel = false
462 self.httpModule = http
463 switch (self.agentClass) {
464 case ForeverAgent.SSL:
465 self.agentClass = ForeverAgent
468 self.agentClass = http.Agent
471 // nothing we can do. just hope for the best
475 // if there's an agent, then get a new one.
478 self.agent = self.getAgent()
483 Request.prototype.getAgent = function () {
484 var Agent = this.agentClass
486 if (this.agentOptions) {
487 for (var i in this.agentOptions) {
488 options[i] = this.agentOptions[i]
491 if (this.ca) options.ca = this.ca
492 if (this.ciphers) options.ciphers = this.ciphers
493 if (this.secureProtocol) options.secureProtocol = this.secureProtocol
494 if (typeof this.rejectUnauthorized !== 'undefined') options.rejectUnauthorized = this.rejectUnauthorized
496 if (this.cert && this.key) {
497 options.key = this.key
498 options.cert = this.cert
503 // different types of agents are in different pools
504 if (Agent !== this.httpModule.Agent) {
505 poolKey += Agent.name
508 if (!this.httpModule.globalAgent) {
510 options.host = this.host
511 options.port = this.port
512 if (poolKey) poolKey += ':'
513 poolKey += this.host + ':' + this.port
516 // ca option is only relevant if proxy or destination are https
517 var proxy = this.proxy
518 if (typeof proxy === 'string') proxy = url.parse(proxy)
519 var isHttps = (proxy && proxy.protocol === 'https:') || this.uri.protocol === 'https:'
522 if (poolKey) poolKey += ':'
523 poolKey += options.ca
526 if (typeof options.rejectUnauthorized !== 'undefined') {
527 if (poolKey) poolKey += ':'
528 poolKey += options.rejectUnauthorized
532 poolKey += options.cert.toString('ascii') + options.key.toString('ascii')
534 if (options.ciphers) {
535 if (poolKey) poolKey += ':'
536 poolKey += options.ciphers
539 if (options.secureProtocol) {
540 if (poolKey) poolKey += ':'
541 poolKey += options.secureProtocol
545 if (this.pool === globalPool && !poolKey && Object.keys(options).length === 0 && this.httpModule.globalAgent) {
546 // not doing anything special. Use the globalAgent
547 return this.httpModule.globalAgent
550 // we're using a stored agent. Make sure it's protocol-specific
551 poolKey = this.uri.protocol + poolKey
553 // already generated an agent for this setting
554 if (this.pool[poolKey]) return this.pool[poolKey]
556 return this.pool[poolKey] = new Agent(options)
559 Request.prototype.start = function () {
560 // start() is called once we are ready to send the outgoing HTTP request.
561 // this is usually called on the first write(), end() or on nextTick()
564 if (self._aborted) return
567 self.method = self.method || 'GET'
568 self.href = self.uri.href
570 if (self.src && self.src.stat && self.src.stat.size && !self.hasHeader('content-length')) {
571 self.setHeader('content-length', self.src.stat.size)
574 self.aws(self._aws, true)
577 // We have a method named auth, which is completely different from the http.request
578 // auth option. If we don't remove it, we're gonna have a bad time.
579 var reqOptions = copy(self)
580 delete reqOptions.auth
582 debug('make request', self.uri.href)
583 self.req = self.httpModule.request(reqOptions, self.onResponse.bind(self))
585 if (self.timeout && !self.timeoutTimer) {
586 self.timeoutTimer = setTimeout(function () {
588 var e = new Error("ETIMEDOUT")
590 self.emit("error", e)
593 // Set additional timeout on socket - in case if remote
594 // server freeze after sending headers
595 if (self.req.setTimeout) { // only works on node 0.6+
596 self.req.setTimeout(self.timeout, function () {
599 var e = new Error("ESOCKETTIMEDOUT")
600 e.code = "ESOCKETTIMEDOUT"
601 self.emit("error", e)
607 self.req.on('error', self.clientErrorHandler)
608 self.req.on('drain', function() {
611 self.on('end', function() {
612 if ( self.req.connection ) self.req.connection.removeListener('error', self._parserErrorHandler)
614 self.emit('request', self.req)
616 Request.prototype.onResponse = function (response) {
618 debug('onResponse', self.uri.href, response.statusCode, response.headers)
619 response.on('end', function() {
620 debug('response end', self.uri.href, response.statusCode, response.headers)
623 if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) {
624 response.connection.once('error', self._parserErrorHandler)
627 debug('aborted', self.uri.href)
631 if (self._paused) response.pause()
632 else response.resume()
634 self.response = response
635 response.request = self
636 response.toJSON = toJSON
638 // XXX This is different on 0.10, because SSL is strict by default
639 if (self.httpModule === https &&
641 !response.client.authorized) {
642 debug('strict ssl error', self.uri.href)
643 var sslErr = response.client.authorizationError
644 self.emit('error', new Error('SSL Error: '+ sslErr))
648 if (self.setHost && self.hasHeader('host')) delete self.headers[self.hasHeader('host')]
649 if (self.timeout && self.timeoutTimer) {
650 clearTimeout(self.timeoutTimer)
651 self.timeoutTimer = null
654 var addCookie = function (cookie) {
656 var targetCookieJar = self._jar.setCookie?self._jar:cookieJar;
658 //set the cookie if it's domain in the href's domain.
659 targetCookieJar.setCookie(cookie, self.uri.href, function(err){
661 console.warn('set cookie failed,'+ err)
668 if (hasHeader('set-cookie', response.headers) && (!self._disableCookies)) {
669 var headerName = hasHeader('set-cookie', response.headers)
670 if (Array.isArray(response.headers[headerName])) response.headers[headerName].forEach(addCookie)
671 else addCookie(response.headers[headerName])
674 var redirectTo = null
675 if (response.statusCode >= 300 && response.statusCode < 400 && hasHeader('location', response.headers)) {
676 var location = response.headers[hasHeader('location', response.headers)]
677 debug('redirect', location)
679 if (self.followAllRedirects) {
680 redirectTo = location
681 } else if (self.followRedirect) {
682 switch (self.method) {
687 // Do not follow redirects
690 redirectTo = location
694 } else if (response.statusCode == 401 && self._hasAuth && !self._sentAuth) {
695 var authHeader = response.headers[hasHeader('www-authenticate', response.headers)]
696 var authVerb = authHeader && authHeader.split(' ')[0]
697 debug('reauth', authVerb)
701 self.auth(self._user, self._pass, true)
702 redirectTo = self.uri
706 // TODO: More complete implementation of RFC 2617.
707 // - check challenge.algorithm
708 // - support algorithm="MD5-sess"
709 // - handle challenge.domain
710 // - support qop="auth-int" only
711 // - handle Authentication-Info (not necessarily?)
712 // - check challenge.stale (not necessarily?)
713 // - increase nc (not necessarily?)
715 // http://tools.ietf.org/html/rfc2617#section-3
716 // https://github.com/bagder/curl/blob/master/lib/http_digest.c
719 var re = /([a-z0-9_-]+)=(?:"([^"]+)"|([a-z0-9_-]+))/gi
721 var match = re.exec(authHeader)
723 challenge[match[1]] = match[2] || match[3];
726 var ha1 = md5(self._user + ':' + challenge.realm + ':' + self._pass)
727 var ha2 = md5(self.method + ':' + self.uri.path)
728 var qop = /(^|,)\s*auth\s*($|,)/.test(challenge.qop) && 'auth'
729 var nc = qop && '00000001'
730 var cnonce = qop && uuid().replace(/-/g, '')
731 var digestResponse = qop ? md5(ha1 + ':' + challenge.nonce + ':' + nc + ':' + cnonce + ':' + qop + ':' + ha2) : md5(ha1 + ':' + challenge.nonce + ':' + ha2)
733 username: self._user,
734 realm: challenge.realm,
735 nonce: challenge.nonce,
738 response: digestResponse,
741 algorithm: challenge.algorithm,
742 opaque: challenge.opaque
746 for (var k in authValues) {
747 if (!authValues[k]) {
749 } else if (k === 'qop' || k === 'nc' || k === 'algorithm') {
750 authHeader.push(k + '=' + authValues[k])
752 authHeader.push(k + '="' + authValues[k] + '"')
755 authHeader = 'Digest ' + authHeader.join(', ')
756 self.setHeader('authorization', authHeader)
757 self._sentAuth = true
759 redirectTo = self.uri
765 debug('redirect to', redirectTo)
767 // ignore any potential response body. it cannot possibly be useful
768 // to us at this point.
769 if (self._paused) response.resume()
771 if (self._redirectsFollowed >= self.maxRedirects) {
772 self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop "+self.uri.href))
775 self._redirectsFollowed += 1
777 if (!isUrl.test(redirectTo)) {
778 redirectTo = url.resolve(self.uri.href, redirectTo)
781 var uriPrev = self.uri
782 self.uri = url.parse(redirectTo)
784 // handle the case where we change protocol from https to http or vice versa
785 if (self.uri.protocol !== uriPrev.protocol) {
786 self._updateProtocol()
790 { statusCode : response.statusCode
791 , redirectUri: redirectTo
794 if (self.followAllRedirects && response.statusCode != 401) self.method = 'GET'
795 // self.method = 'GET' // Force all redirects to use GET || commented out fixes #215
800 if (response.statusCode != 401) {
801 // Remove parameters from the previous response, unless this is the second request
802 // for a server that requires digest authentication.
806 if (self.hasHeader('host')) delete self.headers[self.hasHeader('host')]
807 if (self.hasHeader('content-type')) delete self.headers[self.hasHeader('content-type')]
808 if (self.hasHeader('content-length')) delete self.headers[self.hasHeader('content-length')]
812 self.emit('redirect');
815 return // Ignore the rest of the response
817 self._redirectsFollowed = self._redirectsFollowed || 0
818 // Be a good stream and emit end when the response is finished.
819 // Hack to emit end on close because of a core bug that never fires end
820 response.on('close', function () {
821 if (!self._ended) self.response.emit('end')
825 if (self.dests.length !== 0) {
826 console.error("Ignoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.")
828 response.setEncoding(self.encoding)
832 self.emit('response', response)
834 self.dests.forEach(function (dest) {
838 response.on("data", function (chunk) {
839 self._destdata = true
840 self.emit("data", chunk)
842 response.on("end", function (chunk) {
844 self.emit("end", chunk)
846 response.on("close", function () {self.emit("close")})
851 self.on("data", function (chunk) {
853 bodyLen += chunk.length
855 self.on("end", function () {
856 debug('end event', self.uri.href)
858 debug('aborted', self.uri.href)
862 if (buffer.length && Buffer.isBuffer(buffer[0])) {
863 debug('has body', self.uri.href, bodyLen)
864 var body = new Buffer(bodyLen)
866 buffer.forEach(function (chunk) {
867 chunk.copy(body, i, 0, chunk.length)
870 if (self.encoding === null) {
873 response.body = body.toString(self.encoding)
875 } else if (buffer.length) {
876 // The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
877 // Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
878 if (self.encoding === 'utf8' && buffer[0].length > 0 && buffer[0][0] === "\uFEFF") {
879 buffer[0] = buffer[0].substring(1)
881 response.body = buffer.join('')
886 response.body = JSON.parse(response.body)
889 debug('emitting complete', self.uri.href)
890 if(response.body == undefined && !self._json) {
893 self.emit('complete', response, response.body)
898 self.on("end", function () {
900 debug('aborted', self.uri.href)
903 self.emit('complete', response);
907 debug('finish init function', self.uri.href)
910 Request.prototype.abort = function () {
916 else if (this.response) {
917 this.response.abort()
923 Request.prototype.pipeDest = function (dest) {
924 var response = this.response
925 // Called after the response is received
926 if (dest.headers && !dest.headersSent) {
927 if (hasHeader('content-type', response.headers)) {
928 var ctname = hasHeader('content-type', response.headers)
929 if (dest.setHeader) dest.setHeader(ctname, response.headers[ctname])
930 else dest.headers[ctname] = response.headers[ctname]
933 if (hasHeader('content-length', response.headers)) {
934 var clname = hasHeader('content-length', response.headers)
935 if (dest.setHeader) dest.setHeader(clname, response.headers[clname])
936 else dest.headers[clname] = response.headers[clname]
939 if (dest.setHeader && !dest.headersSent) {
940 for (var i in response.headers) {
941 dest.setHeader(i, response.headers[i])
943 dest.statusCode = response.statusCode
945 if (this.pipefilter) this.pipefilter(response, dest)
949 Request.prototype.setHeader = function (name, value, clobber) {
950 if (clobber === undefined) clobber = true
951 if (clobber || !this.hasHeader(name)) this.headers[name] = value
952 else this.headers[this.hasHeader(name)] += ',' + value
955 Request.prototype.setHeaders = function (headers) {
956 for (var i in headers) {this.setHeader(i, headers[i])}
959 Request.prototype.hasHeader = function (header, headers) {
960 var headers = Object.keys(headers || this.headers)
961 , lheaders = headers.map(function (h) {return h.toLowerCase()})
963 header = header.toLowerCase()
964 for (var i=0;i<lheaders.length;i++) {
965 if (lheaders[i] === header) return headers[i]
970 var hasHeader = Request.prototype.hasHeader
972 Request.prototype.qs = function (q, clobber) {
974 if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
981 if (qs.stringify(base) === ''){
985 this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
987 this.path = this.uri.path
991 Request.prototype.form = function (form) {
993 this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8')
994 this.body = qs.stringify(form).toString('utf8')
997 // create form-data object
998 this._form = new FormData()
1001 Request.prototype.multipart = function (multipart) {
1005 if (!self.hasHeader('content-type')) {
1006 self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
1008 var headerName = self.hasHeader('content-type');
1009 self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
1012 if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
1014 if (self.preambleCRLF) {
1015 self.body.push(new Buffer('\r\n'))
1018 multipart.forEach(function (part) {
1019 var body = part.body
1020 if(body == null) throw Error('Body attribute missing in multipart.')
1022 var preamble = '--' + self.boundary + '\r\n'
1023 Object.keys(part).forEach(function (key) {
1024 preamble += key + ': ' + part[key] + '\r\n'
1027 self.body.push(new Buffer(preamble))
1028 self.body.push(new Buffer(body))
1029 self.body.push(new Buffer('\r\n'))
1031 self.body.push(new Buffer('--' + self.boundary + '--'))
1034 Request.prototype.json = function (val) {
1037 if (!self.hasHeader('accept')) self.setHeader('accept', 'application/json')
1040 if (typeof val === 'boolean') {
1041 if (typeof this.body === 'object') {
1042 this.body = safeStringify(this.body)
1043 self.setHeader('content-type', 'application/json')
1046 this.body = safeStringify(val)
1047 self.setHeader('content-type', 'application/json')
1051 Request.prototype.getHeader = function (name, headers) {
1052 var result, re, match
1053 if (!headers) headers = this.headers
1054 Object.keys(headers).forEach(function (key) {
1055 re = new RegExp(name, 'i')
1056 match = key.match(re)
1057 if (match) result = headers[key]
1061 var getHeader = Request.prototype.getHeader
1063 Request.prototype.auth = function (user, pass, sendImmediately) {
1064 if (typeof user !== 'string' || (pass !== undefined && typeof pass !== 'string')) {
1065 throw new Error('auth() received invalid user or password')
1069 this._hasAuth = true
1070 var header = typeof pass !== 'undefined' ? user + ':' + pass : user
1071 if (sendImmediately || typeof sendImmediately == 'undefined') {
1072 this.setHeader('authorization', 'Basic ' + toBase64(header))
1073 this._sentAuth = true
1077 Request.prototype.aws = function (opts, now) {
1082 var date = new Date()
1083 this.setHeader('date', date.toUTCString())
1086 , secret: opts.secret
1087 , verb: this.method.toUpperCase()
1089 , contentType: this.getHeader('content-type') || ''
1090 , md5: this.getHeader('content-md5') || ''
1091 , amazonHeaders: aws.canonicalizeHeaders(this.headers)
1093 if (opts.bucket && this.path) {
1094 auth.resource = '/' + opts.bucket + this.path
1095 } else if (opts.bucket && !this.path) {
1096 auth.resource = '/' + opts.bucket
1097 } else if (!opts.bucket && this.path) {
1098 auth.resource = this.path
1099 } else if (!opts.bucket && !this.path) {
1102 auth.resource = aws.canonicalizeResource(auth.resource)
1103 this.setHeader('authorization', aws.authorization(auth))
1107 Request.prototype.httpSignature = function (opts) {
1109 httpSignature.signRequest({
1110 getHeader: function(header) {
1111 return getHeader(header, req.headers)
1113 setHeader: function(header, value) {
1114 req.setHeader(header, value)
1116 method: this.method,
1119 debug('httpSignature authorization', this.getHeader('authorization'))
1124 Request.prototype.hawk = function (opts) {
1125 this.setHeader('Authorization', hawk.client.header(this.uri, this.method, opts).field)
1128 Request.prototype.oauth = function (_oauth) {
1130 if (this.hasHeader('content-type') &&
1131 this.getHeader('content-type').slice(0, 'application/x-www-form-urlencoded'.length) ===
1132 'application/x-www-form-urlencoded'
1134 form = qs.parse(this.body)
1136 if (this.uri.query) {
1137 form = qs.parse(this.uri.query)
1139 if (!form) form = {}
1141 for (var i in form) oa[i] = form[i]
1142 for (var i in _oauth) oa['oauth_'+i] = _oauth[i]
1143 if (!oa.oauth_version) oa.oauth_version = '1.0'
1144 if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString()
1145 if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '')
1147 oa.oauth_signature_method = 'HMAC-SHA1'
1149 var consumer_secret = oa.oauth_consumer_secret
1150 delete oa.oauth_consumer_secret
1151 var token_secret = oa.oauth_token_secret
1152 delete oa.oauth_token_secret
1153 var timestamp = oa.oauth_timestamp
1155 var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname
1156 var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret)
1158 // oa.oauth_signature = signature
1159 for (var i in form) {
1160 if ( i.slice(0, 'oauth_') in _oauth) {
1163 delete oa['oauth_'+i]
1164 if (i !== 'x_auth_mode') delete oa[i]
1167 oa.oauth_timestamp = timestamp
1168 var authHeader = 'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',')
1169 authHeader += ',oauth_signature="' + oauth.rfc3986(signature) + '"'
1170 this.setHeader('Authorization', authHeader)
1173 Request.prototype.jar = function (jar) {
1176 if (this._redirectsFollowed === 0) {
1177 this.originalCookieHeader = this.getHeader('cookie')
1183 this._disableCookies = true
1185 var targetCookieJar = (jar && jar.getCookieString)?jar:cookieJar;
1186 var urihref = this.uri.href
1188 //fetch cookie in the Specified host
1189 targetCookieJar.getCookieString(urihref, function(err, hrefCookie){
1191 console.warn('get cookieString failed,' +err)
1193 cookies = hrefCookie
1199 //if need cookie and cookie is not empty
1200 if (cookies && cookies.length) {
1201 if (this.originalCookieHeader) {
1202 // Don't overwrite existing Cookie header
1203 this.setHeader('cookie', this.originalCookieHeader + '; ' + cookies)
1205 this.setHeader('cookie', cookies)
1214 Request.prototype.pipe = function (dest, opts) {
1215 if (this.response) {
1216 if (this._destdata) {
1217 throw new Error("You cannot pipe after data has been emitted from the response.")
1218 } else if (this._ended) {
1219 throw new Error("You cannot pipe after the response has been ended.")
1221 stream.Stream.prototype.pipe.call(this, dest, opts)
1226 this.dests.push(dest)
1227 stream.Stream.prototype.pipe.call(this, dest, opts)
1231 Request.prototype.write = function () {
1232 if (!this._started) this.start()
1233 return this.req.write.apply(this.req, arguments)
1235 Request.prototype.end = function (chunk) {
1236 if (chunk) this.write(chunk)
1237 if (!this._started) this.start()
1240 Request.prototype.pause = function () {
1241 if (!this.response) this._paused = true
1242 else this.response.pause.apply(this.response, arguments)
1244 Request.prototype.resume = function () {
1245 if (!this.response) this._paused = false
1246 else this.response.resume.apply(this.response, arguments)
1248 Request.prototype.destroy = function () {
1249 if (!this._ended) this.end()
1250 else if (this.response) this.response.destroy()
1253 function toJSON () {
1254 return getSafe(this, '__' + (((1+Math.random())*0x10000)|0).toString(16))
1257 Request.prototype.toJSON = toJSON
1260 module.exports = Request