Produced by Araxis Merge on 7/26/2017 10:13:32 AM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\AraxisMergeCompare\Pri_un\TAR\request-2.71.0\package | request.js | Tue Apr 12 13:02:11 2016 UTC |
| 2 | C:\AraxisMergeCompare\Pri_re\TAR\request-2.71.0\package | request.js | Wed Jul 26 14:45:28 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 2 | 2866 |
| Changed | 1 | 4 |
| Inserted | 0 | 0 |
| Removed | 0 | 0 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | 'use stric t' | |
| 2 | ||
| 3 | var http = require(' http') | |
| 4 | , https = require( 'https') | |
| 5 | , url = require('u rl') | |
| 6 | , util = require(' util') | |
| 7 | , stream = require ('stream') | |
| 8 | , zlib = require(' zlib') | |
| 9 | , bl = r equire('bl ') | |
| 10 | , hawk = require(' hawk') | |
| 11 | , aws2 = require(' aws-sign2' ) | |
| 12 | , httpSi gnature = require('h ttp-signat ure') | |
| 13 | , mime = require(' mime-types ') | |
| 14 | , string stream = r equire('st ringstream ') | |
| 15 | , casele ss = requi re('casele ss') | |
| 16 | , Foreve rAgent = r equire('fo rever-agen t') | |
| 17 | , FormDa ta = requi re('form-d ata') | |
| 18 | , extend = require ('extend') | |
| 19 | , isstre am = requi re('isstre am') | |
| 20 | , isType dArray = r equire('is -typedarra y').strict | |
| 21 | , helper s = requir e('./lib/h elpers') | |
| 22 | , cookie s = requir e('./lib/c ookies') | |
| 23 | , getPro xyFromURI = require( './lib/get ProxyFromU RI') | |
| 24 | , Querys tring = re quire('./l ib/queryst ring').Que rystring | |
| 25 | , Har = require('. /lib/har') .Har | |
| 26 | , Auth = require(' ./lib/auth ').Auth | |
| 27 | , OAuth = require( './lib/oau th').OAuth | |
| 28 | , Multip art = requ ire('./lib /multipart ').Multipa rt | |
| 29 | , Redire ct = requi re('./lib/ redirect') .Redirect | |
| 30 | , Tunnel = require ('./lib/tu nnel').Tun nel | |
| 31 | ||
| 32 | var safeSt ringify = helpers.sa feStringif y | |
| 33 | , isRead Stream = h elpers.isR eadStream | |
| 34 | , toBase 64 = helpe rs.toBase6 4 | |
| 35 | , defer = helpers. defer | |
| 36 | , copy = helpers.c opy | |
| 37 | , versio n = helper s.version | |
| 38 | , global CookieJar = cookies. jar() | |
| 39 | ||
| 40 | ||
| 41 | var global Pool = {} | |
| 42 | ||
| 43 | function f ilterForNo nReserved( reserved, options) { | |
| 44 | // Filte r out prop erties tha t are not reserved. | |
| 45 | // Reser ved values are passe d in at ca ll site. | |
| 46 | ||
| 47 | var obje ct = {} | |
| 48 | for (var i in opti ons) { | |
| 49 | var no tReserved = (reserve d.indexOf( i) === -1) | |
| 50 | if (no tReserved) { | |
| 51 | obje ct[i] = op tions[i] | |
| 52 | } | |
| 53 | } | |
| 54 | return o bject | |
| 55 | } | |
| 56 | ||
| 57 | function f ilterOutRe servedFunc tions(rese rved, opti ons) { | |
| 58 | // Filte r out prop erties tha t are func tions and are reserv ed. | |
| 59 | // Reser ved values are passe d in at ca ll site. | |
| 60 | ||
| 61 | var obje ct = {} | |
| 62 | for (var i in opti ons) { | |
| 63 | var is Reserved = !(reserve d.indexOf( i) === -1) | |
| 64 | var is Function = (typeof o ptions[i] === 'funct ion') | |
| 65 | if (!( isReserved && isFunc tion)) { | |
| 66 | obje ct[i] = op tions[i] | |
| 67 | } | |
| 68 | } | |
| 69 | return o bject | |
| 70 | ||
| 71 | } | |
| 72 | ||
| 73 | // Functio n for prop erly handl ing a conn ection err or | |
| 74 | function c onnectionE rrorHandle r(error) { | |
| 75 | var sock et = this | |
| 76 | if (sock et.res) { | |
| 77 | if (so cket.res.r equest) { | |
| 78 | sock et.res.req uest.emit( 'error', e rror) | |
| 79 | } else { | |
| 80 | sock et.res.emi t('error', error) | |
| 81 | } | |
| 82 | } else { | |
| 83 | socket ._httpMess age.emit(' error', er ror) | |
| 84 | } | |
| 85 | } | |
| 86 | ||
| 87 | // Return a simpler request ob ject to al low serial ization | |
| 88 | function r equestToJS ON() { | |
| 89 | var self = this | |
| 90 | return { | |
| 91 | uri: s elf.uri, | |
| 92 | method : self.met hod, | |
| 93 | header s: self.he aders | |
| 94 | } | |
| 95 | } | |
| 96 | ||
| 97 | // Return a simpler response o bject to a llow seria lization | |
| 98 | function r esponseToJ SON() { | |
| 99 | var self = this | |
| 100 | return { | |
| 101 | status Code: self .statusCod e, | |
| 102 | body: self.body, | |
| 103 | header s: self.he aders, | |
| 104 | reques t: request ToJSON.cal l(self.req uest) | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | function R equest (op tions) { | |
| 109 | // if gi ven the me thod prope rty in opt ions, set property e xplicitMet hod to tru e | |
| 110 | ||
| 111 | // exten d the Requ est instan ce with an y non-rese rved prope rties | |
| 112 | // remov e any rese rved funct ions from the option s object | |
| 113 | // set R equest ins tance to b e readable and writa ble | |
| 114 | // call init | |
| 115 | ||
| 116 | var self = this | |
| 117 | ||
| 118 | // start with HAR, then over ride with additional options | |
| 119 | if (opti ons.har) { | |
| 120 | self._ har = new Har(self) | |
| 121 | option s = self._ har.option s(options) | |
| 122 | } | |
| 123 | ||
| 124 | stream.S tream.call (self) | |
| 125 | var rese rved = Obj ect.keys(R equest.pro totype) | |
| 126 | var nonR eserved = filterForN onReserved (reserved, options) | |
| 127 | ||
| 128 | extend(s elf, nonRe served) | |
| 129 | options = filterOu tReservedF unctions(r eserved, o ptions) | |
| 130 | ||
| 131 | self.rea dable = tr ue | |
| 132 | self.wri table = tr ue | |
| 133 | if (opti ons.method ) { | |
| 134 | self.e xplicitMet hod = true | |
| 135 | } | |
| 136 | self._qs = new Que rystring(s elf) | |
| 137 | self._au th = new A uth(self) | |
| 138 | self._oa uth = new OAuth(self ) | |
| 139 | self._mu ltipart = new Multip art(self) | |
| 140 | self._re direct = n ew Redirec t(self) | |
| 141 | self._tu nnel = new Tunnel(se lf) | |
| 142 | self.ini t(options) | |
| 143 | } | |
| 144 | ||
| 145 | util.inher its(Reques t, stream. Stream) | |
| 146 | ||
| 147 | // Debuggi ng | |
| 148 | Request.de bug = proc ess.env.NO DE_DEBUG & & /\breque st\b/.test (process.e nv.NODE_DE BUG) | |
| 149 | function d ebug() { | |
| 150 | if (Requ est.debug) { | |
| 151 | consol e.error('R EQUEST %s' , util.for mat.apply( util, argu ments)) | |
| 152 | } | |
| 153 | } | |
| 154 | Request.pr ototype.de bug = debu g | |
| 155 | ||
| 156 | Request.pr ototype.in it = funct ion (optio ns) { | |
| 157 | // init( ) contains all the c ode to set up the req uest objec t. | |
| 158 | // the a ctual outg oing reque st is not started un til start( ) is calle d | |
| 159 | // this function i s called f rom both t he constru ctor and o n redirect . | |
| 160 | var self = this | |
| 161 | if (!opt ions) { | |
| 162 | option s = {} | |
| 163 | } | |
| 164 | self.hea ders = sel f.headers ? copy(sel f.headers) : {} | |
| 165 | ||
| 166 | // Delet e headers with value undefined since the y break | |
| 167 | // Clien tRequest.O utgoingMes sage.setHe ader in no de 0.12 | |
| 168 | for (var headerNam e in self. headers) { | |
| 169 | if (ty peof self. headers[he aderName] === 'undef ined') { | |
| 170 | dele te self.he aders[head erName] | |
| 171 | } | |
| 172 | } | |
| 173 | ||
| 174 | caseless .httpify(s elf, self. headers) | |
| 175 | ||
| 176 | if (!sel f.method) { | |
| 177 | self.m ethod = op tions.meth od || 'GET ' | |
| 178 | } | |
| 179 | if (!sel f.localAdd ress) { | |
| 180 | self.l ocalAddres s = option s.localAdd ress | |
| 181 | } | |
| 182 | ||
| 183 | self._qs .init(opti ons) | |
| 184 | ||
| 185 | debug(op tions) | |
| 186 | if (!sel f.pool && self.pool !== false) { | |
| 187 | self.p ool = glob alPool | |
| 188 | } | |
| 189 | self.des ts = self. dests || [ ] | |
| 190 | self.__i sRequestRe quest = tr ue | |
| 191 | ||
| 192 | // Prote ct against double ca llback | |
| 193 | if (!sel f._callbac k && self. callback) { | |
| 194 | self._ callback = self.call back | |
| 195 | self.c allback = function ( ) { | |
| 196 | if ( self._call backCalled ) { | |
| 197 | re turn // Pr int a warn ing maybe? | |
| 198 | } | |
| 199 | self ._callback Called = t rue | |
| 200 | self ._callback .apply(sel f, argumen ts) | |
| 201 | } | |
| 202 | self.o n('error', self.call back.bind( )) | |
| 203 | self.o n('complet e', self.c allback.bi nd(self, n ull)) | |
| 204 | } | |
| 205 | ||
| 206 | // Peopl e use this property instead al l the time , so suppo rt it | |
| 207 | if (!sel f.uri && s elf.url) { | |
| 208 | self.u ri = self. url | |
| 209 | delete self.url | |
| 210 | } | |
| 211 | ||
| 212 | // If th ere's a ba seUrl, the n use it a s the base URL (i.e. uri must be | |
| 213 | // speci fied as a relative p ath and is appended to baseUrl ). | |
| 214 | if (self .baseUrl) { | |
| 215 | if (ty peof self. baseUrl != = 'string' ) { | |
| 216 | retu rn self.em it('error' , new Erro r('options .baseUrl m ust be a s tring')) | |
| 217 | } | |
| 218 | ||
| 219 | if (ty peof self. uri !== 's tring') { | |
| 220 | retu rn self.em it('error' , new Erro r('options .uri must be a strin g when usi ng options .baseUrl') ) | |
| 221 | } | |
| 222 | ||
| 223 | if (se lf.uri.ind exOf('//') === 0 || self.uri.i ndexOf(':/ /') !== -1 ) { | |
| 224 | retu rn self.em it('error' , new Erro r('options .uri must be a path when using options.b aseUrl')) | |
| 225 | } | |
| 226 | ||
| 227 | // Han dle all ca ses to mak e sure tha t there's only one s lash betwe en | |
| 228 | // bas eUrl and u ri. | |
| 229 | var ba seUrlEndsW ithSlash = self.base Url.lastIn dexOf('/') === self. baseUrl.le ngth - 1 | |
| 230 | var ur iStartsWit hSlash = s elf.uri.in dexOf('/') === 0 | |
| 231 | ||
| 232 | if (ba seUrlEndsW ithSlash & & uriStart sWithSlash ) { | |
| 233 | self .uri = sel f.baseUrl + self.uri .slice(1) | |
| 234 | } else if (baseU rlEndsWith Slash || u riStartsWi thSlash) { | |
| 235 | self .uri = sel f.baseUrl + self.uri | |
| 236 | } else if (self. uri === '' ) { | |
| 237 | self .uri = sel f.baseUrl | |
| 238 | } else { | |
| 239 | self .uri = sel f.baseUrl + '/' + se lf.uri | |
| 240 | } | |
| 241 | delete self.base Url | |
| 242 | } | |
| 243 | ||
| 244 | // A URI is needed by this p oint, emit error if we haven't been able to get on e | |
| 245 | if (!sel f.uri) { | |
| 246 | return self.emit ('error', new Error( 'options.u ri is a re quired arg ument')) | |
| 247 | } | |
| 248 | ||
| 249 | // If a string URI /URL was g iven, pars e it into a URL obje ct | |
| 250 | if (type of self.ur i === 'str ing') { | |
| 251 | self.u ri = url.p arse(self. uri) | |
| 252 | } | |
| 253 | ||
| 254 | // Some URL object s are not from a URL parsed st ring and n eed href a dded | |
| 255 | if (!sel f.uri.href ) { | |
| 256 | self.u ri.href = url.format (self.uri) | |
| 257 | } | |
| 258 | ||
| 259 | // DEPRE CATED: War ning for u sers of th e old Unix Sockets U RL Scheme | |
| 260 | if (self .uri.proto col === 'u nix:') { | |
| 261 | return self.emit ('error', new Error( '`unix://` URL schem e is no lo nger suppo rted. Plea se use the format `h ttp://unix :SOCKET:PA TH`')) | |
| 262 | } | |
| 263 | ||
| 264 | // Suppo rt Unix So ckets | |
| 265 | if (self .uri.host === 'unix' ) { | |
| 266 | self.e nableUnixS ocket() | |
| 267 | } | |
| 268 | ||
| 269 | if (self .strictSSL === false ) { | |
| 270 | self.r ejectUnaut horized = false | |
| 271 | } | |
| 272 | ||
| 273 | if (!sel f.uri.path name) {sel f.uri.path name = '/' } | |
| 274 | ||
| 275 | if (!(se lf.uri.hos t || (self .uri.hostn ame && sel f.uri.port )) && !sel f.uri.isUn ix) { | |
| 276 | // Inv alid URI: it may gen erate lot of bad err ors, like 'TypeError : Cannot c all method `indexOf` of undefi ned' in Co okieJar | |
| 277 | // Det ect and re ject it as soon as p ossible | |
| 278 | var fa ultyUri = url.format (self.uri) | |
| 279 | var me ssage = 'I nvalid URI "' + faul tyUri + '" ' | |
| 280 | if (Ob ject.keys( options).l ength === 0) { | |
| 281 | // N o option ? This can be the sig n of a red irect | |
| 282 | // A s this is a case whe re the use r cannot d o anything (they did n't call r equest dir ectly with this URL) | |
| 283 | // t hey should be warned that it c an be caus ed by a re direction (can save some hair) | |
| 284 | mess age += '. This can b e caused b y a crappy redirecti on.' | |
| 285 | } | |
| 286 | // Thi s error wa s fatal | |
| 287 | self.a bort() | |
| 288 | return self.emit ('error', new Error( message)) | |
| 289 | } | |
| 290 | ||
| 291 | if (!sel f.hasOwnPr operty('pr oxy')) { | |
| 292 | self.p roxy = get ProxyFromU RI(self.ur i) | |
| 293 | } | |
| 294 | ||
| 295 | self.tun nel = self ._tunnel.i sEnabled() | |
| 296 | if (self .proxy) { | |
| 297 | self._ tunnel.set up(options ) | |
| 298 | } | |
| 299 | ||
| 300 | self._re direct.onR equest(opt ions) | |
| 301 | ||
| 302 | self.set Host = fal se | |
| 303 | if (!sel f.hasHeade r('host')) { | |
| 304 | var ho stHeaderNa me = self. originalHo stHeaderNa me || 'hos t' | |
| 305 | self.s etHeader(h ostHeaderN ame, self. uri.hostna me) | |
| 306 | if (se lf.uri.por t) { | |
| 307 | if ( !(self.ur i.port === 80 && sel f.uri.prot ocol === ' http:') && | |
| 308 | !(self.ur i.port === 443 && se lf.uri.pro tocol === 'https:') ) { | |
| 309 | se lf.setHead er(hostHea derName, s elf.getHea der('host' ) + (':' + self.uri. port) ) | |
| 310 | } | |
| 311 | } | |
| 312 | self.s etHost = t rue | |
| 313 | } | |
| 314 | ||
| 315 | self.jar (self._jar || option s.jar) | |
| 316 | ||
| 317 | if (!sel f.uri.port ) { | |
| 318 | if (self.u ri.protoco l === 'htt p:') {self .uri. PORT = PORT } | |
| 319 | else if (s elf.uri.pr otocol === 'https:') {self.uri . PORT = PORT } | |
| 320 | } | |
| 321 | ||
| 322 | if (self .proxy && !self.tunn el) { | |
| 323 | self.p ort = self .proxy.por t | |
| 324 | self.h ost = self .proxy.hos tname | |
| 325 | } else { | |
| 326 | self.p ort = self .uri.port | |
| 327 | self.h ost = self .uri.hostn ame | |
| 328 | } | |
| 329 | ||
| 330 | if (opti ons.form) { | |
| 331 | self.f orm(option s.form) | |
| 332 | } | |
| 333 | ||
| 334 | if (opti ons.formDa ta) { | |
| 335 | var fo rmData = o ptions.for mData | |
| 336 | var re questForm = self.for m() | |
| 337 | var ap pendFormVa lue = func tion (key, value) { | |
| 338 | if ( value.hasO wnProperty ('value') && value.h asOwnPrope rty('optio ns')) { | |
| 339 | re questForm. append(key , value.va lue, value .options) | |
| 340 | } el se { | |
| 341 | re questForm. append(key , value) | |
| 342 | } | |
| 343 | } | |
| 344 | for (v ar formKey in formDa ta) { | |
| 345 | if ( formData.h asOwnPrope rty(formKe y)) { | |
| 346 | va r formValu e = formDa ta[formKey ] | |
| 347 | if (formValu e instance of Array) { | |
| 348 | for (var j = 0; j < formValue. length; j+ +) { | |
| 349 | appendFo rmValue(fo rmKey, for mValue[j]) | |
| 350 | } | |
| 351 | } else { | |
| 352 | appendForm Value(form Key, formV alue) | |
| 353 | } | |
| 354 | } | |
| 355 | } | |
| 356 | } | |
| 357 | ||
| 358 | if (opti ons.qs) { | |
| 359 | self.q s(options. qs) | |
| 360 | } | |
| 361 | ||
| 362 | if (self .uri.path) { | |
| 363 | self.p ath = self .uri.path | |
| 364 | } else { | |
| 365 | self.p ath = self .uri.pathn ame + (sel f.uri.sear ch || '') | |
| 366 | } | |
| 367 | ||
| 368 | if (self .path.leng th === 0) { | |
| 369 | self.p ath = '/' | |
| 370 | } | |
| 371 | ||
| 372 | // Auth must happe n last in case signi ng is depe ndent on o ther heade rs | |
| 373 | if (opti ons.aws) { | |
| 374 | self.a ws(options .aws) | |
| 375 | } | |
| 376 | ||
| 377 | if (opti ons.hawk) { | |
| 378 | self.h awk(option s.hawk) | |
| 379 | } | |
| 380 | ||
| 381 | if (opti ons.httpSi gnature) { | |
| 382 | self.h ttpSignatu re(options .httpSigna ture) | |
| 383 | } | |
| 384 | ||
| 385 | if (opti ons.auth) { | |
| 386 | if (Ob ject.proto type.hasOw nProperty. call(optio ns.auth, ' username') ) { | |
| 387 | opti ons.auth.u ser = opti ons.auth.u sername | |
| 388 | } | |
| 389 | if (Ob ject.proto type.hasOw nProperty. call(optio ns.auth, ' password') ) { | |
| 390 | opti ons.auth.p ass = opti ons.auth.p assword | |
| 391 | } | |
| 392 | ||
| 393 | self.a uth( | |
| 394 | opti ons.auth.u ser, | |
| 395 | opti ons.auth.p ass, | |
| 396 | opti ons.auth.s endImmedia tely, | |
| 397 | opti ons.auth.b earer | |
| 398 | ) | |
| 399 | } | |
| 400 | ||
| 401 | if (self .gzip && ! self.hasHe ader('acce pt-encodin g')) { | |
| 402 | self.s etHeader(' accept-enc oding', 'g zip') | |
| 403 | } | |
| 404 | ||
| 405 | if (self .uri.auth && !self.h asHeader(' authorizat ion')) { | |
| 406 | var ur iAuthPiece s = self.u ri.auth.sp lit(':').m ap(functio n(item) {r eturn self ._qs.unesc ape(item)} ) | |
| 407 | self.a uth(uriAut hPieces[0] , uriAuthP ieces.slic e(1).join( ':'), true ) | |
| 408 | } | |
| 409 | ||
| 410 | if (!sel f.tunnel & & self.pro xy && self .proxy.aut h && !self .hasHeader ('proxy-au thorizatio n')) { | |
| 411 | var pr oxyAuthPie ces = self .proxy.aut h.split(': ').map(fun ction(item ) {return self._qs.u nescape(it em)}) | |
| 412 | var au thHeader = 'Basic ' + toBase64 (proxyAuth Pieces.joi n(':')) | |
| 413 | self.s etHeader(' proxy-auth orization' , authHead er) | |
| 414 | } | |
| 415 | ||
| 416 | if (self .proxy && !self.tunn el) { | |
| 417 | self.p ath = (sel f.uri.prot ocol + '// ' + self.u ri.host + self.path) | |
| 418 | } | |
| 419 | ||
| 420 | if (opti ons.json) { | |
| 421 | self.j son(option s.json) | |
| 422 | } | |
| 423 | if (opti ons.multip art) { | |
| 424 | self.m ultipart(o ptions.mul tipart) | |
| 425 | } | |
| 426 | ||
| 427 | if (opti ons.time) { | |
| 428 | self.t iming = tr ue | |
| 429 | self.e lapsedTime = self.el apsedTime || 0 | |
| 430 | } | |
| 431 | ||
| 432 | function setConten tLength () { | |
| 433 | if (is TypedArray (self.body )) { | |
| 434 | self .body = ne w Buffer(s elf.body) | |
| 435 | } | |
| 436 | ||
| 437 | if (!s elf.hasHea der('conte nt-length' )) { | |
| 438 | var length | |
| 439 | if ( typeof sel f.body === 'string') { | |
| 440 | le ngth = Buf fer.byteLe ngth(self. body) | |
| 441 | } | |
| 442 | else if (Array .isArray(s elf.body)) { | |
| 443 | le ngth = sel f.body.red uce(functi on (a, b) {return a + b.length }, 0) | |
| 444 | } | |
| 445 | else { | |
| 446 | le ngth = sel f.body.len gth | |
| 447 | } | |
| 448 | ||
| 449 | if ( length) { | |
| 450 | se lf.setHead er('conten t-length', length) | |
| 451 | } el se { | |
| 452 | se lf.emit('e rror', new Error('Ar gument err or, option s.body.')) | |
| 453 | } | |
| 454 | } | |
| 455 | } | |
| 456 | if (self .body && ! isstream(s elf.body)) { | |
| 457 | setCon tentLength () | |
| 458 | } | |
| 459 | ||
| 460 | if (opti ons.oauth) { | |
| 461 | self.o auth(optio ns.oauth) | |
| 462 | } else i f (self._o auth.param s && self. hasHeader( 'authoriza tion')) { | |
| 463 | self.o auth(self. _oauth.par ams) | |
| 464 | } | |
| 465 | ||
| 466 | var prot ocol = sel f.proxy && !self.tun nel ? self .proxy.pro tocol : se lf.uri.pro tocol | |
| 467 | , defa ultModules = {'http: ':http, 'h ttps:':htt ps} | |
| 468 | , http Modules = self.httpM odules || {} | |
| 469 | ||
| 470 | self.htt pModule = httpModule s[protocol ] || defau ltModules[ protocol] | |
| 471 | ||
| 472 | if (!sel f.httpModu le) { | |
| 473 | return self.emit ('error', new Error( 'Invalid p rotocol: ' + protoco l)) | |
| 474 | } | |
| 475 | ||
| 476 | if (opti ons.ca) { | |
| 477 | self.c a = option s.ca | |
| 478 | } | |
| 479 | ||
| 480 | if (!sel f.agent) { | |
| 481 | if (op tions.agen tOptions) { | |
| 482 | self .agentOpti ons = opti ons.agentO ptions | |
| 483 | } | |
| 484 | ||
| 485 | if (op tions.agen tClass) { | |
| 486 | self .agentClas s = option s.agentCla ss | |
| 487 | } else if (optio ns.forever ) { | |
| 488 | var v = versio n() | |
| 489 | // u se Forever Agent in n ode 0.10- only | |
| 490 | if ( v.major == = 0 && v.m inor <= 10 ) { | |
| 491 | se lf.agentCl ass = prot ocol === ' http:' ? F oreverAgen t : Foreve rAgent.SSL | |
| 492 | } el se { | |
| 493 | se lf.agentCl ass = self .httpModul e.Agent | |
| 494 | se lf.agentOp tions = se lf.agentOp tions || { } | |
| 495 | se lf.agentOp tions.keep Alive = tr ue | |
| 496 | } | |
| 497 | } else { | |
| 498 | self .agentClas s = self.h ttpModule. Agent | |
| 499 | } | |
| 500 | } | |
| 501 | ||
| 502 | if (self .pool === false) { | |
| 503 | self.a gent = fal se | |
| 504 | } else { | |
| 505 | self.a gent = sel f.agent || self.getN ewAgent() | |
| 506 | } | |
| 507 | ||
| 508 | self.on( 'pipe', fu nction (sr c) { | |
| 509 | if (se lf.ntick & & self._st arted) { | |
| 510 | self .emit('err or', new E rror('You cannot pip e to this stream aft er the out bound requ est has st arted.')) | |
| 511 | } | |
| 512 | self.s rc = src | |
| 513 | if (is ReadStream (src)) { | |
| 514 | if ( !self.hasH eader('con tent-type' )) { | |
| 515 | se lf.setHead er('conten t-type', m ime.lookup (src.path) ) | |
| 516 | } | |
| 517 | } else { | |
| 518 | if ( src.header s) { | |
| 519 | fo r (var i i n src.head ers) { | |
| 520 | if (!self. hasHeader( i)) { | |
| 521 | self.set Header(i, src.header s[i]) | |
| 522 | } | |
| 523 | } | |
| 524 | } | |
| 525 | if ( self._json && !self. hasHeader( 'content-t ype')) { | |
| 526 | se lf.setHead er('conten t-type', ' applicatio n/json') | |
| 527 | } | |
| 528 | if ( src.method && !self. explicitMe thod) { | |
| 529 | se lf.method = src.meth od | |
| 530 | } | |
| 531 | } | |
| 532 | ||
| 533 | // sel f.on('pipe ', functio n () { | |
| 534 | // c onsole.err or('You ha ve already piped to this strea m. Pipeing twice is likely to break the request.') | |
| 535 | // }) | |
| 536 | }) | |
| 537 | ||
| 538 | defer(fu nction () { | |
| 539 | if (se lf._aborte d) { | |
| 540 | retu rn | |
| 541 | } | |
| 542 | ||
| 543 | var en d = functi on () { | |
| 544 | if ( self._form ) { | |
| 545 | if (!self._a uth.hasAut h) { | |
| 546 | self._form .pipe(self ) | |
| 547 | } | |
| 548 | el se if (sel f._auth.ha sAuth && s elf._auth. sentAuth) { | |
| 549 | self._form .pipe(self ) | |
| 550 | } | |
| 551 | } | |
| 552 | if ( self._mult ipart && s elf._multi part.chunk ed) { | |
| 553 | se lf._multip art.body.p ipe(self) | |
| 554 | } | |
| 555 | if ( self.body) { | |
| 556 | if (isstream (self.body )) { | |
| 557 | self.body. pipe(self) | |
| 558 | } else { | |
| 559 | setContent Length() | |
| 560 | if (Array. isArray(se lf.body)) { | |
| 561 | self.bod y.forEach( function ( part) { | |
| 562 | self.w rite(part) | |
| 563 | }) | |
| 564 | } else { | |
| 565 | self.wri te(self.bo dy) | |
| 566 | } | |
| 567 | self.end() | |
| 568 | } | |
| 569 | } el se if (sel f.requestB odyStream) { | |
| 570 | co nsole.warn ('options. requestBod yStream is deprecate d, please pass the r equest obj ect to str eam.pipe.' ) | |
| 571 | se lf.request BodyStream .pipe(self ) | |
| 572 | } el se if (!se lf.src) { | |
| 573 | if (self._au th.hasAuth && !self. _auth.sent Auth) { | |
| 574 | self.end() | |
| 575 | return | |
| 576 | } | |
| 577 | if (self.met hod !== 'G ET' && typ eof self.m ethod !== 'undefined ') { | |
| 578 | self.setHe ader('cont ent-length ', 0) | |
| 579 | } | |
| 580 | se lf.end() | |
| 581 | } | |
| 582 | } | |
| 583 | ||
| 584 | if (se lf._form & & !self.ha sHeader('c ontent-len gth')) { | |
| 585 | // B efore endi ng the req uest, we h ad to comp ute the le ngth of th e whole fo rm, asyncl y | |
| 586 | self .setHeader (self._for m.getHeade rs(), true ) | |
| 587 | self ._form.get Length(fun ction (err , length) { | |
| 588 | if (!err && !isNaN(len gth)) { | |
| 589 | self.setHe ader('cont ent-length ', length) | |
| 590 | } | |
| 591 | en d() | |
| 592 | }) | |
| 593 | } else { | |
| 594 | end( ) | |
| 595 | } | |
| 596 | ||
| 597 | self.n tick = tru e | |
| 598 | }) | |
| 599 | ||
| 600 | } | |
| 601 | ||
| 602 | Request.pr ototype.ge tNewAgent = function () { | |
| 603 | var self = this | |
| 604 | var Agen t = self.a gentClass | |
| 605 | var opti ons = {} | |
| 606 | if (self .agentOpti ons) { | |
| 607 | for (v ar i in se lf.agentOp tions) { | |
| 608 | opti ons[i] = s elf.agentO ptions[i] | |
| 609 | } | |
| 610 | } | |
| 611 | if (self .ca) { | |
| 612 | option s.ca = sel f.ca | |
| 613 | } | |
| 614 | if (self .ciphers) { | |
| 615 | option s.ciphers = self.cip hers | |
| 616 | } | |
| 617 | if (self .securePro tocol) { | |
| 618 | option s.securePr otocol = s elf.secure Protocol | |
| 619 | } | |
| 620 | if (self .secureOpt ions) { | |
| 621 | option s.secureOp tions = se lf.secureO ptions | |
| 622 | } | |
| 623 | if (type of self.re jectUnauth orized !== 'undefine d') { | |
| 624 | option s.rejectUn authorized = self.re jectUnauth orized | |
| 625 | } | |
| 626 | ||
| 627 | if (self .cert && s elf.key) { | |
| 628 | option s.key = se lf.key | |
| 629 | option s.cert = s elf.cert | |
| 630 | } | |
| 631 | ||
| 632 | if (self .pfx) { | |
| 633 | option s.pfx = se lf.pfx | |
| 634 | } | |
| 635 | ||
| 636 | if (self .passphras e) { | |
| 637 | option s.passphra se = self. passphrase | |
| 638 | } | |
| 639 | ||
| 640 | var pool Key = '' | |
| 641 | ||
| 642 | // diffe rent types of agents are in di fferent po ols | |
| 643 | if (Agen t !== self .httpModul e.Agent) { | |
| 644 | poolKe y += Agent .name | |
| 645 | } | |
| 646 | ||
| 647 | // ca op tion is on ly relevan t if proxy or destin ation are https | |
| 648 | var prox y = self.p roxy | |
| 649 | if (type of proxy = == 'string ') { | |
| 650 | proxy = url.pars e(proxy) | |
| 651 | } | |
| 652 | var isHt tps = (pro xy && prox y.protocol === 'http s:') || th is.uri.pro tocol === 'https:' | |
| 653 | ||
| 654 | if (isHt tps) { | |
| 655 | if (op tions.ca) { | |
| 656 | if ( poolKey) { | |
| 657 | po olKey += ' :' | |
| 658 | } | |
| 659 | pool Key += opt ions.ca | |
| 660 | } | |
| 661 | ||
| 662 | if (ty peof optio ns.rejectU nauthorize d !== 'und efined') { | |
| 663 | if ( poolKey) { | |
| 664 | po olKey += ' :' | |
| 665 | } | |
| 666 | pool Key += opt ions.rejec tUnauthori zed | |
| 667 | } | |
| 668 | ||
| 669 | if (op tions.cert ) { | |
| 670 | if ( poolKey) { | |
| 671 | po olKey += ' :' | |
| 672 | } | |
| 673 | pool Key += opt ions.cert. toString(' ascii') + options.ke y.toString ('ascii') | |
| 674 | } | |
| 675 | ||
| 676 | if (op tions.pfx) { | |
| 677 | if ( poolKey) { | |
| 678 | po olKey += ' :' | |
| 679 | } | |
| 680 | pool Key += opt ions.pfx.t oString('a scii') | |
| 681 | } | |
| 682 | ||
| 683 | if (op tions.ciph ers) { | |
| 684 | if ( poolKey) { | |
| 685 | po olKey += ' :' | |
| 686 | } | |
| 687 | pool Key += opt ions.ciphe rs | |
| 688 | } | |
| 689 | ||
| 690 | if (op tions.secu reProtocol ) { | |
| 691 | if ( poolKey) { | |
| 692 | po olKey += ' :' | |
| 693 | } | |
| 694 | pool Key += opt ions.secur eProtocol | |
| 695 | } | |
| 696 | ||
| 697 | if (op tions.secu reOptions) { | |
| 698 | if ( poolKey) { | |
| 699 | po olKey += ' :' | |
| 700 | } | |
| 701 | pool Key += opt ions.secur eOptions | |
| 702 | } | |
| 703 | } | |
| 704 | ||
| 705 | if (self .pool === globalPool && !poolK ey && Obje ct.keys(op tions).len gth === 0 && self.ht tpModule.g lobalAgent ) { | |
| 706 | // not doing any thing spec ial. Use the global Agent | |
| 707 | return self.http Module.glo balAgent | |
| 708 | } | |
| 709 | ||
| 710 | // we're using a s tored agen t. Make s ure it's p rotocol-sp ecific | |
| 711 | poolKey = self.uri .protocol + poolKey | |
| 712 | ||
| 713 | // gener ate a new agent for this setti ng if none yet exist s | |
| 714 | if (!sel f.pool[poo lKey]) { | |
| 715 | self.p ool[poolKe y] = new A gent(optio ns) | |
| 716 | // pro perly set maxSockets on new ag ents | |
| 717 | if (se lf.pool.ma xSockets) { | |
| 718 | self .pool[pool Key].maxSo ckets = se lf.pool.ma xSockets | |
| 719 | } | |
| 720 | } | |
| 721 | ||
| 722 | return s elf.pool[p oolKey] | |
| 723 | } | |
| 724 | ||
| 725 | Request.pr ototype.st art = func tion () { | |
| 726 | // start () is call ed once we are ready to send t he outgoin g HTTP req uest. | |
| 727 | // this is usually called on the first write(), end() or o n nextTick () | |
| 728 | var self = this | |
| 729 | ||
| 730 | if (self ._aborted) { | |
| 731 | return | |
| 732 | } | |
| 733 | ||
| 734 | self._st arted = tr ue | |
| 735 | self.met hod = self .method || 'GET' | |
| 736 | self.hre f = self.u ri.href | |
| 737 | ||
| 738 | if (self .src && se lf.src.sta t && self. src.stat.s ize && !se lf.hasHead er('conten t-length') ) { | |
| 739 | self.s etHeader(' content-le ngth', sel f.src.stat .size) | |
| 740 | } | |
| 741 | if (self ._aws) { | |
| 742 | self.a ws(self._a ws, true) | |
| 743 | } | |
| 744 | ||
| 745 | // We ha ve a metho d named au th, which is complet ely differ ent from t he http.re quest | |
| 746 | // auth option. I f we don't remove it , we're go nna have a bad time. | |
| 747 | var reqO ptions = c opy(self) | |
| 748 | delete r eqOptions. auth | |
| 749 | ||
| 750 | debug('m ake reques t', self.u ri.href) | |
| 751 | ||
| 752 | try { | |
| 753 | self.r eq = self. httpModule .request(r eqOptions) | |
| 754 | } catch (err) { | |
| 755 | self.e mit('error ', err) | |
| 756 | return | |
| 757 | } | |
| 758 | ||
| 759 | if (self .timing) { | |
| 760 | self.s tartTime = new Date( ).getTime( ) | |
| 761 | } | |
| 762 | ||
| 763 | if (self .timeout & & !self.ti meoutTimer ) { | |
| 764 | var ti meout = se lf.timeout < 0 ? 0 : self.time out | |
| 765 | // Set a timeout in memory - this bl ock will t hrow if th e server t akes more | |
| 766 | // tha n `timeout ` to write the HTTP status and headers ( correspond ing to | |
| 767 | // the on('respo nse') even t on the c lient). NB : this mea sures wall -clock | |
| 768 | // tim e, not the time betw een bytes sent by th e server. | |
| 769 | self.t imeoutTime r = setTim eout(funct ion () { | |
| 770 | var connectTim eout = sel f.req.sock et && self .req.socke t.readable === false | |
| 771 | self .abort() | |
| 772 | var e = new Er ror('ETIME DOUT') | |
| 773 | e.co de = 'ETIM EDOUT' | |
| 774 | e.co nnect = co nnectTimeo ut | |
| 775 | self .emit('err or', e) | |
| 776 | }, tim eout) | |
| 777 | ||
| 778 | if (se lf.req.set Timeout) { // only w orks on no de 0.6+ | |
| 779 | // S et an addi tional tim eout on th e socket, via the `s etsockopt` syscall. | |
| 780 | // T his timeou t sets the amount of time to w ait *betwe en* bytes sent | |
| 781 | // f rom the se rver, and may or may not corre spond to t he wall-cl ock time | |
| 782 | // e lapsed fro m the star t of the r equest. | |
| 783 | // | |
| 784 | // I n particul ar, it's u seful for erroring i f the serv er fails t o send | |
| 785 | // d ata halfwa y through streaming a response . | |
| 786 | self .req.setTi meout(time out, funct ion () { | |
| 787 | if (self.req ) { | |
| 788 | self.req.a bort() | |
| 789 | var e = ne w Error('E SOCKETTIME DOUT') | |
| 790 | e.code = ' ESOCKETTIM EDOUT' | |
| 791 | e.connect = false | |
| 792 | self.emit( 'error', e ) | |
| 793 | } | |
| 794 | }) | |
| 795 | } | |
| 796 | } | |
| 797 | ||
| 798 | self.req .on('respo nse', self .onRequest Response.b ind(self)) | |
| 799 | self.req .on('error ', self.on RequestErr or.bind(se lf)) | |
| 800 | self.req .on('drain ', functio n() { | |
| 801 | self.e mit('drain ') | |
| 802 | }) | |
| 803 | self.req .on('socke t', functi on(socket) { | |
| 804 | self.e mit('socke t', socket ) | |
| 805 | }) | |
| 806 | ||
| 807 | self.on( 'end', fun ction() { | |
| 808 | if ( s elf.req.co nnection ) { | |
| 809 | self .req.conne ction.remo veListener ('error', connection ErrorHandl er) | |
| 810 | } | |
| 811 | }) | |
| 812 | self.emi t('request ', self.re q) | |
| 813 | } | |
| 814 | ||
| 815 | Request.pr ototype.on RequestErr or = funct ion (error ) { | |
| 816 | var self = this | |
| 817 | if (self ._aborted) { | |
| 818 | return | |
| 819 | } | |
| 820 | if (self .req && se lf.req._re usedSocket && error. code === ' ECONNRESET ' | |
| 821 | && s elf.agent. addRequest Noreuse) { | |
| 822 | self.a gent = { a ddRequest: self.agen t.addReque stNoreuse. bind(self. agent) } | |
| 823 | self.s tart() | |
| 824 | self.r eq.end() | |
| 825 | return | |
| 826 | } | |
| 827 | if (self .timeout & & self.tim eoutTimer) { | |
| 828 | clearT imeout(sel f.timeoutT imer) | |
| 829 | self.t imeoutTime r = null | |
| 830 | } | |
| 831 | self.emi t('error', error) | |
| 832 | } | |
| 833 | ||
| 834 | Request.pr ototype.on RequestRes ponse = fu nction (re sponse) { | |
| 835 | var self = this | |
| 836 | debug('o nRequestRe sponse', s elf.uri.hr ef, respon se.statusC ode, respo nse.header s) | |
| 837 | response .on('end', function( ) { | |
| 838 | if (se lf.timing) { | |
| 839 | self .elapsedTi me += (new Date().ge tTime() - self.start Time) | |
| 840 | debu g('elapsed time', se lf.elapsed Time) | |
| 841 | resp onse.elaps edTime = s elf.elapse dTime | |
| 842 | } | |
| 843 | debug( 'response end', self .uri.href, response. statusCode , response .headers) | |
| 844 | }) | |
| 845 | ||
| 846 | // The c heck on re sponse.con nection is a workaro und for br owserify. | |
| 847 | if (resp onse.conne ction && r esponse.co nnection.l isteners(' error').in dexOf(conn ectionErro rHandler) === -1) { | |
| 848 | respon se.connect ion.setMax Listeners( 0) | |
| 849 | respon se.connect ion.once(' error', co nnectionEr rorHandler ) | |
| 850 | } | |
| 851 | if (self ._aborted) { | |
| 852 | debug( 'aborted', self.uri. href) | |
| 853 | respon se.resume( ) | |
| 854 | return | |
| 855 | } | |
| 856 | ||
| 857 | self.res ponse = re sponse | |
| 858 | response .request = self | |
| 859 | response .toJSON = responseTo JSON | |
| 860 | ||
| 861 | // XXX T his is dif ferent on 0.10, beca use SSL is strict by default | |
| 862 | if (self .httpModul e === http s && | |
| 863 | self .strictSSL && (!resp onse.hasOw nProperty( 'socket') || | |
| 864 | !res ponse.sock et.authori zed)) { | |
| 865 | debug( 'strict ss l error', self.uri.h ref) | |
| 866 | var ss lErr = res ponse.hasO wnProperty ('socket') ? respons e.socket.a uthorizati onError : self.uri.h ref + ' do es not sup port SSL' | |
| 867 | self.e mit('error ', new Err or('SSL Er ror: ' + s slErr)) | |
| 868 | return | |
| 869 | } | |
| 870 | ||
| 871 | // Save the origin al host be fore any r edirect (i f it chang es, we nee d to | |
| 872 | // remov e any auth orization headers). Also reme mber the c ase of the header | |
| 873 | // name because lo ts of brok en servers expect Ho st instead of host a nd we | |
| 874 | // want the caller to be abl e to speci fy this. | |
| 875 | self.ori ginalHost = self.get Header('ho st') | |
| 876 | if (!sel f.original HostHeader Name) { | |
| 877 | self.o riginalHos tHeaderNam e = self.h asHeader(' host') | |
| 878 | } | |
| 879 | if (self .setHost) { | |
| 880 | self.r emoveHeade r('host') | |
| 881 | } | |
| 882 | if (self .timeout & & self.tim eoutTimer) { | |
| 883 | clearT imeout(sel f.timeoutT imer) | |
| 884 | self.t imeoutTime r = null | |
| 885 | } | |
| 886 | ||
| 887 | var targ etCookieJa r = (self. _jar && se lf._jar.se tCookie) ? self._jar : globalC ookieJar | |
| 888 | var addC ookie = fu nction (co okie) { | |
| 889 | //set the cookie if it's d omain in t he href's domain. | |
| 890 | try { | |
| 891 | targ etCookieJa r.setCooki e(cookie, self.uri.h ref, {igno reError: t rue}) | |
| 892 | } catc h (e) { | |
| 893 | self .emit('err or', e) | |
| 894 | } | |
| 895 | } | |
| 896 | ||
| 897 | response .caseless = caseless (response. headers) | |
| 898 | ||
| 899 | if (resp onse.casel ess.has('s et-cookie' ) && (!sel f._disable Cookies)) { | |
| 900 | var he aderName = response. caseless.h as('set-co okie') | |
| 901 | if (Ar ray.isArra y(response .headers[h eaderName] )) { | |
| 902 | resp onse.heade rs[headerN ame].forEa ch(addCook ie) | |
| 903 | } else { | |
| 904 | addC ookie(resp onse.heade rs[headerN ame]) | |
| 905 | } | |
| 906 | } | |
| 907 | ||
| 908 | if (self ._redirect .onRespons e(response )) { | |
| 909 | return // Ignore the rest of the res ponse | |
| 910 | } else { | |
| 911 | // Be a good str eam and em it end whe n the resp onse is fi nished. | |
| 912 | // Hac k to emit end on clo se because of a core bug that never fire s end | |
| 913 | respon se.on('clo se', funct ion () { | |
| 914 | if ( !self._end ed) { | |
| 915 | se lf.respons e.emit('en d') | |
| 916 | } | |
| 917 | }) | |
| 918 | ||
| 919 | respon se.on('end ', functio n () { | |
| 920 | self ._ended = true | |
| 921 | }) | |
| 922 | ||
| 923 | var re sponseCont ent | |
| 924 | if (se lf.gzip) { | |
| 925 | var contentEnc oding = re sponse.hea ders['cont ent-encodi ng'] || 'i dentity' | |
| 926 | cont entEncodin g = conten tEncoding. trim().toL owerCase() | |
| 927 | ||
| 928 | if ( contentEnc oding === 'gzip') { | |
| 929 | re sponseCont ent = zlib .createGun zip() | |
| 930 | re sponse.pip e(response Content) | |
| 931 | } el se { | |
| 932 | // Since pre vious vers ions didn' t check fo r Content- Encoding h eader, | |
| 933 | // ignore an y invalid values to preserve b ackwards-c ompatibili ty | |
| 934 | if (contentE ncoding != = 'identit y') { | |
| 935 | debug('ign oring unre cognized C ontent-Enc oding ' + contentEnc oding) | |
| 936 | } | |
| 937 | re sponseCont ent = resp onse | |
| 938 | } | |
| 939 | } else { | |
| 940 | resp onseConten t = respon se | |
| 941 | } | |
| 942 | ||
| 943 | if (se lf.encodin g) { | |
| 944 | if ( self.dests .length != = 0) { | |
| 945 | co nsole.erro r('Ignorin g encoding parameter as this s tream is b eing piped to anothe r stream w hich makes the encod ing option invalid.' ) | |
| 946 | } el se if (res ponseConte nt.setEnco ding) { | |
| 947 | re sponseCont ent.setEnc oding(self .encoding) | |
| 948 | } el se { | |
| 949 | // Should on ly occur o n node pre -v0.9.4 (j oyent/node @9b5abe5) with | |
| 950 | // zlib stre ams. | |
| 951 | // If/When s upport for 0.9.4 is dropped, t his should be unnece ssary. | |
| 952 | re sponseCont ent = resp onseConten t.pipe(str ingstream( self.encod ing)) | |
| 953 | } | |
| 954 | } | |
| 955 | ||
| 956 | if (se lf._paused ) { | |
| 957 | resp onseConten t.pause() | |
| 958 | } | |
| 959 | ||
| 960 | self.r esponseCon tent = res ponseConte nt | |
| 961 | ||
| 962 | self.e mit('respo nse', resp onse) | |
| 963 | ||
| 964 | self.d ests.forEa ch(functio n (dest) { | |
| 965 | self .pipeDest( dest) | |
| 966 | }) | |
| 967 | ||
| 968 | respon seContent. on('data', function (chunk) { | |
| 969 | self ._destdata = true | |
| 970 | self .emit('dat a', chunk) | |
| 971 | }) | |
| 972 | respon seContent. on('end', function ( chunk) { | |
| 973 | self .emit('end ', chunk) | |
| 974 | }) | |
| 975 | respon seContent. on('error' , function (error) { | |
| 976 | self .emit('err or', error ) | |
| 977 | }) | |
| 978 | respon seContent. on('close' , function () {self. emit('clos e')}) | |
| 979 | ||
| 980 | if (se lf.callbac k) { | |
| 981 | self .readRespo nseBody(re sponse) | |
| 982 | } | |
| 983 | //if n o callback | |
| 984 | else { | |
| 985 | self .on('end', function () { | |
| 986 | if (self._ab orted) { | |
| 987 | debug('abo rted', sel f.uri.href ) | |
| 988 | return | |
| 989 | } | |
| 990 | se lf.emit('c omplete', response) | |
| 991 | }) | |
| 992 | } | |
| 993 | } | |
| 994 | debug('f inish init function' , self.uri .href) | |
| 995 | } | |
| 996 | ||
| 997 | Request.pr ototype.re adResponse Body = fun ction (res ponse) { | |
| 998 | var self = this | |
| 999 | debug('r eading res ponse\'s b ody') | |
| 1000 | var buff er = bl() | |
| 1001 | , stri ngs = [] | |
| 1002 | ||
| 1003 | self.on( 'data', fu nction (ch unk) { | |
| 1004 | if (Bu ffer.isBuf fer(chunk) ) { | |
| 1005 | buff er.append( chunk) | |
| 1006 | } else { | |
| 1007 | stri ngs.push(c hunk) | |
| 1008 | } | |
| 1009 | }) | |
| 1010 | self.on( 'end', fun ction () { | |
| 1011 | debug( 'end event ', self.ur i.href) | |
| 1012 | if (se lf._aborte d) { | |
| 1013 | debu g('aborted ', self.ur i.href) | |
| 1014 | retu rn | |
| 1015 | } | |
| 1016 | ||
| 1017 | if (bu ffer.lengt h) { | |
| 1018 | debu g('has bod y', self.u ri.href, b uffer.leng th) | |
| 1019 | if ( self.encod ing === nu ll) { | |
| 1020 | // response. body = buf fer | |
| 1021 | // can't mov e to this until http s://github .com/rvagg /bl/issues /13 | |
| 1022 | re sponse.bod y = buffer .slice() | |
| 1023 | } el se { | |
| 1024 | re sponse.bod y = buffer .toString( self.encod ing) | |
| 1025 | } | |
| 1026 | } else if (strin gs.length) { | |
| 1027 | // T he UTF8 BO M [0xEF,0x BB,0xBF] i s converte d to [0xFE ,0xFF] in the JS UTC 16/UCS2 re presentati on. | |
| 1028 | // S trip this value out when the e ncoding is set to 'u tf8', as u pstream co nsumers wo n't expect it and it breaks JS ON.parse() . | |
| 1029 | if ( self.encod ing === 'u tf8' && st rings[0].l ength > 0 && strings [0][0] === '\uFEFF') { | |
| 1030 | st rings[0] = strings[0 ].substrin g(1) | |
| 1031 | } | |
| 1032 | resp onse.body = strings. join('') | |
| 1033 | } | |
| 1034 | ||
| 1035 | if (se lf._json) { | |
| 1036 | try { | |
| 1037 | re sponse.bod y = JSON.p arse(respo nse.body, self._json Reviver) | |
| 1038 | } ca tch (e) { | |
| 1039 | de bug('inval id JSON re ceived', s elf.uri.hr ef) | |
| 1040 | } | |
| 1041 | } | |
| 1042 | debug( 'emitting complete', self.uri. href) | |
| 1043 | if (ty peof respo nse.body = == 'undefi ned' && !s elf._json) { | |
| 1044 | resp onse.body = self.enc oding === null ? new Buffer(0) : '' | |
| 1045 | } | |
| 1046 | self.e mit('compl ete', resp onse, resp onse.body) | |
| 1047 | }) | |
| 1048 | } | |
| 1049 | ||
| 1050 | Request.pr ototype.ab ort = func tion () { | |
| 1051 | var self = this | |
| 1052 | self._ab orted = tr ue | |
| 1053 | ||
| 1054 | if (self .req) { | |
| 1055 | self.r eq.abort() | |
| 1056 | } | |
| 1057 | else if (self.resp onse) { | |
| 1058 | self.r esponse.de stroy() | |
| 1059 | } | |
| 1060 | ||
| 1061 | self.emi t('abort') | |
| 1062 | } | |
| 1063 | ||
| 1064 | Request.pr ototype.pi peDest = f unction (d est) { | |
| 1065 | var self = this | |
| 1066 | var resp onse = sel f.response | |
| 1067 | // Calle d after th e response is receiv ed | |
| 1068 | if (dest .headers & & !dest.he adersSent) { | |
| 1069 | if (re sponse.cas eless.has( 'content-t ype')) { | |
| 1070 | var ctname = r esponse.ca seless.has ('content- type') | |
| 1071 | if ( dest.setHe ader) { | |
| 1072 | de st.setHead er(ctname, response. headers[ct name]) | |
| 1073 | } | |
| 1074 | else { | |
| 1075 | de st.headers [ctname] = response. headers[ct name] | |
| 1076 | } | |
| 1077 | } | |
| 1078 | ||
| 1079 | if (re sponse.cas eless.has( 'content-l ength')) { | |
| 1080 | var clname = r esponse.ca seless.has ('content- length') | |
| 1081 | if ( dest.setHe ader) { | |
| 1082 | de st.setHead er(clname, response. headers[cl name]) | |
| 1083 | } el se { | |
| 1084 | de st.headers [clname] = response. headers[cl name] | |
| 1085 | } | |
| 1086 | } | |
| 1087 | } | |
| 1088 | if (dest .setHeader && !dest. headersSen t) { | |
| 1089 | for (v ar i in re sponse.hea ders) { | |
| 1090 | // I f the resp onse conte nt is bein g decoded, the Conte nt-Encodin g header | |
| 1091 | // o f the resp onse doesn 't represe nt the pip ed content , so don't pass it. | |
| 1092 | if ( !self.gzip || i !== 'content-e ncoding') { | |
| 1093 | de st.setHead er(i, resp onse.heade rs[i]) | |
| 1094 | } | |
| 1095 | } | |
| 1096 | dest.s tatusCode = response .statusCod e | |
| 1097 | } | |
| 1098 | if (self .pipefilte r) { | |
| 1099 | self.p ipefilter( response, dest) | |
| 1100 | } | |
| 1101 | } | |
| 1102 | ||
| 1103 | Request.pr ototype.qs = functio n (q, clob ber) { | |
| 1104 | var self = this | |
| 1105 | var base | |
| 1106 | if (!clo bber && se lf.uri.que ry) { | |
| 1107 | base = self._qs. parse(self .uri.query ) | |
| 1108 | } else { | |
| 1109 | base = {} | |
| 1110 | } | |
| 1111 | ||
| 1112 | for (var i in q) { | |
| 1113 | base[i ] = q[i] | |
| 1114 | } | |
| 1115 | ||
| 1116 | var qs = self._qs. stringify( base) | |
| 1117 | ||
| 1118 | if (qs = == '') { | |
| 1119 | return self | |
| 1120 | } | |
| 1121 | ||
| 1122 | self.uri = url.par se(self.ur i.href.spl it('?')[0] + '?' + q s) | |
| 1123 | self.url = self.ur i | |
| 1124 | self.pat h = self.u ri.path | |
| 1125 | ||
| 1126 | if (self .uri.host === 'unix' ) { | |
| 1127 | self.e nableUnixS ocket() | |
| 1128 | } | |
| 1129 | ||
| 1130 | return s elf | |
| 1131 | } | |
| 1132 | Request.pr ototype.fo rm = funct ion (form) { | |
| 1133 | var self = this | |
| 1134 | if (form ) { | |
| 1135 | if (!/ ^applicati on\/x-www- form-urlen coded\b/.t est(self.g etHeader(' content-ty pe'))) { | |
| 1136 | self .setHeader ('content- type', 'ap plication/ x-www-form -urlencode d') | |
| 1137 | } | |
| 1138 | self.b ody = (typ eof form = == 'string ') | |
| 1139 | ? se lf._qs.rfc 3986(form. toString(' utf8')) | |
| 1140 | : se lf._qs.str ingify(for m).toStrin g('utf8') | |
| 1141 | return self | |
| 1142 | } | |
| 1143 | // creat e form-dat a object | |
| 1144 | self._fo rm = new F ormData() | |
| 1145 | self._fo rm.on('err or', funct ion(err) { | |
| 1146 | err.me ssage = 'f orm-data: ' + err.me ssage | |
| 1147 | self.e mit('error ', err) | |
| 1148 | self.a bort() | |
| 1149 | }) | |
| 1150 | return s elf._form | |
| 1151 | } | |
| 1152 | Request.pr ototype.mu ltipart = function ( multipart) { | |
| 1153 | var self = this | |
| 1154 | ||
| 1155 | self._mu ltipart.on Request(mu ltipart) | |
| 1156 | ||
| 1157 | if (!sel f._multipa rt.chunked ) { | |
| 1158 | self.b ody = self ._multipar t.body | |
| 1159 | } | |
| 1160 | ||
| 1161 | return s elf | |
| 1162 | } | |
| 1163 | Request.pr ototype.js on = funct ion (val) { | |
| 1164 | var self = this | |
| 1165 | ||
| 1166 | if (!sel f.hasHeade r('accept' )) { | |
| 1167 | self.s etHeader(' accept', ' applicatio n/json') | |
| 1168 | } | |
| 1169 | ||
| 1170 | if (type of self.js onReplacer === 'func tion') { | |
| 1171 | self._ jsonReplac er = self. jsonReplac er | |
| 1172 | } | |
| 1173 | ||
| 1174 | self._js on = true | |
| 1175 | if (type of val === 'boolean' ) { | |
| 1176 | if (se lf.body != = undefine d) { | |
| 1177 | if ( !/^applica tion\/x-ww w-form-url encoded\b/ .test(self .getHeader ('content- type'))) { | |
| 1178 | se lf.body = safeString ify(self.b ody, self. _jsonRepla cer) | |
| 1179 | } el se { | |
| 1180 | se lf.body = self._qs.r fc3986(sel f.body) | |
| 1181 | } | |
| 1182 | if ( !self.hasH eader('con tent-type' )) { | |
| 1183 | se lf.setHead er('conten t-type', ' applicatio n/json') | |
| 1184 | } | |
| 1185 | } | |
| 1186 | } else { | |
| 1187 | self.b ody = safe Stringify( val, self. _jsonRepla cer) | |
| 1188 | if (!s elf.hasHea der('conte nt-type')) { | |
| 1189 | self .setHeader ('content- type', 'ap plication/ json') | |
| 1190 | } | |
| 1191 | } | |
| 1192 | ||
| 1193 | if (type of self.js onReviver === 'funct ion') { | |
| 1194 | self._ jsonRevive r = self.j sonReviver | |
| 1195 | } | |
| 1196 | ||
| 1197 | return s elf | |
| 1198 | } | |
| 1199 | Request.pr ototype.ge tHeader = function ( name, head ers) { | |
| 1200 | var self = this | |
| 1201 | var resu lt, re, ma tch | |
| 1202 | if (!hea ders) { | |
| 1203 | header s = self.h eaders | |
| 1204 | } | |
| 1205 | Object.k eys(header s).forEach (function (key) { | |
| 1206 | if (ke y.length ! == name.le ngth) { | |
| 1207 | retu rn | |
| 1208 | } | |
| 1209 | re = n ew RegExp( name, 'i') | |
| 1210 | match = key.matc h(re) | |
| 1211 | if (ma tch) { | |
| 1212 | resu lt = heade rs[key] | |
| 1213 | } | |
| 1214 | }) | |
| 1215 | return r esult | |
| 1216 | } | |
| 1217 | Request.pr ototype.en ableUnixSo cket = fun ction () { | |
| 1218 | // Get t he socket & request paths from the URL | |
| 1219 | var unix Parts = th is.uri.pat h.split(': ') | |
| 1220 | , host = unixPar ts[0] | |
| 1221 | , path = unixPar ts[1] | |
| 1222 | // Apply unix prop erties to request | |
| 1223 | this.soc ketPath = host | |
| 1224 | this.uri .pathname = path | |
| 1225 | this.uri .path = pa th | |
| 1226 | this.uri .host = ho st | |
| 1227 | this.uri .hostname = host | |
| 1228 | this.uri .isUnix = true | |
| 1229 | } | |
| 1230 | ||
| 1231 | ||
| 1232 | Request.pr ototype.au th = funct ion (user, pass, sen dImmediate ly, bearer ) { | |
| 1233 | var self = this | |
| 1234 | ||
| 1235 | self._au th.onReque st(user, p ass, sendI mmediately , bearer) | |
| 1236 | ||
| 1237 | return s elf | |
| 1238 | } | |
| 1239 | Request.pr ototype.aw s = functi on (opts, now) { | |
| 1240 | var self = this | |
| 1241 | ||
| 1242 | if (!now ) { | |
| 1243 | self._ aws = opts | |
| 1244 | return self | |
| 1245 | } | |
| 1246 | ||
| 1247 | if (opts .sign_vers ion == 4 | | opts.sig n_version == '4') { | |
| 1248 | var aw s4 = requi re('aws4') | |
| 1249 | // use aws4 | |
| 1250 | var op tions = { | |
| 1251 | host : self.uri .host, | |
| 1252 | path : self.uri .path, | |
| 1253 | meth od: self.m ethod, | |
| 1254 | head ers: { | |
| 1255 | 'c ontent-typ e': self.g etHeader(' content-ty pe') || '' | |
| 1256 | }, | |
| 1257 | body : self.bod y | |
| 1258 | } | |
| 1259 | var si gnRes = aw s4.sign(op tions, { | |
| 1260 | acce ssKeyId: o pts.key, | |
| 1261 | secr etAccessKe y: opts.se cret | |
| 1262 | }) | |
| 1263 | self.s etHeader(' authorizat ion', sign Res.header s.Authoriz ation) | |
| 1264 | self.s etHeader(' x-amz-date ', signRes .headers[' X-Amz-Date ']) | |
| 1265 | } | |
| 1266 | else { | |
| 1267 | // def ault: use aws-sign2 | |
| 1268 | var da te = new D ate() | |
| 1269 | self.s etHeader(' date', dat e.toUTCStr ing()) | |
| 1270 | var au th = | |
| 1271 | { ke y: opts.ke y | |
| 1272 | , se cret: opts .secret | |
| 1273 | , ve rb: self.m ethod.toUp perCase() | |
| 1274 | , da te: date | |
| 1275 | , co ntentType: self.getH eader('con tent-type' ) || '' | |
| 1276 | , md 5: self.ge tHeader('c ontent-md5 ') || '' | |
| 1277 | , am azonHeader s: aws2.ca nonicalize Headers(se lf.headers ) | |
| 1278 | } | |
| 1279 | var pa th = self. uri.path | |
| 1280 | if (op ts.bucket && path) { | |
| 1281 | auth .resource = '/' + op ts.bucket + path | |
| 1282 | } else if (opts. bucket && !path) { | |
| 1283 | auth .resource = '/' + op ts.bucket | |
| 1284 | } else if (!opts .bucket && path) { | |
| 1285 | auth .resource = path | |
| 1286 | } else if (!opts .bucket && !path) { | |
| 1287 | auth .resource = '/' | |
| 1288 | } | |
| 1289 | auth.r esource = aws2.canon icalizeRes ource(auth .resource) | |
| 1290 | self.s etHeader(' authorizat ion', aws2 .authoriza tion(auth) ) | |
| 1291 | } | |
| 1292 | ||
| 1293 | return s elf | |
| 1294 | } | |
| 1295 | Request.pr ototype.ht tpSignatur e = functi on (opts) { | |
| 1296 | var self = this | |
| 1297 | httpSign ature.sign Request({ | |
| 1298 | getHea der: funct ion(header ) { | |
| 1299 | retu rn self.ge tHeader(he ader, self .headers) | |
| 1300 | }, | |
| 1301 | setHea der: funct ion(header , value) { | |
| 1302 | self .setHeader (header, v alue) | |
| 1303 | }, | |
| 1304 | method : self.met hod, | |
| 1305 | path: self.path | |
| 1306 | }, opts) | |
| 1307 | debug('h ttpSignatu re authori zation', s elf.getHea der('autho rization') ) | |
| 1308 | ||
| 1309 | return s elf | |
| 1310 | } | |
| 1311 | Request.pr ototype.ha wk = funct ion (opts) { | |
| 1312 | var self = this | |
| 1313 | self.set Header('Au thorizatio n', hawk.c lient.head er(self.ur i, self.me thod, opts ).field) | |
| 1314 | } | |
| 1315 | Request.pr ototype.oa uth = func tion (_oau th) { | |
| 1316 | var self = this | |
| 1317 | ||
| 1318 | self._oa uth.onRequ est(_oauth ) | |
| 1319 | ||
| 1320 | return s elf | |
| 1321 | } | |
| 1322 | ||
| 1323 | Request.pr ototype.ja r = functi on (jar) { | |
| 1324 | var self = this | |
| 1325 | var cook ies | |
| 1326 | ||
| 1327 | if (self ._redirect .redirects Followed = == 0) { | |
| 1328 | self.o riginalCoo kieHeader = self.get Header('co okie') | |
| 1329 | } | |
| 1330 | ||
| 1331 | if (!jar ) { | |
| 1332 | // dis able cooki es | |
| 1333 | cookie s = false | |
| 1334 | self._ disableCoo kies = tru e | |
| 1335 | } else { | |
| 1336 | var ta rgetCookie Jar = (jar && jar.ge tCookieStr ing) ? jar : globalC ookieJar | |
| 1337 | var ur ihref = se lf.uri.hre f | |
| 1338 | //fetc h cookie i n the Spec ified host | |
| 1339 | if (ta rgetCookie Jar) { | |
| 1340 | cook ies = targ etCookieJa r.getCooki eString(ur ihref) | |
| 1341 | } | |
| 1342 | } | |
| 1343 | ||
| 1344 | //if nee d cookie a nd cookie is not emp ty | |
| 1345 | if (cook ies && coo kies.lengt h) { | |
| 1346 | if (se lf.origina lCookieHea der) { | |
| 1347 | // D on't overw rite exist ing Cookie header | |
| 1348 | self .setHeader ('cookie', self.orig inalCookie Header + ' ; ' + cook ies) | |
| 1349 | } else { | |
| 1350 | self .setHeader ('cookie', cookies) | |
| 1351 | } | |
| 1352 | } | |
| 1353 | self._ja r = jar | |
| 1354 | return s elf | |
| 1355 | } | |
| 1356 | ||
| 1357 | ||
| 1358 | // Stream API | |
| 1359 | Request.pr ototype.pi pe = funct ion (dest, opts) { | |
| 1360 | var self = this | |
| 1361 | ||
| 1362 | if (self .response) { | |
| 1363 | if (se lf._destda ta) { | |
| 1364 | self .emit('err or', new E rror('You cannot pip e after da ta has bee n emitted from the r esponse.') ) | |
| 1365 | } else if (self. _ended) { | |
| 1366 | self .emit('err or', new E rror('You cannot pip e after th e response has been ended.')) | |
| 1367 | } else { | |
| 1368 | stre am.Stream. prototype. pipe.call( self, dest , opts) | |
| 1369 | self .pipeDest( dest) | |
| 1370 | retu rn dest | |
| 1371 | } | |
| 1372 | } else { | |
| 1373 | self.d ests.push( dest) | |
| 1374 | stream .Stream.pr ototype.pi pe.call(se lf, dest, opts) | |
| 1375 | return dest | |
| 1376 | } | |
| 1377 | } | |
| 1378 | Request.pr ototype.wr ite = func tion () { | |
| 1379 | var self = this | |
| 1380 | if (self ._aborted) {return} | |
| 1381 | ||
| 1382 | if (!sel f._started ) { | |
| 1383 | self.s tart() | |
| 1384 | } | |
| 1385 | return s elf.req.wr ite.apply( self.req, arguments) | |
| 1386 | } | |
| 1387 | Request.pr ototype.en d = functi on (chunk) { | |
| 1388 | var self = this | |
| 1389 | if (self ._aborted) {return} | |
| 1390 | ||
| 1391 | if (chun k) { | |
| 1392 | self.w rite(chunk ) | |
| 1393 | } | |
| 1394 | if (!sel f._started ) { | |
| 1395 | self.s tart() | |
| 1396 | } | |
| 1397 | if (self .req) { | |
| 1398 | self.r eq.end() | |
| 1399 | } | |
| 1400 | } | |
| 1401 | Request.pr ototype.pa use = func tion () { | |
| 1402 | var self = this | |
| 1403 | if (!sel f.response Content) { | |
| 1404 | self._ paused = t rue | |
| 1405 | } else { | |
| 1406 | self.r esponseCon tent.pause .apply(sel f.response Content, a rguments) | |
| 1407 | } | |
| 1408 | } | |
| 1409 | Request.pr ototype.re sume = fun ction () { | |
| 1410 | var self = this | |
| 1411 | if (!sel f.response Content) { | |
| 1412 | self._ paused = f alse | |
| 1413 | } else { | |
| 1414 | self.r esponseCon tent.resum e.apply(se lf.respons eContent, arguments) | |
| 1415 | } | |
| 1416 | } | |
| 1417 | Request.pr ototype.de stroy = fu nction () { | |
| 1418 | var self = this | |
| 1419 | if (!sel f._ended) { | |
| 1420 | self.e nd() | |
| 1421 | } else i f (self.re sponse) { | |
| 1422 | self.r esponse.de stroy() | |
| 1423 | } | |
| 1424 | } | |
| 1425 | ||
| 1426 | Request.de faultProxy HeaderWhit eList = | |
| 1427 | Tunnel.d efaultProx yHeaderWhi teList.sli ce() | |
| 1428 | ||
| 1429 | Request.de faultProxy HeaderExcl usiveList = | |
| 1430 | Tunnel.d efaultProx yHeaderExc lusiveList .slice() | |
| 1431 | ||
| 1432 | // Exports | |
| 1433 | ||
| 1434 | Request.pr ototype.to JSON = req uestToJSON | |
| 1435 | module.exp orts = Req uest |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.