Tizen 2.1 base
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / prompt / node_modules / winston / node_modules / loggly / node_modules / request / main.js
1 // Copyright 2010-2012 Mikeal Rogers
2 //
3 //    Licensed under the Apache License, Version 2.0 (the "License");
4 //    you may not use this file except in compliance with the License.
5 //    You may obtain a copy of the License at
6 //
7 //        http://www.apache.org/licenses/LICENSE-2.0
8 //
9 //    Unless required by applicable law or agreed to in writing, software
10 //    distributed under the License is distributed on an "AS IS" BASIS,
11 //    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //    See the License for the specific language governing permissions and
13 //    limitations under the License.
14
15 var http = require('http')
16   , https = false
17   , tls = false
18   , url = require('url')
19   , util = require('util')
20   , stream = require('stream')
21   , qs = require('querystring')
22   , mimetypes = require('./mimetypes')
23   , oauth = require('./oauth')
24   , uuid = require('./uuid')
25   , ForeverAgent = require('./forever')
26   , Cookie = require('./vendor/cookie')
27   , CookieJar = require('./vendor/cookie/jar')
28   , cookieJar = new CookieJar
29   , tunnel = require('./tunnel')
30   , aws = require('./aws')
31   ;
32   
33 if (process.logging) {
34   var log = process.logging('request')
35 }
36
37 try {
38   https = require('https')
39 } catch (e) {}
40
41 try {
42   tls = require('tls')
43 } catch (e) {}
44
45 function toBase64 (str) {
46   return (new Buffer(str || "", "ascii")).toString("base64")
47 }
48
49 // Hacky fix for pre-0.4.4 https
50 if (https && !https.Agent) {
51   https.Agent = function (options) {
52     http.Agent.call(this, options)
53   }
54   util.inherits(https.Agent, http.Agent)
55   https.Agent.prototype._getConnection = function (host, port, cb) {
56     var s = tls.connect(port, host, this.options, function () {
57       // do other checks here?
58       if (cb) cb()
59     })
60     return s
61   }
62 }
63
64 function isReadStream (rs) {
65   if (rs.readable && rs.path && rs.mode) {
66     return true
67   }
68 }
69
70 function copy (obj) {
71   var o = {}
72   Object.keys(obj).forEach(function (i) {
73     o[i] = obj[i]
74   })
75   return o
76 }
77
78 var isUrl = /^https?:/
79
80 var globalPool = {}
81
82 function Request (options) {
83   stream.Stream.call(this)
84   this.readable = true
85   this.writable = true
86
87   if (typeof options === 'string') {
88     options = {uri:options}
89   }
90   
91   var reserved = Object.keys(Request.prototype)
92   for (var i in options) {
93     if (reserved.indexOf(i) === -1) {
94       this[i] = options[i]
95     } else {
96       if (typeof options[i] === 'function') {
97         delete options[i]
98       }
99     }
100   }
101   options = copy(options)
102   
103   this.init(options)
104 }
105 util.inherits(Request, stream.Stream)
106 Request.prototype.init = function (options) {
107   var self = this
108   
109   if (!options) options = {}
110   
111   if (!self.pool && self.pool !== false) self.pool = globalPool
112   self.dests = []
113   self.__isRequestRequest = true
114   
115   // Protect against double callback
116   if (!self._callback && self.callback) {
117     self._callback = self.callback
118     self.callback = function () {
119       if (self._callbackCalled) return // Print a warning maybe?
120       self._callback.apply(self, arguments)
121       self._callbackCalled = true
122     }
123     self.on('error', self.callback.bind())
124     self.on('complete', self.callback.bind(self, null))
125   }
126
127   if (self.url) {
128     // People use this property instead all the time so why not just support it.
129     self.uri = self.url
130     delete self.url
131   }
132
133   if (!self.uri) {
134     throw new Error("options.uri is a required argument")
135   } else {
136     if (typeof self.uri == "string") self.uri = url.parse(self.uri)
137   }
138   if (self.proxy) {
139     if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
140
141     // do the HTTP CONNECT dance using koichik/node-tunnel
142     if (http.globalAgent && self.uri.protocol === "https:") {
143       self.tunnel = true
144       var tunnelFn = self.proxy.protocol === "http:"
145                    ? tunnel.httpsOverHttp : tunnel.httpsOverHttps
146
147       var tunnelOptions = { proxy: { host: self.proxy.hostname
148                                    , port: +self.proxy.port 
149                                    , proxyAuth: self.proxy.auth }
150                           , ca: this.ca }
151
152       self.agent = tunnelFn(tunnelOptions)
153       self.tunnel = true
154     }
155   }
156
157   if (!self.uri.host || !self.uri.pathname) {
158     // Invalid URI: it may generate lot of bad errors, like "TypeError: Cannot call method 'indexOf' of undefined" in CookieJar
159     // Detect and reject it as soon as possible
160     var faultyUri = url.format(self.uri)
161     var message = 'Invalid URI "' + faultyUri + '"'
162     if (Object.keys(options).length === 0) {
163       // No option ? This can be the sign of a redirect
164       // As this is a case where the user cannot do anything (he didn't call request directly with this URL)
165       // he should be warned that it can be caused by a redirection (can save some hair)
166       message += '. This can be caused by a crappy redirection.'
167     }
168     self.emit('error', new Error(message))
169     return // This error was fatal
170   }
171
172   self._redirectsFollowed = self._redirectsFollowed || 0
173   self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10
174   self.followRedirect = (self.followRedirect !== undefined) ? self.followRedirect : true
175   self.followAllRedirects = (self.followAllRedirects !== undefined) ? self.followAllRedirects : false;
176   if (self.followRedirect || self.followAllRedirects)
177     self.redirects = self.redirects || []
178
179   self.headers = self.headers ? copy(self.headers) : {}
180
181   self.setHost = false
182   if (!self.headers.host) {
183     self.headers.host = self.uri.hostname
184     if (self.uri.port) {
185       if ( !(self.uri.port === 80 && self.uri.protocol === 'http:') &&
186            !(self.uri.port === 443 && self.uri.protocol === 'https:') )
187       self.headers.host += (':'+self.uri.port)
188     }
189     self.setHost = true
190   }
191   
192   self.jar(self._jar || options.jar)
193
194   if (!self.uri.pathname) {self.uri.pathname = '/'}
195   if (!self.uri.port) {
196     if (self.uri.protocol == 'http:') {self.uri.port = 80}
197     else if (self.uri.protocol == 'https:') {self.uri.port = 443}
198   }
199
200   if (self.proxy && !self.tunnel) {
201     self.port = self.proxy.port
202     self.host = self.proxy.hostname
203   } else {
204     self.port = self.uri.port
205     self.host = self.uri.hostname
206   }
207
208   self.clientErrorHandler = function (error) {
209     if (self._aborted) return
210     
211     if (self.setHost) delete self.headers.host
212     if (self.req._reusedSocket && error.code === 'ECONNRESET'
213         && self.agent.addRequestNoreuse) {
214       self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
215       self.start()
216       self.req.end()
217       return
218     }
219     if (self.timeout && self.timeoutTimer) {
220       clearTimeout(self.timeoutTimer)
221       self.timeoutTimer = null
222     }
223     self.emit('error', error)
224   }
225
226   if (options.form) {
227     self.form(options.form)
228   }
229
230   if (options.oauth) {
231     self.oauth(options.oauth)
232   }
233   
234   if (options.aws) {
235     self.aws(options.aws)
236   }
237
238   if (self.uri.auth && !self.headers.authorization) {
239     self.headers.authorization = "Basic " + toBase64(self.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
240   }
241   if (self.proxy && self.proxy.auth && !self.headers['proxy-authorization'] && !self.tunnel) {
242     self.headers['proxy-authorization'] = "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
243   }
244
245   if (options.qs) self.qs(options.qs)
246
247   if (self.uri.path) {
248     self.path = self.uri.path
249   } else {
250     self.path = self.uri.pathname + (self.uri.search || "")
251   }
252
253   if (self.path.length === 0) self.path = '/'
254
255   if (self.proxy && !self.tunnel) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
256
257   if (options.json) {
258     self.json(options.json)
259   } else if (options.multipart) {
260     self.boundary = uuid()
261     self.multipart(options.multipart)
262   }
263
264   if (self.body) {
265     var length = 0
266     if (!Buffer.isBuffer(self.body)) {
267       if (Array.isArray(self.body)) {
268         for (var i = 0; i < self.body.length; i++) {
269           length += self.body[i].length
270         }
271       } else {
272         self.body = new Buffer(self.body)
273         length = self.body.length
274       }
275     } else {
276       length = self.body.length
277     }
278     if (length) {
279       self.headers['content-length'] = length
280     } else {
281       throw new Error('Argument error, options.body.')
282     }
283   }
284
285   var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
286     , defaultModules = {'http:':http, 'https:':https}
287     , httpModules = self.httpModules || {}
288     ;
289   self.httpModule = httpModules[protocol] || defaultModules[protocol]
290
291   if (!self.httpModule) throw new Error("Invalid protocol")
292
293   if (options.ca) self.ca = options.ca
294
295   if (!self.agent) {
296     if (options.agentOptions) self.agentOptions = options.agentOptions
297
298     if (options.agentClass) {
299       self.agentClass = options.agentClass
300     } else if (options.forever) {
301       self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
302     } else {
303       self.agentClass = self.httpModule.Agent
304     }
305   }
306
307   if (self.pool === false) {
308     self.agent = false
309   } else {
310     self.agent = self.agent || self.getAgent()
311     if (self.maxSockets) {
312       // Don't use our pooling if node has the refactored client
313       self.agent.maxSockets = self.maxSockets
314     }
315     if (self.pool.maxSockets) {
316       // Don't use our pooling if node has the refactored client
317       self.agent.maxSockets = self.pool.maxSockets
318     }
319   }
320
321   self.once('pipe', function (src) {
322     if (self.ntick) throw new Error("You cannot pipe to this stream after the first nextTick() after creation of the request stream.")
323     self.src = src
324     if (isReadStream(src)) {
325       if (!self.headers['content-type'] && !self.headers['Content-Type'])
326         self.headers['content-type'] = mimetypes.lookup(src.path.slice(src.path.lastIndexOf('.')+1))
327     } else {
328       if (src.headers) {
329         for (var i in src.headers) {
330           if (!self.headers[i]) {
331             self.headers[i] = src.headers[i]
332           }
333         }
334       }
335       if (src.method && !self.method) {
336         self.method = src.method
337       }
338     }
339
340     self.on('pipe', function () {
341       console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
342     })
343   })
344
345   process.nextTick(function () {
346     if (self._aborted) return
347     
348     if (self.body) {
349       if (Array.isArray(self.body)) {
350         self.body.forEach(function (part) {
351           self.write(part)
352         })
353       } else {
354         self.write(self.body)
355       }
356       self.end()
357     } else if (self.requestBodyStream) {
358       console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.")
359       self.requestBodyStream.pipe(self)
360     } else if (!self.src) {
361       if (self.method !== 'GET' && typeof self.method !== 'undefined') {
362         self.headers['content-length'] = 0;
363       }
364       self.end();
365     }
366     self.ntick = true
367   })
368 }
369
370 Request.prototype.getAgent = function () {
371   var Agent = this.agentClass
372   var options = {}
373   if (this.agentOptions) {
374     for (var i in this.agentOptions) {
375       options[i] = this.agentOptions[i]
376     }
377   }
378   if (this.ca) options.ca = this.ca
379
380   var poolKey = ''
381
382   // different types of agents are in different pools
383   if (Agent !== this.httpModule.Agent) {
384     poolKey += Agent.name
385   }
386
387   if (!this.httpModule.globalAgent) {
388     // node 0.4.x
389     options.host = this.host
390     options.port = this.port
391     if (poolKey) poolKey += ':'
392     poolKey += this.host + ':' + this.port
393   }
394
395   if (options.ca) {
396     if (poolKey) poolKey += ':'
397     poolKey += options.ca
398   }
399
400   if (!poolKey && Agent === this.httpModule.Agent && this.httpModule.globalAgent) {
401     // not doing anything special.  Use the globalAgent
402     return this.httpModule.globalAgent
403   }
404
405   // already generated an agent for this setting
406   if (this.pool[poolKey]) return this.pool[poolKey]
407
408   return this.pool[poolKey] = new Agent(options)
409 }
410
411 Request.prototype.start = function () {
412   var self = this
413   
414   if (self._aborted) return
415   
416   self._started = true
417   self.method = self.method || 'GET'
418   self.href = self.uri.href
419   if (log) log('%method %href', self)
420   
421   if (self.src && self.src.stat && self.src.stat.size) {
422     self.headers['content-length'] = self.src.stat.size
423   }
424   if (self._aws) {
425     self.aws(self._aws, true)
426   }
427   
428   self.req = self.httpModule.request(self, function (response) {
429     if (self._aborted) return
430     if (self._paused) response.pause()
431     
432     self.response = response
433     response.request = self
434     response.toJSON = toJSON
435
436     if (self.httpModule === https &&
437         self.strictSSL &&
438         !response.client.authorized) {
439       var sslErr = response.client.authorizationError
440       self.emit('error', new Error('SSL Error: '+ sslErr))
441       return
442     }
443
444     if (self.setHost) delete self.headers.host
445     if (self.timeout && self.timeoutTimer) {
446       clearTimeout(self.timeoutTimer)
447       self.timeoutTimer = null
448     }  
449     
450     var addCookie = function (cookie) {
451       if (self._jar) self._jar.add(new Cookie(cookie))
452       else cookieJar.add(new Cookie(cookie))
453     }
454
455     if (response.headers['set-cookie'] && (!self._disableCookies)) {
456       if (Array.isArray(response.headers['set-cookie'])) response.headers['set-cookie'].forEach(addCookie)
457       else addCookie(response.headers['set-cookie'])
458     }
459
460     if (response.statusCode >= 300 && response.statusCode < 400  &&
461         (self.followAllRedirects ||
462          (self.followRedirect && (self.method !== 'PUT' && self.method !== 'POST' && self.method !== 'DELETE'))) &&
463         response.headers.location) {
464       if (self._redirectsFollowed >= self.maxRedirects) {
465         self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."))
466         return
467       }
468       self._redirectsFollowed += 1
469
470       if (!isUrl.test(response.headers.location)) {
471         response.headers.location = url.resolve(self.uri.href, response.headers.location)
472       }
473       self.uri = response.headers.location
474       self.redirects.push(
475         { statusCode : response.statusCode
476         , redirectUri: response.headers.location 
477         }
478       )
479       if (self.followAllRedirects) self.method = 'GET'
480       // self.method = 'GET'; // Force all redirects to use GET || commented out fixes #215
481       delete self.req
482       delete self.agent
483       delete self._started
484       delete self.body
485       if (self.headers) {
486         delete self.headers.host
487       }
488       if (log) log('Redirect to %uri', self)
489       self.init()
490       return // Ignore the rest of the response
491     } else {
492       self._redirectsFollowed = self._redirectsFollowed || 0
493       // Be a good stream and emit end when the response is finished.
494       // Hack to emit end on close because of a core bug that never fires end
495       response.on('close', function () {
496         if (!self._ended) self.response.emit('end')
497       })
498
499       if (self.encoding) {
500         if (self.dests.length !== 0) {
501           console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.")
502         } else {
503           response.setEncoding(self.encoding)
504         }
505       }
506
507       self.dests.forEach(function (dest) {
508         self.pipeDest(dest)
509       })
510
511       response.on("data", function (chunk) {
512         self._destdata = true
513         self.emit("data", chunk)
514       })
515       response.on("end", function (chunk) {
516         self._ended = true
517         self.emit("end", chunk)
518       })
519       response.on("close", function () {self.emit("close")})
520
521       self.emit('response', response)
522
523       if (self.callback) {
524         var buffer = []
525         var bodyLen = 0
526         self.on("data", function (chunk) {
527           buffer.push(chunk)
528           bodyLen += chunk.length
529         })
530         self.on("end", function () {
531           if (self._aborted) return
532           
533           if (buffer.length && Buffer.isBuffer(buffer[0])) {
534             var body = new Buffer(bodyLen)
535             var i = 0
536             buffer.forEach(function (chunk) {
537               chunk.copy(body, i, 0, chunk.length)
538               i += chunk.length
539             })
540             if (self.encoding === null) {
541               response.body = body
542             } else {
543               response.body = body.toString()
544             }
545           } else if (buffer.length) {
546             response.body = buffer.join('')
547           }
548
549           if (self._json) {
550             try {
551               response.body = JSON.parse(response.body)
552             } catch (e) {}
553           }
554           
555           self.emit('complete', response, response.body)
556         })
557       }
558     }
559   })
560
561   if (self.timeout && !self.timeoutTimer) {
562     self.timeoutTimer = setTimeout(function () {
563       self.req.abort()
564       var e = new Error("ETIMEDOUT")
565       e.code = "ETIMEDOUT"
566       self.emit("error", e)
567     }, self.timeout)
568     
569     // Set additional timeout on socket - in case if remote
570     // server freeze after sending headers
571     if (self.req.setTimeout) { // only works on node 0.6+
572       self.req.setTimeout(self.timeout, function () {
573         if (self.req) {
574           self.req.abort()
575           var e = new Error("ESOCKETTIMEDOUT")
576           e.code = "ESOCKETTIMEDOUT"
577           self.emit("error", e)
578         }
579       })
580     }
581   }
582   
583   self.req.on('error', self.clientErrorHandler)
584   self.req.on('drain', function() {
585     self.emit('drain')
586   })
587   
588   self.emit('request', self.req)
589 }
590
591 Request.prototype.abort = function () {
592   this._aborted = true;
593   
594   if (this.req) {
595     this.req.abort()
596   }
597   else if (this.response) {
598     this.response.abort()
599   }
600   
601   this.emit("abort")
602 }
603
604 Request.prototype.pipeDest = function (dest) {
605   var response = this.response
606   // Called after the response is received
607   if (dest.headers) {
608     dest.headers['content-type'] = response.headers['content-type']
609     if (response.headers['content-length']) {
610       dest.headers['content-length'] = response.headers['content-length']
611     }
612   }
613   if (dest.setHeader) {
614     for (var i in response.headers) {
615       dest.setHeader(i, response.headers[i])
616     }
617     dest.statusCode = response.statusCode
618   }
619   if (this.pipefilter) this.pipefilter(response, dest)
620 }
621
622 // Composable API
623 Request.prototype.setHeader = function (name, value, clobber) {
624   if (clobber === undefined) clobber = true
625   if (clobber || !this.headers.hasOwnProperty(name)) this.headers[name] = value
626   else this.headers[name] += ',' + value
627   return this
628 }
629 Request.prototype.setHeaders = function (headers) {
630   for (var i in headers) {this.setHeader(i, headers[i])}
631   return this
632 }
633 Request.prototype.qs = function (q, clobber) {
634   var base
635   if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
636   else base = {}
637   
638   for (var i in q) {
639     base[i] = q[i]
640   }
641   
642   this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
643   this.url = this.uri
644   
645   return this
646 }
647 Request.prototype.form = function (form) {
648   this.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8'
649   this.body = qs.stringify(form).toString('utf8')
650   return this
651 }
652 Request.prototype.multipart = function (multipart) {
653   var self = this
654   self.body = []
655
656   if (!self.headers['content-type']) {
657     self.headers['content-type'] = 'multipart/related; boundary=' + self.boundary;
658   } else {
659     self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=' + self.boundary;
660   }
661
662   console.log('boundary >> ' + self.boundary)
663
664   if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
665
666   multipart.forEach(function (part) {
667     var body = part.body
668     if(body == null) throw Error('Body attribute missing in multipart.')
669     delete part.body
670     var preamble = '--' + self.boundary + '\r\n'
671     Object.keys(part).forEach(function (key) {
672       preamble += key + ': ' + part[key] + '\r\n'
673     })
674     preamble += '\r\n'
675     self.body.push(new Buffer(preamble))
676     self.body.push(new Buffer(body))
677     self.body.push(new Buffer('\r\n'))
678   })
679   self.body.push(new Buffer('--' + self.boundary + '--'))
680   return self
681 }
682 Request.prototype.json = function (val) {
683   this.setHeader('content-type', 'application/json')
684   this.setHeader('accept', 'application/json')
685   this._json = true
686   if (typeof val === 'boolean') {
687     if (typeof this.body === 'object') this.body = JSON.stringify(this.body)
688   } else {
689     this.body = JSON.stringify(val)
690   }
691   return this
692 }
693 Request.prototype.aws = function (opts, now) {
694   if (!now) {
695     this._aws = opts
696     return this
697   }
698   var date = new Date()
699   this.setHeader('date', date.toUTCString())
700   this.setHeader('authorization', aws.authorization(
701     { key: opts.key
702     , secret: opts.secret
703     , verb: this.method
704     , date: date
705     , resource: aws.canonicalizeResource('/' + opts.bucket + this.path)
706     , contentType: this.headers['content-type'] || ''
707     , md5: this.headers['content-md5'] || ''
708     , amazonHeaders: aws.canonicalizeHeaders(this.headers)
709     }
710   ))
711   
712   return this
713 }
714
715 Request.prototype.oauth = function (_oauth) {
716   var form
717   if (this.headers['content-type'] && 
718       this.headers['content-type'].slice(0, 'application/x-www-form-urlencoded'.length) ===
719         'application/x-www-form-urlencoded' 
720      ) {
721     form = qs.parse(this.body)
722   }
723   if (this.uri.query) {
724     form = qs.parse(this.uri.query)
725   } 
726   if (!form) form = {}
727   var oa = {}
728   for (var i in form) oa[i] = form[i]
729   for (var i in _oauth) oa['oauth_'+i] = _oauth[i]
730   if (!oa.oauth_version) oa.oauth_version = '1.0'
731   if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( (new Date()).getTime() / 1000 ).toString()
732   if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '')
733   
734   oa.oauth_signature_method = 'HMAC-SHA1'
735   
736   var consumer_secret = oa.oauth_consumer_secret
737   delete oa.oauth_consumer_secret
738   var token_secret = oa.oauth_token_secret
739   delete oa.oauth_token_secret
740   
741   var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname
742   var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret)
743   
744   // oa.oauth_signature = signature
745   for (var i in form) {
746     if ( i.slice(0, 'oauth_') in _oauth) {
747       // skip 
748     } else {
749       delete oa['oauth_'+i]
750     }
751   }
752   this.headers.Authorization = 
753     'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',')
754   this.headers.Authorization += ',oauth_signature="'+oauth.rfc3986(signature)+'"'
755   return this
756 }
757 Request.prototype.jar = function (jar) {
758   var cookies
759   
760   if (this._redirectsFollowed === 0) {
761     this.originalCookieHeader = this.headers.cookie
762   }
763   
764   if (jar === false) {
765     // disable cookies
766     cookies = false;
767     this._disableCookies = true;
768   } else if (jar) {
769     // fetch cookie from the user defined cookie jar
770     cookies = jar.get({ url: this.uri.href })
771   } else {
772     // fetch cookie from the global cookie jar
773     cookies = cookieJar.get({ url: this.uri.href })
774   }
775   
776   if (cookies && cookies.length) {
777     var cookieString = cookies.map(function (c) {
778       return c.name + "=" + c.value
779     }).join("; ")
780
781     if (this.originalCookieHeader) {
782       // Don't overwrite existing Cookie header
783       this.headers.cookie = this.originalCookieHeader + '; ' + cookieString
784     } else {
785       this.headers.cookie = cookieString
786     }
787   }
788   this._jar = jar
789   return this
790 }
791
792
793 // Stream API
794 Request.prototype.pipe = function (dest, opts) {
795   if (this.response) {
796     if (this._destdata) {
797       throw new Error("You cannot pipe after data has been emitted from the response.")
798     } else if (this._ended) {
799       throw new Error("You cannot pipe after the response has been ended.")
800     } else {
801       stream.Stream.prototype.pipe.call(this, dest, opts)
802       this.pipeDest(dest)
803       return dest
804     }
805   } else {
806     this.dests.push(dest)
807     stream.Stream.prototype.pipe.call(this, dest, opts)
808     return dest
809   }
810 }
811 Request.prototype.write = function () {
812   if (!this._started) this.start()
813   return this.req.write.apply(this.req, arguments)
814 }
815 Request.prototype.end = function (chunk) {
816   if (chunk) this.write(chunk)
817   if (!this._started) this.start()
818   this.req.end()
819 }
820 Request.prototype.pause = function () {
821   if (!this.response) this._paused = true
822   else this.response.pause.apply(this.response, arguments)
823 }
824 Request.prototype.resume = function () {
825   if (!this.response) this._paused = false
826   else this.response.resume.apply(this.response, arguments)
827 }
828 Request.prototype.destroy = function () {
829   if (!this._ended) this.end()
830 }
831
832 // organize params for post, put, head, del
833 function initParams(uri, options, callback) {
834   if ((typeof options === 'function') && !callback) callback = options;
835   if (options && typeof options === 'object') {
836     options.uri = uri;
837   } else if (typeof uri === 'string') {
838     options = {uri:uri};
839   } else {
840     options = uri;
841     uri = options.uri;
842   }
843   return { uri: uri, options: options, callback: callback };
844 }
845
846 function request (uri, options, callback) {
847   if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
848   if ((typeof options === 'function') && !callback) callback = options;
849   if (options && typeof options === 'object') {
850     options.uri = uri;
851   } else if (typeof uri === 'string') {
852     options = {uri:uri};
853   } else {
854     options = uri;
855   }
856
857   if (callback) options.callback = callback;
858   var r = new Request(options)
859   return r
860 }
861
862 module.exports = request
863
864 request.defaults = function (options) {
865   var def = function (method) {
866     var d = function (uri, opts, callback) {
867       var params = initParams(uri, opts, callback);
868       for (var i in options) {
869         if (params.options[i] === undefined) params.options[i] = options[i]
870       }
871       return method(params.options, params.callback)
872     }
873     return d
874   }
875   var de = def(request)
876   de.get = def(request.get)
877   de.post = def(request.post)
878   de.put = def(request.put)
879   de.head = def(request.head)
880   de.del = def(request.del)
881   de.cookie = def(request.cookie)
882   de.jar = def(request.jar)
883   return de
884 }
885
886 request.forever = function (agentOptions, optionsArg) {
887   var options = {}
888   if (optionsArg) {
889     for (option in optionsArg) {
890       options[option] = optionsArg[option]
891     }
892   }
893   if (agentOptions) options.agentOptions = agentOptions
894   options.forever = true
895   return request.defaults(options)
896 }
897
898 request.get = request
899 request.post = function (uri, options, callback) {
900   var params = initParams(uri, options, callback);
901   params.options.method = 'POST';
902   return request(params.uri || null, params.options, params.callback)
903 }
904 request.put = function (uri, options, callback) {
905   var params = initParams(uri, options, callback);
906   params.options.method = 'PUT'
907   return request(params.uri || null, params.options, params.callback)
908 }
909 request.head = function (uri, options, callback) {
910   var params = initParams(uri, options, callback);
911   params.options.method = 'HEAD'
912   if (params.options.body || 
913       params.options.requestBodyStream || 
914       (params.options.json && typeof params.options.json !== 'boolean') || 
915       params.options.multipart) {
916     throw new Error("HTTP HEAD requests MUST NOT include a request body.")
917   }
918   return request(params.uri || null, params.options, params.callback)
919 }
920 request.del = function (uri, options, callback) {
921   var params = initParams(uri, options, callback);
922   params.options.method = 'DELETE'
923   return request(params.uri || null, params.options, params.callback)
924 }
925 request.jar = function () {
926   return new CookieJar
927 }
928 request.cookie = function (str) {
929   if (str && str.uri) str = str.uri
930   if (typeof str !== 'string') throw new Error("The cookie function only accepts STRING as param")
931   return new Cookie(str)
932 }
933
934 // Safe toJSON
935
936 function getSafe (self, uuid) {  
937   if (typeof self === 'object' || typeof self === 'function') var safe = {}
938   if (Array.isArray(self)) var safe = []
939
940   var recurse = []
941   
942   Object.defineProperty(self, uuid, {})
943   
944   var attrs = Object.keys(self).filter(function (i) {
945     if (i === uuid) return false 
946     if ( (typeof self[i] !== 'object' && typeof self[i] !== 'function') || self[i] === null) return true
947     return !(Object.getOwnPropertyDescriptor(self[i], uuid))
948   })
949   
950   
951   for (var i=0;i<attrs.length;i++) {
952     if ( (typeof self[attrs[i]] !== 'object' && typeof self[attrs[i]] !== 'function') || 
953           self[attrs[i]] === null
954         ) {
955       safe[attrs[i]] = self[attrs[i]]
956     } else {
957       recurse.push(attrs[i])
958       Object.defineProperty(self[attrs[i]], uuid, {})
959     }
960   }
961
962   for (var i=0;i<recurse.length;i++) {
963     safe[recurse[i]] = getSafe(self[recurse[i]], uuid)
964   }
965   
966   return safe
967 }
968
969 function toJSON () {
970   return getSafe(this, (((1+Math.random())*0x10000)|0).toString(16))
971 }
972
973 Request.prototype.toJSON = toJSON
974