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. For reference:
707 // http://tools.ietf.org/html/rfc2617#section-3
708 // https://github.com/bagder/curl/blob/master/lib/http_digest.c
710 var matches = authHeader.match(/([a-z0-9_-]+)="([^"]+)"/gi)
713 for (var i = 0; i < matches.length; i++) {
714 var eqPos = matches[i].indexOf('=')
715 var key = matches[i].substring(0, eqPos)
716 var quotedValue = matches[i].substring(eqPos + 1)
717 challenge[key] = quotedValue.substring(1, quotedValue.length - 1)
720 var ha1 = md5(self._user + ':' + challenge.realm + ':' + self._pass)
721 var ha2 = md5(self.method + ':' + self.uri.path)
722 var cnonce = uuid().replace(/-/g, '')
723 var digestResponse = md5(ha1 + ':' + challenge.nonce + ':1:' + cnonce + ':auth:' + ha2)
725 username: self._user,
726 realm: challenge.realm,
727 nonce: challenge.nonce,
730 response: digestResponse,
736 for (var k in authValues) {
737 authHeader.push(k + '="' + authValues[k] + '"')
739 authHeader = 'Digest ' + authHeader.join(', ')
740 self.setHeader('authorization', authHeader)
741 self._sentAuth = true
743 redirectTo = self.uri
749 debug('redirect to', redirectTo)
751 // ignore any potential response body. it cannot possibly be useful
752 // to us at this point.
753 if (self._paused) response.resume()
755 if (self._redirectsFollowed >= self.maxRedirects) {
756 self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop "+self.uri.href))
759 self._redirectsFollowed += 1
761 if (!isUrl.test(redirectTo)) {
762 redirectTo = url.resolve(self.uri.href, redirectTo)
765 var uriPrev = self.uri
766 self.uri = url.parse(redirectTo)
768 // handle the case where we change protocol from https to http or vice versa
769 if (self.uri.protocol !== uriPrev.protocol) {
770 self._updateProtocol()
774 { statusCode : response.statusCode
775 , redirectUri: redirectTo
778 if (self.followAllRedirects && response.statusCode != 401) self.method = 'GET'
779 // self.method = 'GET' // Force all redirects to use GET || commented out fixes #215
784 if (response.statusCode != 401) {
785 // Remove parameters from the previous response, unless this is the second request
786 // for a server that requires digest authentication.
790 if (self.hasHeader('host')) delete self.headers[self.hasHeader('host')]
791 if (self.hasHeader('content-type')) delete self.headers[self.hasHeader('content-type')]
792 if (self.hasHeader('content-length')) delete self.headers[self.hasHeader('content-length')]
796 self.emit('redirect');
799 return // Ignore the rest of the response
801 self._redirectsFollowed = self._redirectsFollowed || 0
802 // Be a good stream and emit end when the response is finished.
803 // Hack to emit end on close because of a core bug that never fires end
804 response.on('close', function () {
805 if (!self._ended) self.response.emit('end')
809 if (self.dests.length !== 0) {
810 console.error("Ignoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.")
812 response.setEncoding(self.encoding)
816 self.emit('response', response)
818 self.dests.forEach(function (dest) {
822 response.on("data", function (chunk) {
823 self._destdata = true
824 self.emit("data", chunk)
826 response.on("end", function (chunk) {
828 self.emit("end", chunk)
830 response.on("close", function () {self.emit("close")})
835 self.on("data", function (chunk) {
837 bodyLen += chunk.length
839 self.on("end", function () {
840 debug('end event', self.uri.href)
842 debug('aborted', self.uri.href)
846 if (buffer.length && Buffer.isBuffer(buffer[0])) {
847 debug('has body', self.uri.href, bodyLen)
848 var body = new Buffer(bodyLen)
850 buffer.forEach(function (chunk) {
851 chunk.copy(body, i, 0, chunk.length)
854 if (self.encoding === null) {
857 response.body = body.toString(self.encoding)
859 } else if (buffer.length) {
860 // The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
861 // Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
862 if (self.encoding === 'utf8' && buffer[0].length > 0 && buffer[0][0] === "\uFEFF") {
863 buffer[0] = buffer[0].substring(1)
865 response.body = buffer.join('')
870 response.body = JSON.parse(response.body)
873 debug('emitting complete', self.uri.href)
874 if(response.body == undefined && !self._json) {
877 self.emit('complete', response, response.body)
882 self.on("end", function () {
884 debug('aborted', self.uri.href)
887 self.emit('complete', response);
891 debug('finish init function', self.uri.href)
894 Request.prototype.abort = function () {
900 else if (this.response) {
901 this.response.abort()
907 Request.prototype.pipeDest = function (dest) {
908 var response = this.response
909 // Called after the response is received
910 if (dest.headers && !dest.headersSent) {
911 if (hasHeader('content-type', response.headers)) {
912 var ctname = hasHeader('content-type', response.headers)
913 if (dest.setHeader) dest.setHeader(ctname, response.headers[ctname])
914 else dest.headers[ctname] = response.headers[ctname]
917 if (hasHeader('content-length', response.headers)) {
918 var clname = hasHeader('content-length', response.headers)
919 if (dest.setHeader) dest.setHeader(clname, response.headers[clname])
920 else dest.headers[clname] = response.headers[clname]
923 if (dest.setHeader && !dest.headersSent) {
924 for (var i in response.headers) {
925 dest.setHeader(i, response.headers[i])
927 dest.statusCode = response.statusCode
929 if (this.pipefilter) this.pipefilter(response, dest)
933 Request.prototype.setHeader = function (name, value, clobber) {
934 if (clobber === undefined) clobber = true
935 if (clobber || !this.hasHeader(name)) this.headers[name] = value
936 else this.headers[this.hasHeader(name)] += ',' + value
939 Request.prototype.setHeaders = function (headers) {
940 for (var i in headers) {this.setHeader(i, headers[i])}
943 Request.prototype.hasHeader = function (header, headers) {
944 var headers = Object.keys(headers || this.headers)
945 , lheaders = headers.map(function (h) {return h.toLowerCase()})
947 header = header.toLowerCase()
948 for (var i=0;i<lheaders.length;i++) {
949 if (lheaders[i] === header) return headers[i]
954 var hasHeader = Request.prototype.hasHeader
956 Request.prototype.qs = function (q, clobber) {
958 if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
965 if (qs.stringify(base) === ''){
969 this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
971 this.path = this.uri.path
975 Request.prototype.form = function (form) {
977 this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8')
978 this.body = qs.stringify(form).toString('utf8')
981 // create form-data object
982 this._form = new FormData()
985 Request.prototype.multipart = function (multipart) {
989 if (!self.hasHeader('content-type')) {
990 self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
992 var headerName = self.hasHeader('content-type');
993 self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
996 if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
998 if (self.preambleCRLF) {
999 self.body.push(new Buffer('\r\n'))
1002 multipart.forEach(function (part) {
1003 var body = part.body
1004 if(body == null) throw Error('Body attribute missing in multipart.')
1006 var preamble = '--' + self.boundary + '\r\n'
1007 Object.keys(part).forEach(function (key) {
1008 preamble += key + ': ' + part[key] + '\r\n'
1011 self.body.push(new Buffer(preamble))
1012 self.body.push(new Buffer(body))
1013 self.body.push(new Buffer('\r\n'))
1015 self.body.push(new Buffer('--' + self.boundary + '--'))
1018 Request.prototype.json = function (val) {
1021 if (!self.hasHeader('accept')) self.setHeader('accept', 'application/json')
1024 if (typeof val === 'boolean') {
1025 if (typeof this.body === 'object') {
1026 this.body = safeStringify(this.body)
1027 self.setHeader('content-type', 'application/json')
1030 this.body = safeStringify(val)
1031 self.setHeader('content-type', 'application/json')
1035 Request.prototype.getHeader = function (name, headers) {
1036 var result, re, match
1037 if (!headers) headers = this.headers
1038 Object.keys(headers).forEach(function (key) {
1039 re = new RegExp(name, 'i')
1040 match = key.match(re)
1041 if (match) result = headers[key]
1045 var getHeader = Request.prototype.getHeader
1047 Request.prototype.auth = function (user, pass, sendImmediately) {
1048 if (typeof user !== 'string' || (pass !== undefined && typeof pass !== 'string')) {
1049 throw new Error('auth() received invalid user or password')
1053 this._hasAuth = true
1054 var header = typeof pass !== 'undefined' ? user + ':' + pass : user
1055 if (sendImmediately || typeof sendImmediately == 'undefined') {
1056 this.setHeader('authorization', 'Basic ' + toBase64(header))
1057 this._sentAuth = true
1061 Request.prototype.aws = function (opts, now) {
1066 var date = new Date()
1067 this.setHeader('date', date.toUTCString())
1070 , secret: opts.secret
1071 , verb: this.method.toUpperCase()
1073 , contentType: this.getHeader('content-type') || ''
1074 , md5: this.getHeader('content-md5') || ''
1075 , amazonHeaders: aws.canonicalizeHeaders(this.headers)
1077 if (opts.bucket && this.path) {
1078 auth.resource = '/' + opts.bucket + this.path
1079 } else if (opts.bucket && !this.path) {
1080 auth.resource = '/' + opts.bucket
1081 } else if (!opts.bucket && this.path) {
1082 auth.resource = this.path
1083 } else if (!opts.bucket && !this.path) {
1086 auth.resource = aws.canonicalizeResource(auth.resource)
1087 this.setHeader('authorization', aws.authorization(auth))
1091 Request.prototype.httpSignature = function (opts) {
1093 httpSignature.signRequest({
1094 getHeader: function(header) {
1095 return getHeader(header, req.headers)
1097 setHeader: function(header, value) {
1098 req.setHeader(header, value)
1100 method: this.method,
1103 debug('httpSignature authorization', this.getHeader('authorization'))
1108 Request.prototype.hawk = function (opts) {
1109 this.setHeader('Authorization', hawk.client.header(this.uri, this.method, opts).field)
1112 Request.prototype.oauth = function (_oauth) {
1114 if (this.hasHeader('content-type') &&
1115 this.getHeader('content-type').slice(0, 'application/x-www-form-urlencoded'.length) ===
1116 'application/x-www-form-urlencoded'
1118 form = qs.parse(this.body)
1120 if (this.uri.query) {
1121 form = qs.parse(this.uri.query)
1123 if (!form) form = {}
1125 for (var i in form) oa[i] = form[i]
1126 for (var i in _oauth) oa['oauth_'+i] = _oauth[i]
1127 if (!oa.oauth_version) oa.oauth_version = '1.0'
1128 if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( Date.now() / 1000 ).toString()
1129 if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '')
1131 oa.oauth_signature_method = 'HMAC-SHA1'
1133 var consumer_secret = oa.oauth_consumer_secret
1134 delete oa.oauth_consumer_secret
1135 var token_secret = oa.oauth_token_secret
1136 delete oa.oauth_token_secret
1137 var timestamp = oa.oauth_timestamp
1139 var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname
1140 var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret)
1142 // oa.oauth_signature = signature
1143 for (var i in form) {
1144 if ( i.slice(0, 'oauth_') in _oauth) {
1147 delete oa['oauth_'+i]
1148 if (i !== 'x_auth_mode') delete oa[i]
1151 oa.oauth_timestamp = timestamp
1152 var authHeader = 'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',')
1153 authHeader += ',oauth_signature="' + oauth.rfc3986(signature) + '"'
1154 this.setHeader('Authorization', authHeader)
1157 Request.prototype.jar = function (jar) {
1160 if (this._redirectsFollowed === 0) {
1161 this.originalCookieHeader = this.getHeader('cookie')
1167 this._disableCookies = true
1169 var targetCookieJar = (jar && jar.getCookieString)?jar:cookieJar;
1170 var urihref = this.uri.href
1172 //fetch cookie in the Specified host
1173 targetCookieJar.getCookieString(urihref, function(err, hrefCookie){
1175 console.warn('get cookieString failed,' +err)
1177 cookies = hrefCookie
1183 //if need cookie and cookie is not empty
1184 if (cookies && cookies.length) {
1185 if (this.originalCookieHeader) {
1186 // Don't overwrite existing Cookie header
1187 this.setHeader('cookie', this.originalCookieHeader + '; ' + cookies)
1189 this.setHeader('cookie', cookies)
1198 Request.prototype.pipe = function (dest, opts) {
1199 if (this.response) {
1200 if (this._destdata) {
1201 throw new Error("You cannot pipe after data has been emitted from the response.")
1202 } else if (this._ended) {
1203 throw new Error("You cannot pipe after the response has been ended.")
1205 stream.Stream.prototype.pipe.call(this, dest, opts)
1210 this.dests.push(dest)
1211 stream.Stream.prototype.pipe.call(this, dest, opts)
1215 Request.prototype.write = function () {
1216 if (!this._started) this.start()
1217 return this.req.write.apply(this.req, arguments)
1219 Request.prototype.end = function (chunk) {
1220 if (chunk) this.write(chunk)
1221 if (!this._started) this.start()
1224 Request.prototype.pause = function () {
1225 if (!this.response) this._paused = true
1226 else this.response.pause.apply(this.response, arguments)
1228 Request.prototype.resume = function () {
1229 if (!this.response) this._paused = false
1230 else this.response.resume.apply(this.response, arguments)
1232 Request.prototype.destroy = function () {
1233 if (!this._ended) this.end()
1234 else if (this.response) this.response.destroy()
1237 function toJSON () {
1238 return getSafe(this, '__' + (((1+Math.random())*0x10000)|0).toString(16))
1241 Request.prototype.toJSON = toJSON
1244 module.exports = Request