vulcan 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vulcan/cli.rb +20 -10
- data/lib/vulcan/version.rb +1 -1
- data/server/Procfile +1 -1
- data/server/bin/web +10 -0
- data/server/lib/cloudant.coffee +20 -0
- data/server/lib/logger.coffee +17 -0
- data/server/lib/on.coffee +10 -0
- data/server/lib/spawner.coffee +65 -0
- data/server/node_modules/coffee-script/CNAME +1 -0
- data/server/node_modules/coffee-script/LICENSE +22 -0
- data/server/node_modules/coffee-script/README +51 -0
- data/server/node_modules/coffee-script/Rakefile +78 -0
- data/server/node_modules/coffee-script/bin/cake +7 -0
- data/server/node_modules/coffee-script/bin/coffee +7 -0
- data/server/node_modules/coffee-script/extras/jsl.conf +44 -0
- data/server/node_modules/coffee-script/lib/coffee-script/browser.js +92 -0
- data/server/node_modules/coffee-script/lib/coffee-script/cake.js +111 -0
- data/server/node_modules/coffee-script/lib/coffee-script/coffee-script.js +167 -0
- data/server/node_modules/coffee-script/lib/coffee-script/command.js +500 -0
- data/server/node_modules/coffee-script/lib/coffee-script/grammar.js +606 -0
- data/server/node_modules/coffee-script/lib/coffee-script/helpers.js +77 -0
- data/server/node_modules/coffee-script/lib/coffee-script/index.js +11 -0
- data/server/node_modules/coffee-script/lib/coffee-script/lexer.js +788 -0
- data/server/node_modules/coffee-script/lib/coffee-script/nodes.js +2986 -0
- data/server/node_modules/coffee-script/lib/coffee-script/optparse.js +138 -0
- data/server/node_modules/coffee-script/lib/coffee-script/parser.js +683 -0
- data/server/node_modules/coffee-script/lib/coffee-script/repl.js +261 -0
- data/server/node_modules/coffee-script/lib/coffee-script/rewriter.js +349 -0
- data/server/node_modules/coffee-script/lib/coffee-script/scope.js +146 -0
- data/server/node_modules/coffee-script/package.json +55 -0
- data/server/node_modules/connect-form/History.md +0 -6
- data/server/node_modules/connect-form/lib/connect-form.js +2 -4
- data/server/node_modules/connect-form/node_modules/formidable/Readme.md +64 -36
- data/server/node_modules/connect-form/node_modules/formidable/lib/incoming_form.js +5 -1
- data/server/node_modules/connect-form/node_modules/formidable/package.json +20 -6
- data/server/node_modules/connect-form/node_modules/formidable/test/common.js +5 -6
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/file/funkyfilename.txt +1 -0
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/js/special-chars-in-filename.js +1 -1
- data/server/node_modules/connect-form/node_modules/formidable/test/{slow → integration}/test-fixtures.js +38 -33
- data/server/node_modules/connect-form/node_modules/formidable/test/legacy/simple/test-incoming-form.js +11 -0
- data/server/node_modules/connect-form/node_modules/formidable/test/run.js +1 -6
- data/server/node_modules/connect-form/node_modules/formidable/test/unit/test-incoming-form.js +63 -0
- data/server/node_modules/connect-form/package.json +27 -5
- data/server/node_modules/cradle/README.md +10 -10
- data/server/node_modules/cradle/lib/cradle.js +117 -523
- data/server/node_modules/cradle/lib/cradle/database/attachments.js +120 -0
- data/server/node_modules/cradle/lib/cradle/database/changes.js +56 -0
- data/server/node_modules/cradle/lib/cradle/database/documents.js +215 -0
- data/server/node_modules/cradle/lib/cradle/database/index.js +65 -0
- data/server/node_modules/cradle/lib/cradle/database/views.js +125 -0
- data/server/node_modules/cradle/node_modules/follow/LICENSE +202 -0
- data/server/node_modules/cradle/node_modules/follow/README.md +164 -0
- data/server/node_modules/cradle/node_modules/follow/Rakefile +54 -0
- data/server/node_modules/cradle/node_modules/follow/api.js +35 -0
- data/server/node_modules/cradle/node_modules/follow/browser/eventemitter2.js +453 -0
- data/server/node_modules/cradle/node_modules/follow/browser/export.js +78 -0
- data/server/node_modules/cradle/node_modules/follow/browser/index.html +14 -0
- data/server/node_modules/cradle/node_modules/follow/browser/jquery-1.6.1.min.js +18 -0
- data/server/node_modules/cradle/node_modules/follow/browser/log4js.js +46 -0
- data/server/node_modules/cradle/node_modules/follow/browser/main.js +92 -0
- data/server/node_modules/cradle/node_modules/follow/browser/querystring.js +28 -0
- data/server/node_modules/cradle/node_modules/follow/browser/request.jquery.js +237 -0
- data/server/node_modules/cradle/node_modules/follow/browser/require.js +33 -0
- data/server/node_modules/cradle/node_modules/follow/browser/util.js +28 -0
- data/server/node_modules/cradle/node_modules/follow/cli.js +101 -0
- data/server/node_modules/cradle/node_modules/follow/lib/feed.js +556 -0
- data/server/node_modules/cradle/node_modules/follow/lib/index.js +66 -0
- data/server/node_modules/cradle/node_modules/follow/lib/stream.js +305 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/LICENSE +55 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/README.md +285 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/main.js +618 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/mimetypes.js +146 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/oauth.js +34 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/package.json +42 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/googledoodle.png +0 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/run.sh +6 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/server.js +57 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-body.js +90 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-cookie.js +29 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-cookiejar.js +90 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-errors.js +30 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-oauth.js +109 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-pipes.js +167 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-proxy.js +39 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/tests/test-timeout.js +87 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/uuid.js +19 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/vendor/cookie/index.js +55 -0
- data/server/node_modules/cradle/node_modules/follow/node_modules/request/vendor/cookie/jar.js +72 -0
- data/server/node_modules/cradle/node_modules/follow/package.json +45 -0
- data/server/node_modules/cradle/node_modules/follow/test/couch.js +153 -0
- data/server/node_modules/cradle/node_modules/follow/test/follow.js +136 -0
- data/server/node_modules/cradle/node_modules/follow/test/issues.js +178 -0
- data/server/node_modules/cradle/node_modules/follow/test/issues/10.js +24 -0
- data/server/node_modules/cradle/node_modules/follow/test/stream.js +493 -0
- data/server/node_modules/cradle/node_modules/request/LICENSE +55 -0
- data/server/node_modules/cradle/node_modules/request/README.md +287 -0
- data/server/node_modules/cradle/node_modules/request/forever.js +103 -0
- data/server/node_modules/cradle/node_modules/request/main.js +913 -0
- data/server/node_modules/cradle/node_modules/request/mimetypes.js +152 -0
- data/server/node_modules/cradle/node_modules/request/oauth.js +34 -0
- data/server/node_modules/cradle/node_modules/request/package.json +42 -0
- data/server/node_modules/cradle/node_modules/request/tests/googledoodle.png +0 -0
- data/server/node_modules/cradle/node_modules/request/tests/run.js +38 -0
- data/server/node_modules/cradle/node_modules/request/tests/server.js +82 -0
- data/server/node_modules/cradle/node_modules/request/tests/squid.conf +77 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.cnf +20 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.crl +0 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.crt +17 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.csr +13 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.key +18 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/ca.srl +1 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.cnf +19 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.crt +16 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.csr +11 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.js +28 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/ca/server.key +9 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/npm-ca.crt +16 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/test.crt +15 -0
- data/server/node_modules/cradle/node_modules/request/tests/ssl/test.key +15 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-body.js +95 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-cookie.js +29 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-cookiejar.js +90 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-defaults.js +68 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-errors.js +37 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-headers.js +52 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-httpModule.js +94 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-https-strict.js +97 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-https.js +86 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-oauth.js +117 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-params.js +92 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-pipes.js +202 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-proxy.js +39 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-qs.js +28 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-redirect.js +154 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-timeout.js +87 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-toJSON.js +14 -0
- data/server/node_modules/cradle/node_modules/request/tests/test-tunnel.js +61 -0
- data/server/node_modules/cradle/node_modules/request/tunnel.js +229 -0
- data/server/node_modules/cradle/node_modules/request/uuid.js +19 -0
- data/server/node_modules/cradle/node_modules/request/vendor/cookie/index.js +65 -0
- data/server/node_modules/cradle/node_modules/request/vendor/cookie/jar.js +72 -0
- data/server/node_modules/cradle/node_modules/vargs/package.json +33 -10
- data/server/node_modules/cradle/package.json +50 -12
- data/server/node_modules/cradle/test/cache-test.js +1 -4
- data/server/node_modules/cradle/test/connection-test.js +179 -0
- data/server/node_modules/cradle/test/database-attachment-test.js +344 -0
- data/server/node_modules/cradle/test/database-cache-test.js +132 -0
- data/server/node_modules/cradle/test/database-test.js +219 -0
- data/server/node_modules/cradle/test/database-view-test.js +141 -0
- data/server/node_modules/cradle/test/fixtures/databases.json +28 -1
- data/server/node_modules/cradle/test/helpers/seed.js +14 -5
- data/server/node_modules/cradle/test/response-test.js +1 -1
- data/server/node_modules/express/History.md +16 -0
- data/server/node_modules/express/bin/express +7 -6
- data/server/node_modules/express/lib-cov/application.js +510 -0
- data/server/node_modules/express/lib-cov/express.js +65 -0
- data/server/node_modules/express/lib-cov/middleware.js +54 -0
- data/server/node_modules/express/lib-cov/request.js +225 -0
- data/server/node_modules/express/lib-cov/response.js +611 -0
- data/server/node_modules/express/lib-cov/router/collection.js +40 -0
- data/server/node_modules/express/lib-cov/router/index.js +515 -0
- data/server/node_modules/express/lib-cov/router/methods.js +9 -0
- data/server/node_modules/express/lib-cov/router/route.js +68 -0
- data/server/node_modules/express/lib-cov/utils.js +151 -0
- data/server/node_modules/express/lib-cov/view.js +81 -0
- data/server/node_modules/express/lib/express.js +1 -1
- data/server/node_modules/express/lib/http.js +1 -2
- data/server/node_modules/express/lib/request.js +2 -2
- data/server/node_modules/express/lib/router/methods.js +10 -1
- data/server/node_modules/express/node_modules/connect/lib/connect.js +1 -1
- data/server/node_modules/express/node_modules/connect/lib/http.js +3 -2
- data/server/node_modules/express/node_modules/connect/lib/middleware/limit.js +0 -2
- data/server/node_modules/express/node_modules/connect/lib/middleware/session.js +1 -2
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/Readme.md +42 -25
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/lib/incoming_form.js +1 -0
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/package.json +14 -3
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/legacy/simple/test-incoming-form.js +11 -0
- data/server/node_modules/express/node_modules/connect/package.json +35 -7
- data/server/node_modules/express/node_modules/mime/package.json +30 -8
- data/server/node_modules/express/node_modules/mkdirp/README.markdown +35 -2
- data/server/node_modules/express/node_modules/mkdirp/examples/pow.js +1 -1
- data/server/node_modules/express/node_modules/mkdirp/index.js +72 -13
- data/server/node_modules/express/node_modules/mkdirp/package.json +39 -21
- data/server/node_modules/express/node_modules/mkdirp/test/chmod.js +38 -0
- data/server/node_modules/express/node_modules/mkdirp/test/clobber.js +37 -0
- data/server/node_modules/express/node_modules/mkdirp/test/perm.js +32 -0
- data/server/node_modules/express/node_modules/mkdirp/test/perm_sync.js +39 -0
- data/server/node_modules/express/node_modules/mkdirp/test/sync.js +27 -0
- data/server/node_modules/express/node_modules/mkdirp/test/umask.js +28 -0
- data/server/node_modules/express/node_modules/mkdirp/test/umask_sync.js +27 -0
- data/server/node_modules/express/node_modules/qs/History.md +10 -0
- data/server/node_modules/express/node_modules/qs/Readme.md +9 -2
- data/server/node_modules/express/node_modules/qs/examples.js +3 -0
- data/server/node_modules/express/node_modules/qs/lib/querystring.js +8 -6
- data/server/node_modules/express/node_modules/qs/package.json +26 -8
- data/server/node_modules/express/node_modules/qs/test/parse.js +13 -1
- data/server/node_modules/express/node_modules/qs/test/stringify.js +45 -37
- data/server/node_modules/express/package.json +55 -16
- data/server/node_modules/express/test.js +41 -0
- data/server/node_modules/knox/package.json +26 -4
- data/server/node_modules/node-uuid/package.json +40 -11
- data/server/node_modules/node-uuid/test/test.js +1 -1
- data/server/node_modules/nodemon/README.md +120 -0
- data/server/node_modules/nodemon/nodemon.js +518 -0
- data/server/node_modules/nodemon/nodemonignore.example +11 -0
- data/server/node_modules/nodemon/package.json +49 -0
- data/server/node_modules/restler/README.md +144 -94
- data/server/node_modules/restler/lib/multipartform.js +2 -0
- data/server/node_modules/restler/lib/restler.js +218 -61
- data/server/node_modules/restler/package.json +35 -8
- data/server/node_modules/restler/test/all.js +6 -1
- data/server/node_modules/restler/test/restler.js +624 -118
- data/server/package.json +14 -10
- data/server/web.coffee +64 -0
- data/server/web.js +15 -3
- metadata +170 -57
- data/server/index.js +0 -14
- data/server/node_modules/connect-form/LICENSE +0 -22
- data/server/node_modules/connect-form/index.js +0 -100
- data/server/node_modules/connect-form/node_modules/formidable/test/fast/test-incoming-form.js +0 -45
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/no-filename/generic.http +0 -13
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-chrome-13.http +0 -26
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-firefox-3.6.http +0 -24
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-safari-5.http +0 -23
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-chrome-12.http +0 -24
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-7.http +0 -22
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-8.http +0 -22
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-safari-5.http +0 -22
- data/server/node_modules/connect-form/node_modules/formidable/test/fixture/multi_video.upload +0 -0
- data/server/node_modules/cradle/Makefile +0 -10
- data/server/node_modules/cradle/test/cradle-test.js +0 -650
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/no-filename/generic.http +0 -13
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-chrome-13.http +0 -26
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-firefox-3.6.http +0 -24
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/osx-safari-5.http +0 -23
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-chrome-12.http +0 -24
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-7.http +0 -22
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-ie-8.http +0 -22
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/http/special-chars-in-filename/xp-safari-5.http +0 -22
- data/server/node_modules/express/node_modules/connect/node_modules/formidable/test/fixture/multi_video.upload +0 -0
- data/server/node_modules/express/node_modules/connect/test.js +0 -52
- data/server/node_modules/express/testing/foo/app.js +0 -35
- data/server/node_modules/express/testing/foo/package.json +0 -9
- data/server/node_modules/express/testing/foo/public/stylesheets/style.css +0 -8
- data/server/node_modules/express/testing/foo/routes/index.js +0 -10
- data/server/node_modules/express/testing/foo/views/index.jade +0 -2
- data/server/node_modules/express/testing/foo/views/layout.jade +0 -6
- data/server/node_modules/express/testing/index.js +0 -43
- data/server/node_modules/express/testing/public/test.txt +0 -2971
- data/server/node_modules/express/testing/views/page.html +0 -1
- data/server/node_modules/express/testing/views/page.jade +0 -3
- data/server/node_modules/express/testing/views/test.md +0 -1
- data/server/node_modules/express/testing/views/user/index.jade +0 -1
- data/server/node_modules/express/testing/views/user/list.jade +0 -1
- data/server/node_modules/knox/lib/knox/mime/index.js +0 -308
- data/server/node_modules/knox/lib/knox/mime/test.js +0 -59
- data/server/node_modules/node-uuid/test/benchmark-native +0 -0
- data/server/node_modules/on/index.js +0 -13
- data/server/node_modules/restler/test/test_helper.js +0 -163
- data/server/node_modules/spawner/index.js +0 -106
@@ -0,0 +1,55 @@
|
|
1
|
+
Apache License
|
2
|
+
|
3
|
+
Version 2.0, January 2004
|
4
|
+
|
5
|
+
http://www.apache.org/licenses/
|
6
|
+
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
|
+
|
9
|
+
1. Definitions.
|
10
|
+
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
16
|
+
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
18
|
+
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
20
|
+
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
22
|
+
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
24
|
+
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
26
|
+
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
28
|
+
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
30
|
+
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
32
|
+
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
34
|
+
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
36
|
+
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
38
|
+
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
40
|
+
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
42
|
+
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
44
|
+
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
46
|
+
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
48
|
+
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
50
|
+
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
52
|
+
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
54
|
+
|
55
|
+
END OF TERMS AND CONDITIONS
|
@@ -0,0 +1,287 @@
|
|
1
|
+
# Request -- Simplified HTTP request method
|
2
|
+
|
3
|
+
## Install
|
4
|
+
|
5
|
+
<pre>
|
6
|
+
npm install request
|
7
|
+
</pre>
|
8
|
+
|
9
|
+
Or from source:
|
10
|
+
|
11
|
+
<pre>
|
12
|
+
git clone git://github.com/mikeal/request.git
|
13
|
+
cd request
|
14
|
+
npm link
|
15
|
+
</pre>
|
16
|
+
|
17
|
+
## Super simple to use
|
18
|
+
|
19
|
+
Request is designed to be the simplest way possible to make http calls. It support HTTPS and follows redirects by default.
|
20
|
+
|
21
|
+
```javascript
|
22
|
+
var request = require('request');
|
23
|
+
request('http://www.google.com', function (error, response, body) {
|
24
|
+
if (!error && response.statusCode == 200) {
|
25
|
+
console.log(body) // Print the google web page.
|
26
|
+
}
|
27
|
+
})
|
28
|
+
```
|
29
|
+
|
30
|
+
## Streaming
|
31
|
+
|
32
|
+
You can stream any response to a file stream.
|
33
|
+
|
34
|
+
```javascript
|
35
|
+
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
|
36
|
+
```
|
37
|
+
|
38
|
+
You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types, in this case `application/json`, and use the proper content-type in the PUT request if one is not already provided in the headers.
|
39
|
+
|
40
|
+
```javascript
|
41
|
+
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
|
42
|
+
```
|
43
|
+
|
44
|
+
Request can also pipe to itself. When doing so the content-type and content-length will be preserved in the PUT headers.
|
45
|
+
|
46
|
+
```javascript
|
47
|
+
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
|
48
|
+
```
|
49
|
+
|
50
|
+
Now let's get fancy.
|
51
|
+
|
52
|
+
```javascript
|
53
|
+
http.createServer(function (req, resp) {
|
54
|
+
if (req.url === '/doodle.png') {
|
55
|
+
if (req.method === 'PUT') {
|
56
|
+
req.pipe(request.put('http://mysite.com/doodle.png'))
|
57
|
+
} else if (req.method === 'GET' || req.method === 'HEAD') {
|
58
|
+
request.get('http://mysite.com/doodle.png').pipe(resp)
|
59
|
+
}
|
60
|
+
}
|
61
|
+
})
|
62
|
+
```
|
63
|
+
|
64
|
+
You can also pipe() from a http.ServerRequest instance and to a http.ServerResponse instance. The HTTP method and headers will be sent as well as the entity-body data. Which means that, if you don't really care about security, you can do:
|
65
|
+
|
66
|
+
```javascript
|
67
|
+
http.createServer(function (req, resp) {
|
68
|
+
if (req.url === '/doodle.png') {
|
69
|
+
var x = request('http://mysite.com/doodle.png')
|
70
|
+
req.pipe(x)
|
71
|
+
x.pipe(resp)
|
72
|
+
}
|
73
|
+
})
|
74
|
+
```
|
75
|
+
|
76
|
+
And since pipe() returns the destination stream in node 0.5.x you can do one line proxying :)
|
77
|
+
|
78
|
+
```javascript
|
79
|
+
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
|
80
|
+
```
|
81
|
+
|
82
|
+
Also, none of this new functionality conflicts with requests previous features, it just expands them.
|
83
|
+
|
84
|
+
```javascript
|
85
|
+
var r = request.defaults({'proxy':'http://localproxy.com'})
|
86
|
+
|
87
|
+
http.createServer(function (req, resp) {
|
88
|
+
if (req.url === '/doodle.png') {
|
89
|
+
r.get('http://google.com/doodle.png').pipe(resp)
|
90
|
+
}
|
91
|
+
})
|
92
|
+
```
|
93
|
+
|
94
|
+
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.
|
95
|
+
|
96
|
+
## OAuth Signing
|
97
|
+
|
98
|
+
```javascript
|
99
|
+
// Twitter OAuth
|
100
|
+
var qs = require('querystring')
|
101
|
+
, oauth =
|
102
|
+
{ callback: 'http://mysite.com/callback/'
|
103
|
+
, consumer_key: CONSUMER_KEY
|
104
|
+
, consumer_secret: CONSUMER_SECRET
|
105
|
+
}
|
106
|
+
, url = 'https://api.twitter.com/oauth/request_token'
|
107
|
+
;
|
108
|
+
request.post({url:url, oauth:oauth}, function (e, r, body) {
|
109
|
+
// Assume by some stretch of magic you aquired the verifier
|
110
|
+
var access_token = qs.parse(body)
|
111
|
+
, oauth =
|
112
|
+
{ consumer_key: CONSUMER_KEY
|
113
|
+
, consumer_secret: CONSUMER_SECRET
|
114
|
+
, token: access_token.oauth_token
|
115
|
+
, verifier: VERIFIER
|
116
|
+
, token_secret: access_token.oauth_token_secret
|
117
|
+
}
|
118
|
+
, url = 'https://api.twitter.com/oauth/access_token'
|
119
|
+
;
|
120
|
+
request.post({url:url, oauth:oauth}, function (e, r, body) {
|
121
|
+
var perm_token = qs.parse(body)
|
122
|
+
, oauth =
|
123
|
+
{ consumer_key: CONSUMER_KEY
|
124
|
+
, consumer_secret: CONSUMER_SECRET
|
125
|
+
, token: perm_token.oauth_token
|
126
|
+
, token_secret: perm_token.oauth_token_secret
|
127
|
+
}
|
128
|
+
, url = 'https://api.twitter.com/1/users/show.json?'
|
129
|
+
, params =
|
130
|
+
{ screen_name: perm_token.screen_name
|
131
|
+
, user_id: perm_token.user_id
|
132
|
+
}
|
133
|
+
;
|
134
|
+
url += qs.stringify(params)
|
135
|
+
request.get({url:url, oauth:oauth, json:true}, function (e, r, user) {
|
136
|
+
console.log(user)
|
137
|
+
})
|
138
|
+
})
|
139
|
+
})
|
140
|
+
```
|
141
|
+
|
142
|
+
|
143
|
+
|
144
|
+
### request(options, callback)
|
145
|
+
|
146
|
+
The first argument can be either a url or an options object. The only required option is uri, all others are optional.
|
147
|
+
|
148
|
+
* `uri` || `url` - fully qualified uri or a parsed url object from url.parse()
|
149
|
+
* `qs` - object containing querystring values to be appended to the uri
|
150
|
+
* `method` - http method, defaults to GET
|
151
|
+
* `headers` - http headers, defaults to {}
|
152
|
+
* `body` - entity body for POST and PUT requests. Must be buffer or string.
|
153
|
+
* `form` - sets `body` but to querystring representation of value and adds `Content-type: application/x-www-form-urlencoded; charset=utf-8` header.
|
154
|
+
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header.
|
155
|
+
* `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
|
156
|
+
* `followRedirect` - follow HTTP 3xx responses as redirects. defaults to true.
|
157
|
+
* `followAllRedirects` - follow non-GET HTTP 3xx responses as redirects. defaults to false.
|
158
|
+
* `maxRedirects` - the maximum number of redirects to follow, defaults to 10.
|
159
|
+
* `encoding` - Encoding to be used on `setEncoding` of response data. If set to `null`, the body is returned as a Buffer.
|
160
|
+
* `pool` - A hash object containing the agents for these requests. If omitted this request will use the global pool which is set to node's default maxSockets.
|
161
|
+
* `pool.maxSockets` - Integer containing the maximum amount of sockets in the pool.
|
162
|
+
* `timeout` - Integer containing the number of milliseconds to wait for a request to respond before aborting the request
|
163
|
+
* `proxy` - An HTTP proxy to be used. Support proxy Auth with Basic Auth the same way it's supported with the `url` parameter by embedding the auth info in the uri.
|
164
|
+
* `oauth` - Options for OAuth HMAC-SHA1 signing, see documentation above.
|
165
|
+
* `strictSSL` - Set to `true` to require that SSL certificates be valid. Note: to use your own certificate authority, you need to specify an agent that was created with that ca as an option.
|
166
|
+
* `jar` - Set to `false` if you don't want cookies to be remembered for future use or define your custom cookie jar (see examples section)
|
167
|
+
|
168
|
+
|
169
|
+
The callback argument gets 3 arguments. The first is an error when applicable (usually from the http.Client option not the http.ClientRequest object). The second in an http.ClientResponse object. The third is the response body String or Buffer.
|
170
|
+
|
171
|
+
## Convenience methods
|
172
|
+
|
173
|
+
There are also shorthand methods for different HTTP METHODs and some other conveniences.
|
174
|
+
|
175
|
+
### request.defaults(options)
|
176
|
+
|
177
|
+
This method returns a wrapper around the normal request API that defaults to whatever options you pass in to it.
|
178
|
+
|
179
|
+
### request.put
|
180
|
+
|
181
|
+
Same as request() but defaults to `method: "PUT"`.
|
182
|
+
|
183
|
+
```javascript
|
184
|
+
request.put(url)
|
185
|
+
```
|
186
|
+
|
187
|
+
### request.post
|
188
|
+
|
189
|
+
Same as request() but defaults to `method: "POST"`.
|
190
|
+
|
191
|
+
```javascript
|
192
|
+
request.post(url)
|
193
|
+
```
|
194
|
+
|
195
|
+
### request.head
|
196
|
+
|
197
|
+
Same as request() but defaults to `method: "HEAD"`.
|
198
|
+
|
199
|
+
```javascript
|
200
|
+
request.head(url)
|
201
|
+
```
|
202
|
+
|
203
|
+
### request.del
|
204
|
+
|
205
|
+
Same as request() but defaults to `method: "DELETE"`.
|
206
|
+
|
207
|
+
```javascript
|
208
|
+
request.del(url)
|
209
|
+
```
|
210
|
+
|
211
|
+
### request.get
|
212
|
+
|
213
|
+
Alias to normal request method for uniformity.
|
214
|
+
|
215
|
+
```javascript
|
216
|
+
request.get(url)
|
217
|
+
```
|
218
|
+
### request.cookie
|
219
|
+
|
220
|
+
Function that creates a new cookie.
|
221
|
+
|
222
|
+
```javascript
|
223
|
+
request.cookie('cookie_string_here')
|
224
|
+
```
|
225
|
+
### request.jar
|
226
|
+
|
227
|
+
Function that creates a new cookie jar.
|
228
|
+
|
229
|
+
```javascript
|
230
|
+
request.jar()
|
231
|
+
```
|
232
|
+
|
233
|
+
|
234
|
+
## Examples:
|
235
|
+
|
236
|
+
```javascript
|
237
|
+
var request = require('request')
|
238
|
+
, rand = Math.floor(Math.random()*100000000).toString()
|
239
|
+
;
|
240
|
+
request(
|
241
|
+
{ method: 'PUT'
|
242
|
+
, uri: 'http://mikeal.iriscouch.com/testjs/' + rand
|
243
|
+
, multipart:
|
244
|
+
[ { 'content-type': 'application/json'
|
245
|
+
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
|
246
|
+
}
|
247
|
+
, { body: 'I am an attachment' }
|
248
|
+
]
|
249
|
+
}
|
250
|
+
, function (error, response, body) {
|
251
|
+
if(response.statusCode == 201){
|
252
|
+
console.log('document saved as: http://mikeal.iriscouch.com/testjs/'+ rand)
|
253
|
+
} else {
|
254
|
+
console.log('error: '+ response.statusCode)
|
255
|
+
console.log(body)
|
256
|
+
}
|
257
|
+
}
|
258
|
+
)
|
259
|
+
```
|
260
|
+
Cookies are enabled by default (so they can be used in subsequent requests). To disable cookies set jar to false (either in defaults or in the options sent).
|
261
|
+
|
262
|
+
```javascript
|
263
|
+
var request = request.defaults({jar: false})
|
264
|
+
request('http://www.google.com', function () {
|
265
|
+
request('http://images.google.com')
|
266
|
+
})
|
267
|
+
```
|
268
|
+
|
269
|
+
If you to use a custom cookie jar (instead of letting request use its own global cookie jar) you do so by setting the jar default or by specifying it as an option:
|
270
|
+
|
271
|
+
```javascript
|
272
|
+
var j = request.jar()
|
273
|
+
var request = request.defaults({jar:j})
|
274
|
+
request('http://www.google.com', function () {
|
275
|
+
request('http://images.google.com')
|
276
|
+
})
|
277
|
+
```
|
278
|
+
OR
|
279
|
+
|
280
|
+
```javascript
|
281
|
+
var j = request.jar()
|
282
|
+
var cookie = request.cookie('your_cookie_here')
|
283
|
+
j.add(cookie)
|
284
|
+
request({url: 'http://www.google.com', jar: j}, function () {
|
285
|
+
request('http://images.google.com')
|
286
|
+
})
|
287
|
+
```
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module.exports = ForeverAgent
|
2
|
+
ForeverAgent.SSL = ForeverAgentSSL
|
3
|
+
|
4
|
+
var util = require('util')
|
5
|
+
, Agent = require('http').Agent
|
6
|
+
, net = require('net')
|
7
|
+
, tls = require('tls')
|
8
|
+
, AgentSSL = require('https').Agent
|
9
|
+
|
10
|
+
function ForeverAgent(options) {
|
11
|
+
var self = this
|
12
|
+
self.options = options || {}
|
13
|
+
self.requests = {}
|
14
|
+
self.sockets = {}
|
15
|
+
self.freeSockets = {}
|
16
|
+
self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
|
17
|
+
self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
|
18
|
+
self.on('free', function(socket, host, port) {
|
19
|
+
var name = host + ':' + port
|
20
|
+
if (self.requests[name] && self.requests[name].length) {
|
21
|
+
self.requests[name].shift().onSocket(socket)
|
22
|
+
} else if (self.sockets[name].length < self.minSockets) {
|
23
|
+
if (!self.freeSockets[name]) self.freeSockets[name] = []
|
24
|
+
self.freeSockets[name].push(socket)
|
25
|
+
|
26
|
+
// if an error happens while we don't use the socket anyway, meh, throw the socket away
|
27
|
+
function onIdleError() {
|
28
|
+
socket.destroy()
|
29
|
+
}
|
30
|
+
socket._onIdleError = onIdleError
|
31
|
+
socket.on('error', onIdleError)
|
32
|
+
} else {
|
33
|
+
// If there are no pending requests just destroy the
|
34
|
+
// socket and it will get removed from the pool. This
|
35
|
+
// gets us out of timeout issues and allows us to
|
36
|
+
// default to Connection:keep-alive.
|
37
|
+
socket.destroy();
|
38
|
+
}
|
39
|
+
})
|
40
|
+
|
41
|
+
}
|
42
|
+
util.inherits(ForeverAgent, Agent)
|
43
|
+
|
44
|
+
ForeverAgent.defaultMinSockets = 5
|
45
|
+
|
46
|
+
|
47
|
+
ForeverAgent.prototype.createConnection = net.createConnection
|
48
|
+
ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
|
49
|
+
ForeverAgent.prototype.addRequest = function(req, host, port) {
|
50
|
+
var name = host + ':' + port
|
51
|
+
if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
|
52
|
+
var idleSocket = this.freeSockets[name].pop()
|
53
|
+
idleSocket.removeListener('error', idleSocket._onIdleError)
|
54
|
+
delete idleSocket._onIdleError
|
55
|
+
req._reusedSocket = true
|
56
|
+
req.onSocket(idleSocket)
|
57
|
+
} else {
|
58
|
+
this.addRequestNoreuse(req, host, port)
|
59
|
+
}
|
60
|
+
}
|
61
|
+
|
62
|
+
ForeverAgent.prototype.removeSocket = function(s, name, host, port) {
|
63
|
+
if (this.sockets[name]) {
|
64
|
+
var index = this.sockets[name].indexOf(s);
|
65
|
+
if (index !== -1) {
|
66
|
+
this.sockets[name].splice(index, 1);
|
67
|
+
}
|
68
|
+
} else if (this.sockets[name] && this.sockets[name].length === 0) {
|
69
|
+
// don't leak
|
70
|
+
delete this.sockets[name];
|
71
|
+
delete this.requests[name];
|
72
|
+
}
|
73
|
+
|
74
|
+
if (this.freeSockets[name]) {
|
75
|
+
var index = this.freeSockets[name].indexOf(s)
|
76
|
+
if (index !== -1) {
|
77
|
+
this.freeSockets[name].splice(index, 1)
|
78
|
+
if (this.freeSockets[name].length === 0) {
|
79
|
+
delete this.freeSockets[name]
|
80
|
+
}
|
81
|
+
}
|
82
|
+
}
|
83
|
+
|
84
|
+
if (this.requests[name] && this.requests[name].length) {
|
85
|
+
// If we have pending requests and a socket gets closed a new one
|
86
|
+
// needs to be created to take over in the pool for the one that closed.
|
87
|
+
this.createSocket(name, host, port).emit('free');
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
function ForeverAgentSSL (options) {
|
92
|
+
ForeverAgent.call(this, options)
|
93
|
+
}
|
94
|
+
util.inherits(ForeverAgentSSL, ForeverAgent)
|
95
|
+
|
96
|
+
ForeverAgentSSL.prototype.createConnection = createConnectionSSL
|
97
|
+
ForeverAgentSSL.prototype.addRequestNoreuse = AgentSSL.prototype.addRequest
|
98
|
+
|
99
|
+
function createConnectionSSL (port, host, options) {
|
100
|
+
options.port = port
|
101
|
+
options.host = host
|
102
|
+
return tls.connect(options)
|
103
|
+
}
|
@@ -0,0 +1,913 @@
|
|
1
|
+
// Copyright 2010-2012 Mikeal Rogers
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
var http = require('http')
|
16
|
+
, https = false
|
17
|
+
, tls = false
|
18
|
+
, url = require('url')
|
19
|
+
, util = require('util')
|
20
|
+
, stream = require('stream')
|
21
|
+
, qs = require('querystring')
|
22
|
+
, mimetypes = require('./mimetypes')
|
23
|
+
, oauth = require('./oauth')
|
24
|
+
, uuid = require('./uuid')
|
25
|
+
, ForeverAgent = require('./forever')
|
26
|
+
, Cookie = require('./vendor/cookie')
|
27
|
+
, CookieJar = require('./vendor/cookie/jar')
|
28
|
+
, cookieJar = new CookieJar
|
29
|
+
, tunnel = require('./tunnel')
|
30
|
+
;
|
31
|
+
|
32
|
+
if (process.logging) {
|
33
|
+
var log = process.logging('request')
|
34
|
+
}
|
35
|
+
|
36
|
+
try {
|
37
|
+
https = require('https')
|
38
|
+
} catch (e) {}
|
39
|
+
|
40
|
+
try {
|
41
|
+
tls = require('tls')
|
42
|
+
} catch (e) {}
|
43
|
+
|
44
|
+
function toBase64 (str) {
|
45
|
+
return (new Buffer(str || "", "ascii")).toString("base64")
|
46
|
+
}
|
47
|
+
|
48
|
+
// Hacky fix for pre-0.4.4 https
|
49
|
+
if (https && !https.Agent) {
|
50
|
+
https.Agent = function (options) {
|
51
|
+
http.Agent.call(this, options)
|
52
|
+
}
|
53
|
+
util.inherits(https.Agent, http.Agent)
|
54
|
+
https.Agent.prototype._getConnection = function(host, port, cb) {
|
55
|
+
var s = tls.connect(port, host, this.options, function() {
|
56
|
+
// do other checks here?
|
57
|
+
if (cb) cb()
|
58
|
+
})
|
59
|
+
return s
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
function isReadStream (rs) {
|
64
|
+
if (rs.readable && rs.path && rs.mode) {
|
65
|
+
return true
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
function copy (obj) {
|
70
|
+
var o = {}
|
71
|
+
Object.keys(obj).forEach(function (i) {
|
72
|
+
o[i] = obj[i]
|
73
|
+
})
|
74
|
+
return o
|
75
|
+
}
|
76
|
+
|
77
|
+
var isUrl = /^https?:/
|
78
|
+
|
79
|
+
var globalPool = {}
|
80
|
+
|
81
|
+
function Request (options) {
|
82
|
+
stream.Stream.call(this)
|
83
|
+
this.readable = true
|
84
|
+
this.writable = true
|
85
|
+
|
86
|
+
if (typeof options === 'string') {
|
87
|
+
options = {uri:options}
|
88
|
+
}
|
89
|
+
|
90
|
+
var reserved = Object.keys(Request.prototype)
|
91
|
+
for (var i in options) {
|
92
|
+
if (reserved.indexOf(i) === -1) {
|
93
|
+
this[i] = options[i]
|
94
|
+
} else {
|
95
|
+
if (typeof options[i] === 'function') {
|
96
|
+
delete options[i]
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
options = copy(options)
|
101
|
+
|
102
|
+
this.init(options)
|
103
|
+
}
|
104
|
+
util.inherits(Request, stream.Stream)
|
105
|
+
Request.prototype.init = function (options) {
|
106
|
+
var self = this
|
107
|
+
|
108
|
+
if (!options) options = {}
|
109
|
+
|
110
|
+
if (!self.pool) self.pool = globalPool
|
111
|
+
self.dests = []
|
112
|
+
self.__isRequestRequest = true
|
113
|
+
|
114
|
+
// Protect against double callback
|
115
|
+
if (!self._callback && self.callback) {
|
116
|
+
self._callback = self.callback
|
117
|
+
self.callback = function () {
|
118
|
+
if (self._callbackCalled) return // Print a warning maybe?
|
119
|
+
self._callback.apply(self, arguments)
|
120
|
+
self._callbackCalled = true
|
121
|
+
}
|
122
|
+
self.on('error', self.callback.bind())
|
123
|
+
self.on('complete', self.callback.bind(self, null))
|
124
|
+
}
|
125
|
+
|
126
|
+
if (self.url) {
|
127
|
+
// People use this property instead all the time so why not just support it.
|
128
|
+
self.uri = self.url
|
129
|
+
delete self.url
|
130
|
+
}
|
131
|
+
|
132
|
+
if (!self.uri) {
|
133
|
+
throw new Error("options.uri is a required argument")
|
134
|
+
} else {
|
135
|
+
if (typeof self.uri == "string") self.uri = url.parse(self.uri)
|
136
|
+
}
|
137
|
+
if (self.proxy) {
|
138
|
+
if (typeof self.proxy == 'string') self.proxy = url.parse(self.proxy)
|
139
|
+
|
140
|
+
// do the HTTP CONNECT dance using koichik/node-tunnel
|
141
|
+
if (http.globalAgent && self.uri.protocol === "https:") {
|
142
|
+
self.tunnel = true
|
143
|
+
var tunnelFn = self.proxy.protocol === "http:"
|
144
|
+
? tunnel.httpsOverHttp : tunnel.httpsOverHttps
|
145
|
+
|
146
|
+
var tunnelOptions = { proxy: { host: self.proxy.hostname
|
147
|
+
, port: +self.proxy.port
|
148
|
+
, proxyAuth: self.proxy.auth }
|
149
|
+
, ca: this.ca }
|
150
|
+
|
151
|
+
self.agent = tunnelFn(tunnelOptions)
|
152
|
+
self.tunnel = true
|
153
|
+
}
|
154
|
+
}
|
155
|
+
|
156
|
+
self._redirectsFollowed = self._redirectsFollowed || 0
|
157
|
+
self.maxRedirects = (self.maxRedirects !== undefined) ? self.maxRedirects : 10
|
158
|
+
self.followRedirect = (self.followRedirect !== undefined) ? self.followRedirect : true
|
159
|
+
self.followAllRedirects = (self.followAllRedirects !== undefined) ? self.followAllRedirects : false;
|
160
|
+
if (self.followRedirect || self.followAllRedirects)
|
161
|
+
self.redirects = self.redirects || []
|
162
|
+
|
163
|
+
self.headers = self.headers ? copy(self.headers) : {}
|
164
|
+
|
165
|
+
self.setHost = false
|
166
|
+
if (!self.headers.host) {
|
167
|
+
self.headers.host = self.uri.hostname
|
168
|
+
if (self.uri.port) {
|
169
|
+
if ( !(self.uri.port === 80 && self.uri.protocol === 'http:') &&
|
170
|
+
!(self.uri.port === 443 && self.uri.protocol === 'https:') )
|
171
|
+
self.headers.host += (':'+self.uri.port)
|
172
|
+
}
|
173
|
+
self.setHost = true
|
174
|
+
}
|
175
|
+
|
176
|
+
self.jar(self._jar || options.jar)
|
177
|
+
|
178
|
+
if (!self.uri.pathname) {self.uri.pathname = '/'}
|
179
|
+
if (!self.uri.port) {
|
180
|
+
if (self.uri.protocol == 'http:') {self.uri.port = 80}
|
181
|
+
else if (self.uri.protocol == 'https:') {self.uri.port = 443}
|
182
|
+
}
|
183
|
+
|
184
|
+
if (self.proxy && !self.tunnel) {
|
185
|
+
self.port = self.proxy.port
|
186
|
+
self.host = self.proxy.hostname
|
187
|
+
} else {
|
188
|
+
self.port = self.uri.port
|
189
|
+
self.host = self.uri.hostname
|
190
|
+
}
|
191
|
+
|
192
|
+
self.clientErrorHandler = function (error) {
|
193
|
+
if (self._aborted) return
|
194
|
+
|
195
|
+
if (self.setHost) delete self.headers.host
|
196
|
+
if (self.req._reusedSocket && error.code === 'ECONNRESET'
|
197
|
+
&& self.agent.addRequestNoreuse) {
|
198
|
+
self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
|
199
|
+
self.start()
|
200
|
+
self.req.end()
|
201
|
+
return
|
202
|
+
}
|
203
|
+
if (self.timeout && self.timeoutTimer) {
|
204
|
+
clearTimeout(self.timeoutTimer)
|
205
|
+
self.timeoutTimer = null
|
206
|
+
}
|
207
|
+
self.emit('error', error)
|
208
|
+
}
|
209
|
+
|
210
|
+
if (options.form) {
|
211
|
+
self.form(options.form)
|
212
|
+
}
|
213
|
+
|
214
|
+
if (options.oauth) {
|
215
|
+
self.oauth(options.oauth)
|
216
|
+
}
|
217
|
+
|
218
|
+
if (self.uri.auth && !self.headers.authorization) {
|
219
|
+
self.headers.authorization = "Basic " + toBase64(self.uri.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
|
220
|
+
}
|
221
|
+
if (self.proxy && self.proxy.auth && !self.headers['proxy-authorization'] && !self.tunnel) {
|
222
|
+
self.headers['proxy-authorization'] = "Basic " + toBase64(self.proxy.auth.split(':').map(function(item){ return qs.unescape(item)}).join(':'))
|
223
|
+
}
|
224
|
+
|
225
|
+
if (options.qs) self.qs(options.qs)
|
226
|
+
|
227
|
+
if (self.uri.path) {
|
228
|
+
self.path = self.uri.path
|
229
|
+
} else {
|
230
|
+
self.path = self.uri.pathname + (self.uri.search || "")
|
231
|
+
}
|
232
|
+
|
233
|
+
if (self.path.length === 0) self.path = '/'
|
234
|
+
|
235
|
+
if (self.proxy && !self.tunnel) self.path = (self.uri.protocol + '//' + self.uri.host + self.path)
|
236
|
+
|
237
|
+
if (options.json) {
|
238
|
+
self.json(options.json)
|
239
|
+
} else if (options.multipart) {
|
240
|
+
self.multipart(options.multipart)
|
241
|
+
}
|
242
|
+
|
243
|
+
if (self.body) {
|
244
|
+
var length = 0
|
245
|
+
if (!Buffer.isBuffer(self.body)) {
|
246
|
+
if (Array.isArray(self.body)) {
|
247
|
+
for (var i = 0; i < self.body.length; i++) {
|
248
|
+
length += self.body[i].length
|
249
|
+
}
|
250
|
+
} else {
|
251
|
+
self.body = new Buffer(self.body)
|
252
|
+
length = self.body.length
|
253
|
+
}
|
254
|
+
} else {
|
255
|
+
length = self.body.length
|
256
|
+
}
|
257
|
+
if (length) {
|
258
|
+
self.headers['content-length'] = length
|
259
|
+
} else {
|
260
|
+
throw new Error('Argument error, options.body.')
|
261
|
+
}
|
262
|
+
}
|
263
|
+
|
264
|
+
var protocol = self.proxy && !self.tunnel ? self.proxy.protocol : self.uri.protocol
|
265
|
+
, defaultModules = {'http:':http, 'https:':https}
|
266
|
+
, httpModules = self.httpModules || {}
|
267
|
+
;
|
268
|
+
self.httpModule = httpModules[protocol] || defaultModules[protocol]
|
269
|
+
|
270
|
+
if (!self.httpModule) throw new Error("Invalid protocol")
|
271
|
+
|
272
|
+
if (options.ca) self.ca = options.ca
|
273
|
+
|
274
|
+
if (!self.agent) {
|
275
|
+
if (options.agentOptions) self.agentOptions = options.agentOptions
|
276
|
+
|
277
|
+
if (options.agentClass) {
|
278
|
+
self.agentClass = options.agentClass
|
279
|
+
} else if (options.forever) {
|
280
|
+
self.agentClass = protocol === 'http:' ? ForeverAgent : ForeverAgent.SSL
|
281
|
+
} else {
|
282
|
+
self.agentClass = self.httpModule.Agent
|
283
|
+
}
|
284
|
+
}
|
285
|
+
|
286
|
+
if (self.pool === false) {
|
287
|
+
self.agent = false
|
288
|
+
} else {
|
289
|
+
self.agent = self.agent || self.getAgent()
|
290
|
+
if (self.maxSockets) {
|
291
|
+
// Don't use our pooling if node has the refactored client
|
292
|
+
self.agent.maxSockets = self.maxSockets
|
293
|
+
}
|
294
|
+
if (self.pool.maxSockets) {
|
295
|
+
// Don't use our pooling if node has the refactored client
|
296
|
+
self.agent.maxSockets = self.pool.maxSockets
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
self.once('pipe', function (src) {
|
301
|
+
if (self.ntick) throw new Error("You cannot pipe to this stream after the first nextTick() after creation of the request stream.")
|
302
|
+
self.src = src
|
303
|
+
if (isReadStream(src)) {
|
304
|
+
if (!self.headers['content-type'] && !self.headers['Content-Type'])
|
305
|
+
self.headers['content-type'] = mimetypes.lookup(src.path.slice(src.path.lastIndexOf('.')+1))
|
306
|
+
} else {
|
307
|
+
if (src.headers) {
|
308
|
+
for (var i in src.headers) {
|
309
|
+
if (!self.headers[i]) {
|
310
|
+
self.headers[i] = src.headers[i]
|
311
|
+
}
|
312
|
+
}
|
313
|
+
}
|
314
|
+
if (src.method && !self.method) {
|
315
|
+
self.method = src.method
|
316
|
+
}
|
317
|
+
}
|
318
|
+
|
319
|
+
self.on('pipe', function () {
|
320
|
+
console.error("You have already piped to this stream. Pipeing twice is likely to break the request.")
|
321
|
+
})
|
322
|
+
})
|
323
|
+
|
324
|
+
process.nextTick(function () {
|
325
|
+
if (self._aborted) return
|
326
|
+
|
327
|
+
if (self.body) {
|
328
|
+
if (Array.isArray(self.body)) {
|
329
|
+
self.body.forEach(function(part) {
|
330
|
+
self.write(part)
|
331
|
+
})
|
332
|
+
} else {
|
333
|
+
self.write(self.body)
|
334
|
+
}
|
335
|
+
self.end()
|
336
|
+
} else if (self.requestBodyStream) {
|
337
|
+
console.warn("options.requestBodyStream is deprecated, please pass the request object to stream.pipe.")
|
338
|
+
self.requestBodyStream.pipe(self)
|
339
|
+
} else if (!self.src) {
|
340
|
+
self.headers['content-length'] = 0
|
341
|
+
self.end()
|
342
|
+
}
|
343
|
+
self.ntick = true
|
344
|
+
})
|
345
|
+
}
|
346
|
+
|
347
|
+
Request.prototype.getAgent = function () {
|
348
|
+
var Agent = this.agentClass
|
349
|
+
var options = {}
|
350
|
+
if (this.agentOptions) {
|
351
|
+
for (var i in this.agentOptions) {
|
352
|
+
options[i] = this.agentOptions[i]
|
353
|
+
}
|
354
|
+
}
|
355
|
+
if (this.ca) options.ca = this.ca
|
356
|
+
|
357
|
+
var poolKey = ''
|
358
|
+
|
359
|
+
// different types of agents are in different pools
|
360
|
+
if (Agent !== this.httpModule.Agent) {
|
361
|
+
poolKey += Agent.name
|
362
|
+
}
|
363
|
+
|
364
|
+
if (!this.httpModule.globalAgent) {
|
365
|
+
// node 0.4.x
|
366
|
+
options.host = this.host
|
367
|
+
options.port = this.port
|
368
|
+
if (poolKey) poolKey += ':'
|
369
|
+
poolKey += this.host + ':' + this.port
|
370
|
+
}
|
371
|
+
|
372
|
+
if (options.ca) {
|
373
|
+
if (poolKey) poolKey += ':'
|
374
|
+
poolKey += options.ca
|
375
|
+
}
|
376
|
+
|
377
|
+
if (!poolKey && Agent === this.httpModule.Agent && this.httpModule.globalAgent) {
|
378
|
+
// not doing anything special. Use the globalAgent
|
379
|
+
return this.httpModule.globalAgent
|
380
|
+
}
|
381
|
+
|
382
|
+
// already generated an agent for this setting
|
383
|
+
if (this.pool[poolKey]) return this.pool[poolKey]
|
384
|
+
|
385
|
+
return this.pool[poolKey] = new Agent(options)
|
386
|
+
}
|
387
|
+
|
388
|
+
Request.prototype.start = function () {
|
389
|
+
var self = this
|
390
|
+
|
391
|
+
if (self._aborted) return
|
392
|
+
|
393
|
+
self._started = true
|
394
|
+
self.method = self.method || 'GET'
|
395
|
+
self.href = self.uri.href
|
396
|
+
if (log) log('%method %href', self)
|
397
|
+
self.req = self.httpModule.request(self, function (response) {
|
398
|
+
if (self._aborted) return
|
399
|
+
if (self._paused) response.pause()
|
400
|
+
|
401
|
+
self.response = response
|
402
|
+
response.request = self
|
403
|
+
response.toJSON = toJSON
|
404
|
+
|
405
|
+
if (self.httpModule === https &&
|
406
|
+
self.strictSSL &&
|
407
|
+
!response.client.authorized) {
|
408
|
+
var sslErr = response.client.authorizationError
|
409
|
+
self.emit('error', new Error('SSL Error: '+ sslErr))
|
410
|
+
return
|
411
|
+
}
|
412
|
+
|
413
|
+
if (self.setHost) delete self.headers.host
|
414
|
+
if (self.timeout && self.timeoutTimer) {
|
415
|
+
clearTimeout(self.timeoutTimer)
|
416
|
+
self.timeoutTimer = null
|
417
|
+
}
|
418
|
+
|
419
|
+
if (response.headers['set-cookie'] && (!self._disableCookies)) {
|
420
|
+
response.headers['set-cookie'].forEach(function(cookie) {
|
421
|
+
if (self._jar) self._jar.add(new Cookie(cookie))
|
422
|
+
else cookieJar.add(new Cookie(cookie))
|
423
|
+
})
|
424
|
+
}
|
425
|
+
|
426
|
+
if (response.statusCode >= 300 && response.statusCode < 400 &&
|
427
|
+
(self.followAllRedirects ||
|
428
|
+
(self.followRedirect && (self.method !== 'PUT' && self.method !== 'POST' && self.method !== 'DELETE'))) &&
|
429
|
+
response.headers.location) {
|
430
|
+
if (self._redirectsFollowed >= self.maxRedirects) {
|
431
|
+
self.emit('error', new Error("Exceeded maxRedirects. Probably stuck in a redirect loop."))
|
432
|
+
return
|
433
|
+
}
|
434
|
+
self._redirectsFollowed += 1
|
435
|
+
|
436
|
+
if (!isUrl.test(response.headers.location)) {
|
437
|
+
response.headers.location = url.resolve(self.uri.href, response.headers.location)
|
438
|
+
}
|
439
|
+
self.uri = response.headers.location
|
440
|
+
self.redirects.push(
|
441
|
+
{ statusCode : response.statusCode
|
442
|
+
, redirectUri: response.headers.location
|
443
|
+
}
|
444
|
+
)
|
445
|
+
if (self.followAllRedirects) self.method = 'GET'
|
446
|
+
// self.method = 'GET'; // Force all redirects to use GET || commented out fixes #215
|
447
|
+
delete self.req
|
448
|
+
delete self.agent
|
449
|
+
delete self._started
|
450
|
+
delete self.body
|
451
|
+
if (self.headers) {
|
452
|
+
delete self.headers.host
|
453
|
+
}
|
454
|
+
if (log) log('Redirect to %uri', self)
|
455
|
+
self.init()
|
456
|
+
return // Ignore the rest of the response
|
457
|
+
} else {
|
458
|
+
self._redirectsFollowed = self._redirectsFollowed || 0
|
459
|
+
// Be a good stream and emit end when the response is finished.
|
460
|
+
// Hack to emit end on close because of a core bug that never fires end
|
461
|
+
response.on('close', function () {
|
462
|
+
if (!self._ended) self.response.emit('end')
|
463
|
+
})
|
464
|
+
|
465
|
+
if (self.encoding) {
|
466
|
+
if (self.dests.length !== 0) {
|
467
|
+
console.error("Ingoring encoding parameter as this stream is being piped to another stream which makes the encoding option invalid.")
|
468
|
+
} else {
|
469
|
+
response.setEncoding(self.encoding)
|
470
|
+
}
|
471
|
+
}
|
472
|
+
|
473
|
+
self.dests.forEach(function (dest) {
|
474
|
+
self.pipeDest(dest)
|
475
|
+
})
|
476
|
+
|
477
|
+
response.on("data", function (chunk) {
|
478
|
+
self._destdata = true
|
479
|
+
self.emit("data", chunk)
|
480
|
+
})
|
481
|
+
response.on("end", function (chunk) {
|
482
|
+
self._ended = true
|
483
|
+
self.emit("end", chunk)
|
484
|
+
})
|
485
|
+
response.on("close", function () {self.emit("close")})
|
486
|
+
|
487
|
+
self.emit('response', response)
|
488
|
+
|
489
|
+
if (self.callback) {
|
490
|
+
var buffer = []
|
491
|
+
var bodyLen = 0
|
492
|
+
self.on("data", function (chunk) {
|
493
|
+
buffer.push(chunk)
|
494
|
+
bodyLen += chunk.length
|
495
|
+
})
|
496
|
+
self.on("end", function () {
|
497
|
+
if (self._aborted) return
|
498
|
+
|
499
|
+
if (buffer.length && Buffer.isBuffer(buffer[0])) {
|
500
|
+
var body = new Buffer(bodyLen)
|
501
|
+
var i = 0
|
502
|
+
buffer.forEach(function (chunk) {
|
503
|
+
chunk.copy(body, i, 0, chunk.length)
|
504
|
+
i += chunk.length
|
505
|
+
})
|
506
|
+
if (self.encoding === null) {
|
507
|
+
response.body = body
|
508
|
+
} else {
|
509
|
+
response.body = body.toString()
|
510
|
+
}
|
511
|
+
} else if (buffer.length) {
|
512
|
+
response.body = buffer.join('')
|
513
|
+
}
|
514
|
+
|
515
|
+
if (self._json) {
|
516
|
+
try {
|
517
|
+
response.body = JSON.parse(response.body)
|
518
|
+
} catch (e) {}
|
519
|
+
}
|
520
|
+
|
521
|
+
self.emit('complete', response, response.body)
|
522
|
+
})
|
523
|
+
}
|
524
|
+
}
|
525
|
+
})
|
526
|
+
|
527
|
+
if (self.timeout && !self.timeoutTimer) {
|
528
|
+
self.timeoutTimer = setTimeout(function() {
|
529
|
+
self.req.abort()
|
530
|
+
var e = new Error("ETIMEDOUT")
|
531
|
+
e.code = "ETIMEDOUT"
|
532
|
+
self.emit("error", e)
|
533
|
+
}, self.timeout)
|
534
|
+
|
535
|
+
// Set additional timeout on socket - in case if remote
|
536
|
+
// server freeze after sending headers
|
537
|
+
if (self.req.setTimeout) { // only works on node 0.6+
|
538
|
+
self.req.setTimeout(self.timeout, function(){
|
539
|
+
if (self.req) {
|
540
|
+
self.req.abort()
|
541
|
+
var e = new Error("ESOCKETTIMEDOUT")
|
542
|
+
e.code = "ESOCKETTIMEDOUT"
|
543
|
+
self.emit("error", e)
|
544
|
+
}
|
545
|
+
})
|
546
|
+
}
|
547
|
+
}
|
548
|
+
|
549
|
+
self.req.on('error', self.clientErrorHandler)
|
550
|
+
|
551
|
+
self.emit('request', self.req)
|
552
|
+
}
|
553
|
+
|
554
|
+
Request.prototype.abort = function() {
|
555
|
+
this._aborted = true;
|
556
|
+
|
557
|
+
if (this.req) {
|
558
|
+
this.req.abort()
|
559
|
+
}
|
560
|
+
else if (this.response) {
|
561
|
+
this.response.abort()
|
562
|
+
}
|
563
|
+
|
564
|
+
this.emit("abort")
|
565
|
+
}
|
566
|
+
|
567
|
+
Request.prototype.pipeDest = function (dest) {
|
568
|
+
var response = this.response
|
569
|
+
// Called after the response is received
|
570
|
+
if (dest.headers) {
|
571
|
+
dest.headers['content-type'] = response.headers['content-type']
|
572
|
+
if (response.headers['content-length']) {
|
573
|
+
dest.headers['content-length'] = response.headers['content-length']
|
574
|
+
}
|
575
|
+
}
|
576
|
+
if (dest.setHeader) {
|
577
|
+
for (var i in response.headers) {
|
578
|
+
dest.setHeader(i, response.headers[i])
|
579
|
+
}
|
580
|
+
dest.statusCode = response.statusCode
|
581
|
+
}
|
582
|
+
if (this.pipefilter) this.pipefilter(response, dest)
|
583
|
+
}
|
584
|
+
|
585
|
+
// Composable API
|
586
|
+
Request.prototype.setHeader = function (name, value, clobber) {
|
587
|
+
if (clobber === undefined) clobber = true
|
588
|
+
if (clobber || !this.headers.hasOwnProperty(name)) this.headers[name] = value
|
589
|
+
else this.headers[name] += ',' + value
|
590
|
+
return this
|
591
|
+
}
|
592
|
+
Request.prototype.setHeaders = function (headers) {
|
593
|
+
for (i in headers) {this.setHeader(i, headers[i])}
|
594
|
+
return this
|
595
|
+
}
|
596
|
+
Request.prototype.qs = function (q, clobber) {
|
597
|
+
var base
|
598
|
+
if (!clobber && this.uri.query) base = qs.parse(this.uri.query)
|
599
|
+
else base = {}
|
600
|
+
|
601
|
+
for (var i in q) {
|
602
|
+
base[i] = q[i]
|
603
|
+
}
|
604
|
+
|
605
|
+
this.uri = url.parse(this.uri.href.split('?')[0] + '?' + qs.stringify(base))
|
606
|
+
this.url = this.uri
|
607
|
+
|
608
|
+
return this
|
609
|
+
}
|
610
|
+
Request.prototype.form = function (form) {
|
611
|
+
this.headers['content-type'] = 'application/x-www-form-urlencoded; charset=utf-8'
|
612
|
+
this.body = qs.stringify(form).toString('utf8')
|
613
|
+
return this
|
614
|
+
}
|
615
|
+
Request.prototype.multipart = function (multipart) {
|
616
|
+
var self = this
|
617
|
+
self.body = []
|
618
|
+
|
619
|
+
if (!self.headers['content-type']) {
|
620
|
+
self.headers['content-type'] = 'multipart/related; boundary=frontier';
|
621
|
+
} else {
|
622
|
+
self.headers['content-type'] = self.headers['content-type'].split(';')[0] + '; boundary=frontier';
|
623
|
+
}
|
624
|
+
|
625
|
+
if (!multipart.forEach) throw new Error('Argument error, options.multipart.')
|
626
|
+
|
627
|
+
multipart.forEach(function (part) {
|
628
|
+
var body = part.body
|
629
|
+
if(!body) throw Error('Body attribute missing in multipart.')
|
630
|
+
delete part.body
|
631
|
+
var preamble = '--frontier\r\n'
|
632
|
+
Object.keys(part).forEach(function(key){
|
633
|
+
preamble += key + ': ' + part[key] + '\r\n'
|
634
|
+
})
|
635
|
+
preamble += '\r\n'
|
636
|
+
self.body.push(new Buffer(preamble))
|
637
|
+
self.body.push(new Buffer(body))
|
638
|
+
self.body.push(new Buffer('\r\n'))
|
639
|
+
})
|
640
|
+
self.body.push(new Buffer('--frontier--'))
|
641
|
+
return self
|
642
|
+
}
|
643
|
+
Request.prototype.json = function (val) {
|
644
|
+
this.setHeader('content-type', 'application/json')
|
645
|
+
this.setHeader('accept', 'application/json')
|
646
|
+
this._json = true
|
647
|
+
if (typeof val === 'boolean') {
|
648
|
+
if (typeof this.body === 'object') this.body = JSON.stringify(this.body)
|
649
|
+
} else {
|
650
|
+
this.body = JSON.stringify(val)
|
651
|
+
}
|
652
|
+
return this
|
653
|
+
}
|
654
|
+
Request.prototype.oauth = function (_oauth) {
|
655
|
+
var form
|
656
|
+
if (this.headers['content-type'] &&
|
657
|
+
this.headers['content-type'].slice(0, 'application/x-www-form-urlencoded'.length) ===
|
658
|
+
'application/x-www-form-urlencoded'
|
659
|
+
) {
|
660
|
+
form = qs.parse(this.body)
|
661
|
+
}
|
662
|
+
if (this.uri.query) {
|
663
|
+
form = qs.parse(this.uri.query)
|
664
|
+
}
|
665
|
+
if (!form) form = {}
|
666
|
+
var oa = {}
|
667
|
+
for (var i in form) oa[i] = form[i]
|
668
|
+
for (var i in _oauth) oa['oauth_'+i] = _oauth[i]
|
669
|
+
if (!oa.oauth_version) oa.oauth_version = '1.0'
|
670
|
+
if (!oa.oauth_timestamp) oa.oauth_timestamp = Math.floor( (new Date()).getTime() / 1000 ).toString()
|
671
|
+
if (!oa.oauth_nonce) oa.oauth_nonce = uuid().replace(/-/g, '')
|
672
|
+
|
673
|
+
oa.oauth_signature_method = 'HMAC-SHA1'
|
674
|
+
|
675
|
+
var consumer_secret = oa.oauth_consumer_secret
|
676
|
+
delete oa.oauth_consumer_secret
|
677
|
+
var token_secret = oa.oauth_token_secret
|
678
|
+
delete oa.oauth_token_secret
|
679
|
+
|
680
|
+
var baseurl = this.uri.protocol + '//' + this.uri.host + this.uri.pathname
|
681
|
+
var signature = oauth.hmacsign(this.method, baseurl, oa, consumer_secret, token_secret)
|
682
|
+
|
683
|
+
// oa.oauth_signature = signature
|
684
|
+
for (var i in form) {
|
685
|
+
if ( i.slice(0, 'oauth_') in _oauth) {
|
686
|
+
// skip
|
687
|
+
} else {
|
688
|
+
delete oa['oauth_'+i]
|
689
|
+
}
|
690
|
+
}
|
691
|
+
this.headers.Authorization =
|
692
|
+
'OAuth '+Object.keys(oa).sort().map(function (i) {return i+'="'+oauth.rfc3986(oa[i])+'"'}).join(',')
|
693
|
+
this.headers.Authorization += ',oauth_signature="'+oauth.rfc3986(signature)+'"'
|
694
|
+
return this
|
695
|
+
}
|
696
|
+
Request.prototype.jar = function (jar) {
|
697
|
+
var cookies
|
698
|
+
|
699
|
+
if (this._redirectsFollowed === 0) {
|
700
|
+
this.originalCookieHeader = this.headers.cookie
|
701
|
+
}
|
702
|
+
|
703
|
+
if (jar === false) {
|
704
|
+
// disable cookies
|
705
|
+
cookies = false;
|
706
|
+
this._disableCookies = true;
|
707
|
+
} else if (jar) {
|
708
|
+
// fetch cookie from the user defined cookie jar
|
709
|
+
cookies = jar.get({ url: this.uri.href })
|
710
|
+
} else {
|
711
|
+
// fetch cookie from the global cookie jar
|
712
|
+
cookies = cookieJar.get({ url: this.uri.href })
|
713
|
+
}
|
714
|
+
|
715
|
+
if (cookies && cookies.length) {
|
716
|
+
var cookieString = cookies.map(function (c) {
|
717
|
+
return c.name + "=" + c.value
|
718
|
+
}).join("; ")
|
719
|
+
|
720
|
+
if (this.originalCookieHeader) {
|
721
|
+
// Don't overwrite existing Cookie header
|
722
|
+
this.headers.cookie = this.originalCookieHeader + '; ' + cookieString
|
723
|
+
} else {
|
724
|
+
this.headers.cookie = cookieString
|
725
|
+
}
|
726
|
+
}
|
727
|
+
this._jar = jar
|
728
|
+
return this
|
729
|
+
}
|
730
|
+
|
731
|
+
|
732
|
+
// Stream API
|
733
|
+
Request.prototype.pipe = function (dest, opts) {
|
734
|
+
if (this.response) {
|
735
|
+
if (this._destdata) {
|
736
|
+
throw new Error("You cannot pipe after data has been emitted from the response.")
|
737
|
+
} else if (this._ended) {
|
738
|
+
throw new Error("You cannot pipe after the response has been ended.")
|
739
|
+
} else {
|
740
|
+
stream.Stream.prototype.pipe.call(this, dest, opts)
|
741
|
+
this.pipeDest(dest)
|
742
|
+
return dest
|
743
|
+
}
|
744
|
+
} else {
|
745
|
+
this.dests.push(dest)
|
746
|
+
stream.Stream.prototype.pipe.call(this, dest, opts)
|
747
|
+
return dest
|
748
|
+
}
|
749
|
+
}
|
750
|
+
Request.prototype.write = function () {
|
751
|
+
if (!this._started) this.start()
|
752
|
+
this.req.write.apply(this.req, arguments)
|
753
|
+
}
|
754
|
+
Request.prototype.end = function (chunk) {
|
755
|
+
if (chunk) this.write(chunk)
|
756
|
+
if (!this._started) this.start()
|
757
|
+
this.req.end()
|
758
|
+
}
|
759
|
+
Request.prototype.pause = function () {
|
760
|
+
if (!this.response) this._paused = true
|
761
|
+
else this.response.pause.apply(this.response, arguments)
|
762
|
+
}
|
763
|
+
Request.prototype.resume = function () {
|
764
|
+
if (!this.response) this._paused = false
|
765
|
+
else this.response.resume.apply(this.response, arguments)
|
766
|
+
}
|
767
|
+
Request.prototype.destroy = function () {
|
768
|
+
if (!this._ended) this.end()
|
769
|
+
}
|
770
|
+
|
771
|
+
// organize params for post, put, head, del
|
772
|
+
function initParams(uri, options, callback) {
|
773
|
+
if ((typeof options === 'function') && !callback) callback = options;
|
774
|
+
if (typeof options === 'object') {
|
775
|
+
options.uri = uri;
|
776
|
+
} else if (typeof uri === 'string') {
|
777
|
+
options = {uri:uri};
|
778
|
+
} else {
|
779
|
+
options = uri;
|
780
|
+
uri = options.uri;
|
781
|
+
}
|
782
|
+
return { uri: uri, options: options, callback: callback };
|
783
|
+
}
|
784
|
+
|
785
|
+
function request (uri, options, callback) {
|
786
|
+
if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')
|
787
|
+
if ((typeof options === 'function') && !callback) callback = options;
|
788
|
+
if (typeof options === 'object') {
|
789
|
+
options.uri = uri;
|
790
|
+
} else if (typeof uri === 'string') {
|
791
|
+
options = {uri:uri};
|
792
|
+
} else {
|
793
|
+
options = uri;
|
794
|
+
}
|
795
|
+
|
796
|
+
if (callback) options.callback = callback;
|
797
|
+
var r = new Request(options)
|
798
|
+
return r
|
799
|
+
}
|
800
|
+
|
801
|
+
module.exports = request
|
802
|
+
|
803
|
+
request.defaults = function (options) {
|
804
|
+
var def = function (method) {
|
805
|
+
var d = function (uri, opts, callback) {
|
806
|
+
var params = initParams(uri, opts, callback);
|
807
|
+
for (var i in options) {
|
808
|
+
if (params.options[i] === undefined) params.options[i] = options[i]
|
809
|
+
}
|
810
|
+
return method(params.options, params.callback)
|
811
|
+
}
|
812
|
+
return d
|
813
|
+
}
|
814
|
+
var de = def(request)
|
815
|
+
de.get = def(request.get)
|
816
|
+
de.post = def(request.post)
|
817
|
+
de.put = def(request.put)
|
818
|
+
de.head = def(request.head)
|
819
|
+
de.del = def(request.del)
|
820
|
+
de.cookie = def(request.cookie)
|
821
|
+
de.jar = def(request.jar)
|
822
|
+
return de
|
823
|
+
}
|
824
|
+
|
825
|
+
request.forever = function (agentOptions, optionsArg) {
|
826
|
+
var options = {}
|
827
|
+
if (optionsArg) {
|
828
|
+
for (option in optionsArg) {
|
829
|
+
options[option] = optionsArg[option]
|
830
|
+
}
|
831
|
+
}
|
832
|
+
if (agentOptions) options.agentOptions = agentOptions
|
833
|
+
options.forever = true
|
834
|
+
return request.defaults(options)
|
835
|
+
}
|
836
|
+
|
837
|
+
request.get = request
|
838
|
+
request.post = function (uri, options, callback) {
|
839
|
+
var params = initParams(uri, options, callback);
|
840
|
+
params.options.method = 'POST';
|
841
|
+
return request(params.uri || null, params.options, params.callback)
|
842
|
+
}
|
843
|
+
request.put = function (uri, options, callback) {
|
844
|
+
var params = initParams(uri, options, callback);
|
845
|
+
params.options.method = 'PUT'
|
846
|
+
return request(params.uri || null, params.options, params.callback)
|
847
|
+
}
|
848
|
+
request.head = function (uri, options, callback) {
|
849
|
+
var params = initParams(uri, options, callback);
|
850
|
+
params.options.method = 'HEAD'
|
851
|
+
if (params.options.body ||
|
852
|
+
params.options.requestBodyStream ||
|
853
|
+
(params.options.json && typeof params.options.json !== 'boolean') ||
|
854
|
+
params.options.multipart) {
|
855
|
+
throw new Error("HTTP HEAD requests MUST NOT include a request body.")
|
856
|
+
}
|
857
|
+
return request(params.uri || null, params.options, params.callback)
|
858
|
+
}
|
859
|
+
request.del = function (uri, options, callback) {
|
860
|
+
var params = initParams(uri, options, callback);
|
861
|
+
params.options.method = 'DELETE'
|
862
|
+
return request(params.uri || null, params.options, params.callback)
|
863
|
+
}
|
864
|
+
request.jar = function () {
|
865
|
+
return new CookieJar
|
866
|
+
}
|
867
|
+
request.cookie = function (str) {
|
868
|
+
if (str && str.uri) str = str.uri
|
869
|
+
if (typeof str !== 'string') throw new Error("The cookie function only accepts STRING as param")
|
870
|
+
return new Cookie(str)
|
871
|
+
}
|
872
|
+
|
873
|
+
// Safe toJSON
|
874
|
+
|
875
|
+
function getSafe (self, uuid) {
|
876
|
+
if (typeof self === 'object' || typeof self === 'function') var safe = {}
|
877
|
+
if (Array.isArray(self)) var safe = []
|
878
|
+
|
879
|
+
var recurse = []
|
880
|
+
|
881
|
+
Object.defineProperty(self, uuid, {})
|
882
|
+
|
883
|
+
var attrs = Object.keys(self).filter(function (i) {
|
884
|
+
if (i === uuid) return false
|
885
|
+
if ( (typeof self[i] !== 'object' && typeof self[i] !== 'function') || self[i] === null) return true
|
886
|
+
return !(Object.getOwnPropertyDescriptor(self[i], uuid))
|
887
|
+
})
|
888
|
+
|
889
|
+
|
890
|
+
for (var i=0;i<attrs.length;i++) {
|
891
|
+
if ( (typeof self[attrs[i]] !== 'object' && typeof self[attrs[i]] !== 'function') ||
|
892
|
+
self[attrs[i]] === null
|
893
|
+
) {
|
894
|
+
safe[attrs[i]] = self[attrs[i]]
|
895
|
+
} else {
|
896
|
+
recurse.push(attrs[i])
|
897
|
+
Object.defineProperty(self[attrs[i]], uuid, {})
|
898
|
+
}
|
899
|
+
}
|
900
|
+
|
901
|
+
for (var i=0;i<recurse.length;i++) {
|
902
|
+
safe[recurse[i]] = getSafe(self[recurse[i]], uuid)
|
903
|
+
}
|
904
|
+
|
905
|
+
return safe
|
906
|
+
}
|
907
|
+
|
908
|
+
function toJSON () {
|
909
|
+
return getSafe(this, (((1+Math.random())*0x10000)|0).toString(16))
|
910
|
+
}
|
911
|
+
|
912
|
+
Request.prototype.toJSON = toJSON
|
913
|
+
|