UNPKG

44.7 kBJavaScriptView Raw
1'use strict'
2
3var http = require('http')
4var https = require('https')
5var url = require('url')
6var util = require('util')
7var stream = require('stream')
8var zlib = require('zlib')
9var hawk = require('hawk')
10var aws2 = require('aws-sign2')
11var aws4 = require('aws4')
12var httpSignature = require('http-signature')
13var mime = require('mime-types')
14var stringstream = require('stringstream')
15var caseless = require('caseless')
16var ForeverAgent = require('forever-agent')
17var FormData = require('form-data')
18var extend = require('extend')
19var isstream = require('isstream')
20var isTypedArray = require('is-typedarray').strict
21var helpers = require('./lib/helpers')
22var cookies = require('./lib/cookies')
23var getProxyFromURI = require('./lib/getProxyFromURI')
24var Querystring = require('./lib/querystring').Querystring
25var Har = require('./lib/har').Har
26var Auth = require('./lib/auth').Auth
27var OAuth = require('./lib/oauth').OAuth
28var Multipart = require('./lib/multipart').Multipart
29var Redirect = require('./lib/redirect').Redirect
30var Tunnel = require('./lib/tunnel').Tunnel
31var now = require('performance-now')
32var Buffer = require('safe-buffer').Buffer
33
34var safeStringify = helpers.safeStringify
35var isReadStream = helpers.isReadStream
36var toBase64 = helpers.toBase64
37var defer = helpers.defer
38var copy = helpers.copy
39var version = helpers.version
40var globalCookieJar = cookies.jar()
41
42var globalPool = {}
43
44function filterForNonReserved (reserved, options) {
45 // Filter out properties that are not reserved.
46 // Reserved values are passed in at call site.
47
48 var object = {}
49 for (var i in options) {
50 var notReserved = (reserved.indexOf(i) === -1)
51 if (notReserved) {
52 object[i] = options[i]
53 }
54 }
55 return object
56}
57
58function filterOutReservedFunctions (reserved, options) {
59 // Filter out properties that are functions and are reserved.
60 // Reserved values are passed in at call site.
61
62 var object = {}
63 for (var i in options) {
64 var isReserved = !(reserved.indexOf(i) === -1)
65 var isFunction = (typeof options[i] === 'function')
66 if (!(isReserved && isFunction)) {
67 object[i] = options[i]
68 }
69 }
70 return object
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 // When used with an IPv6 address, `host` will provide
292 // the correct bracketed format, unlike using `hostname` and
293 // optionally adding the `port` when necessary.
294 self.setHeader(hostHeaderName, self.uri.host)
295 self.setHost = true
296 }
297
298 self.jar(self._jar || options.jar)
299
300 if (!self.uri.port) {
301 if (self.uri.protocol === 'http:') { self.uri.port = 80 } else if (self.uri.protocol === 'https:') { self.uri.port = 443 }
302 }
303
304 if (self.proxy && !self.tunnel) {
305 self.port = self.proxy.port
306 self.host = self.proxy.hostname
307 } else {
308 self.port = self.uri.port
309 self.host = self.uri.hostname
310 }
311
312 if (options.form) {
313 self.form(options.form)
314 }
315
316 if (options.formData) {
317 var formData = options.formData
318 var requestForm = self.form()
319 var appendFormValue = function (key, value) {
320 if (value && value.hasOwnProperty('value') && value.hasOwnProperty('options')) {
321 requestForm.append(key, value.value, value.options)
322 } else {
323 requestForm.append(key, value)
324 }
325 }
326 for (var formKey in formData) {
327 if (formData.hasOwnProperty(formKey)) {
328 var formValue = formData[formKey]
329 if (formValue instanceof Array) {
330 for (var j = 0; j < formValue.length; j++) {
331 appendFormValue(formKey, formValue[j])
332 }
333 } else {
334 appendFormValue(formKey, formValue)
335 }
336 }
337 }
338 }
339
340 if (options.qs) {
341 self.qs(options.qs)
342 }
343
344 if (self.uri.path) {
345 self.path = self.uri.path
346 } else {
347 self.path = self.uri.pathname + (self.uri.search || '')
348 }
349
350 if (self.path.length === 0) {
351 self.path = '/'
352 }
353
354 // Auth must happen last in case signing is dependent on other headers
355 if (options.aws) {
356 self.aws(options.aws)
357 }
358
359 if (options.hawk) {
360 self.hawk(options.hawk)
361 }
362
363 if (options.httpSignature) {
364 self.httpSignature(options.httpSignature)
365 }
366
367 if (options.auth) {
368 if (Object.prototype.hasOwnProperty.call(options.auth, 'username')) {
369 options.auth.user = options.auth.username
370 }
371 if (Object.prototype.hasOwnProperty.call(options.auth, 'password')) {
372 options.auth.pass = options.auth.password
373 }
374
375 self.auth(
376 options.auth.user,
377 options.auth.pass,
378 options.auth.sendImmediately,
379 options.auth.bearer
380 )
381 }
382
383 if (self.gzip && !self.hasHeader('accept-encoding')) {
384 self.setHeader('accept-encoding', 'gzip, deflate')
385 }
386
387 if (self.uri.auth && !self.hasHeader('authorization')) {
388 var uriAuthPieces = self.uri.auth.split(':').map(function (item) { return self._qs.unescape(item) })
389 self.auth(uriAuthPieces[0], uriAuthPieces.slice(1).join(':'), true)
390 }
391
392 if (!self.tunnel && self.proxy && self.proxy.auth && !self.hasHeader('proxy-authorization')) {
393 var proxyAuthPieces = self.proxy.auth.split(':').map(function (item) { return self._qs.unescape(item) })
394 var authHeader = 'Basic ' + toBase64(proxyAuthPieces.join(':'))
395 self.setHeader('proxy-authorization', authHeader)
396 }
397
398 if (self.proxy && !self.tunnel) {
399 self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
400 }
401
402 if (options.json) {
403 self.json(options.json)
404 }
405 if (options.multipart) {
406 self.multipart(options.multipart)
407 }
408
409 if (options.time) {
410 self.timing = true
411
412 // NOTE: elapsedTime is deprecated in favor of .timings
413 self.elapsedTime = self.elapsedTime || 0
414 }
415
416 function setContentLength () {
417 if (isTypedArray(self.body)) {
418 self.body = Buffer.from(self.body)
419 }
420
421 if (!self.hasHeader('content-length')) {
422 var length
423 if (typeof self.body === 'string') {
424 length = Buffer.byteLength(self.body)
425 } else if (Array.isArray(self.body)) {
426 length = self.body.reduce(function (a, b) { return a + b.length }, 0)
427 } else {
428 length = self.body.length
429 }
430
431 if (length) {
432 self.setHeader('content-length', length)
433 } else {
434 self.emit('error', new Error('Argument error, options.body.'))
435 }
436 }
437 }
438 if (self.body && !isstream(self.body)) {
439 setContentLength()
440 }
441
442 if (options.oauth) {
443 self.oauth(options.oauth)
444 } else if (self._oauth.params && self.hasHeader('authorization')) {
445 self.oauth(self._oauth.params)
446 }
447
448 var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
449 var defaultModules = {'http:': http, 'https:': https}
450 var httpModules = self.httpModules || {}
451
452 self.httpModule = httpModules[protocol] || defaultModules[protocol]
453
454 if (!self.httpModule) {
455 return self.emit('error', new Error('Invalid protocol: ' + protocol))
456 }
457
458 if (options.ca) {
459 self.ca = options.ca
460 }
461
462 if (!self.agent) {
463 if (options.agentOptions) {
464 self.agentOptions = options.agentOptions
465 }
466
467 if (options.agentClass) {
468 self.agentClass = options.agentClass
469 } else if (options.forever) {
470 var v = version()
471 // use ForeverAgent in node 0.10- only
472 if (v.major === 0 && v.minor <= 10) {
473 self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
474 } else {
475 self.agentClass = self.httpModule.Agent
476 self.agentOptions = self.agentOptions || {}
477 self.agentOptions.keepAlive = true
478 }
479 } else {
480 self.agentClass = self.httpModule.Agent
481 }
482 }
483
484 if (self.pool === false) {
485 self.agent = false
486 } else {
487 self.agent = self.agent || self.getNewAgent()
488 }
489
490 self.on('pipe', function (src) {
491 if (self.ntick && self._started) {
492 self.emit('error', new Error('You cannot pipe to this stream after the outbound request has started.'))
493 }
494 self.src = src
495 if (isReadStream(src)) {
496 if (!self.hasHeader('content-type')) {
497 self.setHeader('content-type', mime.lookup(src.path))
498 }
499 } else {
500 if (src.headers) {
501 for (var i in src.headers) {
502 if (!self.hasHeader(i)) {
503 self.setHeader(i, src.headers[i])
504 }
505 }
506 }
507 if (self._json && !self.hasHeader('content-type')) {
508 self.setHeader('content-type', 'application/json')
509 }
510 if (src.method && !self.explicitMethod) {
511 self.method = src.method
512 }
513 }
514
515 // self.on('pipe', function () {
516 // console.error('You have already piped to this stream. Pipeing twice is likely to break the request.')
517 // })
518 })
519
520 defer(function () {
521 if (self._aborted) {
522 return
523 }
524
525 var end = function () {
526 if (self._form) {
527 if (!self._auth.hasAuth) {
528 self._form.pipe(self)
529 } else if (self._auth.hasAuth && self._auth.sentAuth) {
530 self._form.pipe(self)
531 }
532 }
533 if (self._multipart && self._multipart.chunked) {
534 self._multipart.body.pipe(self)
535 }
536 if (self.body) {
537 if (isstream(self.body)) {
538 self.body.pipe(self)
539 } else {
540 setContentLength()
541 if (Array.isArray(self.body)) {
542 self.body.forEach(function (part) {
543 self.write(part)
544 })
545 } else {
546 self.write(self.body)
547 }
548 self.end()
549 }
550 } else if (self.requestBodyStream) {
551 console.warn('options.requestBodyStream is deprecated, please pass the request object to stream.pipe.')
552 self.requestBodyStream.pipe(self)
553 } else if (!self.src) {
554 if (self._auth.hasAuth && !self._auth.sentAuth) {
555 self.end()
556 return
557 }
558 if (self.method !== 'GET' && typeof self.method !== 'undefined') {
559 self.setHeader('content-length', 0)
560 }
561 self.end()
562 }
563 }
564
565 if (self._form && !self.hasHeader('content-length')) {
566 // Before ending the request, we had to compute the length of the whole form, asyncly
567 self.setHeader(self._form.getHeaders(), true)
568 self._form.getLength(function (err, length) {
569 if (!err && !isNaN(length)) {
570 self.setHeader('content-length', length)
571 }
572 end()
573 })
574 } else {
575 end()
576 }
577
578 self.ntick = true
579 })
580}
581
582Request.prototype.getNewAgent = function () {
583 var self = this
584 var Agent = self.agentClass
585 var options = {}
586 if (self.agentOptions) {
587 for (var i in self.agentOptions) {
588 options[i] = self.agentOptions[i]
589 }
590 }
591 if (self.ca) {
592 options.ca = self.ca
593 }
594 if (self.ciphers) {
595 options.ciphers = self.ciphers
596 }
597 if (self.secureProtocol) {
598 options.secureProtocol = self.secureProtocol
599 }
600 if (self.secureOptions) {
601 options.secureOptions = self.secureOptions
602 }
603 if (typeof self.rejectUnauthorized !== 'undefined') {
604 options.rejectUnauthorized = self.rejectUnauthorized
605 }
606
607 if (self.cert && self.key) {
608 options.key = self.key
609 options.cert = self.cert
610 }
611
612 if (self.pfx) {
613 options.pfx = self.pfx
614 }
615
616 if (self.passphrase) {
617 options.passphrase = self.passphrase
618 }
619
620 var poolKey = ''
621
622 // different types of agents are in different pools
623 if (Agent !== self.httpModule.Agent) {
624 poolKey += Agent.name
625 }
626
627 // ca option is only relevant if proxy or destination are https
628 var proxy = self.proxy
629 if (typeof proxy === 'string') {
630 proxy = url.parse(proxy)
631 }
632 var isHttps = (proxy && proxy.protocol === 'https:') || this.uri.protocol === 'https:'
633
634 if (isHttps) {
635 if (options.ca) {
636 if (poolKey) {
637 poolKey += ':'
638 }
639 poolKey += options.ca
640 }
641
642 if (typeof options.rejectUnauthorized !== 'undefined') {
643 if (poolKey) {
644 poolKey += ':'
645 }
646 poolKey += options.rejectUnauthorized
647 }
648
649 if (options.cert) {
650 if (poolKey) {
651 poolKey += ':'
652 }
653 poolKey += options.cert.toString('ascii') + options.key.toString('ascii')
654 }
655
656 if (options.pfx) {
657 if (poolKey) {
658 poolKey += ':'
659 }
660 poolKey += options.pfx.toString('ascii')
661 }
662
663 if (options.ciphers) {
664 if (poolKey) {
665 poolKey += ':'
666 }
667 poolKey += options.ciphers
668 }
669
670 if (options.secureProtocol) {
671 if (poolKey) {
672 poolKey += ':'
673 }
674 poolKey += options.secureProtocol
675 }
676
677 if (options.secureOptions) {
678 if (poolKey) {
679 poolKey += ':'
680 }
681 poolKey += options.secureOptions
682 }
683 }
684
685 if (self.pool === globalPool && !poolKey && Object.keys(options).length === 0 && self.httpModule.globalAgent) {
686 // not doing anything special. Use the globalAgent
687 return self.httpModule.globalAgent
688 }
689
690 // we're using a stored agent. Make sure it's protocol-specific
691 poolKey = self.uri.protocol + poolKey
692
693 // generate a new agent for this setting if none yet exists
694 if (!self.pool[poolKey]) {
695 self.pool[poolKey] = new Agent(options)
696 // properly set maxSockets on new agents
697 if (self.pool.maxSockets) {
698 self.pool[poolKey].maxSockets = self.pool.maxSockets
699 }
700 }
701
702 return self.pool[poolKey]
703}
704
705Request.prototype.start = function () {
706 // start() is called once we are ready to send the outgoing HTTP request.
707 // this is usually called on the first write(), end() or on nextTick()
708 var self = this
709
710 if (self.timing) {
711 // All timings will be relative to this request's startTime. In order to do this,
712 // we need to capture the wall-clock start time (via Date), immediately followed
713 // by the high-resolution timer (via now()). While these two won't be set
714 // at the _exact_ same time, they should be close enough to be able to calculate
715 // high-resolution, monotonically non-decreasing timestamps relative to startTime.
716 var startTime = new Date().getTime()
717 var startTimeNow = now()
718 }
719
720 if (self._aborted) {
721 return
722 }
723
724 self._started = true
725 self.method = self.method || 'GET'
726 self.href = self.uri.href
727
728 if (self.src && self.src.stat && self.src.stat.size && !self.hasHeader('content-length')) {
729 self.setHeader('content-length', self.src.stat.size)
730 }
731 if (self._aws) {
732 self.aws(self._aws, true)
733 }
734
735 // We have a method named auth, which is completely different from the http.request
736 // auth option. If we don't remove it, we're gonna have a bad time.
737 var reqOptions = copy(self)
738 delete reqOptions.auth
739
740 debug('make request', self.uri.href)
741
742 // node v6.8.0 now supports a `timeout` value in `http.request()`, but we
743 // should delete it for now since we handle timeouts manually for better
744 // consistency with node versions before v6.8.0
745 delete reqOptions.timeout
746
747 try {
748 self.req = self.httpModule.request(reqOptions)
749 } catch (err) {
750 self.emit('error', err)
751 return
752 }
753
754 if (self.timing) {
755 self.startTime = startTime
756 self.startTimeNow = startTimeNow
757
758 // Timing values will all be relative to startTime (by comparing to startTimeNow
759 // so we have an accurate clock)
760 self.timings = {}
761 }
762
763 var timeout
764 if (self.timeout && !self.timeoutTimer) {
765 if (self.timeout < 0) {
766 timeout = 0
767 } else if (typeof self.timeout === 'number' && isFinite(self.timeout)) {
768 timeout = self.timeout
769 }
770 }
771
772 self.req.on('response', self.onRequestResponse.bind(self))
773 self.req.on('error', self.onRequestError.bind(self))
774 self.req.on('drain', function () {
775 self.emit('drain')
776 })
777
778 self.req.on('socket', function (socket) {
779 // `._connecting` was the old property which was made public in node v6.1.0
780 var isConnecting = socket._connecting || socket.connecting
781 if (self.timing) {
782 self.timings.socket = now() - self.startTimeNow
783
784 if (isConnecting) {
785 var onLookupTiming = function () {
786 self.timings.lookup = now() - self.startTimeNow
787 }
788
789 var onConnectTiming = function () {
790 self.timings.connect = now() - self.startTimeNow
791 }
792
793 socket.once('lookup', onLookupTiming)
794 socket.once('connect', onConnectTiming)
795
796 // clean up timing event listeners if needed on error
797 self.req.once('error', function () {
798 socket.removeListener('lookup', onLookupTiming)
799 socket.removeListener('connect', onConnectTiming)
800 })
801 }
802 }
803
804 var setReqTimeout = function () {
805 // This timeout sets the amount of time to wait *between* bytes sent
806 // from the server once connected.
807 //
808 // In particular, it's useful for erroring if the server fails to send
809 // data halfway through streaming a response.
810 self.req.setTimeout(timeout, function () {
811 if (self.req) {
812 self.abort()
813 var e = new Error('ESOCKETTIMEDOUT')
814 e.code = 'ESOCKETTIMEDOUT'
815 e.connect = false
816 self.emit('error', e)
817 }
818 })
819 }
820 if (timeout !== undefined) {
821 // Only start the connection timer if we're actually connecting a new
822 // socket, otherwise if we're already connected (because this is a
823 // keep-alive connection) do not bother. This is important since we won't
824 // get a 'connect' event for an already connected socket.
825 if (isConnecting) {
826 var onReqSockConnect = function () {
827 socket.removeListener('connect', onReqSockConnect)
828 clearTimeout(self.timeoutTimer)
829 self.timeoutTimer = null
830 setReqTimeout()
831 }
832
833 socket.on('connect', onReqSockConnect)
834
835 self.req.on('error', function (err) { // eslint-disable-line handle-callback-err
836 socket.removeListener('connect', onReqSockConnect)
837 })
838
839 // Set a timeout in memory - this block will throw if the server takes more
840 // than `timeout` to write the HTTP status and headers (corresponding to
841 // the on('response') event on the client). NB: this measures wall-clock
842 // time, not the time between bytes sent by the server.
843 self.timeoutTimer = setTimeout(function () {
844 socket.removeListener('connect', onReqSockConnect)
845 self.abort()
846 var e = new Error('ETIMEDOUT')
847 e.code = 'ETIMEDOUT'
848 e.connect = true
849 self.emit('error', e)
850 }, timeout)
851 } else {
852 // We're already connected
853 setReqTimeout()
854 }
855 }
856 self.emit('socket', socket)
857 })
858
859 self.emit('request', self.req)
860}
861
862Request.prototype.onRequestError = function (error) {
863 var self = this
864 if (self._aborted) {
865 return
866 }
867 if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET' &&
868 self.agent.addRequestNoreuse) {
869 self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
870 self.start()
871 self.req.end()
872 return
873 }
874 if (self.timeout && self.timeoutTimer) {
875 clearTimeout(self.timeoutTimer)
876 self.timeoutTimer = null
877 }
878 self.emit('error', error)
879}
880
881Request.prototype.onRequestResponse = function (response) {
882 var self = this
883
884 if (self.timing) {
885 self.timings.response = now() - self.startTimeNow
886 }
887
888 debug('onRequestResponse', self.uri.href, response.statusCode, response.headers)
889 response.on('end', function () {
890 if (self.timing) {
891 self.timings.end = now() - self.startTimeNow
892 response.timingStart = self.startTime
893
894 // fill in the blanks for any periods that didn't trigger, such as
895 // no lookup or connect due to keep alive
896 if (!self.timings.socket) {
897 self.timings.socket = 0
898 }
899 if (!self.timings.lookup) {
900 self.timings.lookup = self.timings.socket
901 }
902 if (!self.timings.connect) {
903 self.timings.connect = self.timings.lookup
904 }
905 if (!self.timings.response) {
906 self.timings.response = self.timings.connect
907 }
908
909 debug('elapsed time', self.timings.end)
910
911 // elapsedTime includes all redirects
912 self.elapsedTime += Math.round(self.timings.end)
913
914 // NOTE: elapsedTime is deprecated in favor of .timings
915 response.elapsedTime = self.elapsedTime
916
917 // timings is just for the final fetch
918 response.timings = self.timings
919
920 // pre-calculate phase timings as well
921 response.timingPhases = {
922 wait: self.timings.socket,
923 dns: self.timings.lookup - self.timings.socket,
924 tcp: self.timings.connect - self.timings.lookup,
925 firstByte: self.timings.response - self.timings.connect,
926 download: self.timings.end - self.timings.response,
927 total: self.timings.end
928 }
929 }
930 debug('response end', self.uri.href, response.statusCode, response.headers)
931 })
932
933 if (self._aborted) {
934 debug('aborted', self.uri.href)
935 response.resume()
936 return
937 }
938
939 self.response = response
940 response.request = self
941 response.toJSON = responseToJSON
942
943 // XXX This is different on 0.10, because SSL is strict by default
944 if (self.httpModule === https &&
945 self.strictSSL && (!response.hasOwnProperty('socket') ||
946 !response.socket.authorized)) {
947 debug('strict ssl error', self.uri.href)
948 var sslErr = response.hasOwnProperty('socket') ? response.socket.authorizationError : self.uri.href + ' does not support SSL'
949 self.emit('error', new Error('SSL Error: ' + sslErr))
950 return
951 }
952
953 // Save the original host before any redirect (if it changes, we need to
954 // remove any authorization headers). Also remember the case of the header
955 // name because lots of broken servers expect Host instead of host and we
956 // want the caller to be able to specify this.
957 self.originalHost = self.getHeader('host')
958 if (!self.originalHostHeaderName) {
959 self.originalHostHeaderName = self.hasHeader('host')
960 }
961 if (self.setHost) {
962 self.removeHeader('host')
963 }
964 if (self.timeout && self.timeoutTimer) {
965 clearTimeout(self.timeoutTimer)
966 self.timeoutTimer = null
967 }
968
969 var targetCookieJar = (self._jar && self._jar.setCookie) ? self._jar : globalCookieJar
970 var addCookie = function (cookie) {
971 // set the cookie if it's domain in the href's domain.
972 try {
973 targetCookieJar.setCookie(cookie, self.uri.href, {ignoreError: true})
974 } catch (e) {
975 self.emit('error', e)
976 }
977 }
978
979 response.caseless = caseless(response.headers)
980
981 if (response.caseless.has('set-cookie') && (!self._disableCookies)) {
982 var headerName = response.caseless.has('set-cookie')
983 if (Array.isArray(response.headers[headerName])) {
984 response.headers[headerName].forEach(addCookie)
985 } else {
986 addCookie(response.headers[headerName])
987 }
988 }
989
990 if (self._redirect.onResponse(response)) {
991 return // Ignore the rest of the response
992 } else {
993 // Be a good stream and emit end when the response is finished.
994 // Hack to emit end on close because of a core bug that never fires end
995 response.on('close', function () {
996 if (!self._ended) {
997 self.response.emit('end')
998 }
999 })
1000
1001 response.once('end', function () {
1002 self._ended = true
1003 })
1004
1005 var noBody = function (code) {
1006 return (
1007 self.method === 'HEAD' ||
1008 // Informational
1009 (code >= 100 && code < 200) ||
1010 // No Content
1011 code === 204 ||
1012 // Not Modified
1013 code === 304
1014 )
1015 }
1016
1017 var responseContent
1018 if (self.gzip && !noBody(response.statusCode)) {
1019 var contentEncoding = response.headers['content-encoding'] || 'identity'
1020 contentEncoding = contentEncoding.trim().toLowerCase()
1021
1022 // Be more lenient with decoding compressed responses, since (very rarely)
1023 // servers send slightly invalid gzip responses that are still accepted
1024 // by common browsers.
1025 // Always using Z_SYNC_FLUSH is what cURL does.
1026 var zlibOptions = {
1027 flush: zlib.Z_SYNC_FLUSH,
1028 finishFlush: zlib.Z_SYNC_FLUSH
1029 }
1030
1031 if (contentEncoding === 'gzip') {
1032 responseContent = zlib.createGunzip(zlibOptions)
1033 response.pipe(responseContent)
1034 } else if (contentEncoding === 'deflate') {
1035 responseContent = zlib.createInflate(zlibOptions)
1036 response.pipe(responseContent)
1037 } else {
1038 // Since previous versions didn't check for Content-Encoding header,
1039 // ignore any invalid values to preserve backwards-compatibility
1040 if (contentEncoding !== 'identity') {
1041 debug('ignoring unrecognized Content-Encoding ' + contentEncoding)
1042 }
1043 responseContent = response
1044 }
1045 } else {
1046 responseContent = response
1047 }
1048
1049 if (self.encoding) {
1050 if (self.dests.length !== 0) {
1051 console.error('Ignoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.')
1052 } else if (responseContent.setEncoding) {
1053 responseContent.setEncoding(self.encoding)
1054 } else {
1055 // Should only occur on node pre-v0.9.4 (joyent/node@9b5abe5) with
1056 // zlib streams.
1057 // If/When support for 0.9.4 is dropped, this should be unnecessary.
1058 responseContent = responseContent.pipe(stringstream(self.encoding))
1059 }
1060 }
1061
1062 if (self._paused) {
1063 responseContent.pause()
1064 }
1065
1066 self.responseContent = responseContent
1067
1068 self.emit('response', response)
1069
1070 self.dests.forEach(function (dest) {
1071 self.pipeDest(dest)
1072 })
1073
1074 responseContent.on('data', function (chunk) {
1075 if (self.timing && !self.responseStarted) {
1076 self.responseStartTime = (new Date()).getTime()
1077
1078 // NOTE: responseStartTime is deprecated in favor of .timings
1079 response.responseStartTime = self.responseStartTime
1080 }
1081 self._destdata = true
1082 self.emit('data', chunk)
1083 })
1084 responseContent.once('end', function (chunk) {
1085 self.emit('end', chunk)
1086 })
1087 responseContent.on('error', function (error) {
1088 self.emit('error', error)
1089 })
1090 responseContent.on('close', function () { self.emit('close') })
1091
1092 if (self.callback) {
1093 self.readResponseBody(response)
1094 } else { // if no callback
1095 self.on('end', function () {
1096 if (self._aborted) {
1097 debug('aborted', self.uri.href)
1098 return
1099 }
1100 self.emit('complete', response)
1101 })
1102 }
1103 }
1104 debug('finish init function', self.uri.href)
1105}
1106
1107Request.prototype.readResponseBody = function (response) {
1108 var self = this
1109 debug("reading response's body")
1110 var buffers = []
1111 var bufferLength = 0
1112 var strings = []
1113
1114 self.on('data', function (chunk) {
1115 if (!Buffer.isBuffer(chunk)) {
1116 strings.push(chunk)
1117 } else if (chunk.length) {
1118 bufferLength += chunk.length
1119 buffers.push(chunk)
1120 }
1121 })
1122 self.on('end', function () {
1123 debug('end event', self.uri.href)
1124 if (self._aborted) {
1125 debug('aborted', self.uri.href)
1126 // `buffer` is defined in the parent scope and used in a closure it exists for the life of the request.
1127 // This can lead to leaky behavior if the user retains a reference to the request object.
1128 buffers = []
1129 bufferLength = 0
1130 return
1131 }
1132
1133 if (bufferLength) {
1134 debug('has body', self.uri.href, bufferLength)
1135 response.body = Buffer.concat(buffers, bufferLength)
1136 if (self.encoding !== null) {
1137 response.body = response.body.toString(self.encoding)
1138 }
1139 // `buffer` is defined in the parent scope and used in a closure it exists for the life of the Request.
1140 // This can lead to leaky behavior if the user retains a reference to the request object.
1141 buffers = []
1142 bufferLength = 0
1143 } else if (strings.length) {
1144 // The UTF8 BOM [0xEF,0xBB,0xBF] is converted to [0xFE,0xFF] in the JS UTC16/UCS2 representation.
1145 // Strip this value out when the encoding is set to 'utf8', as upstream consumers won't expect it and it breaks JSON.parse().
1146 if (self.encoding === 'utf8' && strings[0].length > 0 && strings[0][0] === '\uFEFF') {
1147 strings[0] = strings[0].substring(1)
1148 }
1149 response.body = strings.join('')
1150 }
1151
1152 if (self._json) {
1153 try {
1154 response.body = JSON.parse(response.body, self._jsonReviver)
1155 } catch (e) {
1156 debug('invalid JSON received', self.uri.href)
1157 }
1158 }
1159 debug('emitting complete', self.uri.href)
1160 if (typeof response.body === 'undefined' && !self._json) {
1161 response.body = self.encoding === null ? Buffer.alloc(0) : ''
1162 }
1163 self.emit('complete', response, response.body)
1164 })
1165}
1166
1167Request.prototype.abort = function () {
1168 var self = this
1169 self._aborted = true
1170
1171 if (self.req) {
1172 self.req.abort()
1173 } else if (self.response) {
1174 self.response.destroy()
1175 }
1176
1177 self.emit('abort')
1178}
1179
1180Request.prototype.pipeDest = function (dest) {
1181 var self = this
1182 var response = self.response
1183 // Called after the response is received
1184 if (dest.headers && !dest.headersSent) {
1185 if (response.caseless.has('content-type')) {
1186 var ctname = response.caseless.has('content-type')
1187 if (dest.setHeader) {
1188 dest.setHeader(ctname, response.headers[ctname])
1189 } else {
1190 dest.headers[ctname] = response.headers[ctname]
1191 }
1192 }
1193
1194 if (response.caseless.has('content-length')) {
1195 var clname = response.caseless.has('content-length')
1196 if (dest.setHeader) {
1197 dest.setHeader(clname, response.headers[clname])
1198 } else {
1199 dest.headers[clname] = response.headers[clname]
1200 }
1201 }
1202 }
1203 if (dest.setHeader && !dest.headersSent) {
1204 for (var i in response.headers) {
1205 // If the response content is being decoded, the Content-Encoding header
1206 // of the response doesn't represent the piped content, so don't pass it.
1207 if (!self.gzip || i !== 'content-encoding') {
1208 dest.setHeader(i, response.headers[i])
1209 }
1210 }
1211 dest.statusCode = response.statusCode
1212 }
1213 if (self.pipefilter) {
1214 self.pipefilter(response, dest)
1215 }
1216}
1217
1218Request.prototype.qs = function (q, clobber) {
1219 var self = this
1220 var base
1221 if (!clobber && self.uri.query) {
1222 base = self._qs.parse(self.uri.query)
1223 } else {
1224 base = {}
1225 }
1226
1227 for (var i in q) {
1228 base[i] = q[i]
1229 }
1230
1231 var qs = self._qs.stringify(base)
1232
1233 if (qs === '') {
1234 return self
1235 }
1236
1237 self.uri = url.parse(self.uri.href.split('?')[0] + '?' + qs)
1238 self.url = self.uri
1239 self.path = self.uri.path
1240
1241 if (self.uri.host === 'unix') {
1242 self.enableUnixSocket()
1243 }
1244
1245 return self
1246}
1247Request.prototype.form = function (form) {
1248 var self = this
1249 if (form) {
1250 if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
1251 self.setHeader('content-type', 'application/x-www-form-urlencoded')
1252 }
1253 self.body = (typeof form === 'string')
1254 ? self._qs.rfc3986(form.toString('utf8'))
1255 : self._qs.stringify(form).toString('utf8')
1256 return self
1257 }
1258 // create form-data object
1259 self._form = new FormData()
1260 self._form.on('error', function (err) {
1261 err.message = 'form-data: ' + err.message
1262 self.emit('error', err)
1263 self.abort()
1264 })
1265 return self._form
1266}
1267Request.prototype.multipart = function (multipart) {
1268 var self = this
1269
1270 self._multipart.onRequest(multipart)
1271
1272 if (!self._multipart.chunked) {
1273 self.body = self._multipart.body
1274 }
1275
1276 return self
1277}
1278Request.prototype.json = function (val) {
1279 var self = this
1280
1281 if (!self.hasHeader('accept')) {
1282 self.setHeader('accept', 'application/json')
1283 }
1284
1285 if (typeof self.jsonReplacer === 'function') {
1286 self._jsonReplacer = self.jsonReplacer
1287 }
1288
1289 self._json = true
1290 if (typeof val === 'boolean') {
1291 if (self.body !== undefined) {
1292 if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
1293 self.body = safeStringify(self.body, self._jsonReplacer)
1294 } else {
1295 self.body = self._qs.rfc3986(self.body)
1296 }
1297 if (!self.hasHeader('content-type')) {
1298 self.setHeader('content-type', 'application/json')
1299 }
1300 }
1301 } else {
1302 self.body = safeStringify(val, self._jsonReplacer)
1303 if (!self.hasHeader('content-type')) {
1304 self.setHeader('content-type', 'application/json')
1305 }
1306 }
1307
1308 if (typeof self.jsonReviver === 'function') {
1309 self._jsonReviver = self.jsonReviver
1310 }
1311
1312 return self
1313}
1314Request.prototype.getHeader = function (name, headers) {
1315 var self = this
1316 var result, re, match
1317 if (!headers) {
1318 headers = self.headers
1319 }
1320 Object.keys(headers).forEach(function (key) {
1321 if (key.length !== name.length) {
1322 return
1323 }
1324 re = new RegExp(name, 'i')
1325 match = key.match(re)
1326 if (match) {
1327 result = headers[key]
1328 }
1329 })
1330 return result
1331}
1332Request.prototype.enableUnixSocket = function () {
1333 // Get the socket & request paths from the URL
1334 var unixParts = this.uri.path.split(':')
1335 var host = unixParts[0]
1336 var path = unixParts[1]
1337 // Apply unix properties to request
1338 this.socketPath = host
1339 this.uri.pathname = path
1340 this.uri.path = path
1341 this.uri.host = host
1342 this.uri.hostname = host
1343 this.uri.isUnix = true
1344}
1345
1346Request.prototype.auth = function (user, pass, sendImmediately, bearer) {
1347 var self = this
1348
1349 self._auth.onRequest(user, pass, sendImmediately, bearer)
1350
1351 return self
1352}
1353Request.prototype.aws = function (opts, now) {
1354 var self = this
1355
1356 if (!now) {
1357 self._aws = opts
1358 return self
1359 }
1360
1361 if (opts.sign_version === 4 || opts.sign_version === '4') {
1362 // use aws4
1363 var options = {
1364 host: self.uri.host,
1365 path: self.uri.path,
1366 method: self.method,
1367 headers: {
1368 'content-type': self.getHeader('content-type') || ''
1369 },
1370 body: self.body
1371 }
1372 var signRes = aws4.sign(options, {
1373 accessKeyId: opts.key,
1374 secretAccessKey: opts.secret,
1375 sessionToken: opts.session
1376 })
1377 self.setHeader('authorization', signRes.headers.Authorization)
1378 self.setHeader('x-amz-date', signRes.headers['X-Amz-Date'])
1379 if (signRes.headers['X-Amz-Security-Token']) {
1380 self.setHeader('x-amz-security-token', signRes.headers['X-Amz-Security-Token'])
1381 }
1382 } else {
1383 // default: use aws-sign2
1384 var date = new Date()
1385 self.setHeader('date', date.toUTCString())
1386 var auth = {
1387 key: opts.key,
1388 secret: opts.secret,
1389 verb: self.method.toUpperCase(),
1390 date: date,
1391 contentType: self.getHeader('content-type') || '',
1392 md5: self.getHeader('content-md5') || '',
1393 amazonHeaders: aws2.canonicalizeHeaders(self.headers)
1394 }
1395 var path = self.uri.path
1396 if (opts.bucket && path) {
1397 auth.resource = '/' + opts.bucket + path
1398 } else if (opts.bucket && !path) {
1399 auth.resource = '/' + opts.bucket
1400 } else if (!opts.bucket && path) {
1401 auth.resource = path
1402 } else if (!opts.bucket && !path) {
1403 auth.resource = '/'
1404 }
1405 auth.resource = aws2.canonicalizeResource(auth.resource)
1406 self.setHeader('authorization', aws2.authorization(auth))
1407 }
1408
1409 return self
1410}
1411Request.prototype.httpSignature = function (opts) {
1412 var self = this
1413 httpSignature.signRequest({
1414 getHeader: function (header) {
1415 return self.getHeader(header, self.headers)
1416 },
1417 setHeader: function (header, value) {
1418 self.setHeader(header, value)
1419 },
1420 method: self.method,
1421 path: self.path
1422 }, opts)
1423 debug('httpSignature authorization', self.getHeader('authorization'))
1424
1425 return self
1426}
1427Request.prototype.hawk = function (opts) {
1428 var self = this
1429 self.setHeader('Authorization', hawk.client.header(self.uri, self.method, opts).field)
1430}
1431Request.prototype.oauth = function (_oauth) {
1432 var self = this
1433
1434 self._oauth.onRequest(_oauth)
1435
1436 return self
1437}
1438
1439Request.prototype.jar = function (jar) {
1440 var self = this
1441 var cookies
1442
1443 if (self._redirect.redirectsFollowed === 0) {
1444 self.originalCookieHeader = self.getHeader('cookie')
1445 }
1446
1447 if (!jar) {
1448 // disable cookies
1449 cookies = false
1450 self._disableCookies = true
1451 } else {
1452 var targetCookieJar = (jar && jar.getCookieString) ? jar : globalCookieJar
1453 var urihref = self.uri.href
1454 // fetch cookie in the Specified host
1455 if (targetCookieJar) {
1456 cookies = targetCookieJar.getCookieString(urihref)
1457 }
1458 }
1459
1460 // if need cookie and cookie is not empty
1461 if (cookies && cookies.length) {
1462 if (self.originalCookieHeader) {
1463 // Don't overwrite existing Cookie header
1464 self.setHeader('cookie', self.originalCookieHeader + '; ' + cookies)
1465 } else {
1466 self.setHeader('cookie', cookies)
1467 }
1468 }
1469 self._jar = jar
1470 return self
1471}
1472
1473// Stream API
1474Request.prototype.pipe = function (dest, opts) {
1475 var self = this
1476
1477 if (self.response) {
1478 if (self._destdata) {
1479 self.emit('error', new Error('You cannot pipe after data has been emitted from the response.'))
1480 } else if (self._ended) {
1481 self.emit('error', new Error('You cannot pipe after the response has been ended.'))
1482 } else {
1483 stream.Stream.prototype.pipe.call(self, dest, opts)
1484 self.pipeDest(dest)
1485 return dest
1486 }
1487 } else {
1488 self.dests.push(dest)
1489 stream.Stream.prototype.pipe.call(self, dest, opts)
1490 return dest
1491 }
1492}
1493Request.prototype.write = function () {
1494 var self = this
1495 if (self._aborted) { return }
1496
1497 if (!self._started) {
1498 self.start()
1499 }
1500 if (self.req) {
1501 return self.req.write.apply(self.req, arguments)
1502 }
1503}
1504Request.prototype.end = function (chunk) {
1505 var self = this
1506 if (self._aborted) { return }
1507
1508 if (chunk) {
1509 self.write(chunk)
1510 }
1511 if (!self._started) {
1512 self.start()
1513 }
1514 if (self.req) {
1515 self.req.end()
1516 }
1517}
1518Request.prototype.pause = function () {
1519 var self = this
1520 if (!self.responseContent) {
1521 self._paused = true
1522 } else {
1523 self.responseContent.pause.apply(self.responseContent, arguments)
1524 }
1525}
1526Request.prototype.resume = function () {
1527 var self = this
1528 if (!self.responseContent) {
1529 self._paused = false
1530 } else {
1531 self.responseContent.resume.apply(self.responseContent, arguments)
1532 }
1533}
1534Request.prototype.destroy = function () {
1535 var self = this
1536 if (!self._ended) {
1537 self.end()
1538 } else if (self.response) {
1539 self.response.destroy()
1540 }
1541}
1542
1543Request.defaultProxyHeaderWhiteList =
1544 Tunnel.defaultProxyHeaderWhiteList.slice()
1545
1546Request.defaultProxyHeaderExclusiveList =
1547 Tunnel.defaultProxyHeaderExclusiveList.slice()
1548
1549// Exports
1550
1551Request.prototype.toJSON = requestToJSON
1552module.exports = Request