Fix the security issue.
[aai/esr-server.git] / esr-mgr / src / main / resources / api-doc / lib / shred.bundle.js
1 /*
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 var require = function (file, cwd) {
17     var resolved = require.resolve(file, cwd || '/');
18     var mod = require.modules[resolved];
19     if (!mod) throw new Error(
20         'Failed to resolve module ' + file + ', tried ' + resolved
21     );
22     var res = mod._cached ? mod._cached : mod();
23     return res;
24 }
25
26 require.paths = [];
27 require.modules = {};
28 require.extensions = [".js",".coffee"];
29
30 require._core = {
31     'assert': true,
32     'events': true,
33     'fs': true,
34     'path': true,
35     'vm': true
36 };
37
38 require.resolve = (function () {
39     return function (x, cwd) {
40         if (!cwd) cwd = '/';
41         
42         if (require._core[x]) return x;
43         var path = require.modules.path();
44         var y = cwd || '.';
45         
46         if (x.match(/^(?:\.\.?\/|\/)/)) {
47             var m = loadAsFileSync(path.resolve(y, x))
48                 || loadAsDirectorySync(path.resolve(y, x));
49             if (m) return m;
50         }
51         
52         var n = loadNodeModulesSync(x, y);
53         if (n) return n;
54         
55         throw new Error("Cannot find module '" + x + "'");
56         
57         function loadAsFileSync (x) {
58             if (require.modules[x]) {
59                 return x;
60             }
61             
62             for (var i = 0; i < require.extensions.length; i++) {
63                 var ext = require.extensions[i];
64                 if (require.modules[x + ext]) return x + ext;
65             }
66         }
67         
68         function loadAsDirectorySync (x) {
69             x = x.replace(/\/+$/, '');
70             var pkgfile = x + '/package.json';
71             if (require.modules[pkgfile]) {
72                 var pkg = require.modules[pkgfile]();
73                 var b = pkg.browserify;
74                 if (typeof b === 'object' && b.main) {
75                     var m = loadAsFileSync(path.resolve(x, b.main));
76                     if (m) return m;
77                 }
78                 else if (typeof b === 'string') {
79                     var m = loadAsFileSync(path.resolve(x, b));
80                     if (m) return m;
81                 }
82                 else if (pkg.main) {
83                     var m = loadAsFileSync(path.resolve(x, pkg.main));
84                     if (m) return m;
85                 }
86             }
87             
88             return loadAsFileSync(x + '/index');
89         }
90         
91         function loadNodeModulesSync (x, start) {
92             var dirs = nodeModulesPathsSync(start);
93             for (var i = 0; i < dirs.length; i++) {
94                 var dir = dirs[i];
95                 var m = loadAsFileSync(dir + '/' + x);
96                 if (m) return m;
97                 var n = loadAsDirectorySync(dir + '/' + x);
98                 if (n) return n;
99             }
100             
101             var m = loadAsFileSync(x);
102             if (m) return m;
103         }
104         
105         function nodeModulesPathsSync (start) {
106             var parts;
107             if (start === '/') parts = [ '' ];
108             else parts = path.normalize(start).split('/');
109             
110             var dirs = [];
111             for (var i = parts.length - 1; i >= 0; i--) {
112                 if (parts[i] === 'node_modules') continue;
113                 var dir = parts.slice(0, i + 1).join('/') + '/node_modules';
114                 dirs.push(dir);
115             }
116             
117             return dirs;
118         }
119     };
120 })();
121
122 require.alias = function (from, to) {
123     var path = require.modules.path();
124     var res = null;
125     try {
126         res = require.resolve(from + '/package.json', '/');
127     }
128     catch (err) {
129         res = require.resolve(from, '/');
130     }
131     var basedir = path.dirname(res);
132     
133     var keys = (Object.keys || function (obj) {
134         var res = [];
135         for (var key in obj) res.push(key)
136         return res;
137     })(require.modules);
138     
139     for (var i = 0; i < keys.length; i++) {
140         var key = keys[i];
141         if (key.slice(0, basedir.length + 1) === basedir + '/') {
142             var f = key.slice(basedir.length);
143             require.modules[to + f] = require.modules[basedir + f];
144         }
145         else if (key === basedir) {
146             require.modules[to] = require.modules[basedir];
147         }
148     }
149 };
150
151 require.define = function (filename, fn) {
152     var dirname = require._core[filename]
153         ? ''
154         : require.modules.path().dirname(filename)
155     ;
156     
157     var require_ = function (file) {
158         return require(file, dirname)
159     };
160     require_.resolve = function (name) {
161         return require.resolve(name, dirname);
162     };
163     require_.modules = require.modules;
164     require_.define = require.define;
165     var module_ = { exports : {} };
166     
167     require.modules[filename] = function () {
168         require.modules[filename]._cached = module_.exports;
169         fn.call(
170             module_.exports,
171             require_,
172             module_,
173             module_.exports,
174             dirname,
175             filename
176         );
177         require.modules[filename]._cached = module_.exports;
178         return module_.exports;
179     };
180 };
181
182 if (typeof process === 'undefined') process = {};
183
184 if (!process.nextTick) process.nextTick = (function () {
185     var queue = [];
186     var canPost = typeof window !== 'undefined'
187         && window.postMessage && window.addEventListener
188     ;
189     
190     if (canPost) {
191         window.addEventListener('message', function (ev) {
192             if (ev.source === window && ev.data === 'browserify-tick') {
193                 ev.stopPropagation();
194                 if (queue.length > 0) {
195                     var fn = queue.shift();
196                     fn();
197                 }
198             }
199         }, true);
200     }
201     
202     return function (fn) {
203         if (canPost) {
204             queue.push(fn);
205             window.postMessage('browserify-tick', '*');
206         }
207         else setTimeout(fn, 0);
208     };
209 })();
210
211 if (!process.title) process.title = 'browser';
212
213 if (!process.binding) process.binding = function (name) {
214     if (name === 'evals') return require('vm')
215     else throw new Error('No such module')
216 };
217
218 if (!process.cwd) process.cwd = function () { return '.' };
219
220 require.define("path", function (require, module, exports, __dirname, __filename) {
221     function filter (xs, fn) {
222     var res = [];
223     for (var i = 0; i < xs.length; i++) {
224         if (fn(xs[i], i, xs)) res.push(xs[i]);
225     }
226     return res;
227 }
228
229 // resolves . and .. elements in a path array with directory names there
230 // must be no slashes, empty elements, or device names (c:\) in the array
231 // (so also no leading and trailing slashes - it does not distinguish
232 // relative and absolute paths)
233 function normalizeArray(parts, allowAboveRoot) {
234   // if the path tries to go above the root, `up` ends up > 0
235   var up = 0;
236   for (var i = parts.length; i >= 0; i--) {
237     var last = parts[i];
238     if (last == '.') {
239       parts.splice(i, 1);
240     } else if (last === '..') {
241       parts.splice(i, 1);
242       up++;
243     } else if (up) {
244       parts.splice(i, 1);
245       up--;
246     }
247   }
248
249   // if the path is allowed to go above the root, restore leading ..s
250   if (allowAboveRoot) {
251     for (; up--; up) {
252       parts.unshift('..');
253     }
254   }
255
256   return parts;
257 }
258
259 // Regex to split a filename into [*, dir, basename, ext]
260 // posix version
261 var splitPathRe = /^(.+\/(?!$)|\/)?((?:.+?)?(\.[^.]*)?)$/;
262
263 // path.resolve([from ...], to)
264 // posix version
265 exports.resolve = function() {
266 var resolvedPath = '',
267     resolvedAbsolute = false;
268
269 for (var i = arguments.length; i >= -1 && !resolvedAbsolute; i--) {
270   var path = (i >= 0)
271       ? arguments[i]
272       : process.cwd();
273
274   // Skip empty and invalid entries
275   if (typeof path !== 'string' || !path) {
276     continue;
277   }
278
279   resolvedPath = path + '/' + resolvedPath;
280   resolvedAbsolute = path.charAt(0) === '/';
281 }
282
283 // At this point the path should be resolved to a full absolute path, but
284 // handle relative paths to be safe (might happen when process.cwd() fails)
285
286 // Normalize the path
287 resolvedPath = normalizeArray(filter(resolvedPath.split('/'), function(p) {
288     return !!p;
289   }), !resolvedAbsolute).join('/');
290
291   return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
292 };
293
294 // path.normalize(path)
295 // posix version
296 exports.normalize = function(path) {
297 var isAbsolute = path.charAt(0) === '/',
298     trailingSlash = path.slice(-1) === '/';
299
300 // Normalize the path
301 path = normalizeArray(filter(path.split('/'), function(p) {
302     return !!p;
303   }), !isAbsolute).join('/');
304
305   if (!path && !isAbsolute) {
306     path = '.';
307   }
308   if (path && trailingSlash) {
309     path += '/';
310   }
311   
312   return (isAbsolute ? '/' : '') + path;
313 };
314
315
316 // posix version
317 exports.join = function() {
318   var paths = Array.prototype.slice.call(arguments, 0);
319   return exports.normalize(filter(paths, function(p, index) {
320     return p && typeof p === 'string';
321   }).join('/'));
322 };
323
324
325 exports.dirname = function(path) {
326   var dir = splitPathRe.exec(path)[1] || '';
327   var isWindows = false;
328   if (!dir) {
329     // No dirname
330     return '.';
331   } else if (dir.length === 1 ||
332       (isWindows && dir.length <= 3 && dir.charAt(1) === ':')) {
333     // It is just a slash or a drive letter with a slash
334     return dir;
335   } else {
336     // It is a full dirname, strip trailing slash
337     return dir.substring(0, dir.length - 1);
338   }
339 };
340
341
342 exports.basename = function(path, ext) {
343   var f = splitPathRe.exec(path)[2] || '';
344   // TODO: make this comparison case-insensitive on windows?
345   if (ext && f.substr(-1 * ext.length) === ext) {
346     f = f.substr(0, f.length - ext.length);
347   }
348   return f;
349 };
350
351
352 exports.extname = function(path) {
353   return splitPathRe.exec(path)[3] || '';
354 };
355
356 });
357
358 require.define("/shred.js", function (require, module, exports, __dirname, __filename) {
359     // Shred is an HTTP client library intended to simplify the use of Node's
360 // built-in HTTP library. In particular, we wanted to make it easier to interact
361 // with HTTP-based APIs.
362 // 
363 // See the [examples](./examples.html) for more details.
364
365 // Ax is a nice logging library we wrote. You can use any logger, providing it
366 // has `info`, `warn`, `debug`, and `error` methods that take a string.
367 var Ax = require("ax")
368   , CookieJarLib = require( "cookiejar" )
369   , CookieJar = CookieJarLib.CookieJar
370 ;
371
372 // Shred takes some options, including a logger and request defaults.
373
374 var Shred = function(options) {
375   options = (options||{});
376   this.agent = options.agent;
377   this.defaults = options.defaults||{};
378   this.log = options.logger||(new Ax({ level: "info" }));
379   this._sharedCookieJar = new CookieJar();
380   this.logCurl = options.logCurl || false;
381 };
382
383 // Most of the real work is done in the request and reponse classes.
384  
385 Shred.Request = require("./shred/request");
386 Shred.Response = require("./shred/response");
387
388 // The `request` method kicks off a new request, instantiating a new `Request`
389 // object and passing along whatever default options we were given.
390
391 Shred.prototype = {
392   request: function(options) {
393     options.logger = this.log;
394     options.logCurl = options.logCurl || this.logCurl;
395     options.cookieJar = ( 'cookieJar' in options ) ? options.cookieJar : this._sharedCookieJar; // let them set cookieJar = null
396     options.agent = options.agent || this.agent;
397     // fill in default options
398     for (var key in this.defaults) {
399       if (this.defaults.hasOwnProperty(key) && !options[key]) {
400         options[key] = this.defaults[key]
401       }
402     }
403     return new Shred.Request(options);
404   }
405 };
406
407 // Define a bunch of convenience methods so that you don't have to include
408 // a `method` property in your request options.
409
410 "get put post delete".split(" ").forEach(function(method) {
411   Shred.prototype[method] = function(options) {
412     options.method = method;
413     return this.request(options);
414   };
415 });
416
417
418 module.exports = Shred;
419
420 });
421
422 require.define("/node_modules/ax/package.json", function (require, module, exports, __dirname, __filename) {
423     module.exports = {"main":"./lib/ax.js"}
424 });
425
426 require.define("/node_modules/ax/lib/ax.js", function (require, module, exports, __dirname, __filename) {
427     var inspect = require("util").inspect
428   , fs = require("fs")
429 ;
430
431
432 // this is a quick-and-dirty logger. there are other nicer loggers out there
433 // but the ones i found were also somewhat involved. this one has a Ruby
434 // logger type interface
435 //
436 // we can easily replace this, provide the info, debug, etc. methods are the
437 // same. or, we can change Haiku to use a more standard node.js interface
438
439 var format = function(level,message) {
440   var debug = (level=="debug"||level=="error");
441   if (!message) { return message.toString(); }
442   if (typeof(message) == "object") {
443     if (message instanceof Error && debug) {
444       return message.stack;
445     } else {
446       return inspect(message);
447     }
448   } else {
449     return message.toString();
450   }
451 };
452
453 var noOp = function(message) { return this; }
454 var makeLogger = function(level,fn) {
455   return function(message) { 
456     this.stream.write(this.format(level, message)+"\n");
457     return this;
458   }
459 };
460
461 var Logger = function(options) {
462   var logger = this;
463   var options = options||{};
464
465   // Default options
466   options.level = options.level || "info";
467   options.timestamp = options.timestamp || true;
468   options.prefix = options.prefix || "";
469   logger.options = options;
470
471   // Allows a prefix to be added to the message.
472   //
473   //    var logger = new Ax({ module: 'Haiku' })
474   //    logger.warn('this is going to be awesome!');
475   //    //=> Haiku: this is going to be awesome!
476   //
477   if (logger.options.module){
478     logger.options.prefix = logger.options.module;
479   }
480
481   // Write to stderr or a file
482   if (logger.options.file){
483     logger.stream = fs.createWriteStream(logger.options.file, {"flags": "a"});
484   } else {
485       if(process.title === "node")
486     logger.stream = process.stderr;
487       else if(process.title === "browser")
488     logger.stream = function () {
489       // Work around weird console context issue: http://code.google.com/p/chromium/issues/detail?id=48662
490       return console[logger.options.level].apply(console, arguments);
491     };
492   }
493
494   switch(logger.options.level){
495     case 'debug':
496       ['debug', 'info', 'warn'].forEach(function (level) {
497         logger[level] = Logger.writer(level);
498       });
499     case 'info':
500       ['info', 'warn'].forEach(function (level) {
501         logger[level] = Logger.writer(level);
502       });
503     case 'warn':
504       logger.warn = Logger.writer('warn');
505   }
506 }
507
508 // Used to define logger methods
509 Logger.writer = function(level){
510   return function(message){
511     var logger = this;
512
513     if(process.title === "node")
514   logger.stream.write(logger.format(level, message) + '\n');
515     else if(process.title === "browser")
516   logger.stream(logger.format(level, message) + '\n');
517
518   };
519 }
520
521
522 Logger.prototype = {
523   info: function(){},
524   debug: function(){},
525   warn: function(){},
526   error: Logger.writer('error'),
527   format: function(level, message){
528     if (! message) return '';
529
530     var logger = this
531       , prefix = logger.options.prefix
532       , timestamp = logger.options.timestamp ? " " + (new Date().toISOString()) : ""
533     ;
534
535     return (prefix + timestamp + ": " + message);
536   }
537 };
538
539 module.exports = Logger;
540
541 });
542
543 require.define("util", function (require, module, exports, __dirname, __filename) {
544     // todo
545
546 });
547
548 require.define("fs", function (require, module, exports, __dirname, __filename) {
549     // nothing to see here... no file methods for the browser
550
551 });
552
553 require.define("/node_modules/cookiejar/package.json", function (require, module, exports, __dirname, __filename) {
554     module.exports = {"main":"cookiejar.js"}
555 });
556
557 require.define("/node_modules/cookiejar/cookiejar.js", function (require, module, exports, __dirname, __filename) {
558     exports.CookieAccessInfo=CookieAccessInfo=function CookieAccessInfo(domain,path,secure,script) {
559     if(this instanceof CookieAccessInfo) {
560       this.domain=domain||undefined;
561       this.path=path||"/";
562       this.secure=!!secure;
563       this.script=!!script;
564       return this;
565     }
566     else {
567         return new CookieAccessInfo(domain,path,secure,script)    
568     }
569 }
570
571 exports.Cookie=Cookie=function Cookie(cookiestr) {
572   if(cookiestr instanceof Cookie) {
573     return cookiestr;
574   }
575     else {
576         if(this instanceof Cookie) {
577           this.name = null;
578           this.value = null;
579           this.expiration_date = Infinity;
580           this.path = "/";
581           this.domain = null;
582           this.secure = false; //how to define?
583           this.noscript = false; //httponly
584           if(cookiestr) {
585             this.parse(cookiestr)
586           }
587           return this;
588         }
589         return new Cookie(cookiestr)
590     }
591 }
592
593 Cookie.prototype.toString = function toString() {
594   var str=[this.name+"="+this.value];
595   if(this.expiration_date !== Infinity) {
596     str.push("expires="+(new Date(this.expiration_date)).toGMTString());
597   }
598   if(this.domain) {
599     str.push("domain="+this.domain);
600   }
601   if(this.path) {
602     str.push("path="+this.path);
603   }
604   if(this.secure) {
605     str.push("secure");
606   }
607   if(this.noscript) {
608     str.push("httponly");
609   }
610   return str.join("; ");
611 }
612
613 Cookie.prototype.toValueString = function toValueString() {
614   return this.name+"="+this.value;
615 }
616
617 var cookie_str_splitter=/[:](?=\s*[a-zA-Z0-9_\-]+\s*[=])/g
618 Cookie.prototype.parse = function parse(str) {
619   if(this instanceof Cookie) {
620       var parts=str.split(";")
621       , pair=parts[0].match(/([^=]+)=((?:.|\n)*)/)
622       , key=pair[1]
623       , value=pair[2];
624       this.name = key;
625       this.value = value;
626     
627       for(var i=1;i<parts.length;i++) {
628         pair=parts[i].match(/([^=]+)(?:=((?:.|\n)*))?/)
629         , key=pair[1].trim().toLowerCase()
630         , value=pair[2];
631         switch(key) {
632           case "httponly":
633             this.noscript = true;
634           break;
635           case "expires":
636             this.expiration_date = value
637               ? Number(Date.parse(value))
638               : Infinity;
639           break;
640           case "path":
641             this.path = value
642               ? value.trim()
643               : "";
644           break;
645           case "domain":
646             this.domain = value
647               ? value.trim()
648               : "";
649           break;
650           case "secure":
651             this.secure = true;
652           break
653         }
654       }
655     
656       return this;
657   }
658     return new Cookie().parse(str)
659 }
660
661 Cookie.prototype.matches = function matches(access_info) {
662   if(this.noscript && access_info.script
663   || this.secure && !access_info.secure
664   || !this.collidesWith(access_info)) {
665     return false
666   }
667   return true;
668 }
669
670 Cookie.prototype.collidesWith = function collidesWith(access_info) {
671   if((this.path && !access_info.path) || (this.domain && !access_info.domain)) {
672     return false
673   }
674   if(this.path && access_info.path.indexOf(this.path) !== 0) {
675     return false;
676   }
677   if (this.domain===access_info.domain) {
678     return true;
679   }
680   else if(this.domain && this.domain.charAt(0)===".")
681   {
682     var wildcard=access_info.domain.indexOf(this.domain.slice(1))
683     if(wildcard===-1 || wildcard!==access_info.domain.length-this.domain.length+1) {
684       return false;
685     }
686   }
687   else if(this.domain){
688     return false
689   }
690   return true;
691 }
692
693 exports.CookieJar=CookieJar=function CookieJar() {
694   if(this instanceof CookieJar) {
695       var cookies = {} //name: [Cookie]
696     
697       this.setCookie = function setCookie(cookie) {
698         cookie = Cookie(cookie);
699         //Delete the cookie if the set is past the current time
700         var remove = cookie.expiration_date <= Date.now();
701         if(cookie.name in cookies) {
702           var cookies_list = cookies[cookie.name];
703           for(var i=0;i<cookies_list.length;i++) {
704             var collidable_cookie = cookies_list[i];
705             if(collidable_cookie.collidesWith(cookie)) {
706               if(remove) {
707                 cookies_list.splice(i,1);
708                 if(cookies_list.length===0) {
709                   delete cookies[cookie.name]
710                 }
711                 return false;
712               }
713               else {
714                 return cookies_list[i]=cookie;
715               }
716             }
717           }
718           if(remove) {
719             return false;
720           }
721           cookies_list.push(cookie);
722           return cookie;
723         }
724         else if(remove){
725           return false;
726         }
727         else {
728           return cookies[cookie.name]=[cookie];
729         }
730       }
731       //returns a cookie
732       this.getCookie = function getCookie(cookie_name,access_info) {
733         var cookies_list = cookies[cookie_name];
734         for(var i=0;i<cookies_list.length;i++) {
735           var cookie = cookies_list[i];
736           if(cookie.expiration_date <= Date.now()) {
737             if(cookies_list.length===0) {
738               delete cookies[cookie.name]
739             }
740             continue;
741           }
742           if(cookie.matches(access_info)) {
743             return cookie;
744           }
745         }
746       }
747       //returns a list of cookies
748       this.getCookies = function getCookies(access_info) {
749         var matches=[];
750         for(var cookie_name in cookies) {
751           var cookie=this.getCookie(cookie_name,access_info);
752           if (cookie) {
753             matches.push(cookie);
754           }
755         }
756         matches.toString=function toString(){return matches.join(":");}
757             matches.toValueString=function() {return matches.map(function(c){return c.toValueString();}).join(';');}
758         return matches;
759       }
760     
761       return this;
762   }
763     return new CookieJar()
764 }
765
766
767 //returns list of cookies that were set correctly
768 CookieJar.prototype.setCookies = function setCookies(cookies) {
769   cookies=Array.isArray(cookies)
770     ?cookies
771     :cookies.split(cookie_str_splitter);
772   var successful=[]
773   for(var i=0;i<cookies.length;i++) {
774     var cookie = Cookie(cookies[i]);
775     if(this.setCookie(cookie)) {
776       successful.push(cookie);
777     }
778   }
779   return successful;
780 }
781
782 });
783
784 require.define("/shred/request.js", function (require, module, exports, __dirname, __filename) {
785     // The request object encapsulates a request, creating a Node.js HTTP request and
786 // then handling the response.
787
788 var HTTP = require("http")
789   , HTTPS = require("https")
790   , parseUri = require("./parseUri")
791   , Emitter = require('events').EventEmitter
792   , sprintf = require("sprintf").sprintf
793   , Response = require("./response")
794   , HeaderMixins = require("./mixins/headers")
795   , Content = require("./content")
796 ;
797
798 var STATUS_CODES = HTTP.STATUS_CODES || {
799     100 : 'Continue',
800     101 : 'Switching Protocols',
801     102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
802     200 : 'OK',
803     201 : 'Created',
804     202 : 'Accepted',
805     203 : 'Non-Authoritative Information',
806     204 : 'No Content',
807     205 : 'Reset Content',
808     206 : 'Partial Content',
809     207 : 'Multi-Status', // RFC 4918
810     300 : 'Multiple Choices',
811     301 : 'Moved Permanently',
812     302 : 'Moved Temporarily',
813     303 : 'See Other',
814     304 : 'Not Modified',
815     305 : 'Use Proxy',
816     307 : 'Temporary Redirect',
817     400 : 'Bad Request',
818     401 : 'Unauthorized',
819     402 : 'Payment Required',
820     403 : 'Forbidden',
821     404 : 'Not Found',
822     405 : 'Method Not Allowed',
823     406 : 'Not Acceptable',
824     407 : 'Proxy Authentication Required',
825     408 : 'Request Time-out',
826     409 : 'Conflict',
827     410 : 'Gone',
828     411 : 'Length Required',
829     412 : 'Precondition Failed',
830     413 : 'Request Entity Too Large',
831     414 : 'Request-URI Too Large',
832     415 : 'Unsupported Media Type',
833     416 : 'Requested Range Not Satisfiable',
834     417 : 'Expectation Failed',
835     418 : 'I\'m a teapot', // RFC 2324
836     422 : 'Unprocessable Entity', // RFC 4918
837     423 : 'Locked', // RFC 4918
838     424 : 'Failed Dependency', // RFC 4918
839     425 : 'Unordered Collection', // RFC 4918
840     426 : 'Upgrade Required', // RFC 2817
841     500 : 'Internal Server Error',
842     501 : 'Not Implemented',
843     502 : 'Bad Gateway',
844     503 : 'Service Unavailable',
845     504 : 'Gateway Time-out',
846     505 : 'HTTP Version not supported',
847     506 : 'Variant Also Negotiates', // RFC 2295
848     507 : 'Insufficient Storage', // RFC 4918
849     509 : 'Bandwidth Limit Exceeded',
850     510 : 'Not Extended' // RFC 2774
851 };
852
853 // The Shred object itself constructs the `Request` object. You should rarely
854 // need to do this directly.
855
856 var Request = function(options) {
857   this.log = options.logger;
858   this.cookieJar = options.cookieJar;
859   this.encoding = options.encoding;
860   this.logCurl = options.logCurl;
861   processOptions(this,options||{});
862   createRequest(this);
863 };
864
865 // A `Request` has a number of properties, many of which help with details like
866 // URL parsing or defaulting the port for the request.
867
868 Object.defineProperties(Request.prototype, {
869
870 // - **url**. You can set the `url` property with a valid URL string and all the
871 //   URL-related properties (host, port, etc.) will be automatically set on the
872 //   request object.
873
874   url: {
875     get: function() {
876       if (!this.scheme) { return null; }
877       return sprintf("%s://%s:%s%s",
878           this.scheme, this.host, this.port,
879           (this.proxy ? "/" : this.path) +
880           (this.query ? ("?" + this.query) : ""));
881     },
882     set: function(_url) {
883       _url = parseUri(_url);
884       this.scheme = _url.protocol;
885       this.host = _url.host;
886       this.port = _url.port;
887       this.path = _url.path;
888       this.query = _url.query;
889       return this;
890     },
891     enumerable: true
892   },
893
894 // - **headers**. Returns a hash representing the request headers. You can't set
895 //   this directly, only get it. You can add or modify headers by using the
896 //   `setHeader` or `setHeaders` method. This ensures that the headers are
897 //   normalized - that is, you don't accidentally send `Content-Type` and
898 //   `content-type` headers. Keep in mind that if you modify the returned hash,
899 //   it will *not* modify the request headers.
900
901   headers: {
902     get: function() {
903       return this.getHeaders();
904     },
905     enumerable: true
906   },
907
908 // - **port**. Unless you set the `port` explicitly or include it in the URL, it
909 //   will default based on the scheme.
910
911   port: {
912     get: function() {
913       if (!this._port) {
914         switch(this.scheme) {
915           case "https": return this._port = 443;
916           case "http":
917           default: return this._port = 80;
918         }
919       }
920       return this._port;
921     },
922     set: function(value) { this._port = value; return this; },
923     enumerable: true
924   },
925
926 // - **method**. The request method - `get`, `put`, `post`, etc. that will be
927 //   used to make the request. Defaults to `get`.
928
929   method: {
930     get: function() {
931       return this._method = (this._method||"GET");
932     },
933     set: function(value) {
934       this._method = value; return this;
935     },
936     enumerable: true
937   },
938
939 // - **query**. Can be set either with a query string or a hash (object). Get
940 //   will always return a properly escaped query string or null if there is no
941 //   query component for the request.
942
943   query: {
944     get: function() {return this._query;},
945     set: function(value) {
946       var stringify = function (hash) {
947         var query = "";
948         for (var key in hash) {
949           query += encodeURIComponent(key) + '=' + encodeURIComponent(hash[key]) + '&';
950         }
951         // Remove the last '&'
952         query = query.slice(0, -1);
953         return query;
954       }
955
956       if (value) {
957         if (typeof value === 'object') {
958           value = stringify(value);
959         }
960         this._query = value;
961       } else {
962         this._query = "";
963       }
964       return this;
965     },
966     enumerable: true
967   },
968
969 // - **parameters**. This will return the query parameters in the form of a hash
970 //   (object).
971
972   parameters: {
973     get: function() { return QueryString.parse(this._query||""); },
974     enumerable: true
975   },
976
977 // - **content**. (Aliased as `body`.) Set this to add a content entity to the
978 //   request. Attempts to use the `content-type` header to determine what to do
979 //   with the content value. Get this to get back a [`Content`
980 //   object](./content.html).
981
982   body: {
983     get: function() { return this._body; },
984     set: function(value) {
985       this._body = new Content({
986         data: value,
987         type: this.getHeader("Content-Type")
988       });
989       this.setHeader("Content-Type",this.content.type);
990       this.setHeader("Content-Length",this.content.length);
991       return this;
992     },
993     enumerable: true
994   },
995
996 // - **timeout**. Used to determine how long to wait for a response. Does not
997 //   distinguish between connect timeouts versus request timeouts. Set either in
998 //   milliseconds or with an object with temporal attributes (hours, minutes,
999 //   seconds) and convert it into milliseconds. Get will always return
1000 //   milliseconds.
1001
1002   timeout: {
1003     get: function() { return this._timeout; }, // in milliseconds
1004     set: function(timeout) {
1005       var request = this
1006         , milliseconds = 0;
1007       ;
1008       if (!timeout) return this;
1009       if (typeof timeout==="number") { milliseconds = timeout; }
1010       else {
1011         milliseconds = (timeout.milliseconds||0) +
1012           (1000 * ((timeout.seconds||0) +
1013               (60 * ((timeout.minutes||0) +
1014                 (60 * (timeout.hours||0))))));
1015       }
1016       this._timeout = milliseconds;
1017       return this;
1018     },
1019     enumerable: true
1020   }
1021 });
1022
1023 // Alias `body` property to `content`. Since the [content object](./content.html)
1024 // has a `body` attribute, it's preferable to use `content` since you can then
1025 // access the raw content data using `content.body`.
1026
1027 Object.defineProperty(Request.prototype,"content",
1028     Object.getOwnPropertyDescriptor(Request.prototype, "body"));
1029
1030 // The `Request` object can be pretty overwhelming to view using the built-in
1031 // Node.js inspect method. We want to make it a bit more manageable. This
1032 // probably goes [too far in the other
1033 // direction](https://github.com/spire-io/shred/issues/2).
1034
1035 Request.prototype.inspect = function () {
1036   var request = this;
1037   var headers = this.format_headers();
1038   var summary = ["<Shred Request> ", request.method.toUpperCase(),
1039       request.url].join(" ")
1040   return [ summary, "- Headers:", headers].join("\n");
1041 };
1042
1043 Request.prototype.format_headers = function () {
1044   var array = []
1045   var headers = this._headers
1046   for (var key in headers) {
1047     if (headers.hasOwnProperty(key)) {
1048       var value = headers[key]
1049       array.push("\t" + key + ": " + value);
1050     }
1051   }
1052   return array.join("\n");
1053 };
1054
1055 // Allow chainable 'on's:  shred.get({ ... }).on( ... ).  You can pass in a
1056 // single function, a pair (event, function), or a hash:
1057 // { event: function, event: function }
1058 Request.prototype.on = function (eventOrHash, listener) {
1059   var emitter = this.emitter;
1060   // Pass in a single argument as a function then make it the default response handler
1061   if (arguments.length === 1 && typeof(eventOrHash) === 'function') {
1062     emitter.on('response', eventOrHash);
1063   } else if (arguments.length === 1 && typeof(eventOrHash) === 'object') {
1064     for (var key in eventOrHash) {
1065       if (eventOrHash.hasOwnProperty(key)) {
1066         emitter.on(key, eventOrHash[key]);
1067       }
1068     }
1069   } else {
1070     emitter.on(eventOrHash, listener);
1071   }
1072   return this;
1073 };
1074
1075 // Add in the header methods. Again, these ensure we don't get the same header
1076 // multiple times with different case conventions.
1077 HeaderMixins.gettersAndSetters(Request);
1078
1079 // `processOptions` is called from the constructor to handle all the work
1080 // associated with making sure we do our best to ensure we have a valid request.
1081
1082 var processOptions = function(request,options) {
1083
1084   request.log.debug("Processing request options ..");
1085
1086   // We'll use `request.emitter` to manage the `on` event handlers.
1087   request.emitter = (new Emitter);
1088
1089   request.agent = options.agent;
1090
1091   // Set up the handlers ...
1092   if (options.on) {
1093     for (var key in options.on) {
1094       if (options.on.hasOwnProperty(key)) {
1095         request.emitter.on(key, options.on[key]);
1096       }
1097     }
1098   }
1099
1100   // Make sure we were give a URL or a host
1101   if (!options.url && !options.host) {
1102     request.emitter.emit("request_error",
1103         new Error("No url or url options (host, port, etc.)"));
1104     return;
1105   }
1106
1107   // Allow for the [use of a proxy](http://www.jmarshall.com/easy/http/#proxies).
1108
1109   if (options.url) {
1110     if (options.proxy) {
1111       request.url = options.proxy;
1112       request.path = options.url;
1113     } else {
1114       request.url = options.url;
1115     }
1116   }
1117
1118   // Set the remaining options.
1119   request.query = options.query||options.parameters||request.query ;
1120   request.method = options.method;
1121   request.setHeader("user-agent",options.agent||"Shred");
1122   request.setHeaders(options.headers);
1123
1124   if (request.cookieJar) {
1125     var cookies = request.cookieJar.getCookies( CookieAccessInfo( request.host, request.path ) );
1126     if (cookies.length) {
1127       var cookieString = request.getHeader('cookie')||'';
1128       for (var cookieIndex = 0; cookieIndex < cookies.length; ++cookieIndex) {
1129           if ( cookieString.length && cookieString[ cookieString.length - 1 ] != ';' )
1130           {
1131               cookieString += ';';
1132           }
1133           cookieString += cookies[ cookieIndex ].name + '=' + cookies[ cookieIndex ].value + ';';
1134       }
1135       request.setHeader("cookie", cookieString);
1136     }
1137   }
1138   
1139   // The content entity can be set either using the `body` or `content` attributes.
1140   if (options.body||options.content) {
1141     request.content = options.body||options.content;
1142   }
1143   request.timeout = options.timeout;
1144
1145 };
1146
1147 // `createRequest` is also called by the constructor, after `processOptions`.
1148 // This actually makes the request and processes the response, so `createRequest`
1149 // is a bit of a misnomer.
1150
1151 var createRequest = function(request) {
1152   var timeout ;
1153
1154   request.log.debug("Creating request ..");
1155   request.log.debug(request);
1156
1157   var reqParams = {
1158     host: request.host,
1159     port: request.port,
1160     method: request.method,
1161     path: request.path + (request.query ? '?'+request.query : ""),
1162     headers: request.getHeaders(),
1163     // Node's HTTP/S modules will ignore this, but we are using the
1164     // browserify-http module in the browser for both HTTP and HTTPS, and this
1165     // is how you differentiate the two.
1166     scheme: request.scheme,
1167     // Use a provided agent.  'Undefined' is the default, which uses a global
1168     // agent.
1169     agent: request.agent
1170   };
1171
1172   if (request.logCurl) {
1173     logCurl(request);
1174   }
1175
1176   var http = request.scheme == "http" ? HTTP : HTTPS;
1177
1178   // Set up the real request using the selected library. The request won't be
1179   // sent until we call `.end()`.
1180   request._raw = http.request(reqParams, function(response) {
1181     request.log.debug("Received response ..");
1182
1183     // We haven't timed out and we have a response, so make sure we clear the
1184     // timeout so it doesn't fire while we're processing the response.
1185     clearTimeout(timeout);
1186
1187     // Construct a Shred `Response` object from the response. This will stream
1188     // the response, thus the need for the callback. We can access the response
1189     // entity safely once we're in the callback.
1190     response = new Response(response, request, function(response) {
1191
1192       // Set up some event magic. The precedence is given first to
1193       // status-specific handlers, then to responses for a given event, and then
1194       // finally to the more general `response` handler. In the last case, we
1195       // need to first make sure we're not dealing with a a redirect.
1196       var emit = function(event) {
1197         var emitter = request.emitter;
1198         var textStatus = STATUS_CODES[response.status] ? STATUS_CODES[response.status].toLowerCase() : null;
1199         if (emitter.listeners(response.status).length > 0 || emitter.listeners(textStatus).length > 0) {
1200           emitter.emit(response.status, response);
1201           emitter.emit(textStatus, response);
1202         } else {
1203           if (emitter.listeners(event).length>0) {
1204             emitter.emit(event, response);
1205           } else if (!response.isRedirect) {
1206             emitter.emit("response", response);
1207             //console.warn("Request has no event listener for status code " + response.status);
1208           }
1209         }
1210       };
1211
1212       // Next, check for a redirect. We simply repeat the request with the URL
1213       // given in the `Location` header. We fire a `redirect` event.
1214       if (response.isRedirect) {
1215         request.log.debug("Redirecting to "
1216             + response.getHeader("Location"));
1217         request.url = response.getHeader("Location");
1218         emit("redirect");
1219         createRequest(request);
1220
1221       // Okay, it's not a redirect. Is it an error of some kind?
1222       } else if (response.isError) {
1223         emit("error");
1224       } else {
1225       // It looks like we're good shape. Trigger the `success` event.
1226         emit("success");
1227       }
1228     });
1229   });
1230
1231   // We're still setting up the request. Next, we're going to handle error cases
1232   // where we have no response. We don't emit an error event because that event
1233   // takes a response. We don't response handlers to have to check for a null
1234   // value. However, we [should introduce a different event
1235   // type](https://github.com/spire-io/shred/issues/3) for this type of error.
1236   request._raw.on("error", function(error) {
1237     request.emitter.emit("request_error", error);
1238   });
1239
1240   request._raw.on("socket", function(socket) {
1241     request.emitter.emit("socket", socket);
1242   });
1243
1244   // TCP timeouts should also trigger the "response_error" event.
1245   request._raw.on('socket', function () {
1246     request._raw.socket.on('timeout', function () {
1247       // This should trigger the "error" event on the raw request, which will
1248       // trigger the "response_error" on the shred request.
1249       request._raw.abort();
1250     });
1251   });
1252
1253
1254   // We're almost there. Next, we need to write the request entity to the
1255   // underlying request object.
1256   if (request.content) {
1257     request.log.debug("Streaming body: '" +
1258         request.content.data.slice(0,59) + "' ... ");
1259     request._raw.write(request.content.data);
1260   }
1261
1262   // Finally, we need to set up the timeout. We do this last so that we don't
1263   // start the clock ticking until the last possible moment.
1264   if (request.timeout) {
1265     timeout = setTimeout(function() {
1266       request.log.debug("Timeout fired, aborting request ...");
1267       request._raw.abort();
1268       request.emitter.emit("timeout", request);
1269     },request.timeout);
1270   }
1271
1272   // The `.end()` method will cause the request to fire. Technically, it might
1273   // have already sent the headers and body.
1274   request.log.debug("Sending request ...");
1275   request._raw.end();
1276 };
1277
1278 // Logs the curl command for the request.
1279 var logCurl = function (req) {
1280   var headers = req.getHeaders();
1281   var headerString = "";
1282
1283   for (var key in headers) {
1284     headerString += '-H "' + key + ": " + headers[key] + '" ';
1285   }
1286
1287   var bodyString = ""
1288
1289   if (req.content) {
1290     bodyString += "-d '" + req.content.body + "' ";
1291   }
1292
1293   var query = req.query ? '?' + req.query : "";
1294
1295   console.log("curl " +
1296     "-X " + req.method.toUpperCase() + " " +
1297     req.scheme + "://" + req.host + ":" + req.port + req.path + query + " " +
1298     headerString +
1299     bodyString
1300   );
1301 };
1302
1303
1304 module.exports = Request;
1305
1306 });
1307
1308 require.define("http", function (require, module, exports, __dirname, __filename) {
1309     // todo
1310
1311 });
1312
1313 require.define("https", function (require, module, exports, __dirname, __filename) {
1314     // todo
1315
1316 });
1317
1318 require.define("/shred/parseUri.js", function (require, module, exports, __dirname, __filename) {
1319     // parseUri 1.2.2
1320 // (c) Steven Levithan <stevenlevithan.com>
1321 // MIT License
1322
1323 function parseUri (str) {
1324   var o   = parseUri.options,
1325     m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
1326     uri = {},
1327     i   = 14;
1328
1329   while (i--) uri[o.key[i]] = m[i] || "";
1330
1331   uri[o.q.name] = {};
1332   uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
1333     if ($1) uri[o.q.name][$1] = $2;
1334   });
1335
1336   return uri;
1337 };
1338
1339 parseUri.options = {
1340   strictMode: false,
1341   key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
1342   q:   {
1343     name:   "queryKey",
1344     parser: /(?:^|&)([^&=]*)=?([^&]*)/g
1345   },
1346   parser: {
1347     strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
1348     loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
1349   }
1350 };
1351
1352 module.exports = parseUri;
1353
1354 });
1355
1356 require.define("events", function (require, module, exports, __dirname, __filename) {
1357     if (!process.EventEmitter) process.EventEmitter = function () {};
1358
1359 var EventEmitter = exports.EventEmitter = process.EventEmitter;
1360 var isArray = typeof Array.isArray === 'function'
1361     ? Array.isArray
1362     : function (xs) {
1363         return Object.toString.call(xs) === '[object Array]'
1364     }
1365 ;
1366
1367 // By default EventEmitters will print a warning if more than
1368 // 10 listeners are added to it. This is a useful default which
1369 // helps finding memory leaks.
1370 //
1371 // Obviously not all Emitters should be limited to 10. This function allows
1372 // that to be increased. Set to zero for unlimited.
1373 var defaultMaxListeners = 10;
1374 EventEmitter.prototype.setMaxListeners = function(n) {
1375   if (!this._events) this._events = {};
1376   this._events.maxListeners = n;
1377 };
1378
1379
1380 EventEmitter.prototype.emit = function(type) {
1381   // If there is no 'error' event listener then throw.
1382   if (type === 'error') {
1383     if (!this._events || !this._events.error ||
1384         (isArray(this._events.error) && !this._events.error.length))
1385     {
1386       if (arguments[1] instanceof Error) {
1387         throw arguments[1]; // Unhandled 'error' event
1388       } else {
1389         throw new Error("Uncaught, unspecified 'error' event.");
1390       }
1391       return false;
1392     }
1393   }
1394
1395   if (!this._events) return false;
1396   var handler = this._events[type];
1397   if (!handler) return false;
1398
1399   if (typeof handler == 'function') {
1400     switch (arguments.length) {
1401       // fast cases
1402       case 1:
1403         handler.call(this);
1404         break;
1405       case 2:
1406         handler.call(this, arguments[1]);
1407         break;
1408       case 3:
1409         handler.call(this, arguments[1], arguments[2]);
1410         break;
1411       // slower
1412       default:
1413         var args = Array.prototype.slice.call(arguments, 1);
1414         handler.apply(this, args);
1415     }
1416     return true;
1417
1418   } else if (isArray(handler)) {
1419     var args = Array.prototype.slice.call(arguments, 1);
1420
1421     var listeners = handler.slice();
1422     for (var i = 0, l = listeners.length; i < l; i++) {
1423       listeners[i].apply(this, args);
1424     }
1425     return true;
1426
1427   } else {
1428     return false;
1429   }
1430 };
1431
1432 // EventEmitter is defined in src/node_events.cc
1433 // EventEmitter.prototype.emit() is also defined there.
1434 EventEmitter.prototype.addListener = function(type, listener) {
1435   if ('function' !== typeof listener) {
1436     throw new Error('addListener only takes instances of Function');
1437   }
1438
1439   if (!this._events) this._events = {};
1440
1441   // To avoid recursion in the case that type == "newListeners"! Before
1442   // adding it to the listeners, first emit "newListeners".
1443   this.emit('newListener', type, listener);
1444
1445   if (!this._events[type]) {
1446     // Optimize the case of one listener. Don't need the extra array object.
1447     this._events[type] = listener;
1448   } else if (isArray(this._events[type])) {
1449
1450     // Check for listener leak
1451     if (!this._events[type].warned) {
1452       var m;
1453       if (this._events.maxListeners !== undefined) {
1454         m = this._events.maxListeners;
1455       } else {
1456         m = defaultMaxListeners;
1457       }
1458
1459       if (m && m > 0 && this._events[type].length > m) {
1460         this._events[type].warned = true;
1461         console.error('(node) warning: possible EventEmitter memory ' +
1462                       'leak detected. %d listeners added. ' +
1463                       'Use emitter.setMaxListeners() to increase limit.',
1464                       this._events[type].length);
1465         console.trace();
1466       }
1467     }
1468
1469     // If we've already got an array, just append.
1470     this._events[type].push(listener);
1471   } else {
1472     // Adding the second element, need to change to array.
1473     this._events[type] = [this._events[type], listener];
1474   }
1475
1476   return this;
1477 };
1478
1479 EventEmitter.prototype.on = EventEmitter.prototype.addListener;
1480
1481 EventEmitter.prototype.once = function(type, listener) {
1482   var self = this;
1483   self.on(type, function g() {
1484     self.removeListener(type, g);
1485     listener.apply(this, arguments);
1486   });
1487
1488   return this;
1489 };
1490
1491 EventEmitter.prototype.removeListener = function(type, listener) {
1492   if ('function' !== typeof listener) {
1493     throw new Error('removeListener only takes instances of Function');
1494   }
1495
1496   // does not use listeners(), so no side effect of creating _events[type]
1497   if (!this._events || !this._events[type]) return this;
1498
1499   var list = this._events[type];
1500
1501   if (isArray(list)) {
1502     var i = list.indexOf(listener);
1503     if (i < 0) return this;
1504     list.splice(i, 1);
1505     if (list.length == 0)
1506       delete this._events[type];
1507   } else if (this._events[type] === listener) {
1508     delete this._events[type];
1509   }
1510
1511   return this;
1512 };
1513
1514 EventEmitter.prototype.removeAllListeners = function(type) {
1515   // does not use listeners(), so no side effect of creating _events[type]
1516   if (type && this._events && this._events[type]) this._events[type] = null;
1517   return this;
1518 };
1519
1520 EventEmitter.prototype.listeners = function(type) {
1521   if (!this._events) this._events = {};
1522   if (!this._events[type]) this._events[type] = [];
1523   if (!isArray(this._events[type])) {
1524     this._events[type] = [this._events[type]];
1525   }
1526   return this._events[type];
1527 };
1528
1529 });
1530
1531 require.define("/node_modules/sprintf/package.json", function (require, module, exports, __dirname, __filename) {
1532     module.exports = {"main":"./lib/sprintf"}
1533 });
1534
1535 require.define("/node_modules/sprintf/lib/sprintf.js", function (require, module, exports, __dirname, __filename) {
1536     /**
1537 sprintf() for JavaScript 0.7-beta1
1538 http://www.diveintojavascript.com/projects/javascript-sprintf
1539
1540 Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
1541 All rights reserved.
1542
1543 Redistribution and use in source and binary forms, with or without
1544 modification, are permitted provided that the following conditions are met:
1545     * Redistributions of source code must retain the above copyright
1546       notice, this list of conditions and the following disclaimer.
1547     * Redistributions in binary form must reproduce the above copyright
1548       notice, this list of conditions and the following disclaimer in the
1549       documentation and/or other materials provided with the distribution.
1550     * Neither the name of sprintf() for JavaScript nor the
1551       names of its contributors may be used to endorse or promote products
1552       derived from this software without specific prior written permission.
1553
1554 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
1555 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1556 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1557 DISCLAIMED. IN NO EVENT SHALL Alexandru Marasteanu BE LIABLE FOR ANY
1558 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1559 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1560 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
1561 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1562 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1563 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1564
1565
1566 Changelog:
1567 2010.11.07 - 0.7-beta1-node
1568   - converted it to a node.js compatible module
1569
1570 2010.09.06 - 0.7-beta1
1571   - features: vsprintf, support for named placeholders
1572   - enhancements: format cache, reduced global namespace pollution
1573
1574 2010.05.22 - 0.6:
1575  - reverted to 0.4 and fixed the bug regarding the sign of the number 0
1576  Note:
1577  Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
1578  who warned me about a bug in 0.5, I discovered that the last update was
1579  a regress. I appologize for that.
1580
1581 2010.05.09 - 0.5:
1582  - bug fix: 0 is now preceeded with a + sign
1583  - bug fix: the sign was not at the right position on padded results (Kamal Abdali)
1584  - switched from GPL to BSD license
1585
1586 2007.10.21 - 0.4:
1587  - unit test and patch (David Baird)
1588
1589 2007.09.17 - 0.3:
1590  - bug fix: no longer throws exception on empty paramenters (Hans Pufal)
1591
1592 2007.09.11 - 0.2:
1593  - feature: added argument swapping
1594
1595 2007.04.03 - 0.1:
1596  - initial release
1597 **/
1598
1599 var sprintf = (function() {
1600   function get_type(variable) {
1601     return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase();
1602   }
1603   function str_repeat(input, multiplier) {
1604     for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */}
1605     return output.join('');
1606   }
1607
1608   var str_format = function() {
1609     if (!str_format.cache.hasOwnProperty(arguments[0])) {
1610       str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
1611     }
1612     return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
1613   };
1614
1615   str_format.format = function(parse_tree, argv) {
1616     var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
1617     for (i = 0; i < tree_length; i++) {
1618       node_type = get_type(parse_tree[i]);
1619       if (node_type === 'string') {
1620         output.push(parse_tree[i]);
1621       }
1622       else if (node_type === 'array') {
1623         match = parse_tree[i]; // convenience purposes only
1624         if (match[2]) { // keyword argument
1625           arg = argv[cursor];
1626           for (k = 0; k < match[2].length; k++) {
1627             if (!arg.hasOwnProperty(match[2][k])) {
1628               throw(sprintf('[sprintf] property "%s" does not exist', match[2][k]));
1629             }
1630             arg = arg[match[2][k]];
1631           }
1632         }
1633         else if (match[1]) { // positional argument (explicit)
1634           arg = argv[match[1]];
1635         }
1636         else { // positional argument (implicit)
1637           arg = argv[cursor++];
1638         }
1639
1640         if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {
1641           throw(sprintf('[sprintf] expecting number but found %s', get_type(arg)));
1642         }
1643         switch (match[8]) {
1644           case 'b': arg = arg.toString(2); break;
1645           case 'c': arg = String.fromCharCode(arg); break;
1646           case 'd': arg = parseInt(arg, 10); break;
1647           case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
1648           case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
1649           case 'o': arg = arg.toString(8); break;
1650           case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
1651           case 'u': arg = Math.abs(arg); break;
1652           case 'x': arg = arg.toString(16); break;
1653           case 'X': arg = arg.toString(16).toUpperCase(); break;
1654         }
1655         arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);
1656         pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';
1657         pad_length = match[6] - String(arg).length;
1658         pad = match[6] ? str_repeat(pad_character, pad_length) : '';
1659         output.push(match[5] ? arg + pad : pad + arg);
1660       }
1661     }
1662     return output.join('');
1663   };
1664
1665   str_format.cache = {};
1666
1667   str_format.parse = function(fmt) {
1668     var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
1669     while (_fmt) {
1670       if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {
1671         parse_tree.push(match[0]);
1672       }
1673       else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {
1674         parse_tree.push('%');
1675       }
1676       else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
1677         if (match[2]) {
1678           arg_names |= 1;
1679           var field_list = [], replacement_field = match[2], field_match = [];
1680           if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
1681             field_list.push(field_match[1]);
1682             while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
1683               if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
1684                 field_list.push(field_match[1]);
1685               }
1686               else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {
1687                 field_list.push(field_match[1]);
1688               }
1689               else {
1690                 throw('[sprintf] huh?');
1691               }
1692             }
1693           }
1694           else {
1695             throw('[sprintf] huh?');
1696           }
1697           match[2] = field_list;
1698         }
1699         else {
1700           arg_names |= 2;
1701         }
1702         if (arg_names === 3) {
1703           throw('[sprintf] mixing positional and named placeholders is not (yet) supported');
1704         }
1705         parse_tree.push(match);
1706       }
1707       else {
1708         throw('[sprintf] huh?');
1709       }
1710       _fmt = _fmt.substring(match[0].length);
1711     }
1712     return parse_tree;
1713   };
1714
1715   return str_format;
1716 })();
1717
1718 var vsprintf = function(fmt, argv) {
1719   argv.unshift(fmt);
1720   return sprintf.apply(null, argv);
1721 };
1722
1723 exports.sprintf = sprintf;
1724 exports.vsprintf = vsprintf;
1725 });
1726
1727 require.define("/shred/response.js", function (require, module, exports, __dirname, __filename) {
1728     // The `Response object` encapsulates a Node.js HTTP response.
1729
1730 var Content = require("./content")
1731   , HeaderMixins = require("./mixins/headers")
1732   , CookieJarLib = require( "cookiejar" )
1733   , Cookie = CookieJarLib.Cookie
1734 ;
1735
1736 // Browser doesn't have zlib.
1737 var zlib = null;
1738 try {
1739   zlib = require('zlib');
1740 } catch (e) {
1741   // console.warn("no zlib library");
1742 }
1743
1744 // Iconv doesn't work in browser
1745 var Iconv = null;
1746 try {
1747   Iconv = require('iconv-lite');
1748 } catch (e) {
1749   // console.warn("no iconv library");
1750 }
1751
1752 // Construct a `Response` object. You should never have to do this directly. The
1753 // `Request` object handles this, getting the raw response object and passing it
1754 // in here, along with the request. The callback allows us to stream the response
1755 // and then use the callback to let the request know when it's ready.
1756 var Response = function(raw, request, callback) { 
1757   var response = this;
1758   this._raw = raw;
1759
1760   // The `._setHeaders` method is "private"; you can't otherwise set headers on
1761   // the response.
1762   this._setHeaders.call(this,raw.headers);
1763   
1764   // store any cookies
1765   if (request.cookieJar && this.getHeader('set-cookie')) {
1766     var cookieStrings = this.getHeader('set-cookie');
1767     var cookieObjs = []
1768       , cookie;
1769
1770     for (var i = 0; i < cookieStrings.length; i++) {
1771       var cookieString = cookieStrings[i];
1772       if (!cookieString) {
1773         continue;
1774       }
1775
1776       if (!cookieString.match(/domain\=/i)) {
1777         cookieString += '; domain=' + request.host;
1778       }
1779
1780       if (!cookieString.match(/path\=/i)) {
1781         cookieString += '; path=' + request.path;
1782       }
1783
1784       try {
1785         cookie = new Cookie(cookieString);
1786         if (cookie) {
1787           cookieObjs.push(cookie);
1788         }
1789       } catch (e) {
1790         console.warn("Tried to set bad cookie: " + cookieString);
1791       }
1792     }
1793
1794     request.cookieJar.setCookies(cookieObjs);
1795   }
1796
1797   this.request = request;
1798   this.client = request.client;
1799   this.log = this.request.log;
1800
1801   // Stream the response content entity and fire the callback when we're done.
1802   // Store the incoming data in a array of Buffers which we concatinate into one
1803   // buffer at the end.  We need to use buffers instead of strings here in order
1804   // to preserve binary data.
1805   var chunkBuffers = [];
1806   var dataLength = 0;
1807   raw.on("data", function(chunk) {
1808     chunkBuffers.push(chunk);
1809     dataLength += chunk.length;
1810   });
1811   raw.on("end", function() {
1812     var body;
1813     if (typeof Buffer === 'undefined') {
1814       // Just concatinate into a string
1815       body = chunkBuffers.join('');
1816     } else {
1817       // Initialize new buffer and add the chunks one-at-a-time.
1818       body = new Buffer(dataLength);
1819       for (var i = 0, pos = 0; i < chunkBuffers.length; i++) {
1820         chunkBuffers[i].copy(body, pos);
1821         pos += chunkBuffers[i].length;
1822       }
1823     }
1824
1825     var setBodyAndFinish = function (body) {
1826       response._body = new Content({ 
1827         body: body,
1828         type: response.getHeader("Content-Type")
1829       });
1830       callback(response);
1831     }
1832
1833     if (zlib && response.getHeader("Content-Encoding") === 'gzip'){
1834       zlib.gunzip(body, function (err, gunzippedBody) {
1835         if (Iconv && response.request.encoding){
1836           body = Iconv.fromEncoding(gunzippedBody,response.request.encoding);
1837         } else {
1838           body = gunzippedBody.toString();
1839         }
1840         setBodyAndFinish(body);
1841       })
1842     }
1843     else{
1844        if (response.request.encoding){
1845             body = Iconv.fromEncoding(body,response.request.encoding);
1846         }        
1847       setBodyAndFinish(body);
1848     }
1849   });
1850 };
1851
1852 // The `Response` object can be pretty overwhelming to view using the built-in
1853 // Node.js inspect method. We want to make it a bit more manageable. This
1854 // probably goes [too far in the other
1855 // direction](https://github.com/spire-io/shred/issues/2).
1856
1857 Response.prototype = {
1858   inspect: function() {
1859     var response = this;
1860     var headers = this.format_headers();
1861     var summary = ["<Shred Response> ", response.status].join(" ")
1862     return [ summary, "- Headers:", headers].join("\n");
1863   },
1864   format_headers: function () {
1865     var array = []
1866     var headers = this._headers
1867     for (var key in headers) {
1868       if (headers.hasOwnProperty(key)) {
1869         var value = headers[key]
1870         array.push("\t" + key + ": " + value);
1871       }
1872     }
1873     return array.join("\n");
1874   }
1875 };
1876
1877 // `Response` object properties, all of which are read-only:
1878 Object.defineProperties(Response.prototype, {
1879   
1880 // - **status**. The HTTP status code for the response. 
1881   status: {
1882     get: function() { return this._raw.statusCode; },
1883     enumerable: true
1884   },
1885
1886 // - **content**. The HTTP content entity, if any. Provided as a [content
1887 //   object](./content.html), which will attempt to convert the entity based upon
1888 //   the `content-type` header. The converted value is available as
1889 //   `content.data`. The original raw content entity is available as
1890 //   `content.body`.
1891   body: {
1892     get: function() { return this._body; }
1893   },
1894   content: {
1895     get: function() { return this.body; },
1896     enumerable: true
1897   },
1898
1899 // - **isRedirect**. Is the response a redirect? These are responses with 3xx
1900 //   status and a `Location` header.
1901   isRedirect: {
1902     get: function() {
1903       return (this.status>299
1904           &&this.status<400
1905           &&this.getHeader("Location"));
1906     },
1907     enumerable: true
1908   },
1909
1910 // - **isError**. Is the response an error? These are responses with status of
1911 //   400 or greater.
1912   isError: {
1913     get: function() {
1914       return (this.status === 0 || this.status > 399)
1915     },
1916     enumerable: true
1917   }
1918 });
1919
1920 // Add in the [getters for accessing the normalized headers](./headers.js).
1921 HeaderMixins.getters(Response);
1922 HeaderMixins.privateSetters(Response);
1923
1924 // Work around Mozilla bug #608735 [https://bugzil.la/608735], which causes
1925 // getAllResponseHeaders() to return {} if the response is a CORS request.
1926 // xhr.getHeader still works correctly.
1927 var getHeader = Response.prototype.getHeader;
1928 Response.prototype.getHeader = function (name) {
1929   return (getHeader.call(this,name) ||
1930     (typeof this._raw.getHeader === 'function' && this._raw.getHeader(name)));
1931 };
1932
1933 module.exports = Response;
1934
1935 });
1936
1937 require.define("/shred/content.js", function (require, module, exports, __dirname, __filename) {
1938     
1939 // The purpose of the `Content` object is to abstract away the data conversions
1940 // to and from raw content entities as strings. For example, you want to be able
1941 // to pass in a Javascript object and have it be automatically converted into a
1942 // JSON string if the `content-type` is set to a JSON-based media type.
1943 // Conversely, you want to be able to transparently get back a Javascript object
1944 // in the response if the `content-type` is a JSON-based media-type.
1945
1946 // One limitation of the current implementation is that it [assumes the `charset` is UTF-8](https://github.com/spire-io/shred/issues/5).
1947
1948 // The `Content` constructor takes an options object, which *must* have either a
1949 // `body` or `data` property and *may* have a `type` property indicating the
1950 // media type. If there is no `type` attribute, a default will be inferred.
1951 var Content = function(options) {
1952   this.body = options.body;
1953   this.data = options.data;
1954   this.type = options.type;
1955 };
1956
1957 Content.prototype = {
1958   // Treat `toString()` as asking for the `content.body`. That is, the raw content entity.
1959   //
1960   //     toString: function() { return this.body; }
1961   //
1962   // Commented out, but I've forgotten why. :/
1963 };
1964
1965
1966 // `Content` objects have the following attributes:
1967 Object.defineProperties(Content.prototype,{
1968   
1969 // - **type**. Typically accessed as `content.type`, reflects the `content-type`
1970 //   header associated with the request or response. If not passed as an options
1971 //   to the constructor or set explicitly, it will infer the type the `data`
1972 //   attribute, if possible, and, failing that, will default to `text/plain`.
1973   type: {
1974     get: function() {
1975       if (this._type) {
1976         return this._type;
1977       } else {
1978         if (this._data) {
1979           switch(typeof this._data) {
1980             case "string": return "text/plain";
1981             case "object": return "application/json";
1982           }
1983         }
1984       }
1985       return "text/plain";
1986     },
1987     set: function(value) {
1988       this._type = value;
1989       return this;
1990     },
1991     enumerable: true
1992   },
1993
1994 // - **data**. Typically accessed as `content.data`, reflects the content entity
1995 //   converted into Javascript data. This can be a string, if the `type` is, say,
1996 //   `text/plain`, but can also be a Javascript object. The conversion applied is
1997 //   based on the `processor` attribute. The `data` attribute can also be set
1998 //   directly, in which case the conversion will be done the other way, to infer
1999 //   the `body` attribute.
2000   data: {
2001     get: function() {
2002       if (this._body) {
2003         return this.processor.parser(this._body);
2004       } else {
2005         return this._data;
2006       }
2007     },
2008     set: function(data) {
2009       if (this._body&&data) Errors.setDataWithBody(this);
2010       this._data = data;
2011       return this;
2012     },
2013     enumerable: true
2014   },
2015
2016 // - **body**. Typically accessed as `content.body`, reflects the content entity
2017 //   as a UTF-8 string. It is the mirror of the `data` attribute. If you set the
2018 //   `data` attribute, the `body` attribute will be inferred and vice-versa. If
2019 //   you attempt to set both, an exception is raised.
2020   body: {
2021     get: function() {
2022       if (this._data) {
2023         return this.processor.stringify(this._data);
2024       } else {
2025         return this.processor.stringify(this._body);
2026       }
2027     },
2028     set: function(body) {
2029       if (this._data&&body) Errors.setBodyWithData(this);
2030       this._body = body;
2031       return this;
2032     },
2033     enumerable: true
2034   },
2035
2036 // - **processor**. The functions that will be used to convert to/from `data` and
2037 //   `body` attributes. You can add processors. The two that are built-in are for
2038 //   `text/plain`, which is basically an identity transformation and
2039 //   `application/json` and other JSON-based media types (including custom media
2040 //   types with `+json`). You can add your own processors. See below.
2041   processor: {
2042     get: function() {
2043       var processor = Content.processors[this.type];
2044       if (processor) {
2045         return processor;
2046       } else {
2047         // Return the first processor that matches any part of the
2048         // content type. ex: application/vnd.foobar.baz+json will match json.
2049         var main = this.type.split(";")[0];
2050         var parts = main.split(/\+|\//);
2051         for (var i=0, l=parts.length; i < l; i++) {
2052           processor = Content.processors[parts[i]]
2053         }
2054         return processor || {parser:identity,stringify:toString};
2055       }
2056     },
2057     enumerable: true
2058   },
2059
2060 // - **length**. Typically accessed as `content.length`, returns the length in
2061 //   bytes of the raw content entity.
2062   length: {
2063     get: function() {
2064       if (typeof Buffer !== 'undefined') {
2065         return Buffer.byteLength(this.body);
2066       }
2067       return this.body.length;
2068     }
2069   }
2070 });
2071
2072 Content.processors = {};
2073
2074 // The `registerProcessor` function allows you to add your own processors to
2075 // convert content entities. Each processor consists of a Javascript object with
2076 // two properties:
2077 // - **parser**. The function used to parse a raw content entity and convert it
2078 //   into a Javascript data type.
2079 // - **stringify**. The function used to convert a Javascript data type into a
2080 //   raw content entity.
2081 Content.registerProcessor = function(types,processor) {
2082   
2083 // You can pass an array of types that will trigger this processor, or just one.
2084 // We determine the array via duck-typing here.
2085   if (types.forEach) {
2086     types.forEach(function(type) {
2087       Content.processors[type] = processor;
2088     });
2089   } else {
2090     // If you didn't pass an array, we just use what you pass in.
2091     Content.processors[types] = processor;
2092   }
2093 };
2094
2095 // Register the identity processor, which is used for text-based media types.
2096 var identity = function(x) { return x; }
2097   , toString = function(x) { return x.toString(); }
2098 Content.registerProcessor(
2099   ["text/html","text/plain","text"],
2100   { parser: identity, stringify: toString });
2101
2102 // Register the JSON processor, which is used for JSON-based media types.
2103 Content.registerProcessor(
2104   ["application/json; charset=utf-8","application/json","json"],
2105   {
2106     parser: function(string) {
2107       return JSON.parse(string);
2108     },
2109     stringify: function(data) {
2110       return JSON.stringify(data); }});
2111
2112 // Error functions are defined separately here in an attempt to make the code
2113 // easier to read.
2114 var Errors = {
2115   setDataWithBody: function(object) {
2116     throw new Error("Attempt to set data attribute of a content object " +
2117         "when the body attributes was already set.");
2118   },
2119   setBodyWithData: function(object) {
2120     throw new Error("Attempt to set body attribute of a content object " +
2121         "when the data attributes was already set.");
2122   }
2123 }
2124 module.exports = Content;
2125
2126 });
2127
2128 require.define("/shred/mixins/headers.js", function (require, module, exports, __dirname, __filename) {
2129     // The header mixins allow you to add HTTP header support to any object. This
2130 // might seem pointless: why not simply use a hash? The main reason is that, per
2131 // the [HTTP spec](http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2),
2132 // headers are case-insensitive. So, for example, `content-type` is the same as
2133 // `CONTENT-TYPE` which is the same as `Content-Type`. Since there is no way to
2134 // overload the index operator in Javascript, using a hash to represent the
2135 // headers means it's possible to have two conflicting values for a single
2136 // header.
2137 // 
2138 // The solution to this is to provide explicit methods to set or get headers.
2139 // This also has the benefit of allowing us to introduce additional variations,
2140 // including snake case, which we automatically convert to what Matthew King has
2141 // dubbed "corset case" - the hyphen-separated names with initial caps:
2142 // `Content-Type`. We use corset-case just in case we're dealing with servers
2143 // that haven't properly implemented the spec.
2144
2145 // Convert headers to corset-case. **Example:** `CONTENT-TYPE` will be converted
2146 // to `Content-Type`.
2147
2148 var corsetCase = function(string) {
2149   return string;//.toLowerCase()
2150       //.replace("_","-")
2151       // .replace(/(^|-)(\w)/g, 
2152           // function(s) { return s.toUpperCase(); });
2153 };
2154
2155 // We suspect that `initializeHeaders` was once more complicated ...
2156 var initializeHeaders = function(object) {
2157   return {};
2158 };
2159
2160 // Access the `_headers` property using lazy initialization. **Warning:** If you
2161 // mix this into an object that is using the `_headers` property already, you're
2162 // going to have trouble.
2163 var $H = function(object) {
2164   return object._headers||(object._headers=initializeHeaders(object));
2165 };
2166
2167 // Hide the implementations as private functions, separate from how we expose them.
2168
2169 // The "real" `getHeader` function: get the header after normalizing the name.
2170 var getHeader = function(object,name) {
2171   return $H(object)[corsetCase(name)];
2172 };
2173
2174 // The "real" `getHeader` function: get one or more headers, or all of them
2175 // if you don't ask for any specifics. 
2176 var getHeaders = function(object,names) {
2177   var keys = (names && names.length>0) ? names : Object.keys($H(object));
2178   var hash = keys.reduce(function(hash,key) {
2179     hash[key] = getHeader(object,key);
2180     return hash;
2181   },{});
2182   // Freeze the resulting hash so you don't mistakenly think you're modifying
2183   // the real headers.
2184   Object.freeze(hash);
2185   return hash;
2186 };
2187
2188 // The "real" `setHeader` function: set a header, after normalizing the name.
2189 var setHeader = function(object,name,value) {
2190   $H(object)[corsetCase(name)] = value;
2191   return object;
2192 };
2193
2194 // The "real" `setHeaders` function: set multiple headers based on a hash.
2195 var setHeaders = function(object,hash) {
2196   for( var key in hash ) { setHeader(object,key,hash[key]); };
2197   return this;
2198 };
2199
2200 // Here's where we actually bind the functionality to an object. These mixins work by
2201 // exposing mixin functions. Each function mixes in a specific batch of features.
2202 module.exports = {
2203   
2204   // Add getters.
2205   getters: function(constructor) {
2206     constructor.prototype.getHeader = function(name) { return getHeader(this,name); };
2207     constructor.prototype.getHeaders = function() { return getHeaders(this,arguments); };
2208   },
2209   // Add setters but as "private" methods.
2210   privateSetters: function(constructor) {
2211     constructor.prototype._setHeader = function(key,value) { return setHeader(this,key,value); };
2212     constructor.prototype._setHeaders = function(hash) { return setHeaders(this,hash); };
2213   },
2214   // Add setters.
2215   setters: function(constructor) {
2216     constructor.prototype.setHeader = function(key,value) { return setHeader(this,key,value); };
2217     constructor.prototype.setHeaders = function(hash) { return setHeaders(this,hash); };
2218   },
2219   // Add both getters and setters.
2220   gettersAndSetters: function(constructor) {
2221     constructor.prototype.getHeader = function(name) { return getHeader(this,name); };
2222     constructor.prototype.getHeaders = function() { return getHeaders(this,arguments); };
2223     constructor.prototype.setHeader = function(key,value) { return setHeader(this,key,value); };
2224     constructor.prototype.setHeaders = function(hash) { return setHeaders(this,hash); };
2225   }
2226 };
2227
2228 });
2229
2230 require.define("/node_modules/iconv-lite/package.json", function (require, module, exports, __dirname, __filename) {
2231     module.exports = {}
2232 });
2233
2234 require.define("/node_modules/iconv-lite/index.js", function (require, module, exports, __dirname, __filename) {
2235     // Module exports
2236 var iconv = module.exports = {
2237     toEncoding: function(str, encoding) {
2238         return iconv.getCodec(encoding).toEncoding(str);
2239     },
2240     fromEncoding: function(buf, encoding) {
2241         return iconv.getCodec(encoding).fromEncoding(buf);
2242     },
2243     
2244     defaultCharUnicode: '�',
2245     defaultCharSingleByte: '?',
2246     
2247     // Get correct codec for given encoding.
2248     getCodec: function(encoding) {
2249         var enc = encoding || "utf8";
2250         var codecOptions = undefined;
2251         while (1) {
2252             if (getType(enc) === "String")
2253                 enc = enc.replace(/[- ]/g, "").toLowerCase();
2254             var codec = iconv.encodings[enc];
2255             var type = getType(codec);
2256             if (type === "String") {
2257                 // Link to other encoding.
2258                 codecOptions = {originalEncoding: enc};
2259                 enc = codec;
2260             }
2261             else if (type === "Object" && codec.type != undefined) {
2262                 // Options for other encoding.
2263                 codecOptions = codec;
2264                 enc = codec.type;
2265             } 
2266             else if (type === "Function")
2267                 // Codec itself.
2268                 return codec(codecOptions);
2269             else
2270                 throw new Error("Encoding not recognized: '" + encoding + "' (searched as: '"+enc+"')");
2271         }
2272     },
2273     
2274     // Define basic encodings
2275     encodings: {
2276         internal: function(options) {
2277             return {
2278                 toEncoding: function(str) {
2279                     return new Buffer(ensureString(str), options.originalEncoding);
2280                 },
2281                 fromEncoding: function(buf) {
2282                     return ensureBuffer(buf).toString(options.originalEncoding);
2283                 }
2284             };
2285         },
2286         utf8: "internal",
2287         ucs2: "internal",
2288         binary: "internal",
2289         ascii: "internal",
2290         base64: "internal",
2291         
2292         // Codepage single-byte encodings.
2293         singlebyte: function(options) {
2294             // Prepare chars if needed
2295             if (!options.chars || (options.chars.length !== 128 && options.chars.length !== 256))
2296                 throw new Error("Encoding '"+options.type+"' has incorrect 'chars' (must be of len 128 or 256)");
2297             
2298             if (options.chars.length === 128)
2299                 options.chars = asciiString + options.chars;
2300             
2301             if (!options.charsBuf) {
2302                 options.charsBuf = new Buffer(options.chars, 'ucs2');
2303             }
2304             
2305             if (!options.revCharsBuf) {
2306                 options.revCharsBuf = new Buffer(65536);
2307                 var defChar = iconv.defaultCharSingleByte.charCodeAt(0);
2308                 for (var i = 0; i < options.revCharsBuf.length; i++)
2309                     options.revCharsBuf[i] = defChar;
2310                 for (var i = 0; i < options.chars.length; i++)
2311                     options.revCharsBuf[options.chars.charCodeAt(i)] = i;
2312             }
2313             
2314             return {
2315                 toEncoding: function(str) {
2316                     str = ensureString(str);
2317                     
2318                     var buf = new Buffer(str.length);
2319                     var revCharsBuf = options.revCharsBuf;
2320                     for (var i = 0; i < str.length; i++)
2321                         buf[i] = revCharsBuf[str.charCodeAt(i)];
2322                     
2323                     return buf;
2324                 },
2325                 fromEncoding: function(buf) {
2326                     buf = ensureBuffer(buf);
2327                     
2328                     // Strings are immutable in JS -> we use ucs2 buffer to speed up computations.
2329                     var charsBuf = options.charsBuf;
2330                     var newBuf = new Buffer(buf.length*2);
2331                     var idx1 = 0, idx2 = 0;
2332                     for (var i = 0, _len = buf.length; i < _len; i++) {
2333                         idx1 = buf[i]*2; idx2 = i*2;
2334                         newBuf[idx2] = charsBuf[idx1];
2335                         newBuf[idx2+1] = charsBuf[idx1+1];
2336                     }
2337                     return newBuf.toString('ucs2');
2338                 }
2339             };
2340         },
2341
2342         // Codepage double-byte encodings.
2343         table: function(options) {
2344             var table = options.table, key, revCharsTable = options.revCharsTable;
2345             if (!table) {
2346                 throw new Error("Encoding '" + options.type +"' has incorect 'table' option");
2347             }
2348             if(!revCharsTable) {
2349                 revCharsTable = options.revCharsTable = {};
2350                 for (key in table) {
2351                     revCharsTable[table[key]] = parseInt(key);
2352                 }
2353             }
2354             
2355             return {
2356                 toEncoding: function(str) {
2357                     str = ensureString(str);
2358                     var strLen = str.length;
2359                     var bufLen = strLen;
2360                     for (var i = 0; i < strLen; i++)
2361                         if (str.charCodeAt(i) >> 7)
2362                             bufLen++;
2363
2364                     var newBuf = new Buffer(bufLen), gbkcode, unicode, 
2365                         defaultChar = revCharsTable[iconv.defaultCharUnicode.charCodeAt(0)];
2366
2367                     for (var i = 0, j = 0; i < strLen; i++) {
2368                         unicode = str.charCodeAt(i);
2369                         if (unicode >> 7) {
2370                             gbkcode = revCharsTable[unicode] || defaultChar;
2371                             newBuf[j++] = gbkcode >> 8; //high byte;
2372                             newBuf[j++] = gbkcode & 0xFF; //low byte
2373                         } else {//ascii
2374                             newBuf[j++] = unicode;
2375                         }
2376                     }
2377                     return newBuf;
2378                 },
2379                 fromEncoding: function(buf) {
2380                     buf = ensureBuffer(buf);
2381                     var bufLen = buf.length, strLen = 0;
2382                     for (var i = 0; i < bufLen; i++) {
2383                         strLen++;
2384                         if (buf[i] & 0x80) //the high bit is 1, so this byte is gbkcode's high byte.skip next byte
2385                             i++;
2386                     }
2387                     var newBuf = new Buffer(strLen*2), unicode, gbkcode,
2388                         defaultChar = iconv.defaultCharUnicode.charCodeAt(0);
2389                     
2390                     for (var i = 0, j = 0; i < bufLen; i++, j+=2) {
2391                         gbkcode = buf[i];
2392                         if (gbkcode & 0x80) {
2393                             gbkcode = (gbkcode << 8) + buf[++i];
2394                             unicode = table[gbkcode] || defaultChar;
2395                         } else {
2396                             unicode = gbkcode;
2397                         }
2398                         newBuf[j] = unicode & 0xFF; //low byte
2399                         newBuf[j+1] = unicode >> 8; //high byte
2400                     }
2401                     return newBuf.toString('ucs2');
2402                 }
2403             }
2404         }
2405     }
2406 };
2407
2408 // Add aliases to convert functions
2409 iconv.encode = iconv.toEncoding;
2410 iconv.decode = iconv.fromEncoding;
2411
2412 // Load other encodings from files in /encodings dir.
2413 var encodingsDir = __dirname+"/encodings/",
2414     fs = require('fs');
2415 fs.readdirSync(encodingsDir).forEach(function(file) {
2416     if(fs.statSync(encodingsDir + file).isDirectory()) return;
2417     var encodings = require(encodingsDir + file)
2418     for (var key in encodings)
2419         iconv.encodings[key] = encodings[key]
2420 });
2421
2422 // Utilities
2423 var asciiString = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'+
2424               ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f';
2425
2426 var ensureBuffer = function(buf) {
2427     buf = buf || new Buffer(0);
2428     return (buf instanceof Buffer) ? buf : new Buffer(buf.toString(), "utf8");
2429 }
2430
2431 var ensureString = function(str) {
2432     str = str || "";
2433     return (str instanceof String) ? str : str.toString((str instanceof Buffer) ? 'utf8' : undefined);
2434 }
2435
2436 var getType = function(obj) {
2437     return Object.prototype.toString.call(obj).slice(8, -1);
2438 }
2439
2440
2441 });
2442
2443 require.define("/node_modules/http-browserify/package.json", function (require, module, exports, __dirname, __filename) {
2444     module.exports = {"main":"index.js","browserify":"browser.js"}
2445 });
2446
2447 require.define("/node_modules/http-browserify/browser.js", function (require, module, exports, __dirname, __filename) {
2448     var http = module.exports;
2449 var EventEmitter = require('events').EventEmitter;
2450 var Request = require('./lib/request');
2451
2452 http.request = function (params, cb) {
2453     if (!params) params = {};
2454     if (!params.host) params.host = window.location.host.split(':')[0];
2455     if (!params.port) params.port = window.location.port;
2456     
2457     var req = new Request(new xhrHttp, params);
2458     if (cb) req.on('response', cb);
2459     return req;
2460 };
2461
2462 http.get = function (params, cb) {
2463     params.method = 'GET';
2464     var req = http.request(params, cb);
2465     req.end();
2466     return req;
2467 };
2468
2469 var xhrHttp = (function () {
2470     if (typeof window === 'undefined') {
2471         throw new Error('no window object present');
2472     }
2473     else if (window.XMLHttpRequest) {
2474         return window.XMLHttpRequest;
2475     }
2476     else if (window.ActiveXObject) {
2477         var axs = [
2478             'Msxml2.XMLHTTP.6.0',
2479             'Msxml2.XMLHTTP.3.0',
2480             'Microsoft.XMLHTTP'
2481         ];
2482         for (var i = 0; i < axs.length; i++) {
2483             try {
2484                 var ax = new(window.ActiveXObject)(axs[i]);
2485                 return function () {
2486                     if (ax) {
2487                         var ax_ = ax;
2488                         ax = null;
2489                         return ax_;
2490                     }
2491                     else {
2492                         return new(window.ActiveXObject)(axs[i]);
2493                     }
2494                 };
2495             }
2496             catch (e) {}
2497         }
2498         throw new Error('ajax not supported in this browser')
2499     }
2500     else {
2501         throw new Error('ajax not supported in this browser');
2502     }
2503 })();
2504
2505 http.STATUS_CODES = {
2506     100 : 'Continue',
2507     101 : 'Switching Protocols',
2508     102 : 'Processing', // RFC 2518, obsoleted by RFC 4918
2509     200 : 'OK',
2510     201 : 'Created',
2511     202 : 'Accepted',
2512     203 : 'Non-Authoritative Information',
2513     204 : 'No Content',
2514     205 : 'Reset Content',
2515     206 : 'Partial Content',
2516     207 : 'Multi-Status', // RFC 4918
2517     300 : 'Multiple Choices',
2518     301 : 'Moved Permanently',
2519     302 : 'Moved Temporarily',
2520     303 : 'See Other',
2521     304 : 'Not Modified',
2522     305 : 'Use Proxy',
2523     307 : 'Temporary Redirect',
2524     400 : 'Bad Request',
2525     401 : 'Unauthorized',
2526     402 : 'Payment Required',
2527     403 : 'Forbidden',
2528     404 : 'Not Found',
2529     405 : 'Method Not Allowed',
2530     406 : 'Not Acceptable',
2531     407 : 'Proxy Authentication Required',
2532     408 : 'Request Time-out',
2533     409 : 'Conflict',
2534     410 : 'Gone',
2535     411 : 'Length Required',
2536     412 : 'Precondition Failed',
2537     413 : 'Request Entity Too Large',
2538     414 : 'Request-URI Too Large',
2539     415 : 'Unsupported Media Type',
2540     416 : 'Requested Range Not Satisfiable',
2541     417 : 'Expectation Failed',
2542     418 : 'I\'m a teapot', // RFC 2324
2543     422 : 'Unprocessable Entity', // RFC 4918
2544     423 : 'Locked', // RFC 4918
2545     424 : 'Failed Dependency', // RFC 4918
2546     425 : 'Unordered Collection', // RFC 4918
2547     426 : 'Upgrade Required', // RFC 2817
2548     500 : 'Internal Server Error',
2549     501 : 'Not Implemented',
2550     502 : 'Bad Gateway',
2551     503 : 'Service Unavailable',
2552     504 : 'Gateway Time-out',
2553     505 : 'HTTP Version not supported',
2554     506 : 'Variant Also Negotiates', // RFC 2295
2555     507 : 'Insufficient Storage', // RFC 4918
2556     509 : 'Bandwidth Limit Exceeded',
2557     510 : 'Not Extended' // RFC 2774
2558 };
2559
2560 });
2561
2562 require.define("/node_modules/http-browserify/lib/request.js", function (require, module, exports, __dirname, __filename) {
2563     var EventEmitter = require('events').EventEmitter;
2564 var Response = require('./response');
2565 var isSafeHeader = require('./isSafeHeader');
2566
2567 var Request = module.exports = function (xhr, params) {
2568     var self = this;
2569     self.xhr = xhr;
2570     self.body = '';
2571     
2572     var uri = params.host + ':' + params.port + (params.path || '/');
2573     
2574     xhr.open(
2575         params.method || 'GET',
2576         (params.scheme || 'http') + '://' + uri,
2577         true
2578     );
2579     
2580     if (params.headers) {
2581         Object.keys(params.headers).forEach(function (key) {
2582             if (!isSafeHeader(key)) return;
2583             var value = params.headers[key];
2584             if (Array.isArray(value)) {
2585                 value.forEach(function (v) {
2586                     xhr.setRequestHeader(key, v);
2587                 });
2588             }
2589             else xhr.setRequestHeader(key, value)
2590         });
2591     }
2592     
2593     var res = new Response(xhr);
2594     res.on('ready', function () {
2595         self.emit('response', res);
2596     });
2597     
2598     xhr.onreadystatechange = function () {
2599         res.handle(xhr);
2600     };
2601 };
2602
2603 Request.prototype = new EventEmitter;
2604
2605 Request.prototype.setHeader = function (key, value) {
2606     if ((Array.isArray && Array.isArray(value))
2607     || value instanceof Array) {
2608         for (var i = 0; i < value.length; i++) {
2609             this.xhr.setRequestHeader(key, value[i]);
2610         }
2611     }
2612     else {
2613         this.xhr.setRequestHeader(key, value);
2614     }
2615 };
2616
2617 Request.prototype.write = function (s) {
2618     this.body += s;
2619 };
2620
2621 Request.prototype.end = function (s) {
2622     if (s !== undefined) this.write(s);
2623     this.xhr.send(this.body);
2624 };
2625
2626 });
2627
2628 require.define("/node_modules/http-browserify/lib/response.js", function (require, module, exports, __dirname, __filename) {
2629     var EventEmitter = require('events').EventEmitter;
2630 var isSafeHeader = require('./isSafeHeader');
2631
2632 var Response = module.exports = function (xhr) {
2633     this.xhr = xhr;
2634     this.offset = 0;
2635 };
2636
2637 Response.prototype = new EventEmitter;
2638
2639 var capable = {
2640     streaming : true,
2641     status2 : true
2642 };
2643
2644 function parseHeaders (xhr) {
2645     var lines = xhr.getAllResponseHeaders().split(/\r?\n/);
2646     var headers = {};
2647     for (var i = 0; i < lines.length; i++) {
2648         var line = lines[i];
2649         if (line === '') continue;
2650         
2651         var m = line.match(/^([^:]+):\s*(.*)/);
2652         if (m) {
2653             var key = m[1].toLowerCase(), value = m[2];
2654             
2655             if (headers[key] !== undefined) {
2656                 if ((Array.isArray && Array.isArray(headers[key]))
2657                 || headers[key] instanceof Array) {
2658                     headers[key].push(value);
2659                 }
2660                 else {
2661                     headers[key] = [ headers[key], value ];
2662                 }
2663             }
2664             else {
2665                 headers[key] = value;
2666             }
2667         }
2668         else {
2669             headers[line] = true;
2670         }
2671     }
2672     return headers;
2673 }
2674
2675 Response.prototype.getHeader = function (key) {
2676     var header = this.headers ? this.headers[key.toLowerCase()] : null;
2677     if (header) return header;
2678
2679     // Work around Mozilla bug #608735 [https://bugzil.la/608735], which causes
2680     // getAllResponseHeaders() to return {} if the response is a CORS request.
2681     // xhr.getHeader still works correctly.
2682     if (isSafeHeader(key)) {
2683       return this.xhr.getResponseHeader(key);
2684     }
2685     return null;
2686 };
2687
2688 Response.prototype.handle = function () {
2689     var xhr = this.xhr;
2690     if (xhr.readyState === 2 && capable.status2) {
2691         try {
2692             this.statusCode = xhr.status;
2693             this.headers = parseHeaders(xhr);
2694         }
2695         catch (err) {
2696             capable.status2 = false;
2697         }
2698         
2699         if (capable.status2) {
2700             this.emit('ready');
2701         }
2702     }
2703     else if (capable.streaming && xhr.readyState === 3) {
2704         try {
2705             if (!this.statusCode) {
2706                 this.statusCode = xhr.status;
2707                 this.headers = parseHeaders(xhr);
2708                 this.emit('ready');
2709             }
2710         }
2711         catch (err) {}
2712         
2713         try {
2714             this.write();
2715         }
2716         catch (err) {
2717             capable.streaming = false;
2718         }
2719     }
2720     else if (xhr.readyState === 4) {
2721         if (!this.statusCode) {
2722             this.statusCode = xhr.status;
2723             this.emit('ready');
2724         }
2725         this.write();
2726         
2727         if (xhr.error) {
2728             this.emit('error', xhr.responseText);
2729         }
2730         else this.emit('end');
2731     }
2732 };
2733
2734 Response.prototype.write = function () {
2735     var xhr = this.xhr;
2736     if (xhr.responseText.length > this.offset) {
2737         this.emit('data', xhr.responseText.slice(this.offset));
2738         this.offset = xhr.responseText.length;
2739     }
2740 };
2741
2742 });
2743
2744 require.define("/node_modules/http-browserify/lib/isSafeHeader.js", function (require, module, exports, __dirname, __filename) {
2745     // Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html
2746 var unsafeHeaders = [
2747     "accept-charset",
2748     "accept-encoding",
2749     "access-control-request-headers",
2750     "access-control-request-method",
2751     "connection",
2752     "content-length",
2753     "cookie",
2754     "cookie2",
2755     "content-transfer-encoding",
2756     "date",
2757     "expect",
2758     "host",
2759     "keep-alive",
2760     "origin",
2761     "referer",
2762     "set-cookie",
2763     "te",
2764     "trailer",
2765     "transfer-encoding",
2766     "upgrade",
2767     "user-agent",
2768     "via"
2769 ];
2770
2771 module.exports = function (headerName) {
2772     if (!headerName) return false;
2773     return (unsafeHeaders.indexOf(headerName.toLowerCase()) === -1)
2774 };
2775
2776 });
2777
2778 require.alias("http-browserify", "/node_modules/http");
2779
2780 require.alias("http-browserify", "/node_modules/https");