npm: Upgrade to 1.3.19
[platform/upstream/nodejs.git] / deps / npm / node_modules / request / request.js
1 var optional = require('./lib/optional')
2   , http = require('http')
3   , https = optional('https')
4   , tls = optional('tls')
5   , url = require('url')
6   , util = require('util')
7   , stream = require('stream')
8   , qs = require('qs')
9   , querystring = require('querystring')
10   , crypto = require('crypto')
11
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')
20
21   , ForeverAgent = require('forever-agent')
22   , FormData = optional('form-data')
23
24   , Cookie = optional('tough-cookie')
25   , CookieJar = Cookie && Cookie.CookieJar
26   , cookieJar = CookieJar && new CookieJar
27
28   , copy = require('./lib/copy')
29   , debug = require('./lib/debug')
30   , getSafe = require('./lib/getSafe')
31   ;
32
33 function safeStringify (obj) {
34   var ret
35   try { ret = JSON.stringify(obj) }
36   catch (e) { ret = _safeStringify(obj) }
37   return ret
38 }
39
40 var globalPool = {}
41 var isUrl = /^https?:/i
42
43
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)
48   }
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?
53       if (cb) cb()
54     })
55     return s
56   }
57 }
58
59 function isReadStream (rs) {
60   if (rs.readable && rs.path && rs.mode) {
61     return true
62   }
63 }
64
65 function toBase64 (str) {
66   return (new Buffer(str || "", "ascii")).toString("base64")
67 }
68
69 function md5 (str) {
70   return crypto.createHash('md5').update(str).digest('hex')
71 }
72
73 function Request (options) {
74   stream.Stream.call(this)
75   this.readable = true
76   this.writable = true
77
78   if (typeof options === 'string') {
79     options = {uri:options}
80   }
81
82   var reserved = Object.keys(Request.prototype)
83   for (var i in options) {
84     if (reserved.indexOf(i) === -1) {
85       this[i] = options[i]
86     } else {
87       if (typeof options[i] === 'function') {
88         delete options[i]
89       }
90     }
91   }
92
93   if (options.method) {
94     this.explicitMethod = true
95   }
96
97   this.canTunnel = options.tunnel !== false && tunnel;
98
99   this.init(options)
100 }
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.
106   var self = this
107   if (!options) options = {}
108
109   if (!self.method) self.method = options.method || 'GET'
110   self.localAddress = options.localAddress
111
112   debug(options)
113   if (!self.pool && self.pool !== false) self.pool = globalPool
114   self.dests = self.dests || []
115   self.__isRequestRequest = true
116
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)
124     }
125     self.on('error', self.callback.bind())
126     self.on('complete', self.callback.bind(self, null))
127   }
128
129   if (self.url && !self.uri) {
130     // People use this property instead all the time so why not just support it.
131     self.uri = self.url
132     delete self.url
133   }
134
135   if (!self.uri) {
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"))
138   } else {
139     if (typeof self.uri == "string") self.uri = url.parse(self.uri)
140   }
141
142   if (self.strictSSL === false) {
143     self.rejectUnauthorized = false
144   }
145
146   if (self.proxy) {
147     if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
148
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
153
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
160                           , ca: this.ca }
161
162       self.agent = tunnelFn(tunnelOptions)
163       self.tunnel = true
164     }
165   }
166
167   if (!self.uri.pathname) {self.uri.pathname = '/'}
168
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.'
179     }
180     self.emit('error', new Error(message))
181     return // This error was fatal
182   }
183
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 || []
190
191   self.headers = self.headers ? copy(self.headers) : {}
192
193   self.setHost = false
194   if (!self.hasHeader('host')) {
195     self.setHeader('host', self.uri.hostname)
196     if (self.uri.port) {
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) )
200     }
201     self.setHost = true
202   }
203
204   self.jar(self._jar || options.jar)
205
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}
209   }
210
211   if (self.proxy && !self.tunnel) {
212     self.port = self.proxy.port
213     self.host = self.proxy.hostname
214   } else {
215     self.port = self.uri.port
216     self.host = self.uri.hostname
217   }
218
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) }
224       self.start()
225       self.req.end()
226       return
227     }
228     if (self.timeout && self.timeoutTimer) {
229       clearTimeout(self.timeoutTimer)
230       self.timeoutTimer = null
231     }
232     self.emit('error', error)
233   }
234
235   self._parserErrorHandler = function (error) {
236     if (this.res) {
237       if (this.res.request) {
238         this.res.request.emit('error', error)
239       } else {
240         this.res.emit('error', error)
241       }
242     } else {
243       this._httpMessage.emit('error', error)
244     }
245   }
246
247   if (options.form) {
248     self.form(options.form)
249   }
250
251   if (options.qs) self.qs(options.qs)
252
253   if (self.uri.path) {
254     self.path = self.uri.path
255   } else {
256     self.path = self.uri.pathname + (self.uri.search || "")
257   }
258
259   if (self.path.length === 0) self.path = '/'
260
261
262   // Auth must happen last in case signing is dependent on other headers
263   if (options.oauth) {
264     self.oauth(options.oauth)
265   }
266
267   if (options.aws) {
268     self.aws(options.aws)
269   }
270
271   if (options.hawk) {
272     self.hawk(options.hawk)
273   }
274
275   if (options.httpSignature) {
276     self.httpSignature(options.httpSignature)
277   }
278
279   if (options.auth) {
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
282     
283     self.auth(
284       options.auth.user,
285       options.auth.pass,
286       options.auth.sendImmediately
287     )
288   }
289
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)
293   }
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(':')))
296   }
297
298
299   if (self.proxy && !self.tunnel) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
300
301   if (options.json) {
302     self.json(options.json)
303   } else if (options.multipart) {
304     self.boundary = uuid()
305     self.multipart(options.multipart)
306   }
307
308   if (self.body) {
309     var length = 0
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
314         }
315       } else {
316         self.body = new Buffer(self.body)
317         length = self.body.length
318       }
319     } else {
320       length = self.body.length
321     }
322     if (length) {
323       if (!self.hasHeader('content-length')) self.setHeader('content-length', length)
324     } else {
325       throw new Error('Argument error, options.body.')
326     }
327   }
328
329   var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
330     , defaultModules = {'http:':http, 'https:':https}
331     , httpModules = self.httpModules || {}
332     ;
333   self.httpModule = httpModules[protocol] || defaultModules[protocol]
334
335   if (!self.httpModule) return this.emit('error', new Error("Invalid protocol"))
336
337   if (options.ca) self.ca = options.ca
338
339   if (!self.agent) {
340     if (options.agentOptions) self.agentOptions = options.agentOptions
341
342     if (options.agentClass) {
343       self.agentClass = options.agentClass
344     } else if (options.forever) {
345       self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
346     } else {
347       self.agentClass = self.httpModule.Agent
348     }
349   }
350
351   if (self.pool === false) {
352     self.agent = false
353   } else {
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
358     }
359     if (self.pool.maxSockets) {
360       // Don't use our pooling if node has the refactored client
361       self.agent.maxSockets = self.pool.maxSockets
362     }
363   }
364
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.")
367     self.src = src
368     if (isReadStream(src)) {
369       if (!self.hasHeader('content-type')) self.setHeader('content-type', mime.lookup(src.path))
370     } else {
371       if (src.headers) {
372         for (var i in src.headers) {
373           if (!self.hasHeader(i)) {
374             self.setHeader(i, src.headers[i])
375           }
376         }
377       }
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
382       }
383     }
384
385     // self.on('pipe', function () {
386     //   console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
387     // })
388   })
389
390   process.nextTick(function () {
391     if (self._aborted) return
392
393     if (self._form) {
394       self.setHeaders(self._form.getHeaders())
395       self._form.pipe(self)
396     }
397     if (self.body) {
398       if (Array.isArray(self.body)) {
399         self.body.forEach(function (part) {
400           self.write(part)
401         })
402       } else {
403         self.write(self.body)
404       }
405       self.end()
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)
412       }
413       self.end()
414     }
415     self.ntick = true
416   })
417 }
418
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 () {
423   var self = this
424   var protocol = self.uri.protocol
425
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) {
430       self.tunnel = true
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
437                           , ca: self.ca }
438       self.agent = tunnelFn(tunnelOptions)
439       return
440     }
441
442     self.httpModule = https
443     switch (self.agentClass) {
444       case ForeverAgent:
445         self.agentClass = ForeverAgent.SSL
446         break
447       case http.Agent:
448         self.agentClass = https.Agent
449         break
450       default:
451         // nothing we can do.  Just hope for the best.
452         return
453     }
454
455     // if there's an agent, we need to get a new one.
456     if (self.agent) self.agent = self.getAgent()
457
458   } else {
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
466         break
467       case https.Agent:
468         self.agentClass = http.Agent
469         break
470       default:
471         // nothing we can do.  just hope for the best
472         return
473     }
474
475     // if there's an agent, then get a new one.
476     if (self.agent) {
477       self.agent = null
478       self.agent = self.getAgent()
479     }
480   }
481 }
482
483 Request.prototype.getAgent = function () {
484   var Agent = this.agentClass
485   var options = {}
486   if (this.agentOptions) {
487     for (var i in this.agentOptions) {
488       options[i] = this.agentOptions[i]
489     }
490   }
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
495
496   if (this.cert && this.key) {
497     options.key = this.key
498     options.cert = this.cert
499   }
500
501   var poolKey = ''
502
503   // different types of agents are in different pools
504   if (Agent !== this.httpModule.Agent) {
505     poolKey += Agent.name
506   }
507
508   if (!this.httpModule.globalAgent) {
509     // node 0.4.x
510     options.host = this.host
511     options.port = this.port
512     if (poolKey) poolKey += ':'
513     poolKey += this.host + ':' + this.port
514   }
515
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:'
520   if (isHttps) {
521     if (options.ca) {
522       if (poolKey) poolKey += ':'
523       poolKey += options.ca
524     }
525
526     if (typeof options.rejectUnauthorized !== 'undefined') {
527       if (poolKey) poolKey += ':'
528       poolKey += options.rejectUnauthorized
529     }
530
531     if (options.cert)
532       poolKey += options.cert.toString('ascii') + options.key.toString('ascii')
533
534     if (options.ciphers) {
535       if (poolKey) poolKey += ':'
536       poolKey += options.ciphers
537     }
538
539     if (options.secureProtocol) {
540       if (poolKey) poolKey += ':'
541       poolKey += options.secureProtocol
542     }
543   }
544
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
548   }
549
550   // we're using a stored agent.  Make sure it's protocol-specific
551   poolKey = this.uri.protocol + poolKey
552
553   // already generated an agent for this setting
554   if (this.pool[poolKey]) return this.pool[poolKey]
555
556   return this.pool[poolKey] = new Agent(options)
557 }
558
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()
562   var self = this
563
564   if (self._aborted) return
565
566   self._started = true
567   self.method = self.method || 'GET'
568   self.href = self.uri.href
569
570   if (self.src && self.src.stat && self.src.stat.size && !self.hasHeader('content-length')) {
571     self.setHeader('content-length', self.src.stat.size)
572   }
573   if (self._aws) {
574     self.aws(self._aws, true)
575   }
576
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
581
582   debug('make request', self.uri.href)
583   self.req = self.httpModule.request(reqOptions, self.onResponse.bind(self))
584
585   if (self.timeout && !self.timeoutTimer) {
586     self.timeoutTimer = setTimeout(function () {
587       self.req.abort()
588       var e = new Error("ETIMEDOUT")
589       e.code = "ETIMEDOUT"
590       self.emit("error", e)
591     }, self.timeout)
592
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 () {
597         if (self.req) {
598           self.req.abort()
599           var e = new Error("ESOCKETTIMEDOUT")
600           e.code = "ESOCKETTIMEDOUT"
601           self.emit("error", e)
602         }
603       })
604     }
605   }
606
607   self.req.on('error', self.clientErrorHandler)
608   self.req.on('drain', function() {
609     self.emit('drain')
610   })
611   self.on('end', function() {
612     if ( self.req.connection ) self.req.connection.removeListener('error', self._parserErrorHandler)
613   })
614   self.emit('request', self.req)
615 }
616 Request.prototype.onResponse = function (response) {
617   var self = this
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)
621   });
622
623   if (response.connection.listeners('error').indexOf(self._parserErrorHandler) === -1) {
624     response.connection.once('error', self._parserErrorHandler)
625   }
626   if (self._aborted) {
627     debug('aborted', self.uri.href)
628     response.resume()
629     return
630   }
631   if (self._paused) response.pause()
632   else response.resume()
633
634   self.response = response
635   response.request = self
636   response.toJSON = toJSON
637
638   // XXX This is different on 0.10, because SSL is strict by default
639   if (self.httpModule === https &&
640       self.strictSSL &&
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))
645     return
646   }
647
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
652   }
653
654   var addCookie = function (cookie) {
655     if (self._jar){
656       var targetCookieJar = self._jar.setCookie?self._jar:cookieJar;
657
658       //set the cookie if it's domain in the href's domain.
659       targetCookieJar.setCookie(cookie, self.uri.href, function(err){
660         if (err){
661             console.warn('set cookie failed,'+ err)
662         }
663       })
664     }
665
666   }
667
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])
672   }
673
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)
678
679     if (self.followAllRedirects) {
680       redirectTo = location
681     } else if (self.followRedirect) {
682       switch (self.method) {
683         case 'PATCH':
684         case 'PUT':
685         case 'POST':
686         case 'DELETE':
687           // Do not follow redirects
688           break
689         default:
690           redirectTo = location
691           break
692       }
693     }
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)
698
699     switch (authVerb) {
700       case 'Basic':
701         self.auth(self._user, self._pass, true)
702         redirectTo = self.uri
703         break
704
705       case 'Digest':
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?)
714         // For reference:
715         // http://tools.ietf.org/html/rfc2617#section-3
716         // https://github.com/bagder/curl/blob/master/lib/http_digest.c
717
718         var challenge = {}
719         var re = /([a-z0-9_-]+)=(?:"([^"]+)"|([a-z0-9_-]+))/gi
720         for (;;) {
721           var match = re.exec(authHeader)
722           if (!match) break
723           challenge[match[1]] = match[2] || match[3];
724         }
725
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)
732         var authValues = {
733           username: self._user,
734           realm: challenge.realm,
735           nonce: challenge.nonce,
736           uri: self.uri.path,
737           qop: qop,
738           response: digestResponse,
739           nc: nc,
740           cnonce: cnonce,
741           algorithm: challenge.algorithm,
742           opaque: challenge.opaque
743         }
744
745         authHeader = []
746         for (var k in authValues) {
747           if (!authValues[k]) {
748             //ignore
749           } else if (k === 'qop' || k === 'nc' || k === 'algorithm') {
750             authHeader.push(k + '=' + authValues[k])
751           } else {
752             authHeader.push(k + '="' + authValues[k] + '"')
753           }
754         }
755         authHeader = 'Digest ' + authHeader.join(', ')
756         self.setHeader('authorization', authHeader)
757         self._sentAuth = true
758
759         redirectTo = self.uri
760         break
761     }
762   }
763
764   if (redirectTo) {
765     debug('redirect to', redirectTo)
766
767     // ignore any potential response body.  it cannot possibly be useful
768     // to us at this point.
769     if (self._paused) response.resume()
770
771     if (self._redirectsFollowed >= self.maxRedirects) {
772       self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop "+self.uri.href))
773       return
774     }
775     self._redirectsFollowed += 1
776
777     if (!isUrl.test(redirectTo)) {
778       redirectTo = url.resolve(self.uri.href, redirectTo)
779     }
780
781     var uriPrev = self.uri
782     self.uri = url.parse(redirectTo)
783
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()
787     }
788
789     self.redirects.push(
790       { statusCode : response.statusCode
791       , redirectUri: redirectTo
792       }
793     )
794     if (self.followAllRedirects && response.statusCode != 401) self.method = 'GET'
795     // self.method = 'GET' // Force all redirects to use GET || commented out fixes #215
796     delete self.src
797     delete self.req
798     delete self.agent
799     delete self._started
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.
803       delete self.body
804       delete self._form
805       if (self.headers) {
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')]
809       }
810     }
811
812     self.emit('redirect');
813
814     self.init()
815     return // Ignore the rest of the response
816   } else {
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')
822     })
823
824     if (self.encoding) {
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.")
827       } else {
828         response.setEncoding(self.encoding)
829       }
830     }
831
832     self.emit('response', response)
833
834     self.dests.forEach(function (dest) {
835       self.pipeDest(dest)
836     })
837
838     response.on("data", function (chunk) {
839       self._destdata = true
840       self.emit("data", chunk)
841     })
842     response.on("end", function (chunk) {
843       self._ended = true
844       self.emit("end", chunk)
845     })
846     response.on("close", function () {self.emit("close")})
847
848     if (self.callback) {
849       var buffer = []
850       var bodyLen = 0
851       self.on("data", function (chunk) {
852         buffer.push(chunk)
853         bodyLen += chunk.length
854       })
855       self.on("end", function () {
856         debug('end event', self.uri.href)
857         if (self._aborted) {
858           debug('aborted', self.uri.href)
859           return
860         }
861
862         if (buffer.length && Buffer.isBuffer(buffer[0])) {
863           debug('has body', self.uri.href, bodyLen)
864           var body = new Buffer(bodyLen)
865           var i = 0
866           buffer.forEach(function (chunk) {
867             chunk.copy(body, i, 0, chunk.length)
868             i += chunk.length
869           })
870           if (self.encoding === null) {
871             response.body = body
872           } else {
873             response.body = body.toString(self.encoding)
874           }
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)
880           }
881           response.body = buffer.join('')
882         }
883
884         if (self._json) {
885           try {
886             response.body = JSON.parse(response.body)
887           } catch (e) {}
888         }
889         debug('emitting complete', self.uri.href)
890         if(response.body == undefined && !self._json) {
891           response.body = "";
892         }
893         self.emit('complete', response, response.body)
894       })
895     }
896     //if no callback
897     else{
898       self.on("end", function () {
899         if (self._aborted) {
900           debug('aborted', self.uri.href)
901           return
902         }
903         self.emit('complete', response);
904       });
905     }
906   }
907   debug('finish init function', self.uri.href)
908 }
909
910 Request.prototype.abort = function () {
911   this._aborted = true
912
913   if (this.req) {
914     this.req.abort()
915   }
916   else if (this.response) {
917     this.response.abort()
918   }
919
920   this.emit("abort")
921 }
922
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]
931     }
932
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]
937     }
938   }
939   if (dest.setHeader && !dest.headersSent) {
940     for (var i in response.headers) {
941       dest.setHeader(i, response.headers[i])
942     }
943     dest.statusCode = response.statusCode
944   }
945   if (this.pipefilter) this.pipefilter(response, dest)
946 }
947
948 // Composable API
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
953   return this
954 }
955 Request.prototype.setHeaders = function (headers) {
956   for (var i in headers) {this.setHeader(i, headers[i])}
957   return this
958 }
959 Request.prototype.hasHeader = function (header, headers) {
960   var headers = Object.keys(headers || this.headers)
961     , lheaders = headers.map(function (h) {return h.toLowerCase()})
962     ;
963   header = header.toLowerCase()
964   for (var i=0;i<lheaders.length;i++) {
965     if (lheaders[i] === header) return headers[i]
966   }
967   return false
968 }
969
970 var hasHeader = Request.prototype.hasHeader
971
972 Request.prototype.qs = function (q, clobber) {
973   var base
974   if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
975   else base = {}
976
977   for (var i in q) {
978     base[i] = q[i]
979   }
980
981   if (qs.stringify(base) === ''){
982     return this
983   }
984
985   this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
986   this.url = this.uri
987   this.path = this.uri.path
988
989   return this
990 }
991 Request.prototype.form = function (form) {
992   if (form) {
993     this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8')
994     this.body = qs.stringify(form).toString('utf8')
995     return this
996   }
997   // create form-data object
998   this._form = new FormData()
999   return this._form
1000 }
1001 Request.prototype.multipart = function (multipart) {
1002   var self = this
1003   self.body = []
1004
1005   if (!self.hasHeader('content-type')) {
1006     self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
1007   } else {
1008     var headerName = self.hasHeader('content-type');
1009     self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
1010   }
1011
1012   if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
1013
1014   if (self.preambleCRLF) {
1015     self.body.push(new Buffer('\r\n'))
1016   }
1017
1018   multipart.forEach(function (part) {
1019     var body = part.body
1020     if(body == null) throw Error('Body attribute missing in multipart.')
1021     delete part.body
1022     var preamble = '--' + self.boundary + '\r\n'
1023     Object.keys(part).forEach(function (key) {
1024       preamble += key + ': ' + part[key] + '\r\n'
1025     })
1026     preamble += '\r\n'
1027     self.body.push(new Buffer(preamble))
1028     self.body.push(new Buffer(body))
1029     self.body.push(new Buffer('\r\n'))
1030   })
1031   self.body.push(new Buffer('--' + self.boundary + '--'))
1032   return self
1033 }
1034 Request.prototype.json = function (val) {
1035   var self = this
1036
1037   if (!self.hasHeader('accept')) self.setHeader('accept', 'application/json')
1038
1039   this._json = true
1040   if (typeof val === 'boolean') {
1041     if (typeof this.body === 'object') {
1042       this.body = safeStringify(this.body)
1043       self.setHeader('content-type', 'application/json')
1044     }
1045   } else {
1046     this.body = safeStringify(val)
1047     self.setHeader('content-type', 'application/json')
1048   }
1049   return this
1050 }
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]
1058   })
1059   return result
1060 }
1061 var getHeader = Request.prototype.getHeader
1062
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')
1066   }
1067   this._user = user
1068   this._pass = pass
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
1074   }
1075   return this
1076 }
1077 Request.prototype.aws = function (opts, now) {
1078   if (!now) {
1079     this._aws = opts
1080     return this
1081   }
1082   var date = new Date()
1083   this.setHeader('date', date.toUTCString())
1084   var auth =
1085     { key: opts.key
1086     , secret: opts.secret
1087     , verb: this.method.toUpperCase()
1088     , date: date
1089     , contentType: this.getHeader('content-type') || ''
1090     , md5: this.getHeader('content-md5') || ''
1091     , amazonHeaders: aws.canonicalizeHeaders(this.headers)
1092     }
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) {
1100     auth.resource = '/'
1101   }
1102   auth.resource = aws.canonicalizeResource(auth.resource)
1103   this.setHeader('authorization', aws.authorization(auth))
1104
1105   return this
1106 }
1107 Request.prototype.httpSignature = function (opts) {
1108   var req = this
1109   httpSignature.signRequest({
1110     getHeader: function(header) {
1111       return getHeader(header, req.headers)
1112     },
1113     setHeader: function(header, value) {
1114       req.setHeader(header, value)
1115     },
1116     method: this.method,
1117     path: this.path
1118   }, opts)
1119   debug('httpSignature authorization', this.getHeader('authorization'))
1120
1121   return this
1122 }
1123
1124 Request.prototype.hawk = function (opts) {
1125   this.setHeader('Authorization', hawk.client.header(this.uri, this.method, opts).field)
1126 }
1127
1128 Request.prototype.oauth = function (_oauth) {
1129   var form
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'
1133      ) {
1134     form = qs.parse(this.body)
1135   }
1136   if (this.uri.query) {
1137     form = qs.parse(this.uri.query)
1138   }
1139   if (!form) form = {}
1140   var oa = {}
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, '')
1146
1147   oa.oauth_signature_method = 'HMAC-SHA1'
1148
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
1154
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)
1157
1158   // oa.oauth_signature = signature
1159   for (var i in form) {
1160     if ( i.slice(0, 'oauth_') in _oauth) {
1161       // skip
1162     } else {
1163       delete oa['oauth_'+i]
1164       if (i !== 'x_auth_mode') delete oa[i]
1165     }
1166   }
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)
1171   return this
1172 }
1173 Request.prototype.jar = function (jar) {
1174   var cookies
1175
1176   if (this._redirectsFollowed === 0) {
1177     this.originalCookieHeader = this.getHeader('cookie')
1178   }
1179
1180   if (!jar) {
1181     // disable cookies
1182     cookies = false
1183     this._disableCookies = true
1184   } else {
1185     var targetCookieJar = (jar && jar.getCookieString)?jar:cookieJar;
1186     var urihref = this.uri.href
1187
1188     //fetch cookie in the Specified host
1189     targetCookieJar.getCookieString(urihref, function(err, hrefCookie){
1190       if (err){
1191         console.warn('get cookieString failed,' +err)
1192       } else {
1193         cookies = hrefCookie
1194       }
1195     })
1196
1197   }
1198
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)
1204     } else {
1205       this.setHeader('cookie', cookies)
1206     }
1207   }
1208   this._jar = jar
1209   return this
1210 }
1211
1212
1213 // Stream API
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.")
1220     } else {
1221       stream.Stream.prototype.pipe.call(this, dest, opts)
1222       this.pipeDest(dest)
1223       return dest
1224     }
1225   } else {
1226     this.dests.push(dest)
1227     stream.Stream.prototype.pipe.call(this, dest, opts)
1228     return dest
1229   }
1230 }
1231 Request.prototype.write = function () {
1232   if (!this._started) this.start()
1233   return this.req.write.apply(this.req, arguments)
1234 }
1235 Request.prototype.end = function (chunk) {
1236   if (chunk) this.write(chunk)
1237   if (!this._started) this.start()
1238   this.req.end()
1239 }
1240 Request.prototype.pause = function () {
1241   if (!this.response) this._paused = true
1242   else this.response.pause.apply(this.response, arguments)
1243 }
1244 Request.prototype.resume = function () {
1245   if (!this.response) this._paused = false
1246   else this.response.resume.apply(this.response, arguments)
1247 }
1248 Request.prototype.destroy = function () {
1249   if (!this._ended) this.end()
1250   else if (this.response) this.response.destroy()
1251 }
1252
1253 function toJSON () {
1254   return getSafe(this, '__' + (((1+Math.random())*0x10000)|0).toString(16))
1255 }
1256
1257 Request.prototype.toJSON = toJSON
1258
1259
1260 module.exports = Request