UNPKG

41.4 kBJavaScriptView Raw
1'use strict'
2
3var http = require('http')
4 , https = require('https')
5 , url = require('url')
6 , util = require('util')
7 , stream = require('stream')
8 , zlib = require('zlib')
9 , hawk = require('hawk')
10 , aws2 = require('aws-sign2')
11 , aws4 = require('aws4')
12 , httpSignature = require('http-signature')
13 , mime = require('mime-types')
14 , stringstream = require('stringstream')
15 , caseless = require('caseless')
16 , ForeverAgent = require('forever-agent')
17 , FormData = require('form-data')
18 , extend = require('extend')
19 , isstream = require('isstream')
20 , isTypedArray = require('is-typedarray').strict
21 , helpers = require('./lib/helpers')
22 , cookies = require('./lib/cookies')
23 , getProxyFromURI = require('./lib/getProxyFromURI')
24 , Querystring = require('./lib/querystring').Querystring
25 , Har = require('./lib/har').Har
26 , Auth = require('./lib/auth').Auth
27 , OAuth = require('./lib/oauth').OAuth
28 , Multipart = require('./lib/multipart').Multipart
29 , Redirect = require('./lib/redirect').Redirect
30 , Tunnel = require('./lib/tunnel').Tunnel
31
32var safeStringify = helpers.safeStringify
33 , isReadStream = helpers.isReadStream
34 , toBase64 = helpers.toBase64
35 , defer = helpers.defer
36 , copy = helpers.copy
37 , version = helpers.version
38 , globalCookieJar = cookies.jar()
39
40
41var globalPool = {}
42
43function filterForNonReserved(reserved, options) {
44 // Filter out properties that are not reserved.
45 // Reserved values are passed in at call site.
46
47 var object = {}
48 for (var i in options) {
49 var notReserved = (reserved.indexOf(i) === -1)
50 if (notReserved) {
51 object[i] = options[i]
52 }
53 }
54 return object
55}
56
57function filterOutReservedFunctions(reserved, options) {
58 // Filter out properties that are functions and are reserved.
59 // Reserved values are passed in at call site.
60
61 var object = {}
62 for (var i in options) {
63 var isReserved = !(reserved.indexOf(i) === -1)
64 var isFunction = (typeof options[i] === 'function')
65 if (!(isReserved && isFunction)) {
66 object[i] = options[i]
67 }
68 }
69 return object
70
71}
72
73// Return a simpler request object to allow serialization
74function requestToJSON() {
75 var self = this
76 return {
77 uri: self.uri,
78 method: self.method,
79 headers: self.headers
80 }
81}
82
83// Return a simpler response object to allow serialization
84function responseToJSON() {
85 var self = this
86 return {
87 statusCode: self.statusCode,
88 body: self.body,
89 headers: self.headers,
90 request: requestToJSON.call(self.request)
91 }
92}
93
94function Request (options) {
95 // if given the method property in options, set property explicitMethod to true
96
97 // extend the Request instance with any non-reserved properties
98 // remove any reserved functions from the options object
99 // set Request instance to be readable and writable
100 // call init
101
102 var self = this
103
104 // start with HAR, then override with additional options
105 if (options.har) {
106 self._har = new Har(self)
107 options = self._har.options(options)
108 }
109
110 stream.Stream.call(self)
111 var reserved = Object.keys(Request.prototype)
112 var nonReserved = filterForNonReserved(reserved, options)
113
114 extend(self, nonReserved)
115 options = filterOutReservedFunctions(reserved, options)
116
117 self.readable = true
118 self.writable = true
119 if (options.method) {
120 self.explicitMethod = true
121 }
122 self._qs = new Querystring(self)
123 self._auth = new Auth(self)
124 self._oauth = new OAuth(self)
125 self._multipart = new Multipart(self)
126 self._redirect = new Redirect(self)
127 self._tunnel = new Tunnel(self)
128 self.init(options)
129}
130
131util.inherits(Request, stream.Stream)
132
133// Debugging
134Request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)
135function debug() {
136 if (Request.debug) {
137 console.error('REQUEST %s', util.format.apply(util, arguments))
138 }
139}
140Request.prototype.debug = debug
141
142Request.prototype.init = function (options) {
143 // init() contains all the code to setup the request object.
144 // the actual outgoing request is not started until start() is called
145 // this function is called from both the constructor and on redirect.
146 var self = this
147 if (!options) {
148 options = {}
149 }
150 self.headers = self.headers ? copy(self.headers) : {}
151
152 // Delete headers with value undefined since they break
153 // ClientRequest.OutgoingMessage.setHeader in node 0.12
154 for (var headerName in self.headers) {
155 if (typeof self.headers[headerName] === 'undefined') {
156 delete self.headers[headerName]
157 }
158 }
159
160 caseless.httpify(self, self.headers)
161
162 if (!self.method) {
163 self.method = options.method || 'GET'
164 }
165 if (!self.localAddress) {
166 self.localAddress = options.localAddress
167 }
168
169 self._qs.init(options)
170
171 debug(options)
172 if (!self.pool && self.pool !== false) {
173 self.pool = globalPool
174 }
175 self.dests = self.dests || []
176 self.__isRequestRequest = true
177
178 // Protect against double callback
179 if (!self._callback && self.callback) {
180 self._callback = self.callback
181 self.callback = function () {
182 if (self._callbackCalled) {
183 return // Print a warning maybe?
184 }
185 self._callbackCalled = true
186 self._callback.apply(self, arguments)
187 }
188 self.on('error', self.callback.bind())
189 self.on('complete', self.callback.bind(self, null))
190 }
191
192 // People use this property instead all the time, so support it
193 if (!self.uri && self.url) {
194 self.uri = self.url
195 delete self.url
196 }
197
198 // If there's a baseUrl, then use it as the base URL (i.e. uri must be
199 // specified as a relative path and is appended to baseUrl).
200 if (self.baseUrl) {
201 if (typeof self.baseUrl !== 'string') {
202 return self.emit('error', new Error('options.baseUrl must be a string'))
203 }
204
205 if (typeof self.uri !== 'string') {
206 return self.emit('error', new Error('options.uri must be a string when using options.baseUrl'))
207 }
208
209 if (self.uri.indexOf('//') === 0 || self.uri.indexOf('://') !== -1) {
210 return self.emit('error', new Error('options.uri must be a path when using options.baseUrl'))
211 }
212
213 // Handle all cases to make sure that there's only one slash between
214 // baseUrl and uri.
215 var baseUrlEndsWithSlash = self.baseUrl.lastIndexOf('/') === self.baseUrl.length - 1
216 var uriStartsWithSlash = self.uri.indexOf('/') === 0
217
218 if (baseUrlEndsWithSlash && uriStartsWithSlash) {
219 self.uri = self.baseUrl + self.uri.slice(1)
220 } else if (baseUrlEndsWithSlash || uriStartsWithSlash) {
221 self.uri = self.baseUrl + self.uri
222 } else if (self.uri === '') {
223 self.uri = self.baseUrl
224 } else {
225 self.uri = self.baseUrl + '/' + self.uri
226 }
227 delete self.baseUrl
228 }
229
230 // A URI is needed by this point, emit error if we haven't been able to get one
231 if (!self.uri) {
232 return self.emit('error', new Error('options.uri is a required argument'))
233 }
234
235 // If a string URI/URL was given, parse it into a URL object
236 if (typeof self.uri === 'string') {
237 self.uri = url.parse(self.uri)
238 }
239
240 // Some URL objects are not from a URL parsed string and need href added
241 if (!self.uri.href) {
242 self.uri.href = url.format(self.uri)
243 }
244
245 // DEPRECATED: Warning for users of the old Unix Sockets URL Scheme
246 if (self.uri.protocol === 'unix:') {
247 return self.emit('error', new Error('`unix://` URL scheme is no longer supported. Please use the format `http://unix:SOCKET:PATH`'))
248 }
249
250 // Support Unix Sockets
251 if (self.uri.host === 'unix') {
252 self.enableUnixSocket()
253 }
254
255 if (self.strictSSL === false) {
256 self.rejectUnauthorized = false
257 }
258
259 if (!self.uri.pathname) {self.uri.pathname = '/'}
260
261 if (!(self.uri.host || (self.uri.hostname && self.uri.port)) && !self.uri.isUnix) {
262 // Invalid URI: it may generate lot of bad errors, like 'TypeError: Cannot call method `indexOf` of undefined' in CookieJar
263 // Detect and reject it as soon as possible
264 var faultyUri = url.format(self.uri)
265 var message = 'Invalid URI "' + faultyUri + '"'
266 if (Object.keys(options).length === 0) {
267 // No option ? This can be the sign of a redirect
268 // As this is a case where the user cannot do anything (they didn't call request directly with this URL)
269 // they should be warned that it can be caused by a redirection (can save some hair)
270 message += '. This can be caused by a crappy redirection.'
271 }
272 // This error was fatal
273 self.abort()
274 return self.emit('error', new Error(message))
275 }
276
277 if (!self.hasOwnProperty('proxy')) {
278 self.proxy = getProxyFromURI(self.uri)
279 }
280
281 self.tunnel = self._tunnel.isEnabled()
282 if (self.proxy) {
283 self._tunnel.setup(options)
284 }
285
286 self._redirect.onRequest(options)
287
288 self.setHost = false
289 if (!self.hasHeader('host')) {
290 var hostHeaderName = self.originalHostHeaderName || 'host'
291 self.setHeader(hostHeaderName, self.uri.hostname)
292 if (self.uri.port) {
293 if ( !(self.uri.port === 80 && self.uri.protocol === 'http:') &&
294 !(self.uri.port === 443 && self.uri.protocol === 'https:') ) {
295 self.setHeader(hostHeaderName, self.getHeader('host') + (':' + self.uri.port) )
296 }
297 }
298 self.setHost = true
299 }
300
301 self.jar(self._jar || options.jar)
302
303 if (!self.uri.port) {
304 if (self.uri.protocol === 'http:') {self.uri.port = 80}
305 else if (self.uri.protocol === 'https:') {self.uri.port = 443}
306 }
307
308 if (self.proxy && !self.tunnel) {
309 self.port = self.proxy.port
310 self.host = self.proxy.hostname
311 } else {
312 self.port = self.uri.port
313 self.host = self.uri.hostname
314 }
315
316 if (options.form) {
317 self.form(options.form)
318 }
319
320 if (options.formData) {
321 var formData = options.formData
322 var requestForm = self.form()
323 var appendFormValue = function (key, value) {
324 if (value && value.hasOwnProperty('value') && value.hasOwnProperty('options')) {
325 requestForm.append(key, value.value, value.options)
326 } else {
327 requestForm.append(key, value)
328 }
329 }
330 for (var formKey in formData) {
331 if (formData.hasOwnProperty(formKey)) {
332 var formValue = formData[formKey]
333 if (formValue instanceof Array) {
334 for (var j = 0; j < formValue.length; j++) {
335 appendFormValue(formKey, formValue[j])
336 }
337 } else {
338 appendFormValue(formKey, formValue)
339 }
340 }
341 }
342 }
343
344 if (options.qs) {
345 self.qs(options.qs)
346 }
347
348 if (self.uri.path) {
349 self.path = self.uri.path
350 } else {
351 self.path = self.uri.pathname + (self.uri.search || '')
352 }
353
354 if (self.path.length === 0) {
355 self.path = '/'
356 }
357
358 // Auth must happen last in case signing is dependent on other headers
359 if (options.aws) {
360 self.aws(options.aws)
361 }
362
363 if (options.hawk) {
364 self.hawk(options.hawk)
365 }
366
367 if (options.httpSignature) {
368 self.httpSignature(options.httpSignature)
369 }
370
371 if (options.auth) {
372 if (Object.prototype.hasOwnProperty.call(options.auth, 'username')) {
373 options.auth.user = options.auth.username
374 }
375 if (Object.prototype.hasOwnProperty.call(options.auth, 'password')) {
376 options.auth.pass = options.auth.password
377 }
378
379 self.auth(
380 options.auth.user,
381 options.auth.pass,
382 options.auth.sendImmediately,
383 options.auth.bearer
384 )
385 }
386
387 if (self.gzip && !self.hasHeader('accept-encoding')) {
388 self.setHeader('accept-encoding', 'gzip, deflate')
389 }
390
391 if (self.uri.auth && !self.hasHeader('authorization')) {
392 var uriAuthPieces = self.uri.auth.split(':').map(function(item) {return self._qs.unescape(item)})
393 self.auth(uriAuthPieces[0], uriAuthPieces.slice(1).join(':'), true)
394 }
395
396 if (!self.tunnel && self.proxy && self.proxy.auth && !self.hasHeader('proxy-authorization')) {
397 var proxyAuthPieces = self.proxy.auth.split(':').map(function(item) {return self._qs.unescape(item)})
398 var authHeader = 'Basic ' + toBase64(proxyAuthPieces.join(':'))
399 self.setHeader('proxy-authorization', authHeader)
400 }
401
402 if (self.proxy && !self.tunnel) {
403 self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
404 }
405
406 if (options.json) {
407 self.json(options.json)
408 }
409 if (options.multipart) {
410 self.multipart(options.multipart)
411 }
412
413 if (options.time) {
414 self.timing = true
415 self.elapsedTime = self.elapsedTime || 0
416 }
417
418 function setContentLength () {
419 if (isTypedArray(self.body)) {
420 self.body = new Buffer(self.body)
421 }
422
423 if (!self.hasHeader('content-length')) {
424 var length
425 if (typeof self.body === 'string') {
426 length = Buffer.byteLength(self.body)
427 }
428 else if (Array.isArray(self.body)) {
429 length = self.body.reduce(function (a, b) {return a + b.length}, 0)
430 }
431 else {
432 length = self.body.length
433 }
434
435 if (length) {
436 self.setHeader('content-length', length)
437 } else {
438 self.emit('error', new Error('Argument error, options.body.'))
439 }
440 }
441 }
442 if (self.body && !isstream(self.body)) {
443 setContentLength()
444 }
445
446 if (options.oauth) {
447 self.oauth(options.oauth)
448 } else if (self._oauth.params && self.hasHeader('authorization')) {
449 self.oauth(self._oauth.params)
450 }
451
452 var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
453 , defaultModules = {'http:':http, 'https:':https}
454 , httpModules = self.httpModules || {}
455
456 self.httpModule = httpModules[protocol] || defaultModules[protocol]
457
458 if (!self.httpModule) {
459 return self.emit('error', new Error('Invalid protocol: ' + protocol))
460 }
461
462 if (options.ca) {
463 self.ca = options.ca
464 }
465
466 if (!self.agent) {
467 if (options.agentOptions) {
468 self.agentOptions = options.agentOptions
469 }
470
471 if (options.agentClass) {
472 self.agentClass = options.agentClass
473 } else if (options.forever) {
474 var v = version()
475 // use ForeverAgent in node 0.10- only
476 if (v.major === 0 && v.minor <= 10) {
477 self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
478 } else {
479 self.agentClass = self.httpModule.Agent
480 self.agentOptions = self.agentOptions || {}
481 self.agentOptions.keepAlive = true
482 }
483 } else {
484 self.agentClass = self.httpModule.Agent
485 }
486 }
487
488 if (self.pool === false) {
489 self.agent = false
490 } else {
491 self.agent = self.agent || self.getNewAgent()
492 }
493
494 self.on('pipe', function (src) {
495 if (self.ntick && self._started) {
496 self.emit('error', new Error('You cannot pipe to this stream after the outbound request has started.'))
497 }
498 self.src = src
499 if (isReadStream(src)) {
500 if (!self.hasHeader('content-type')) {
501 self.setHeader('content-type', mime.lookup(src.path))
502 }
503 } else {
504 if (src.headers) {
505 for (var i in src.headers) {
506 if (!self.hasHeader(i)) {
507 self.setHeader(i, src.headers[i])
508 }
509 }
510 }
511 if (self._json && !self.hasHeader('content-type')) {
512 self.setHeader('content-type', 'application/json')
513 }
514 if (src.method && !self.explicitMethod) {
515 self.method = src.method
516 }
517 }
518
519 // self.on('pipe', function () {
520 // console.error('You have already piped to this stream. Pipeing twice is likely to break the request.')
521 // })
522 })
523
524 defer(function () {
525 if (self._aborted) {
526 return
527 }
528
529 var end = function () {
530 if (self._form) {
531 if (!self._auth.hasAuth) {
532 self._form.pipe(self)
533 }
534 else if (self._auth.hasAuth && self._auth.sentAuth) {
535 self._form.pipe(self)
536 }
537 }
538 if (self._multipart && self._multipart.chunked) {
539 self._multipart.body.pipe(self)
540 }
541 if (self.body) {
542 if (isstream(self.body)) {
543 self.body.pipe(self)
544 } else {
545 setContentLength()
546 if (Array.isArray(self.body)) {
547 self.body.forEach(function (part) {
548 self.write(part)
549 })
550 } else {
551 self.write(self.body)
552 }
553 self.end()
554 }
555 } else if (self.requestBodyStream) {
556 console.warn('options.requestBodyStream is deprecated, please pass the request object to stream.pipe.')
557 self.requestBodyStream.pipe(self)
558 } else if (!self.src) {
559 if (self._auth.hasAuth && !self._auth.sentAuth) {
560 self.end()
561 return
562 }
563 if (self.method !== 'GET' && typeof self.method !== 'undefined') {
564 self.setHeader('content-length', 0)
565 }
566 self.end()
567 }
568 }
569
570 if (self._form && !self.hasHeader('content-length')) {
571 // Before ending the request, we had to compute the length of the whole form, asyncly
572 self.setHeader(self._form.getHeaders(), true)
573 self._form.getLength(function (err, length) {
574 if (!err && !isNaN(length)) {
575 self.setHeader('content-length', length)
576 }
577 end()
578 })
579 } else {
580 end()
581 }
582
583 self.ntick = true
584 })
585
586}
587
588Request.prototype.getNewAgent = function () {
589 var self = this
590 var Agent = self.agentClass
591 var options = {}
592 if (self.agentOptions) {
593 for (var i in self.agentOptions) {
594 options[i] = self.agentOptions[i]
595 }
596 }
597 if (self.ca) {
598 options.ca = self.ca
599 }
600 if (self.ciphers) {
601 options.ciphers = self.ciphers
602 }
603 if (self.secureProtocol) {
604 options.secureProtocol = self.secureProtocol
605 }
606 if (self.secureOptions) {
607 options.secureOptions = self.secureOptions
608 }
609 if (typeof self.rejectUnauthorized !== 'undefined') {
610 options.rejectUnauthorized = self.rejectUnauthorized
611 }
612
613 if (self.cert && self.key) {
614 options.key = self.key
615 options.cert = self.cert
616 }
617
618 if (self.pfx) {
619 options.pfx = self.pfx
620 }
621
622 if (self.passphrase) {
623 options.passphrase = self.passphrase
624 }
625
626 var poolKey = ''
627
628 // different types of agents are in different pools
629 if (Agent !== self.httpModule.Agent) {
630 poolKey += Agent.name
631 }
632
633 // ca option is only relevant if proxy or destination are https
634 var proxy = self.proxy
635 if (typeof proxy === 'string') {
636 proxy = url.parse(proxy)
637 }
638 var isHttps = (proxy && proxy.protocol === 'https:') || this.uri.protocol === 'https:'
639
640 if (isHttps) {
641 if (options.ca) {
642 if (poolKey) {
643 poolKey += ':'
644 }
645 poolKey += options.ca
646 }
647
648 if (typeof options.rejectUnauthorized !== 'undefined') {
649 if (poolKey) {
650 poolKey += ':'
651 }
652 poolKey += options.rejectUnauthorized
653 }
654
655 if (options.cert) {
656 if (poolKey) {
657 poolKey += ':'
658 }
659 poolKey += options.cert.toString('ascii') + options.key.toString('ascii')
660 }
661
662 if (options.pfx) {
663 if (poolKey) {
664 poolKey += ':'
665 }
666 poolKey += options.pfx.toString('ascii')
667 }
668
669 if (options.ciphers) {
670 if (poolKey) {
671 poolKey += ':'
672 }
673 poolKey += options.ciphers
674 }
675
676 if (options.secureProtocol) {
677 if (poolKey) {
678 poolKey += ':'
679 }
680 poolKey += options.secureProtocol
681 }
682
683 if (options.secureOptions) {
684 if (poolKey) {
685 poolKey += ':'
686 }
687 poolKey += options.secureOptions
688 }
689 }
690
691 if (self.pool === globalPool && !poolKey && Object.keys(options).length === 0 && self.httpModule.globalAgent) {
692 // not doing anything special. Use the globalAgent
693 return self.httpModule.globalAgent
694 }
695
696 // we're using a stored agent. Make sure it's protocol-specific
697 poolKey = self.uri.protocol + poolKey
698
699 // generate a new agent for this setting if none yet exists
700 if (!self.pool[poolKey]) {
701 self.pool[poolKey] = new Agent(options)
702 // properly set maxSockets on new agents
703 if (self.pool.maxSockets) {
704 self.pool[poolKey].maxSockets = self.pool.maxSockets
705 }
706 }
707
708 return self.pool[poolKey]
709}
710
711Request.prototype.start = function () {
712 // start() is called once we are ready to send the outgoing HTTP request.
713 // this is usually called on the first write(), end() or on nextTick()
714 var self = this
715
716 if (self._aborted) {
717 return
718 }
719
720 self._started = true
721 self.method = self.method || 'GET'
722 self.href = self.uri.href
723
724 if (self.src && self.src.stat && self.src.stat.size && !self.hasHeader('content-length')) {
725 self.setHeader('content-length', self.src.stat.size)
726 }
727 if (self._aws) {
728 self.aws(self._aws, true)
729 }
730
731 // We have a method named auth, which is completely different from the http.request
732 // auth option. If we don't remove it, we're gonna have a bad time.
733 var reqOptions = copy(self)
734 delete reqOptions.auth
735
736 debug('make request', self.uri.href)
737
738 // node v6.8.0 now supports a `timeout` value in `http.request()`, but we
739 // should delete it for now since we handle timeouts manually for better
740 // consistency with node versions before v6.8.0
741 delete reqOptions.timeout
742
743 try {
744 self.req = self.httpModule.request(reqOptions)
745 } catch (err) {
746 self.emit('error', err)
747 return
748 }
749
750 if (self.timing) {
751 self.startTime = new Date().getTime()
752 }
753
754 var timeout
755 if (self.timeout && !self.timeoutTimer) {
756 if (self.timeout < 0) {
757 timeout = 0
758 } else if (typeof self.timeout === 'number' && isFinite(self.timeout)) {
759 timeout = self.timeout
760 }
761 }
762
763 self.req.on('response', self.onRequestResponse.bind(self))
764 self.req.on('error', self.onRequestError.bind(self))
765 self.req.on('drain', function() {
766 self.emit('drain')
767 })
768 self.req.on('socket', function(socket) {
769 // `._connecting` was the old property which was made public in node v6.1.0
770 var isConnecting = socket._connecting || socket.connecting
771 // Only start the connection timer if we're actually connecting a new
772 // socket, otherwise if we're already connected (because this is a
773 // keep-alive connection) do not bother. This is important since we won't
774 // get a 'connect' event for an already connected socket.
775 if (timeout !== undefined && isConnecting) {
776 var onReqSockConnect = function() {
777 socket.removeListener('connect', onReqSockConnect)
778 clearTimeout(self.timeoutTimer)
779 self.timeoutTimer = null
780 // Set an additional timeout on the socket, via the `setsockopt` syscall.
781 // This timeout sets the amount of time to wait *between* bytes sent
782 // from the server once connected.
783 //
784 // In particular, it's useful for erroring if the server fails to send
785 // data halfway through streaming a response.
786 self.req.setTimeout(timeout, function () {
787 if (self.req) {
788 self.abort()
789 var e = new Error('ESOCKETTIMEDOUT')
790 e.code = 'ESOCKETTIMEDOUT'
791 e.connect = false
792 self.emit('error', e)
793 }
794 })
795 }
796
797 socket.on('connect', onReqSockConnect)
798
799 self.req.on('error', function(err) {
800 socket.removeListener('connect', onReqSockConnect)
801 })
802
803 // Set a timeout in memory - this block will throw if the server takes more
804 // than `timeout` to write the HTTP status and headers (corresponding to
805 // the on('response') event on the client). NB: this measures wall-clock
806 // time, not the time between bytes sent by the server.
807 self.timeoutTimer = setTimeout(function () {
808 socket.removeListener('connect', onReqSockConnect)
809 self.abort()
810 var e = new Error('ETIMEDOUT')
811 e.code = 'ETIMEDOUT'
812 e.connect = true
813 self.emit('error', e)
814 }, timeout)
815 }
816 self.emit('socket', socket)
817 })
818
819 self.emit('request', self.req)
820}
821
822Request.prototype.onRequestError = function (error) {
823 var self = this
824 if (self._aborted) {
825 return
826 }
827 if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET'
828 && self.agent.addRequestNoreuse) {
829 self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
830 self.start()
831 self.req.end()
832 return
833 }
834 if (self.timeout && self.timeoutTimer) {
835 clearTimeout(self.timeoutTimer)
836 self.timeoutTimer = null
837 }
838 self.emit('error', error)
839}
840
841Request.prototype.onRequestResponse = function (response) {
842 var self = this
843 debug('onRequestResponse', self.uri.href, response.statusCode, response.headers)
844 response.on('end', function() {
845 if (self.timing) {
846 self.elapsedTime += (new Date().getTime() - self.startTime)
847 debug('elapsed time', self.elapsedTime)
848 response.elapsedTime = self.elapsedTime
849 }
850 debug('response end', self.uri.href, response.statusCode, response.headers)
851 })
852
853 if (self._aborted) {
854 debug('aborted', self.uri.href)
855 response.resume()
856 return
857 }
858
859 self.response = response
860 response.request = self
861 response.toJSON = responseToJSON
862
863 // XXX This is different on 0.10, because SSL is strict by default
864 if (self.httpModule === https &&
865 self.strictSSL && (!response.hasOwnProperty('socket') ||
866 !response.socket.authorized)) {
867 debug('strict ssl error', self.uri.href)
868 var sslErr = response.hasOwnProperty('socket') ? response.socket.authorizationError : self.uri.href + ' does not support SSL'
869 self.emit('error', new Error('SSL Error: ' + sslErr))
870 return
871 }
872
873 // Save the original host before any redirect (if it changes, we need to
874 // remove any authorization headers). Also remember the case of the header
875 // name because lots of broken servers expect Host instead of host and we
876 // want the caller to be able to specify this.
877 self.originalHost = self.getHeader('host')
878 if (!self.originalHostHeaderName) {
879 self.originalHostHeaderName = self.hasHeader('host')
880 }
881 if (self.setHost) {
882 self.removeHeader('host')
883 }
884 if (self.timeout && self.timeoutTimer) {
885 clearTimeout(self.timeoutTimer)
886 self.timeoutTimer = null
887 }
888
889 var targetCookieJar = (self._jar && self._jar.setCookie) ? self._jar : globalCookieJar
890 var addCookie = function (cookie) {
891 //set the cookie if it's domain in the href's domain.
892 try {
893 targetCookieJar.setCookie(cookie, self.uri.href, {ignoreError: true})
894 } catch (e) {
895 self.emit('error', e)
896 }
897 }
898
899 response.caseless = caseless(response.headers)
900
901 if (response.caseless.has('set-cookie') && (!self._disableCookies)) {
902 var headerName = response.caseless.has('set-cookie')
903 if (Array.isArray(response.headers[headerName])) {
904 response.headers[headerName].forEach(addCookie)
905 } else {
906 addCookie(response.headers[headerName])
907 }
908 }
909
910 if (self._redirect.onResponse(response)) {
911 return // Ignore the rest of the response
912 } else {
913 // Be a good stream and emit end when the response is finished.
914 // Hack to emit end on close because of a core bug that never fires end
915 response.on('close', function () {
916 if (!self._ended) {
917 self.response.emit('end')
918 }
919 })
920
921 response.once('end', function () {
922 self._ended = true
923 })
924
925 var noBody = function (code) {
926 return (
927 self.method === 'HEAD'
928 // Informational
929 || (code >= 100 && code < 200)
930 // No Content
931 || code === 204
932 // Not Modified
933 || code === 304
934 )
935 }
936
937 var responseContent
938 if (self.gzip && !noBody(response.statusCode)) {
939 var contentEncoding = response.headers['content-encoding'] || 'identity'
940 contentEncoding = contentEncoding.trim().toLowerCase()
941
942 if (contentEncoding === 'gzip') {
943 responseContent = zlib.createGunzip()
944 response.pipe(responseContent)
945 } else if (contentEncoding === 'deflate') {
946 responseContent = zlib.createInflate()
947 response.pipe(responseContent)
948 } else {
949 // Since previous versions didn't check for Content-Encoding header,
950 // ignore any invalid values to preserve backwards-compatibility
951 if (contentEncoding !== 'identity') {
952 debug('ignoring unrecognized Content-Encoding ' + contentEncoding)
953 }
954 responseContent = response
955 }
956 } else {
957 responseContent = response
958 }
959
960 if (self.encoding) {
961 if (self.dests.length !== 0) {
962 console.error('Ignoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.')
963 } else if (responseContent.setEncoding) {
964 responseContent.setEncoding(self.encoding)
965 } else {
966 // Should only occur on node pre-v0.9.4 (joyent/node@9b5abe5) with
967 // zlib streams.
968 // If/When support for 0.9.4 is dropped, this should be unnecessary.
969 responseContent = responseContent.pipe(stringstream(self.encoding))
970 }
971 }
972
973 if (self._paused) {
974 responseContent.pause()
975 }
976
977 self.responseContent = responseContent
978
979 self.emit('response', response)
980
981 self.dests.forEach(function (dest) {
982 self.pipeDest(dest)
983 })
984
985 responseContent.on('data', function (chunk) {
986 if (self.timing && !self.responseStarted) {
987 self.responseStartTime = (new Date()).getTime()
988 response.responseStartTime = self.responseStartTime
989 }
990 self._destdata = true
991 self.emit('data', chunk)
992 })
993 responseContent.once('end', function (chunk) {
994 self.emit('end', chunk)
995 })
996 responseContent.on('error', function (error) {
997 self.emit('error', error)
998 })
999 responseContent.on('close', function () {self.emit('close')})
1000
1001 if (self.callback) {
1002 self.readResponseBody(response)
1003 }
1004 //if no callback
1005 else {
1006 self.on('end', function () {
1007 if (self._aborted) {
1008 debug('aborted', self.uri.href)
1009 return
1010 }
1011 self.emit('complete', response)
1012 })
1013 }
1014 }
1015 debug('finish init function', self.uri.href)
1016}
1017
1018Request.prototype.readResponseBody = function (response) {
1019 var self = this
1020 debug('reading response\'s body')
1021 var buffers = []
1022 , bufferLength = 0
1023 , strings = []
1024
1025 self.on('data', function (chunk) {
1026 if (!Buffer.isBuffer(chunk)) {
1027 strings.push(chunk)
1028 } else if (chunk.length) {
1029 bufferLength += chunk.length
1030 buffers.push(chunk)
1031 }
1032 })
1033 self.on('end', function () {
1034 debug('end event', self.uri.href)
1035 if (self._aborted) {
1036 debug('aborted', self.uri.href)
1037 // `buffer` is defined in the parent scope and used in a closure it exists for the life of the request.
1038 // This can lead to leaky behavior if the user retains a reference to the request object.
1039 buffers = []
1040 bufferLength = 0
1041 return
1042 }
1043
1044 if (bufferLength) {
1045 debug('has body', self.uri.href, bufferLength)
1046 response.body = Buffer.concat(buffers, bufferLength)
1047 if (self.encoding !== null) {
1048 response.body = response.body.toString(self.encoding)
1049 }
1050 // `buffer` is defined in the parent scope and used in a closure it exists for the life of the Request.
1051 // This can lead to leaky behavior if the user retains a reference to the request object.
1052 buffers = []
1053 bufferLength = 0
1054 } else if (strings.length) {
1055 // The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
1056 // Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
1057 if (self.encoding === 'utf8' && strings[0].length > 0 && strings[0][0] === '\uFEFF') {
1058 strings[0] = strings[0].substring(1)
1059 }
1060 response.body = strings.join('')
1061 }
1062
1063 if (self._json) {
1064 try {
1065 response.body = JSON.parse(response.body, self._jsonReviver)
1066 } catch (e) {
1067 debug('invalid JSON received', self.uri.href)
1068 }
1069 }
1070 debug('emitting complete', self.uri.href)
1071 if (typeof response.body === 'undefined' && !self._json) {
1072 response.body = self.encoding === null ? new Buffer(0) : ''
1073 }
1074 self.emit('complete', response, response.body)
1075 })
1076}
1077
1078Request.prototype.abort = function () {
1079 var self = this
1080 self._aborted = true
1081
1082 if (self.req) {
1083 self.req.abort()
1084 }
1085 else if (self.response) {
1086 self.response.destroy()
1087 }
1088
1089 self.emit('abort')
1090}
1091
1092Request.prototype.pipeDest = function (dest) {
1093 var self = this
1094 var response = self.response
1095 // Called after the response is received
1096 if (dest.headers && !dest.headersSent) {
1097 if (response.caseless.has('content-type')) {
1098 var ctname = response.caseless.has('content-type')
1099 if (dest.setHeader) {
1100 dest.setHeader(ctname, response.headers[ctname])
1101 }
1102 else {
1103 dest.headers[ctname] = response.headers[ctname]
1104 }
1105 }
1106
1107 if (response.caseless.has('content-length')) {
1108 var clname = response.caseless.has('content-length')
1109 if (dest.setHeader) {
1110 dest.setHeader(clname, response.headers[clname])
1111 } else {
1112 dest.headers[clname] = response.headers[clname]
1113 }
1114 }
1115 }
1116 if (dest.setHeader && !dest.headersSent) {
1117 for (var i in response.headers) {
1118 // If the response content is being decoded, the Content-Encoding header
1119 // of the response doesn't represent the piped content, so don't pass it.
1120 if (!self.gzip || i !== 'content-encoding') {
1121 dest.setHeader(i, response.headers[i])
1122 }
1123 }
1124 dest.statusCode = response.statusCode
1125 }
1126 if (self.pipefilter) {
1127 self.pipefilter(response, dest)
1128 }
1129}
1130
1131Request.prototype.qs = function (q, clobber) {
1132 var self = this
1133 var base
1134 if (!clobber && self.uri.query) {
1135 base = self._qs.parse(self.uri.query)
1136 } else {
1137 base = {}
1138 }
1139
1140 for (var i in q) {
1141 base[i] = q[i]
1142 }
1143
1144 var qs = self._qs.stringify(base)
1145
1146 if (qs === '') {
1147 return self
1148 }
1149
1150 self.uri = url.parse(self.uri.href.split('?')[0] + '?' + qs)
1151 self.url = self.uri
1152 self.path = self.uri.path
1153
1154 if (self.uri.host === 'unix') {
1155 self.enableUnixSocket()
1156 }
1157
1158 return self
1159}
1160Request.prototype.form = function (form) {
1161 var self = this
1162 if (form) {
1163 if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
1164 self.setHeader('content-type', 'application/x-www-form-urlencoded')
1165 }
1166 self.body = (typeof form === 'string')
1167 ? self._qs.rfc3986(form.toString('utf8'))
1168 : self._qs.stringify(form).toString('utf8')
1169 return self
1170 }
1171 // create form-data object
1172 self._form = new FormData()
1173 self._form.on('error', function(err) {
1174 err.message = 'form-data: ' + err.message
1175 self.emit('error', err)
1176 self.abort()
1177 })
1178 return self._form
1179}
1180Request.prototype.multipart = function (multipart) {
1181 var self = this
1182
1183 self._multipart.onRequest(multipart)
1184
1185 if (!self._multipart.chunked) {
1186 self.body = self._multipart.body
1187 }
1188
1189 return self
1190}
1191Request.prototype.json = function (val) {
1192 var self = this
1193
1194 if (!self.hasHeader('accept')) {
1195 self.setHeader('accept', 'application/json')
1196 }
1197
1198 if (typeof self.jsonReplacer === 'function') {
1199 self._jsonReplacer = self.jsonReplacer
1200 }
1201
1202 self._json = true
1203 if (typeof val === 'boolean') {
1204 if (self.body !== undefined) {
1205 if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
1206 self.body = safeStringify(self.body, self._jsonReplacer)
1207 } else {
1208 self.body = self._qs.rfc3986(self.body)
1209 }
1210 if (!self.hasHeader('content-type')) {
1211 self.setHeader('content-type', 'application/json')
1212 }
1213 }
1214 } else {
1215 self.body = safeStringify(val, self._jsonReplacer)
1216 if (!self.hasHeader('content-type')) {
1217 self.setHeader('content-type', 'application/json')
1218 }
1219 }
1220
1221 if (typeof self.jsonReviver === 'function') {
1222 self._jsonReviver = self.jsonReviver
1223 }
1224
1225 return self
1226}
1227Request.prototype.getHeader = function (name, headers) {
1228 var self = this
1229 var result, re, match
1230 if (!headers) {
1231 headers = self.headers
1232 }
1233 Object.keys(headers).forEach(function (key) {
1234 if (key.length !== name.length) {
1235 return
1236 }
1237 re = new RegExp(name, 'i')
1238 match = key.match(re)
1239 if (match) {
1240 result = headers[key]
1241 }
1242 })
1243 return result
1244}
1245Request.prototype.enableUnixSocket = function () {
1246 // Get the socket & request paths from the URL
1247 var unixParts = this.uri.path.split(':')
1248 , host = unixParts[0]
1249 , path = unixParts[1]
1250 // Apply unix properties to request
1251 this.socketPath = host
1252 this.uri.pathname = path
1253 this.uri.path = path
1254 this.uri.host = host
1255 this.uri.hostname = host
1256 this.uri.isUnix = true
1257}
1258
1259
1260Request.prototype.auth = function (user, pass, sendImmediately, bearer) {
1261 var self = this
1262
1263 self._auth.onRequest(user, pass, sendImmediately, bearer)
1264
1265 return self
1266}
1267Request.prototype.aws = function (opts, now) {
1268 var self = this
1269
1270 if (!now) {
1271 self._aws = opts
1272 return self
1273 }
1274
1275 if (opts.sign_version == 4 || opts.sign_version == '4') {
1276 // use aws4
1277 var options = {
1278 host: self.uri.host,
1279 path: self.uri.path,
1280 method: self.method,
1281 headers: {
1282 'content-type': self.getHeader('content-type') || ''
1283 },
1284 body: self.body
1285 }
1286 var signRes = aws4.sign(options, {
1287 accessKeyId: opts.key,
1288 secretAccessKey: opts.secret
1289 })
1290 self.setHeader('authorization', signRes.headers.Authorization)
1291 self.setHeader('x-amz-date', signRes.headers['X-Amz-Date'])
1292 }
1293 else {
1294 // default: use aws-sign2
1295 var date = new Date()
1296 self.setHeader('date', date.toUTCString())
1297 var auth =
1298 { key: opts.key
1299 , secret: opts.secret
1300 , verb: self.method.toUpperCase()
1301 , date: date
1302 , contentType: self.getHeader('content-type') || ''
1303 , md5: self.getHeader('content-md5') || ''
1304 , amazonHeaders: aws2.canonicalizeHeaders(self.headers)
1305 }
1306 var path = self.uri.path
1307 if (opts.bucket && path) {
1308 auth.resource = '/' + opts.bucket + path
1309 } else if (opts.bucket && !path) {
1310 auth.resource = '/' + opts.bucket
1311 } else if (!opts.bucket && path) {
1312 auth.resource = path
1313 } else if (!opts.bucket && !path) {
1314 auth.resource = '/'
1315 }
1316 auth.resource = aws2.canonicalizeResource(auth.resource)
1317 self.setHeader('authorization', aws2.authorization(auth))
1318 }
1319
1320 return self
1321}
1322Request.prototype.httpSignature = function (opts) {
1323 var self = this
1324 httpSignature.signRequest({
1325 getHeader: function(header) {
1326 return self.getHeader(header, self.headers)
1327 },
1328 setHeader: function(header, value) {
1329 self.setHeader(header, value)
1330 },
1331 method: self.method,
1332 path: self.path
1333 }, opts)
1334 debug('httpSignature authorization', self.getHeader('authorization'))
1335
1336 return self
1337}
1338Request.prototype.hawk = function (opts) {
1339 var self = this
1340 self.setHeader('Authorization', hawk.client.header(self.uri, self.method, opts).field)
1341}
1342Request.prototype.oauth = function (_oauth) {
1343 var self = this
1344
1345 self._oauth.onRequest(_oauth)
1346
1347 return self
1348}
1349
1350Request.prototype.jar = function (jar) {
1351 var self = this
1352 var cookies
1353
1354 if (self._redirect.redirectsFollowed === 0) {
1355 self.originalCookieHeader = self.getHeader('cookie')
1356 }
1357
1358 if (!jar) {
1359 // disable cookies
1360 cookies = false
1361 self._disableCookies = true
1362 } else {
1363 var targetCookieJar = (jar && jar.getCookieString) ? jar : globalCookieJar
1364 var urihref = self.uri.href
1365 //fetch cookie in the Specified host
1366 if (targetCookieJar) {
1367 cookies = targetCookieJar.getCookieString(urihref)
1368 }
1369 }
1370
1371 //if need cookie and cookie is not empty
1372 if (cookies && cookies.length) {
1373 if (self.originalCookieHeader) {
1374 // Don't overwrite existing Cookie header
1375 self.setHeader('cookie', self.originalCookieHeader + '; ' + cookies)
1376 } else {
1377 self.setHeader('cookie', cookies)
1378 }
1379 }
1380 self._jar = jar
1381 return self
1382}
1383
1384
1385// Stream API
1386Request.prototype.pipe = function (dest, opts) {
1387 var self = this
1388
1389 if (self.response) {
1390 if (self._destdata) {
1391 self.emit('error', new Error('You cannot pipe after data has been emitted from the response.'))
1392 } else if (self._ended) {
1393 self.emit('error', new Error('You cannot pipe after the response has been ended.'))
1394 } else {
1395 stream.Stream.prototype.pipe.call(self, dest, opts)
1396 self.pipeDest(dest)
1397 return dest
1398 }
1399 } else {
1400 self.dests.push(dest)
1401 stream.Stream.prototype.pipe.call(self, dest, opts)
1402 return dest
1403 }
1404}
1405Request.prototype.write = function () {
1406 var self = this
1407 if (self._aborted) {return}
1408
1409 if (!self._started) {
1410 self.start()
1411 }
1412 if (self.req) {
1413 return self.req.write.apply(self.req, arguments)
1414 }
1415}
1416Request.prototype.end = function (chunk) {
1417 var self = this
1418 if (self._aborted) {return}
1419
1420 if (chunk) {
1421 self.write(chunk)
1422 }
1423 if (!self._started) {
1424 self.start()
1425 }
1426 if (self.req) {
1427 self.req.end()
1428 }
1429}
1430Request.prototype.pause = function () {
1431 var self = this
1432 if (!self.responseContent) {
1433 self._paused = true
1434 } else {
1435 self.responseContent.pause.apply(self.responseContent, arguments)
1436 }
1437}
1438Request.prototype.resume = function () {
1439 var self = this
1440 if (!self.responseContent) {
1441 self._paused = false
1442 } else {
1443 self.responseContent.resume.apply(self.responseContent, arguments)
1444 }
1445}
1446Request.prototype.destroy = function () {
1447 var self = this
1448 if (!self._ended) {
1449 self.end()
1450 } else if (self.response) {
1451 self.response.destroy()
1452 }
1453}
1454
1455Request.defaultProxyHeaderWhiteList =
1456 Tunnel.defaultProxyHeaderWhiteList.slice()
1457
1458Request.defaultProxyHeaderExclusiveList =
1459 Tunnel.defaultProxyHeaderExclusiveList.slice()
1460
1461// Exports
1462
1463Request.prototype.toJSON = requestToJSON
1464module.exports = Request