npm: Upgrade to 1.3.17
[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.  For reference:
707         // http://tools.ietf.org/html/rfc2617#section-3
708         // https://github.com/bagder/curl/blob/master/lib/http_digest.c
709
710         var matches = authHeader.match(/([a-z0-9_-]+)="([^"]+)"/gi)
711         var challenge = {}
712
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)
718         }
719
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)
724         var authValues = {
725           username: self._user,
726           realm: challenge.realm,
727           nonce: challenge.nonce,
728           uri: self.uri.path,
729           qop: challenge.qop,
730           response: digestResponse,
731           nc: 1,
732           cnonce: cnonce
733         }
734
735         authHeader = []
736         for (var k in authValues) {
737           authHeader.push(k + '="' + authValues[k] + '"')
738         }
739         authHeader = 'Digest ' + authHeader.join(', ')
740         self.setHeader('authorization', authHeader)
741         self._sentAuth = true
742
743         redirectTo = self.uri
744         break
745     }
746   }
747
748   if (redirectTo) {
749     debug('redirect to', redirectTo)
750
751     // ignore any potential response body.  it cannot possibly be useful
752     // to us at this point.
753     if (self._paused) response.resume()
754
755     if (self._redirectsFollowed >= self.maxRedirects) {
756       self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop "+self.uri.href))
757       return
758     }
759     self._redirectsFollowed += 1
760
761     if (!isUrl.test(redirectTo)) {
762       redirectTo = url.resolve(self.uri.href, redirectTo)
763     }
764
765     var uriPrev = self.uri
766     self.uri = url.parse(redirectTo)
767
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()
771     }
772
773     self.redirects.push(
774       { statusCode : response.statusCode
775       , redirectUri: redirectTo
776       }
777     )
778     if (self.followAllRedirects && response.statusCode != 401) self.method = 'GET'
779     // self.method = 'GET' // Force all redirects to use GET || commented out fixes #215
780     delete self.src
781     delete self.req
782     delete self.agent
783     delete self._started
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.
787       delete self.body
788       delete self._form
789       if (self.headers) {
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')]
793       }
794     }
795
796     self.emit('redirect');
797
798     self.init()
799     return // Ignore the rest of the response
800   } else {
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')
806     })
807
808     if (self.encoding) {
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.")
811       } else {
812         response.setEncoding(self.encoding)
813       }
814     }
815
816     self.emit('response', response)
817
818     self.dests.forEach(function (dest) {
819       self.pipeDest(dest)
820     })
821
822     response.on("data", function (chunk) {
823       self._destdata = true
824       self.emit("data", chunk)
825     })
826     response.on("end", function (chunk) {
827       self._ended = true
828       self.emit("end", chunk)
829     })
830     response.on("close", function () {self.emit("close")})
831
832     if (self.callback) {
833       var buffer = []
834       var bodyLen = 0
835       self.on("data", function (chunk) {
836         buffer.push(chunk)
837         bodyLen += chunk.length
838       })
839       self.on("end", function () {
840         debug('end event', self.uri.href)
841         if (self._aborted) {
842           debug('aborted', self.uri.href)
843           return
844         }
845
846         if (buffer.length && Buffer.isBuffer(buffer[0])) {
847           debug('has body', self.uri.href, bodyLen)
848           var body = new Buffer(bodyLen)
849           var i = 0
850           buffer.forEach(function (chunk) {
851             chunk.copy(body, i, 0, chunk.length)
852             i += chunk.length
853           })
854           if (self.encoding === null) {
855             response.body = body
856           } else {
857             response.body = body.toString(self.encoding)
858           }
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)
864           }
865           response.body = buffer.join('')
866         }
867
868         if (self._json) {
869           try {
870             response.body = JSON.parse(response.body)
871           } catch (e) {}
872         }
873         debug('emitting complete', self.uri.href)
874         if(response.body == undefined && !self._json) {
875           response.body = "";
876         }
877         self.emit('complete', response, response.body)
878       })
879     }
880     //if no callback
881     else{
882       self.on("end", function () {
883         if (self._aborted) {
884           debug('aborted', self.uri.href)
885           return
886         }
887         self.emit('complete', response);
888       });
889     }
890   }
891   debug('finish init function', self.uri.href)
892 }
893
894 Request.prototype.abort = function () {
895   this._aborted = true
896
897   if (this.req) {
898     this.req.abort()
899   }
900   else if (this.response) {
901     this.response.abort()
902   }
903
904   this.emit("abort")
905 }
906
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]
915     }
916
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]
921     }
922   }
923   if (dest.setHeader && !dest.headersSent) {
924     for (var i in response.headers) {
925       dest.setHeader(i, response.headers[i])
926     }
927     dest.statusCode = response.statusCode
928   }
929   if (this.pipefilter) this.pipefilter(response, dest)
930 }
931
932 // Composable API
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
937   return this
938 }
939 Request.prototype.setHeaders = function (headers) {
940   for (var i in headers) {this.setHeader(i, headers[i])}
941   return this
942 }
943 Request.prototype.hasHeader = function (header, headers) {
944   var headers = Object.keys(headers || this.headers)
945     , lheaders = headers.map(function (h) {return h.toLowerCase()})
946     ;
947   header = header.toLowerCase()
948   for (var i=0;i<lheaders.length;i++) {
949     if (lheaders[i] === header) return headers[i]
950   }
951   return false
952 }
953
954 var hasHeader = Request.prototype.hasHeader
955
956 Request.prototype.qs = function (q, clobber) {
957   var base
958   if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
959   else base = {}
960
961   for (var i in q) {
962     base[i] = q[i]
963   }
964
965   if (qs.stringify(base) === ''){
966     return this
967   }
968
969   this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
970   this.url = this.uri
971   this.path = this.uri.path
972
973   return this
974 }
975 Request.prototype.form = function (form) {
976   if (form) {
977     this.setHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8')
978     this.body = qs.stringify(form).toString('utf8')
979     return this
980   }
981   // create form-data object
982   this._form = new FormData()
983   return this._form
984 }
985 Request.prototype.multipart = function (multipart) {
986   var self = this
987   self.body = []
988
989   if (!self.hasHeader('content-type')) {
990     self.setHeader('content-type', 'multipart/related; boundary=' + self.boundary)
991   } else {
992     var headerName = self.hasHeader('content-type');
993     self.setHeader(headerName, self.headers[headerName].split(';')[0] + '; boundary=' + self.boundary)
994   }
995
996   if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
997
998   if (self.preambleCRLF) {
999     self.body.push(new Buffer('\r\n'))
1000   }
1001
1002   multipart.forEach(function (part) {
1003     var body = part.body
1004     if(body == null) throw Error('Body attribute missing in multipart.')
1005     delete part.body
1006     var preamble = '--' + self.boundary + '\r\n'
1007     Object.keys(part).forEach(function (key) {
1008       preamble += key + ': ' + part[key] + '\r\n'
1009     })
1010     preamble += '\r\n'
1011     self.body.push(new Buffer(preamble))
1012     self.body.push(new Buffer(body))
1013     self.body.push(new Buffer('\r\n'))
1014   })
1015   self.body.push(new Buffer('--' + self.boundary + '--'))
1016   return self
1017 }
1018 Request.prototype.json = function (val) {
1019   var self = this
1020
1021   if (!self.hasHeader('accept')) self.setHeader('accept', 'application/json')
1022
1023   this._json = true
1024   if (typeof val === 'boolean') {
1025     if (typeof this.body === 'object') {
1026       this.body = safeStringify(this.body)
1027       self.setHeader('content-type', 'application/json')
1028     }
1029   } else {
1030     this.body = safeStringify(val)
1031     self.setHeader('content-type', 'application/json')
1032   }
1033   return this
1034 }
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]
1042   })
1043   return result
1044 }
1045 var getHeader = Request.prototype.getHeader
1046
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')
1050   }
1051   this._user = user
1052   this._pass = pass
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
1058   }
1059   return this
1060 }
1061 Request.prototype.aws = function (opts, now) {
1062   if (!now) {
1063     this._aws = opts
1064     return this
1065   }
1066   var date = new Date()
1067   this.setHeader('date', date.toUTCString())
1068   var auth =
1069     { key: opts.key
1070     , secret: opts.secret
1071     , verb: this.method.toUpperCase()
1072     , date: date
1073     , contentType: this.getHeader('content-type') || ''
1074     , md5: this.getHeader('content-md5') || ''
1075     , amazonHeaders: aws.canonicalizeHeaders(this.headers)
1076     }
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) {
1084     auth.resource = '/'
1085   }
1086   auth.resource = aws.canonicalizeResource(auth.resource)
1087   this.setHeader('authorization', aws.authorization(auth))
1088
1089   return this
1090 }
1091 Request.prototype.httpSignature = function (opts) {
1092   var req = this
1093   httpSignature.signRequest({
1094     getHeader: function(header) {
1095       return getHeader(header, req.headers)
1096     },
1097     setHeader: function(header, value) {
1098       req.setHeader(header, value)
1099     },
1100     method: this.method,
1101     path: this.path
1102   }, opts)
1103   debug('httpSignature authorization', this.getHeader('authorization'))
1104
1105   return this
1106 }
1107
1108 Request.prototype.hawk = function (opts) {
1109   this.setHeader('Authorization', hawk.client.header(this.uri, this.method, opts).field)
1110 }
1111
1112 Request.prototype.oauth = function (_oauth) {
1113   var form
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'
1117      ) {
1118     form = qs.parse(this.body)
1119   }
1120   if (this.uri.query) {
1121     form = qs.parse(this.uri.query)
1122   }
1123   if (!form) form = {}
1124   var oa = {}
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, '')
1130
1131   oa.oauth_signature_method = 'HMAC-SHA1'
1132
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
1138
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)
1141
1142   // oa.oauth_signature = signature
1143   for (var i in form) {
1144     if ( i.slice(0, 'oauth_') in _oauth) {
1145       // skip
1146     } else {
1147       delete oa['oauth_'+i]
1148       if (i !== 'x_auth_mode') delete oa[i]
1149     }
1150   }
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)
1155   return this
1156 }
1157 Request.prototype.jar = function (jar) {
1158   var cookies
1159
1160   if (this._redirectsFollowed === 0) {
1161     this.originalCookieHeader = this.getHeader('cookie')
1162   }
1163
1164   if (!jar) {
1165     // disable cookies
1166     cookies = false
1167     this._disableCookies = true
1168   } else {
1169     var targetCookieJar = (jar && jar.getCookieString)?jar:cookieJar;
1170     var urihref = this.uri.href
1171
1172     //fetch cookie in the Specified host
1173     targetCookieJar.getCookieString(urihref, function(err, hrefCookie){
1174       if (err){
1175         console.warn('get cookieString failed,' +err)
1176       } else {
1177         cookies = hrefCookie
1178       }
1179     })
1180
1181   }
1182
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)
1188     } else {
1189       this.setHeader('cookie', cookies)
1190     }
1191   }
1192   this._jar = jar
1193   return this
1194 }
1195
1196
1197 // Stream API
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.")
1204     } else {
1205       stream.Stream.prototype.pipe.call(this, dest, opts)
1206       this.pipeDest(dest)
1207       return dest
1208     }
1209   } else {
1210     this.dests.push(dest)
1211     stream.Stream.prototype.pipe.call(this, dest, opts)
1212     return dest
1213   }
1214 }
1215 Request.prototype.write = function () {
1216   if (!this._started) this.start()
1217   return this.req.write.apply(this.req, arguments)
1218 }
1219 Request.prototype.end = function (chunk) {
1220   if (chunk) this.write(chunk)
1221   if (!this._started) this.start()
1222   this.req.end()
1223 }
1224 Request.prototype.pause = function () {
1225   if (!this.response) this._paused = true
1226   else this.response.pause.apply(this.response, arguments)
1227 }
1228 Request.prototype.resume = function () {
1229   if (!this.response) this._paused = false
1230   else this.response.resume.apply(this.response, arguments)
1231 }
1232 Request.prototype.destroy = function () {
1233   if (!this._ended) this.end()
1234   else if (this.response) this.response.destroy()
1235 }
1236
1237 function toJSON () {
1238   return getSafe(this, '__' + (((1+Math.random())*0x10000)|0).toString(16))
1239 }
1240
1241 Request.prototype.toJSON = toJSON
1242
1243
1244 module.exports = Request