Compare commits

...

266 Commits

Author SHA1 Message Date
Jarrett Cruger
9b96cd7251 1.18.1 2020-05-17 17:26:59 -04:00
Jason
335aeeba2f
Skip sending the proxyReq event when the expect header is present (#1447)
* Skip sending the proxyReq event when the expect header is present

* Adjust padding to match advisory

Co-authored-by: Smylnycky, Jason M <jason.smylnycky@cengage.com>
2020-05-17 17:18:42 -04:00
Jason
dba39668ba
Remove node6 support, add node12 to build (#1397) 2020-05-17 17:16:55 -04:00
indexzero
9bbe486c5e [dist] Version bump. 1.18.0 2019-09-17 21:40:59 -04:00
Jason
6e4bef4d1c
Added in auto-changelog module set to keepachangelog format (#1373)
Removed last nodejitsu reference
2019-09-05 14:11:16 -04:00
Nick Gilligan
d05624167c fix 'Modify Response' readme section to avoid unnecessary array copying (#1300) 2019-08-22 02:40:15 -05:00
IdeaHunter
244303b994 Fix incorrect target name for reverse proxy example (#1135) 2019-08-22 02:39:14 -05:00
Ben Costolo
b4028ba78b Fix modify response middleware example (#1139)
* declare app variable to house middleware

* remove deprecated connect.createServer() function call

* specify middleware with app.use() and pass app middleware to an http
server
2019-08-22 02:38:28 -05:00
renovate[bot]
77a98159d2 [dist] Update dependency async to v3 (#1359) 2019-08-22 02:37:57 -05:00
Kristoffer Lundén
c662f9ebcd Fix path to local http-proxy in examples. (#1072) 2019-08-22 02:35:20 -05:00
Li Bin
806e4927c9 fix reverse-proxy example require path (#1067) 2019-08-22 02:34:29 -05:00
Subomi Oluwalana
c8fa599983 Update README.md (#970)
There were errors in the examples highlighted, check issue #969
2019-08-22 02:31:44 -05:00
renovate[bot]
0d9ed366b1 [dist] Update dependency request to ~2.88.0 [SECURITY] (#1357) 2019-08-22 02:30:06 -05:00
renovate[bot]
9d75b981a1 [dist] Update dependency eventemitter3 to v4 (#1365) 2019-08-22 02:29:37 -05:00
renovate[bot]
192b2b980b [dist] Update dependency colors to v1 (#1360) 2019-08-22 02:29:14 -05:00
renovate[bot]
4a657a7126 [dist] Update all non-major dependencies (#1356) 2019-08-22 02:24:41 -05:00
renovate[bot]
7a154f81d1 [dist] Update dependency agentkeepalive to v4 (#1358) 2019-08-22 02:24:25 -05:00
renovate[bot]
749eec65c3 [dist] Update dependency nyc to v14 (#1367) 2019-08-22 02:21:15 -05:00
renovate[bot]
e588213644 [dist] Update dependency concat-stream to v2 (#1363) 2019-08-22 02:16:50 -05:00
indexzero
59c4403e9d [fix] Latest versions. 2019-08-22 02:12:51 -05:00
indexzero
dd1d08b631 [fix test] Update tests. 2019-08-22 02:12:51 -05:00
indexzero
16d4f8a951 [dist] Regenerate package-lock.json. 2019-08-22 02:12:51 -05:00
indexzero
fc93520d74 [dist] .gitattributes all the things. 2019-08-22 02:12:51 -05:00
indexzero
7e4a0e511b [dist] New test fixtures. 2019-08-22 02:12:51 -05:00
indexzero
a9b09cce43 [dist] End of an era. 2019-08-22 02:12:51 -05:00
Renovate Bot
b00911c937 [dist] Update dependency ws to v3 [SECURITY] 2019-08-22 02:12:51 -05:00
Jaggernoth
36bfe566a7 x-forwarded-host overwrite for mutli level proxies (#1267)
With more than 1 proxy the original host was lost, now it will be passed down the proxy chain
2019-08-22 02:09:26 -05:00
Charlie Robbins
91fee3e943
[refactor doc] Complete rename to http-party org. (#1362) 2019-08-22 01:46:16 -05:00
Justin Russell
235f0aa047 Highlight correct lines for createProxyServer (#1117) 2019-08-22 01:41:47 -05:00
Stein Martin Hustad
acdbec09c6 Fix docs for rewrite options - 201 also handled (#1147)
Updates options documentation for location rewrite to include 201 responses. See #1024
2019-08-22 01:40:03 -05:00
Marcin K
569e2ac4fb Update .nyc_output (#1339)
Currently published version includes .nyc_output and coverage
2019-08-22 01:39:17 -05:00
renovate[bot]
cb3171abfa Configure Renovate (#1355)
* Add renovate.json

* [dist] Configure renovate more.
2019-08-22 01:38:48 -05:00
任侠
a3fe02d651 [examples] Restream body before proxying, support for Content-Type of application/x-www-form-urlencoded (#1264) 2018-06-06 12:39:50 -04:00
Jarrett Cruger
42e8e1e099 1.17.0 2018-04-20 11:42:55 -04:00
Jarrett Cruger
e94d52973a [dist] doc updates 2018-04-20 11:35:07 -04:00
Jarrett Cruger
4a37175a52 [test] add test for selfHandleRequest and remove modifyResponse as selfHandleRequest is the only way that functionality works 2018-04-20 11:35:07 -04:00
guoxiangyang
e5c02b8a8a add support for modify response 2018-04-20 11:35:07 -04:00
Jake Furler
2c44039a7c issue #953: stop using writeHead
object.keys in web-incoming.js results in a non-deterministic ordering of keys, which means that in web-outgoing writeHead might be called before setHeader, which throws an error
2018-04-20 11:35:07 -04:00
Thiago Bustamante
8097ae237e Fix "Can't set headers after they are sent" errors
This PR tries to fix "Can't set headers after they are sent" errors.
That are a lot of situations where this error can occurs. In my case, it is happening because I have others middlewares (in an expressjs application that tries to proxy requests). Some of those middlewares (like [passportjs](http://passportjs.org/), or [cors](https://www.npmjs.com/package/cors)) can run ```res.end()``` and when the proxy receive a response, it is already finished.
So, it is necessary to test if we can write on the user response when the proxy response is ready.
I think it could also fix #930, #1168, #908
2018-04-20 11:35:07 -04:00
Jarrett Cruger
abf882e03c [dist] update package-lock.json 2018-04-20 11:35:07 -04:00
Gustav Tiger
bab02e909e Include websocket non-upgrade response
When the server do not accept the upgrade request for websockets the
server's response was previously not included and sent back. Now the
proxy will include the response in these cases. Fixes #890.
2018-04-20 11:35:07 -04:00
n30n0v
c9a556cfa5 Add followRedirects option 2018-04-20 11:35:07 -04:00
Jon Hunter
6f88caf6e4 Add detail about "buffer" option 2018-04-20 11:35:07 -04:00
Radu Serbanescu
d2f9db8241 Add use case for proxy to HTTPS using a PKCS12 client certificate 2018-04-20 11:35:07 -04:00
carpsareokiguess
8231984fb0 fix small typos in README 2018-04-20 11:35:07 -04:00
Jarrett Cruger
81d58c531b [test] for override method feature 2018-04-20 11:35:07 -04:00
Jarrett Cruger
d533a1be43 [dist] document the feature 2018-04-20 11:35:07 -04:00
shaohui.tsh
89f9ef87e0 feat: 添加response自处理参数 2018-04-20 11:35:07 -04:00
Aydin
c5d8466483 Update common.js
Add method parameter to options for overriding the proxy-outgoing HTTP-method
2018-04-20 11:35:07 -04:00
jlaamanen
107c18720c Added timeout option to docs 2018-04-20 11:35:07 -04:00
Jarrett Cruger
de1b80851a [fix] slightly more tolerant 2018-04-20 11:35:07 -04:00
Sean Willis
bc6a23709c Removing unnecessary check since this is a private API 2018-04-20 11:35:07 -04:00
Sean Willis
f5c2381395 Updating docs and adding more tests. 2018-04-20 11:35:07 -04:00
Sean Willis
50f58b4cd9 Forgot 'i' flag when changing from regex shorthand to string. 2018-04-20 11:35:07 -04:00
Sean Willis
2c98416ac2 Adding ability to set cookie path 2018-04-20 11:35:07 -04:00
Jarrett Cruger
543636d0f6 [fix] move badges 2018-04-20 11:35:07 -04:00
Jarrett Cruger
a4bccc332d [dist][test] codecov config 2018-04-20 11:35:07 -04:00
Jarrett Cruger
f4ff1006b9 [wip] proper tests and reporting 2018-04-20 11:35:07 -04:00
Jarrett Cruger
09dcb98456 [dist] make tests work reliably, add package-lock.json 2018-04-20 11:35:07 -04:00
Georgi Yordanov
812757541d Fix overwriting of global options (#1074) 2018-01-02 19:48:17 -05:00
Elad Ben-Israel
c979ba9f2c Update README.md (#1131)
Update link to properties
2017-01-11 15:53:05 -05:00
Jarrett Cruger
e6f24ba617 [fix] rm newline 2016-12-24 17:03:33 -05:00
Ivan Nieto
d73f1ee873 Update README.md with CoC link (#1120)
* Add Code Of Conduct

* Update CODE_OF_CONDUCT.md

Fix placeholder

* Update REAME.md
2016-12-24 17:02:41 -05:00
Ivan Nieto
a539f3cbc1 Add Code Of Conduct (#1119)
* Add Code Of Conduct

* Update CODE_OF_CONDUCT.md

Fix placeholder
2016-12-24 13:27:54 -05:00
Luigi Pinca
d4d85ac5c4 [deps] Update eventemitter3 to version 2.0.x (#1109) 2016-12-16 12:28:52 -05:00
Jarrett Cruger
c1fb596b85 1.16.2 2016-12-06 10:49:02 -05:00
Yuta Shimizu
961f457622 [WIP] Revert default behavior of writeHeaders method (#1104)
* Replace header key only

* Add preserveHeaderKeyCase Option
2016-12-06 09:51:01 -05:00
Jarrett Cruger
ac1a01b1f3 [dist] Version bump. 1.16.1 2016-12-04 10:59:46 -05:00
Kris Williams
8cb451f20c Enable proxy response to have multiple Set-Cookie raw headers #1101 2016-12-04 10:58:49 -05:00
Jarrett Cruger
c252b32f6c 1.16.0 2016-12-02 09:13:10 -05:00
Maarten ter Horst
927357bedc Fix newly introduced error in error handler for ECONNREFUSED in forward proxy (#1100) 2016-12-02 09:09:11 -05:00
Yuta Shimizu
4edbb62cc5 Keep original letter case of response header keys (#1098)
* Keep original letter case of response header keys

* Support node older than v0.11.6

messege.rawHeaders was added in v0.11.6

* Extract duplicated logic to method
2016-12-01 20:03:13 -05:00
Maarten ter Horst
69cf892519 Handle errors for forward request, add test case (#1099) 2016-12-01 09:39:46 -05:00
purificant
2f7f03778c add node 6 to travis 2016-11-29 09:01:06 -05:00
Jarrett Cruger
d8223884f6 1.15.2 2016-10-22 11:47:23 -04:00
Jarrett Cruger
d0f1dfeb82 [fix] style nits 2016-10-22 11:45:16 -04:00
François Leurent
220f5fb795 Expose full callback names 2016-10-22 11:45:16 -04:00
François Leurent
d48f67eb90 Do not rely on func.name (no scope) 2016-10-22 11:45:16 -04:00
François Leurent
61c2889109 Do not rely on func.name (no scope) 2016-10-22 11:45:16 -04:00
François Leurent
fbc266809c With a comment 2016-10-22 11:45:16 -04:00
François Leurent
8eddf45f2a Fix browserification
Browserify fails to resolve the "./http-proxy/" as "./http-proxy/index.js" but as "./http-proxy.js" (so nothing works)
Beeing explicit here does not cost much for http-proxy, yet it's intrinsically complicated for browserify to fix (as trailing slash might be used as a pollyfill shim for native/non-natives addons i.e.  require('url/') vs require('url') )
2016-10-22 11:45:16 -04:00
Niranjan Ojha
f5217d6c20 test case added 2016-10-06 16:24:44 -04:00
Niranjan Ojha
2d01edc5a5 not setting connection header in case of http2 as it is deprecated 2016-10-06 16:24:44 -04:00
Ashish Dahiya
d0a1588639 Add proxy-timeout option to documentation (#1075)
http-proxy provides a [proxyTimeout option](https://github.com/nodejitsu/node-http-proxy/blob/master/lib/http-proxy/passes/web-incoming.js#L122) that allows us to set a timeout on the outgoing socket connection to the target. This timeout is very effective when the upstream target does not respond within an expected time.

I had wasted a few hours searching for this option. Documenting this option can save others a significant amount of time.
2016-10-04 09:54:52 -04:00
Jarrett Cruger
912cd3acae [dist] Version bump. 1.15.1 2016-09-14 17:12:30 -04:00
briman0094
84087208dd Properly write response header optionally including statusMessage (#1061) 2016-09-14 17:08:26 -04:00
Jarrett Cruger
b98c75b1ff [dist] Version bump. 1.15.0 2016-09-14 13:05:56 -04:00
Alex Indigo
b781af641a Made it not to crash with omited Host http header (#1050) 2016-09-14 07:03:48 -04:00
Michael Ira Krufky
cbd5777060 README.md: fix typo: 'ingoing' should be 'incoming' (#1060) 2016-09-14 07:02:30 -04:00
Cole Chamberlain
d8fb344715 Fix for Reason-Phrase being overwritten on proxy response. (#1051)
* Fix for Reason-Phrase being overwritten on proxy response.

Calling res.writeHead has the side effect of defaulting the Reason-Phrase to default ones.  I'm using Reason-Phrase codes to sub-route api responses and they were all being reset.  This change only sets the statusMessage (Reason-Phrase) if it exists on the proxyRes and is successfully passing it through in my tests.

* Fixed spaces and bracket formatting.
2016-09-13 18:19:33 -04:00
Jeremy Judeaux
1e52f660f0 cookieDomainRewrite option (#1009) 2016-08-12 13:42:18 -04:00
wuchangming
9d06ca99d3 Update ntlm-authentication.js (#1025) 2016-08-11 12:37:57 -04:00
Mati B. Terefe
d0e000e1f9 Restream body before proxying (#1027)
Support for bodyparser.json and bodyparser.urlencoded.

Fixes #955 #843 #791
2016-08-11 12:37:32 -04:00
Gabriel Boucher
183b5bb4fc Location rewriting for responses with status 201 (#1024)
Implement rewriting of the location header for responses with status code 201, according to RFC2616 section 10.2.2
2016-08-11 12:36:17 -04:00
Ken
3a347af543 #866 Copy CA from options into outbound proxy (#1042)
While using secure: true for proxy connections, there is no way to pass the trusted root CA(s) or intermediate CA(s). This change allows that to be passed in the httpProxy createServer options and used for the outgoing proxy connection.
2016-08-11 12:20:35 -04:00
Jarrett Cruger
fcfb0b37f6 [dist] Version bump. 1.14.0 2016-06-15 10:52:53 -04:00
Deividy Metheler
42df703830 Emit disconnected event instead of error when ECONNRESET (#966)
* Emit disconnected event instead of error when ECONNRESET

ECONNRESET means the other side of the TCP conversation abruptly
closed its end of the connection.

If we get an ECONNRESET error from the proxy request and the
socket is destroyed this means that the client has closed
his connection, and emit this as an error can lead to
confusion and hacks to filter that kind of message.

I think that the best we can do is abort and emit another event,
so if any caller wants to handle with that kind of error, he can
by listen the disconnected event.

https://github.com/nodejitsu/node-http-proxy/issues/813

* code standards, move econnreset check, change ev name
2016-06-03 13:39:40 -04:00
Jeremy Judeaux
3e966361bc fix test for node 0.10 + socket.io-client@1.4.6 (engine.io-client@1.6.9) (#1010) 2016-05-25 12:18:51 -04:00
Jarrett Cruger
5082acc067 [dist] Version bump. 1.13.3 2016-05-15 23:14:51 -04:00
idjem
433a7408cf fix browserify compatibility (#975) 2016-05-15 15:40:01 -04:00
Muromi Ukari
6baf1498cb alter message error (#998)
message error about port 9001 -> port 9002
2016-04-28 09:15:03 -07:00
Mihai Ene
284903d379 Sanitize header keys before setting them (#997)
Fixes #996.
2016-04-26 13:26:35 -07:00
Jarrett Cruger
b223275ebb Merge pull request #989 from aroder/patch-1
Update ntlm-authentication.js
2016-04-26 13:23:33 -07:00
Adam Roderick
ecdfff408f Update ntlm-authentication.js
missing bracket
2016-04-14 16:21:39 -05:00
Jarrett Cruger
28cecb5a96 Merge pull request #983 from deanshelton913/add-datatype-to-readme
Add expected datatype to readme
2016-03-23 18:36:21 -04:00
deanshelton913
bdb3492b21 Add expected datatype to readme
This confused me while attempting to use this feature
2016-03-23 15:19:50 -07:00
Jarrett Cruger
a4b2857216 Merge pull request #982 from kylehayes/patch-1
Update README
2016-03-22 15:13:09 -04:00
Kyle Hayes
820fc5987c Update README
For clarity
2016-03-22 08:47:14 -07:00
indexzero
f345a1ac2d [dist] Update LICENSE to reflect 2015 changes. 2016-03-12 00:05:21 -08:00
Jarrett Cruger
018352955e Merge pull request #974 from bmac/patch-1
Fix formatting of the `headers` option
2016-03-08 17:26:38 -08:00
Brendan McLoughlin
f8d605d53f Fix formatting of the headers option 2016-03-08 18:07:11 -05:00
Jarrett Cruger
c1440b58f1 Merge pull request #967 from jbacklund/patch-1
Set the x-forwarded-host flag when xfwd is enabled
2016-02-26 09:52:26 -08:00
Jakob Backlund
ef86b50427 Set the x-forwarded-host flag when xfwd is enabled
Reasoning: Rack's request class [makes use of](https://github.com/rack/rack/blob/master/lib/rack/request.rb#L243) this HTTP header. Certain edge-case scenarios (proxying from ember-cli to a Rails backend) can be problematic without this header being present.

/cc @perlun, @jesjos
2016-02-26 14:29:55 +02:00
Jarrett Cruger
e1b2f4c31b [dist] Version bump. 1.13.2 2016-02-17 11:00:08 -05:00
Jarrett Cruger
820f198729 Merge pull request #806 from chimurai/master
Fixed missing documentation: #options.headers
2016-02-16 18:05:28 -05:00
Jarrett Cruger
d2df053677 Merge pull request #950 from caioquirino/master
#949 Proxy example using req instead res on README
2016-02-15 10:36:24 -05:00
Jarrett Cruger
7357200334 Merge pull request #962 from Turbo87/default-mocha-reporter
mocha: Use default reporter
2016-02-15 10:35:41 -05:00
Jarrett Cruger
d19be83858 Merge pull request #961 from Turbo87/transfer-encoding-fix
Remove "transfer-encoding" header if "content-length" is set to zero
2016-02-15 10:35:32 -05:00
Tobias Bieniek
570cf3b4f9 mocha: Use default reporter
Unfortunately the "landing" reporter doesn't really look good on CI servers at all...
2016-02-15 15:47:46 +01:00
Tobias Bieniek
5c46e4b754 Remove "transfer-encoding" header if "content-length" is set to zero 2016-02-15 15:43:14 +01:00
Caio Quirino da Silva
25e56d0182 #949 Proxy example using req instead res on README
README example implies request instead of response parameter
2016-02-02 11:56:39 -02:00
Jarrett Cruger
9d9fa940cf [dist] Version bump. 1.13.1 2016-02-01 21:02:33 -08:00
Jarrett Cruger
2831d9a4f9 Merge pull request #932 from afriza/patch-1
README.md: summary to specify reverse proxy
2016-02-01 20:59:49 -08:00
Jarrett Cruger
a379d160f3 Merge pull request #947 from coderaiser/fix-urlJoin
fix(common) urlJoin replace: ":/" -> "http?s:/"
2016-02-01 20:59:18 -08:00
Jarrett Cruger
7aed1f4559 Merge pull request #948 from Alfredo-Delgado/patch-1
Update README.md
2016-02-01 20:58:45 -08:00
Alfredo Delgado
bfcab93d8d Update README.md
Fixed typo.
2016-01-30 12:26:26 -05:00
coderaiser
7bad3fbca4 fix(common) urlJoin replace: ":/" -> "http?s:/" 2016-01-29 02:47:12 -05:00
Jarrett Cruger
268994ea45 [dist] Version bump. 1.13.0 2016-01-26 17:11:50 -05:00
Jarrett Cruger
58c0fdc761 Merge pull request #934 from Torthu/master
Fix for #839 (Ignore path and the trailing slash)
2016-01-26 15:56:58 -05:00
Charlie Robbins
d73b0d312a Merge pull request #943 from pra85/patch-1
Update license year range to 2016
2016-01-26 01:02:52 -08:00
Prayag Verma
a76e226221 Update license year range to 2016 2016-01-26 14:12:24 +05:30
Jarrett Cruger
db576d75c1 Merge pull request #942 from merpnderp/patch-1
Bump version for npm publish
2016-01-24 15:19:54 -05:00
Kaleb Murphy
e017fa8189 Bump version for npm publish
Would be great to get the NTLM fix into npm.
2016-01-24 14:11:12 -06:00
Jarrett Cruger
646a7493a4 Merge pull request #940 from merpnderp/FixSetHeader
Added check to passes/web-outgoing.js to make sure the header being s…
2016-01-23 12:27:49 -05:00
merpnderp
3b39d2c3dc Added check to passes/web-outgoing.js to make sure the header being set is not undefined, which should be the only falsey value that could accidently show up and break that call. This fixes windows NTLM auth issues behind http-proxy. 2016-01-22 09:17:14 -06:00
Torstein Thune
0cb1d3c68e Added note for appending trailing / when using ignorePath 2016-01-13 15:27:51 +01:00
Torstein Thune
f9540de7b1 Fixed tests depending on ignorePath 2016-01-13 15:24:46 +01:00
Torstein Thune
f2093b5313 No longer appends / to path if ignorePath is set 2016-01-13 09:16:50 +01:00
Afriza N. Arief
41414a56a1 README.md: introduction to specify reverse proxy
clarify proxy type to be reverse proxy in the introduction
2016-01-04 12:40:05 +08:00
Charlie Robbins
6371231086 Merge pull request #825 from pose/patch-1
Created reverse-proxy.js example.
2015-12-29 23:53:21 -05:00
Jarrett Cruger
6c83980f29 Merge pull request #922 from aaronmaturen/sse
SSE example and test
2015-12-07 15:58:22 -05:00
Aaron T. Maturen
e4760727f1 SSE example and test 2015-12-06 19:34:42 -05:00
Charlie Robbins
222a4d0fa1 Merge pull request #912 from nodejitsu/more-structured-readme
More structured readme
2015-11-23 13:12:37 -05:00
donasaur
6106d4c32f Added back to top helpers 2015-11-23 09:46:15 -08:00
donasaur
cd1d7776e8 Organized README more 2015-11-23 09:30:05 -08:00
Jarrett Cruger
642e9cc2cb Merge pull request #910 from nodejitsu/proxy-table-readme
Updated markdown docs to mention proxy rules module
2015-11-22 14:19:09 -05:00
donasaur
eea79cab53 Updated markdown docs to mention proxy rules 2015-11-20 17:28:37 -08:00
Jarrett Cruger
f82ce18d2f [ci] use node 4.2 to test and do not allow failures 2015-11-09 19:43:18 -08:00
Jarrett Cruger
470475b896 Merge pull request #901 from jedverity/master
Add tests for forwarding of continuation frames
2015-11-09 14:53:05 -08:00
Jarrett Cruger
2a5e69d48c Merge pull request #904 from shinnn/deps
Bump requires-port, server and ws
2015-11-06 13:20:22 -08:00
Shinnosuke Watanabe
9ea1e89a2f [fix] bump requires-port, server and ws
npm v3 tries to dedupe the dependencies by default, and keeping
dependencies up-to-date helps better deduplication.

https://github.com/unshiftio/requires-port
https://github.com/npm/node-semver
https://github.com/websockets/ws
2015-11-05 17:49:45 +09:00
Jarrett Cruger
ab42124d54 Merge pull request #903 from nodejitsu/ntlm-authentication
[example] add an example for NTLM authentication
2015-10-30 14:16:10 -07:00
Jarrett Cruger
5d593e8ef1 [example] add an example for NTLM authentication 2015-10-30 14:14:03 -07:00
glortho
64fa520789 Add tests for testing forwarding of continuation frames
This adds two tests that send payloads below and at the threshold for continuation frames. Using node 0.12.7 both tests pass. Using node 4.1.2 the test below the threshold passes but the other fails.
2015-10-28 15:52:37 -04:00
Jarrett Cruger
b5a6d0e583 [dist] Version bump. 1.12.0 2015-10-22 19:27:24 -04:00
Jarrett Cruger
0bc4c783ca Merge pull request #897 from lbrucher/issue-896
Issue #896: provide a "proxyReq" event also for websocket connections.
2015-10-22 19:26:02 -04:00
Laurent Brucher
9752652e76 fixes after PR review 2015-10-22 18:38:58 +02:00
Laurent Brucher
a05fc2d169 Provide a "proxyReq" event also for websocket connections. 2015-10-22 15:56:34 +02:00
Jarrett Cruger
60baca5aed [dist] Version bump. 1.11.3 2015-10-19 09:30:18 -04:00
Charlie Robbins
6b65c428b3 Merge pull request #893 from donasaur/master
Removed unspecified trailing slash in proxy url
2015-10-18 16:15:53 -05:00
donasaur
eb97bf5423 Removed unspecified trailing slash in proxy url 2015-10-18 13:04:06 -07:00
Jarrett Cruger
ec64e4f597 Merge pull request #892 from donasaur/patch-1
Updating the upgrading doc
2015-10-18 10:45:45 -04:00
Don Mai
e666a4e07d Updating the upgrading doc
Recently ran into a case where I had to upgrade the `http-proxy` version for an app, and that app was using a proxy table.

I'm not sure how many users are still using the 0.x.x version of `http-proxy`, but the added link (found from one of the GH issues) may encourage them to switch over to 1.x.x if they were using the old version due to its proxy table support and the activation energy to upgrade was too high.

I might release a module for this eventually, lol, since there was some work involved in creating a proxy table to map paths to paths for the same hostname.
2015-10-18 01:12:19 -07:00
chimurai
c86ae51bb9 docs: options.headers 2015-09-26 23:46:11 +02:00
indexzero
302d981dd2 [dist] Update .travis.yml to be more modern. 2015-09-21 18:22:31 -07:00
Jarrett Cruger
30e3b371de [dist] Version bump. 1.11.2 2015-08-30 17:29:25 -04:00
Jarrett Cruger
cea0e8676b [fix] make more functional 2015-08-30 17:28:05 -04:00
Arttu Liimola
3d2350c54f Replaced Object.keys().map with for in loop. 2015-08-30 17:16:42 -04:00
Arttu Liimola
ca73208749 Websocket key was unnecessary long. 2015-08-30 17:16:36 -04:00
Arttu Liimola
da674ec4df Modify the set-cookie header fix to work with node 0.10.x. 2015-08-30 17:16:07 -04:00
Arttu Liimola
855cebdac4 Added websocket set-cookie headers test 2015-08-30 17:16:00 -04:00
Arttu Liimola
8bfd90c4d9 Use raw headers instead parsed.
Set-Cookie headers are parsed into single header with cookies in array.
This messes up the Set-Cookie headers, because browser receives multiple Set-Cookie headers as single with cookies separted with comma.
2015-08-30 17:15:54 -04:00
Charlie Robbins
931f73dc98 Merge pull request #870 from justsml/patch-1
Update gzip-middleware.js
2015-08-24 12:38:27 -07:00
Daniel Levy
349b843731 Update gzip-middleware.js
Punctuation
2015-08-24 13:23:53 -06:00
Charlie Robbins
2e3eb0950d Merge pull request #863 from stuartpb/patch-1
Fix broken option list indentation
2015-08-14 12:30:42 -07:00
Stuart P. Bentley
1f4fccd2c6 Fix broken option list indentation 2015-08-14 09:02:50 -07:00
Jarrett Cruger
ecbba1a008 Merge pull request #852 from jpatters/patch-1
Added missing configuration options
2015-07-09 09:55:36 -04:00
Jordan
fe3dd8363f Added missing configuration options
Added missing config options for httpProxy.createProxyServer. Updated to include all options listed in `lib/http-proxy.js`
Addresses nodejitsu/node-http-proxy#851
2015-07-09 08:03:23 -03:00
Alberto Pose
38864d0167 Created reverse-proxy.js example. 2015-05-18 20:30:43 -03:00
Jarrett Cruger
76051032e7 Merge pull request #823 from montogeek/patch-1
Added installation instructions
2015-05-13 10:24:09 -07:00
Fernando Montoya
7e82a04a16 Added installation instructions 2015-05-12 21:38:05 -05:00
Jarrett Cruger
1912b62ddb Merge pull request #817 from klammbueddel/master
fixes comment
2015-05-09 14:23:03 -07:00
klammbueddel
3f997b9289 fixes comment 2015-04-30 10:19:40 +02:00
Jarrett Cruger
7e6c66a7e4 [dist] Version bump. 1.11.1 2015-04-22 11:09:36 -04:00
Jarrett Cruger
d26ef56e1b [fix] dont use bind in the one case we do 2015-04-22 11:09:05 -04:00
Jarrett Cruger
607f96c00c [dist] update to new version of EE3 2015-04-22 11:07:24 -04:00
Jarrett Cruger
18c77cafc7 [fix] use the main export for EE3 2015-04-22 11:07:13 -04:00
Jarrett Cruger
934e6c4d54 [dist] Version bump. 1.11.0 2015-04-20 16:48:14 -04:00
Jarrett Cruger
0db8f195d7 Merge pull request #759 from nodejitsu/ignore-path
[api] add an ignorePath option if you want to disregard the path of the ...
2015-04-20 16:47:27 -04:00
Jarrett Cruger
0bd446c680 [dist] Version bump. 1.10.1 2015-04-02 12:41:02 -04:00
Jarrett Cruger
a6ae6c4997 [ci] add 0.12 and iojs to travis 2015-04-02 12:32:08 -04:00
Jarrett Cruger
1b89bc9a76 [dist] add semver and normalize package.json with --save-dev 2015-04-02 12:31:17 -04:00
Jarrett Cruger
c6dfb04a67 [fix] properly support iojs with test checking for HTTPS 2015-04-02 12:30:55 -04:00
Jarrett Cruger
6201ac76f7 Merge pull request #799 from F4-Group/fix_https
Fix default port detection with node 0.12.x
2015-04-02 12:05:03 -04:00
Jeremy Judeaux
5f14bcaa70 fix protocol and default port detection on node 0.12.x, compatible with 0.10.x 2015-04-02 14:23:58 +02:00
Jeremy Judeaux
0ee314c436 fix expected error message when node 0.12.x 2015-04-02 14:22:26 +02:00
Jeremy Judeaux
c33d1616cd force cipher AES128-GCM-SHA256 in https tests 2015-04-02 14:11:45 +02:00
Jarrett Cruger
1dabda241f [dist] Version bump. 1.10.0 2015-04-01 12:24:26 -04:00
Jarrett Cruger
5a969d077b Merge pull request #787 from mokafive/master
Fixes / additions to URL rewriting
2015-04-01 12:20:37 -04:00
Jarrett Cruger
21b30b754d [dist] Version bump. 1.9.1 2015-04-01 12:09:42 -04:00
Jarrett Cruger
aa8f3e9a6e Merge pull request #798 from damonmcminn/master
Fix #747
2015-04-01 11:07:42 -04:00
Damon McMinn
ab37a224aa Fix https://github.com/nodejitsu/node-http-proxy/issues/747 2015-04-01 13:19:03 +01:00
Damon McMinn
d145152655 Add test for https://github.com/nodejitsu/node-http-proxy/issues/747 2015-04-01 13:14:11 +01:00
Jarrett Cruger
87a92a7280 [dist] Version bump. 1.9.0 2015-03-12 18:59:32 -04:00
Jarrett Cruger
507f818df5 Merge pull request #792 from ashubham/master
Adding the nodejs0.12 auth option
2015-03-12 18:58:06 -04:00
ashubham
e907d7bb2a end of file line space 2015-03-12 15:17:26 -07:00
ashubham
7298510e91 space instead of tabs 2015-03-12 15:16:17 -07:00
ashubham
63c9262df5 space instead of tabs 2015-03-12 15:14:49 -07:00
ashubham
ff1626f071 added auth header test 2015-03-12 15:12:53 -07:00
ashubham
df158bfc53 added auth header test 2015-03-12 15:11:56 -07:00
ashubham
f55ffa356a auth header added tests 2015-03-12 13:40:49 -07:00
ashubham
ab5c3e5c81 auth header added 2015-03-12 13:15:06 -07:00
Jarrett Cruger
245d73ae6c Merge pull request #789 from feross/master
fix "x-forwarded-proto" in node 0.12 and iojs
2015-03-11 21:07:38 -04:00
Feross Aboukhadijeh
6d074eff47 fix "x-forwarded-proto" in node 0.12 and iojs
The way to detect TLSSockets in node 0.12 and iojs has changed. You can
just check `socket.encrypted` now :)

Fixes #772
2015-03-11 00:33:34 -07:00
Matt Hauck
26029ba7ac only rewrite redirect urls when it matches target
if functioning as a reverse proxy for host1.foo.com,
with a backend target of backend.foo.com:8080, the
node proxy should only rewrite the redirect if it is
a redirect to somewhere on backend.foo.com:8080
2015-03-09 13:17:52 -07:00
Matt Hauck
14415a5074 refactor some tests for greater readability 2015-03-09 11:49:28 -07:00
Matt Hauck
62e4b75101 Merge pull request #1 from matthauck/master
Add support for auto host rewriting and protocol rewriting
2015-02-05 12:33:10 -08:00
Matt Hauck
7f2f3ac35c Add support for auto host rewriting and protocol rewriting
auto host rewriting allows rewriting to work as expected in most
cases without extra cumbersome configuration

protocol rewriting allows node-http-proxy to be able to listen
over HTTPS and properly reverse-proxy to sites running over HTTP
(to avoid doing SSL twice)
2015-02-05 12:17:26 -08:00
Jarrett Cruger
9eefd4678e [api] add an ignorePath option if you want to disregard the path of the incoming request when proxying to the target server fixes #758 2014-12-23 13:22:49 -05:00
Jarrett Cruger
9ece52fac4 Merge pull request #756 from PanManAms/patch-1
changed highlighted part - very minor
2014-12-22 12:02:36 -05:00
PanManAms
32aa10dfe2 changed highlighted part - very minor 2014-12-20 15:40:46 +01:00
Charlie Robbins
c82c9ece8a Merge pull request #625 from bkochendorfer/master
Update README.md for benchmarks
2014-12-19 02:01:53 -07:00
Jarrett Cruger
3311106c2c [dist] Version bump. 1.8.1 2014-12-17 10:11:42 -07:00
Alistair Jones
402ab05734 Pass HTTPS client parameters.
For more detailed control over HTTPS client behaviour, pass through the
parameters listed here:
http://nodejs.org/api/https.html#https_https_request_options_callback

The `rejectUnauthorized` parameter is omitted, because it overlaps with
the existing `secure` parameter:
https://github.com/nodejitsu/node-http-proxy/blob/master/README.md#using-https

Conflicts:
	test/lib-http-proxy-common-test.js
2014-12-17 10:09:29 -07:00
Jarrett Cruger
f0db5b3f70 [dist] Version bump. 1.8.0 2014-12-17 00:58:17 -07:00
Jarrett Cruger
f30486195c [minor] grammar 2014-12-17 00:57:43 -07:00
Jarrett Cruger
ea0a4ded80 [fix] style spacing wtf 2014-12-17 00:53:51 -07:00
Jorge Leal
8a8a894092 Changed proxyServer and destiny to local variables. 2014-12-17 00:52:41 -07:00
Jorge Leal
c62610e8e4 Deprecated proxySocket event in favor to open event.
Maintained both proxySocket and open for backward compatibility.

Conflicts:
	test/lib-http-proxy-test.js
2014-12-17 00:52:19 -07:00
Jorge
05d18a4e1b Update README.md 2014-12-17 00:51:24 -07:00
Jorge Leal
8bff3ddc12 Added websocket close event test 2014-12-17 00:51:19 -07:00
Jorge
26537866b3 [api] add close event in ws-incoming.js 2014-12-17 00:51:06 -07:00
Charlie Robbins
f92a1b6839 Merge pull request #752 from ezhdan-sugarcrm/tests-fix
Fix variables scope in test
2014-12-13 14:34:45 -07:00
Eugene Zhdan
c1a94176a8 Fix variables scope in test 2014-12-13 21:45:14 +03:00
Arnout Kazemier
099eb86948 Merge pull request #751 from seanhussey/patch-1
Fix typo
2014-12-11 17:26:24 +01:00
Sean Hussey
4bd3c4671f Fix typo 2014-12-11 11:02:09 -05:00
Jarrett Cruger
6a330ff904 [dist] Version bump. 1.7.3 2014-12-08 23:05:01 -05:00
Jarrett Cruger
89f9ca1e89 [test] show that we support protocol without the colon 2014-12-08 23:04:41 -05:00
Jarrett Cruger
c04485671a [fix] use simple regex instead of indexOf to check the protocol to support without the colon fixes #711 2014-12-08 23:04:23 -05:00
Jarrett Cruger
2086e4917c [dist] Version bump. 1.7.2 2014-12-08 16:16:05 -05:00
Jarrett Cruger
71a06aab02 [test] add tests for the changeOrigin cases in properly setting the host header 2014-12-08 16:14:48 -05:00
Jarrett Cruger
501e8c2a9b [fix] properly include port in host header with changeOrigin in all cases fixes #750 2014-12-08 16:14:16 -05:00
Jarrett Cruger
81874f795b [dist] pin down deps and add requires-port 2014-12-08 16:11:28 -05:00
Charlie Robbins
75a9a2de61 Merge pull request #749 from kblanks/patch-1
Fix grammar in README.md
2014-12-03 04:51:11 -07:00
Kailan Blanks
aeb42a3614 Fix grammar in README.md 2014-12-03 11:49:19 +00:00
Jarrett Cruger
56a7b77645 [dist] Version bump. 1.7.1 2014-12-02 10:28:04 -07:00
Jarrett Cruger
9c0b8697bc [fix] fix #738 2014-12-02 10:25:46 -07:00
Jarrett Cruger
410a8ce94c [test] add proper failing test case for #738 2014-12-02 10:25:30 -07:00
Jarrett Cruger
d98d9516ea [fix] simple fixes #748 #744 #746 2014-12-02 09:58:06 -07:00
koolc
70ed1c4273 [Bugfix] Allow for multiple ? in outgoing urls.
Without this fix urls that had multiple ? in them would drop sections
of the url since before there was an assumption of there only being one.
2014-12-01 12:34:16 -05:00
Jarrett Cruger
361d4e3b00 Merge pull request #716 from No9/master
Adding harmon to the README
2014-11-28 12:02:39 -05:00
Jarrett Cruger
276f65a3b8 [dist] Version bump. 1.7.0 2014-11-25 17:31:28 -05:00
Jarrett Cruger
8d68ac0e0f [fix] be defensive and ensure location is in headers before running url.parse() 2014-11-25 17:22:23 -05:00
Jarrett Cruger
48ae5d828c [minor] style consistency 2014-11-25 17:21:24 -05:00
Jarrett Cruger
95a588706b Merge pull request #741 from samccone/sjs/redirect-host-rewrite
Allow optional redirect host rewriting.
2014-11-25 17:08:54 -05:00
Jarrett Cruger
3194d819b4 Merge pull request #742 from jugglinmike/option-content-length
Set `Content-Length` header for OPTIONS requests
2014-11-25 11:15:46 -05:00
Mike Pennisi
8a24a1e18f Set Content-Length header for OPTIONS requests
Web browsers automatically issue an OPTIONS request in advance of other
HTTP requests when the document's domain does not match the target in
order to facilitate Cross-Origin Resource Sharing. These requests are
forbidden from specifying a body and typically do not specify an
(unecessary) `Length` header.

Node.js automatically adds the `Content-Encoding: chunked` header value
to requests that do not specify a `Length` header. This makes proxied
OPTIONS requests from web browsers invalid.

Explicitly set the `Content-Length` header of all `OPTIONS` requests to
"0", disabling the default "chunked" encoding behavior [2].

[1] http://www.w3.org/TR/cors/
[2] http://nodejs.org/api/http.html
2014-11-24 17:14:48 -05:00
Sam Saccone
add81338a9 📝 Add host rewrite docs and specs. 2014-11-23 19:22:07 -05:00
Sam Saccone
daf66a7a88 Allow optional redirect host rewriting. 2014-11-23 19:22:07 -05:00
Jarrett Cruger
aba505d159 Merge pull request #736 from richardkazuomiller/copy-headers
copy headers instead of referencing them so they don't unexpectedly get overwritten
2014-11-12 16:46:51 -05:00
Ricky Miller
84036e9ddd style changes 2014-11-13 06:05:32 +09:00
Ricky Miller
daa2ce0ee3 copy headers instead of referencing them so they don't unexpectedly get overwritten 2014-11-13 04:37:45 +09:00
Jarrett Cruger
69a693034e Merge pull request #735 from jleal52/master
Updated to support error callback on proxy.web and start/proxyReq/end co...
2014-11-12 13:05:04 -05:00
jleal52
9ba8311343 Updated to support error callback on proxy.web and start/proxyReq/end continue working. 2014-11-12 14:41:46 +00:00
no9
9f684d0439 harmon notes 2014-10-15 01:52:27 +01:00
Brett Kochendorfer
4947484806 Update README.md for benchmarks
Remove number of requests flag from `wrk` as it no longer exists. Swapped out for 5 minute duration.
2014-04-20 20:34:08 -05:00
54 changed files with 6015 additions and 433 deletions

6
.auto-changelog Normal file
View File

@ -0,0 +1,6 @@
{
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": true,
"commitLimit": false
}

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
package-lock.json binary

2
.gitignore vendored
View File

@ -6,3 +6,5 @@ notes
primus-proxy.js
tes.js
npm-debug.log
.nyc_output
coverage

View File

@ -2,6 +2,8 @@ test
examples
doc
benchmark
coverage
.nyc_output
.travis.yml
CHANGELOG.md
UPGRADING.md
UPGRADING.md

View File

@ -1,11 +1,12 @@
sudo: false
language: node_js
node_js:
- "0.10"
notifications:
email:
- travis@nodejitsu.com
irc: "irc.freenode.org#nodejitsu"
- "8"
- "10"
- "12"
script:
npm test
- npm test
after_success:
- bash <(curl -s https://codecov.io/bash)
matrix:
fast_finish: true

1872
CHANGELOG.md Normal file

File diff suppressed because it is too large Load Diff

74
CODE_OF_CONDUCT.md Normal file
View File

@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at <https://github.com/http-party/node-http-proxy>. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

View File

@ -1,7 +1,7 @@
node-http-proxy
Copyright (c) Nodejitsu 2013
Copyright (c) 2010-2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -20,4 +20,4 @@
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

354
README.md
View File

@ -1,37 +1,60 @@
<p align="center">
<img src="https://raw.github.com/nodejitsu/node-http-proxy/master/doc/logo.png"/>
<img src="https://raw.github.com/http-party/node-http-proxy/master/doc/logo.png"/>
</p>
node-http-proxy
=======
# node-http-proxy [![Build Status](https://travis-ci.org/http-party/node-http-proxy.svg?branch=master)](https://travis-ci.org/http-party/node-http-proxy) [![codecov](https://codecov.io/gh/http-party/node-http-proxy/branch/master/graph/badge.svg)](https://codecov.io/gh/http-party/node-http-proxy)
`node-http-proxy` is an HTTP programmable proxying library that supports
websockets. It is suitable for implementing components such as
websockets. It is suitable for implementing components such as reverse
proxies and load balancers.
### Build Status
### Table of Contents
* [Installation](#installation)
* [Upgrading from 0.8.x ?](#upgrading-from-08x-)
* [Core Concept](#core-concept)
* [Use Cases](#use-cases)
* [Setup a basic stand-alone proxy server](#setup-a-basic-stand-alone-proxy-server)
* [Setup a stand-alone proxy server with custom server logic](#setup-a-stand-alone-proxy-server-with-custom-server-logic)
* [Setup a stand-alone proxy server with proxy request header re-writing](#setup-a-stand-alone-proxy-server-with-proxy-request-header-re-writing)
* [Modify a response from a proxied server](#modify-a-response-from-a-proxied-server)
* [Setup a stand-alone proxy server with latency](#setup-a-stand-alone-proxy-server-with-latency)
* [Using HTTPS](#using-https)
* [Proxying WebSockets](#proxying-websockets)
* [Options](#options)
* [Listening for proxy events](#listening-for-proxy-events)
* [Shutdown](#shutdown)
* [Miscellaneous](#miscellaneous)
* [Test](#test)
* [ProxyTable API](#proxytable-api)
* [Logo](#logo)
* [Contributing and Issues](#contributing-and-issues)
* [License](#license)
<p align="center">
<a href="https://travis-ci.org/nodejitsu/node-http-proxy" target="_blank">
<img src="https://travis-ci.org/nodejitsu/node-http-proxy.png"/></a>&nbsp;&nbsp;
<a href="https://coveralls.io/r/nodejitsu/node-http-proxy" target="_blank">
<img src="https://coveralls.io/repos/nodejitsu/node-http-proxy/badge.png"/></a>
</p>
### Installation
### Looking to Upgrade from 0.8.x ? Click [here](UPGRADING.md)
`npm install http-proxy --save`
**[Back to top](#table-of-contents)**
### Upgrading from 0.8.x ?
Click [here](UPGRADING.md)
**[Back to top](#table-of-contents)**
### Core Concept
A new proxy is created by calling `createProxyServer` and passing
an `options` object as argument ([valid properties are available here](lib/http-proxy.js#L34-L51))
an `options` object as argument ([valid properties are available here](lib/http-proxy.js#L26-L42))
```javascript
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer(options);
var proxy = httpProxy.createProxyServer(options); // See (†)
```
†Unless listen(..) is invoked on the object, this does not create a webserver. See below.
An object will be returned with four values:
An object will be returned with four methods:
* web `req, res, [options]` (used for proxying regular HTTP(S) requests)
* ws `req, socket, head, [options]` (used for proxying WS(S) requests)
@ -62,10 +85,13 @@ proxy.web(req, res, { target: 'http://mytarget.com:8080' }, function(e) { ... })
When a request is proxied it follows two different pipelines ([available here](lib/http-proxy/passes))
which apply transformations to both the `req` and `res` object.
The first pipeline (ingoing) is responsible for the creation and manipulation of the stream that connects your client to the target.
The first pipeline (incoming) is responsible for the creation and manipulation of the stream that connects your client to the target.
The second pipeline (outgoing) is responsible for the creation and manipulation of the stream that, from your target, returns data
to the client.
**[Back to top](#table-of-contents)**
### Use Cases
#### Setup a basic stand-alone proxy server
@ -75,7 +101,7 @@ var http = require('http'),
//
// Create your proxy server and set the target in the options.
//
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†)
//
// Create your target server
@ -86,9 +112,12 @@ http.createServer(function (req, res) {
res.end();
}).listen(9000);
```
†Invoking listen(..) triggers the creation of a web server. Otherwise, just the proxy instance is created.
**[Back to top](#table-of-contents)**
#### Setup a stand-alone proxy server with custom server logic
This example show how you can proxy a request using your own HTTP server
This example shows how you can proxy a request using your own HTTP server
and also you can put your own logic to handle the request.
```js
@ -108,13 +137,15 @@ var proxy = httpProxy.createProxyServer({});
var server = http.createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, { target: 'http://127.0.0.1:5060' });
proxy.web(req, res, { target: 'http://127.0.0.1:5050' });
});
console.log("listening on port 5050")
server.listen(5050);
```
**[Back to top](#table-of-contents)**
#### Setup a stand-alone proxy server with proxy request header re-writing
This example shows how you can proxy a request using your own HTTP server that
modifies the outgoing proxy request by adding a special header.
@ -144,7 +175,7 @@ var server = http.createServer(function(req, res) {
// You can define here your custom logic to handle the request
// and then proxy the request.
proxy.web(req, res, {
target: 'http://127.0.0.1:5060'
target: 'http://127.0.0.1:5050'
});
});
@ -152,6 +183,15 @@ console.log("listening on port 5050")
server.listen(5050);
```
**[Back to top](#table-of-contents)**
#### Modify a response from a proxied server
Sometimes when you have received a HTML/XML document from the server of origin you would like to modify it before forwarding it on.
[Harmon](https://github.com/No9/harmon) allows you to do this in a streaming style so as to keep the pressure on the proxy to a minimum.
**[Back to top](#table-of-contents)**
#### Setup a stand-alone proxy server with latency
```js
@ -164,11 +204,11 @@ var http = require('http'),
var proxy = httpProxy.createProxyServer();
//
// Create your server that make an operation that take a while
// and then proxy de request
// Create your server that makes an operation that waits a while
// and then proxies the request
//
http.createServer(function (req, res) {
// This simulate an operation that take 500ms in execute
// This simulates an operation that takes 500ms to execute
setTimeout(function () {
proxy.web(req, res, {
target: 'http://localhost:9008'
@ -186,52 +226,10 @@ http.createServer(function (req, res) {
}).listen(9008);
```
#### Listening for proxy events
* `error`: The error event is emitted if the request to the target fail.
* `proxyRes`: This event is emitted if the request to the target got a response.
* `proxySocket`: This event is emitted once the proxy websocket was created and piped into the target websocket.
```js
var httpProxy = require('http-proxy');
// Error example
//
// Http Proxy Server with bad target
//
var proxy = httpProxy.createServer({
target:'http://localhost:9005'
});
proxy.listen(8005);
//
// Listen for the `error` event on `proxy`.
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
//
// Listen for the `proxyRes` event on `proxy`.
//
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
//
// Listen for the `proxySocket` event on `proxy`.
//
proxy.on('proxySocket', function (proxySocket) {
// listen for messages coming FROM the target here
proxySocket.on('data', hybiParseAndLogMessage);
});
```
**[Back to top](#table-of-contents)**
#### Using HTTPS
You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set `secure: true` in the options.
You can activate the validation of a secure SSL certificate to the target connection (avoid self-signed certs), just set `secure: true` in the options.
##### HTTPS -> HTTP
@ -267,6 +265,26 @@ httpProxy.createServer({
}).listen(443);
```
##### HTTP -> HTTPS (using a PKCS12 client certificate)
```js
//
// Create an HTTP proxy server with an HTTPS target
//
httpProxy.createProxyServer({
target: {
protocol: 'https:',
host: 'my-domain-name',
port: 443,
pfx: fs.readFileSync('path/to/certificate.p12'),
passphrase: 'password',
},
changeOrigin: true,
}).listen(8000);
```
**[Back to top](#table-of-contents)**
#### Proxying WebSockets
You can activate the websocket support for the proxy using `ws:true` in the options.
@ -307,30 +325,147 @@ proxyServer.on('upgrade', function (req, socket, head) {
proxyServer.listen(8015);
```
### Contributing and Issues
* Search on Google/Github
* If you can't find anything, open an issue
* If you feel comfortable about fixing the issue, fork the repo
* Commit to your local branch (which must be different from `master`)
* Submit your Pull Request (be sure to include tests and update documentation)
**[Back to top](#table-of-contents)**
### Options
`httpProxy.createProxyServer` supports the following options:
* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
* **secure**: true/false, if you want to verify the SSL Certs
* **xfwd**: true/false, adds x-forward headers
* **toProxy**: passes the absolute URL as the `path` (useful for proxying to proxies)
* **target**: url string to be parsed with the url module
* **forward**: url string to be parsed with the url module
* **agent**: object to be passed to http(s).request (see Node's [https agent](http://nodejs.org/api/https.html#https_class_https_agent) and [http agent](http://nodejs.org/api/http.html#http_class_http_agent) objects)
* **ssl**: object to be passed to https.createServer()
* **ws**: true/false, if you want to proxy websockets
* **xfwd**: true/false, adds x-forward headers
* **secure**: true/false, if you want to verify the SSL Certs
* **toProxy**: true/false, passes the absolute URL as the `path` (useful for proxying to proxies)
* **prependPath**: true/false, Default: true - specify whether you want to prepend the target's path to the proxy path
* **ignorePath**: true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required).
* **localAddress**: Local interface string to bind for outgoing connections
* **changeOrigin**: true/false, Default: false - changes the origin of the host header to the target URL
* **preserveHeaderKeyCase**: true/false, Default: false - specify whether you want to keep letter case of response header key
* **auth**: Basic authentication i.e. 'user:password' to compute an Authorization header.
* **hostRewrite**: rewrites the location hostname on (201/301/302/307/308) redirects.
* **autoRewrite**: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
* **protocolRewrite**: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.
* **cookieDomainRewrite**: rewrites domain of `set-cookie` headers. Possible values:
* `false` (default): disable cookie rewriting
* String: new domain, for example `cookieDomainRewrite: "new.domain"`. To remove the domain, use `cookieDomainRewrite: ""`.
* Object: mapping of domains to new domains, use `"*"` to match all domains.
For example keep one domain unchanged, rewrite one domain and remove other domains:
```
cookieDomainRewrite: {
"unchanged.domain": "unchanged.domain",
"old.domain": "new.domain",
"*": ""
}
```
* **cookiePathRewrite**: rewrites path of `set-cookie` headers. Possible values:
* `false` (default): disable cookie rewriting
* String: new path, for example `cookiePathRewrite: "/newPath/"`. To remove the path, use `cookiePathRewrite: ""`. To set path to root use `cookiePathRewrite: "/"`.
* Object: mapping of paths to new paths, use `"*"` to match all paths.
For example, to keep one path unchanged, rewrite one path and remove other paths:
```
cookiePathRewrite: {
"/unchanged.path/": "/unchanged.path/",
"/old.path/": "/new.path/",
"*": ""
}
```
* **headers**: object with extra headers to be added to target requests.
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
* **selfHandleResponse** true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the `proxyRes` event
* **buffer**: stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:
```
'use strict';
const streamify = require('stream-array');
const HttpProxy = require('http-proxy');
const proxy = new HttpProxy();
module.exports = (req, res, next) => {
proxy.web(req, res, {
target: 'http://localhost:4003/',
buffer: streamify(req.rawBody)
}, next);
};
```
**NOTE:**
`options.ws` and `options.ssl` are optional.
`options.target` and `options.forward` cannot both be missing
If you are using the `proxyServer.listen` method, the following options are also applicable:
* **ssl**: object to be passed to https.createServer()
* **ws**: true/false, if you want to proxy websockets
**[Back to top](#table-of-contents)**
### Listening for proxy events
* `error`: The error event is emitted if the request to the target fail. **We do not do any error handling of messages passed between client and proxy, and messages passed between proxy and target, so it is recommended that you listen on errors and handle them.**
* `proxyReq`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "web" connections
* `proxyReqWs`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "websocket" connections
* `proxyRes`: This event is emitted if the request to the target got a response.
* `open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
* `close`: This event is emitted once the proxy websocket was closed.
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
```js
var httpProxy = require('http-proxy');
// Error example
//
// Http Proxy Server with bad target
//
var proxy = httpProxy.createServer({
target:'http://localhost:9005'
});
proxy.listen(8005);
//
// Listen for the `error` event on `proxy`.
proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
//
// Listen for the `proxyRes` event on `proxy`.
//
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
});
//
// Listen for the `open` event on `proxy`.
//
proxy.on('open', function (proxySocket) {
// listen for messages coming FROM the target here
proxySocket.on('data', hybiParseAndLogMessage);
});
//
// Listen for the `close` event on `proxy`.
//
proxy.on('close', function (res, socket, head) {
// view disconnected websocket connections
console.log('Client disconnected');
});
```
**[Back to top](#table-of-contents)**
### Shutdown
* When testing or running server within another program it may be necessary to close the proxy.
@ -347,21 +482,72 @@ var proxy = new httpProxy.createProxyServer({
proxy.close();
```
### Test
**[Back to top](#table-of-contents)**
### Miscellaneous
If you want to handle your own response after receiving the `proxyRes`, you can do
so with `selfHandleResponse`. As you can see below, if you use this option, you
are able to intercept and read the `proxyRes` but you must also make sure to
reply to the `res` itself otherwise the original client will never receive any
data.
### Modify response
```js
var option = {
target: target,
selfHandleResponse : true
};
proxy.on('proxyRes', function (proxyRes, req, res) {
var body = [];
proxyRes.on('data', function (chunk) {
body.push(chunk);
});
proxyRes.on('end', function () {
body = Buffer.concat(body).toString();
console.log("res from proxied server:", body);
res.end("my response to cli");
});
});
proxy.web(req, res, option);
```
#### ProxyTable API
A proxy table API is available through this add-on [module](https://github.com/donasaur/http-proxy-rules), which lets you define a set of rules to translate matching routes to target routes that the reverse proxy will talk to.
#### Test
```
$ npm test
```
### Logo
#### Logo
Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
**[Back to top](#table-of-contents)**
### Contributing and Issues
* Read carefully our [Code Of Conduct](https://github.com/http-party/node-http-proxy/blob/master/CODE_OF_CONDUCT.md)
* Search on Google/Github
* If you can't find anything, open an issue
* If you feel comfortable about fixing the issue, fork the repo
* Commit to your local branch (which must be different from `master`)
* Submit your Pull Request (be sure to include tests and update documentation)
**[Back to top](#table-of-contents)**
### License
>The MIT License (MIT)
>
>Copyright (c) 2010 - 2013 Nodejitsu Inc.
>Copyright (c) 2010 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
>
>Permission is hereby granted, free of charge, to any person obtaining a copy
>of this software and associated documentation files (the "Software"), to deal
@ -380,5 +566,3 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
>THE SOFTWARE.

View File

@ -12,11 +12,11 @@ httpProxy.createServer({
}).listen(8003);
```
Check the [README.md](https://github.com/nodejitsu/node-http-proxy/blob/caronte/README.md) for a more detailed explanation of the parameters.
Check the [README.md](https://github.com/http-party/node-http-proxy/blob/caronte/README.md) for a more detailed explanation of the parameters.
## Proxying
Web proxying is done by calling the `.web()` method on a Proxy instance. You can check among some use cases in the [examples folder](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/http)
Web proxying is done by calling the `.web()` method on a Proxy instance. You can check among some use cases in the [examples folder](https://github.com/http-party/node-http-proxy/tree/caronte/examples/http)
```javascript
//
@ -32,7 +32,7 @@ httpProxy.createProxyServer({
```
Websockets are proxied by the `.ws()` method. The [examples folder](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/websocket) again provides a lot of useful snippets!
Websockets are proxied by the `.ws()` method. The [examples folder](https://github.com/http-party/node-http-proxy/tree/caronte/examples/websocket) again provides a lot of useful snippets!
```javascript
var proxy = new httpProxy.createProxyServer({
@ -90,7 +90,8 @@ which were in the core and delegate them to eventual "userland" modules.
### Middleware API
The new API makes it really easy to implement code that behaves like the old Middleware API. You can check some examples [here](https://github.com/nodejitsu/node-http-proxy/tree/caronte/examples/middleware)
The new API makes it really easy to implement code that behaves like the old Middleware API. You can check some examples [here](https://github.com/http-party/node-http-proxy/tree/caronte/examples/middleware)
### ProxyTable API
See this [link](https://github.com/donasaur/http-proxy-rules/) for an add-on proxy rules module that you can use to simulate the old ProxyTable API.

View File

@ -10,14 +10,18 @@ All benchmarking shall be done with [wrk](https://github.com/wg/wrk) which _is t
$ wrk
Usage: wrk <options> <url>
Options:
-c, --connections <n> Connections to keep open
-r, --requests <n> Total requests to make
-t, --threads <n> Number of threads to use
-c, --connections <N> Connections to keep open
-d, --duration <T> Duration of test
-t, --threads <N> Number of threads to use
-H, --header <h> Add header to request
-s, --script <S> Load Lua script file
-H, --header <H> Add header to request
--latency Print latency statistics
--timeout <T> Socket/request timeout
-v, --version Print version details
Numeric arguments may include a SI unit (2k, 2M, 2G)
Numeric arguments may include a SI unit (1k, 1M, 1G)
Time arguments may include a time unit (2s, 2m, 2h)
```
## Benchmarks
@ -30,4 +34,4 @@ _This benchmark requires three terminals running:_
1. **A proxy server:** `node benchmark/scripts/proxy.js`
2. **A target server:** `node benchmark/scripts/hello.js`
3. **A wrk process:** `wrk -c 20 -r 10000 -t 2 http://127.0.0.1:8000`
3. **A wrk process:** `wrk -c 20 -d5m -t 2 http://127.0.0.1:8000`

10
codecov.yml Normal file
View File

@ -0,0 +1,10 @@
coverage:
parsers:
javascript:
enable_partials: yes
status:
project:
default:
target: "70%"
patch:
enabled: false

View File

@ -1,7 +1,7 @@
/*
simple-balancer.js: Example of a simple round robin balancer for websockets
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -29,7 +29,7 @@ var http = require('http'),
//
// A simple round-robin load balancing strategy.
//
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
@ -64,16 +64,16 @@ function nextProxy() {
return proxy;
}
//
// Get the 'next' proxy and send the http request
//
// Get the 'next' proxy and send the http request
//
var server = http.createServer(function (req, res) {
var server = http.createServer(function (req, res) {
nextProxy().web(req, res);
});
//
// Get the 'next' proxy and send the upgrade request
//
// Get the 'next' proxy and send the upgrade request
//
server.on('upgrade', function (req, socket, head) {
@ -81,4 +81,3 @@ server.on('upgrade', function (req, socket, head) {
});
server.listen(8001);

View File

@ -1,7 +1,7 @@
/*
simple-balancer.js: Example of a simple round robin balancer
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -28,7 +28,7 @@ var http = require('http'),
httpProxy = require('../../lib/http-proxy');
//
// A simple round-robin load balancing strategy.
//
//
// First, list the servers you want to use in your rotation.
//
var addresses = [

View File

@ -1,7 +1,7 @@
/*
basic-proxy.js: Basic example of proxying over HTTP
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -31,10 +31,10 @@ var util = require('util'),
var welcome = [
'# # ##### ##### ##### ##### ##### #### # # # #',
'# # # # # # # # # # # # # # # # ',
'###### # # # # ##### # # # # # # ## # ',
'# # # # ##### ##### ##### # # ## # ',
'# # # # # # # # # # # # # ',
'# # # # # # # # # # # # # # # # ',
'###### # # # # ##### # # # # # # ## # ',
'# # # # ##### ##### ##### # # ## # ',
'# # # # # # # # # # # # # ',
'# # # # # # # # #### # # # '
].join('\n');

View File

@ -1,7 +1,7 @@
/*
concurrent-proxy.js: check levelof concurrency through proxy.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -44,7 +44,7 @@ httpProxy.createServer({
//
var connections = [],
var connections = [],
go;
http.createServer(function (req, res) {
@ -53,9 +53,9 @@ http.createServer(function (req, res) {
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
});
process.stdout.write(connections.length + ', ');
if (connections.length > 110 || go) {
go = true;
while (connections.length) {

View File

@ -1,7 +1,7 @@
/*
custom-proxy-error.js: Example of using the custom `proxyError` event.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -47,7 +47,7 @@ proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});

View File

@ -1,7 +1,7 @@
/*
error-handling.js: Example of handle erros for HTTP and WebSockets
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,7 +1,7 @@
/*
forward-and-target-proxy.js: Example of proxying over HTTP with additional forward proxy
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,7 +1,7 @@
/*
forward-proxy.js: Example of proxying over HTTP with additional forward proxy
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,7 +1,7 @@
/*
latent-proxy.js: Example of proxying over HTTP with latency
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -0,0 +1,26 @@
var httpProxy = require('../../lib/http-proxy');
var Agent = require('agentkeepalive');
var agent = new Agent({
maxSockets: 100,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs:1000,
timeout: 60000,
keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
});
var proxy = httpProxy.createProxy({ target: 'http://whatever.com', agent: agent });
//
// Modify headers of the response before it gets sent
// So that we handle the NLTM authentication response
//
proxy.on('proxyRes', function (proxyRes) {
var key = 'www-authenticate';
proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
});
require('http').createServer(function (req, res) {
proxy.web(req, res);
}).listen(3000);

View File

@ -1,7 +1,7 @@
/*
proxy-http-to-https.js: Basic example of proxying over HTTP to a target HTTPS server
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,7 +1,7 @@
/*
proxy-https-to-http.js: Basic example of proxying over HTTPS to a target HTTP server
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -32,14 +32,14 @@ var https = require('https'),
colors = require('colors'),
httpProxy = require('../../lib/http-proxy'),
fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures');
//
// Create the target HTTP server
// Create the target HTTP server
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello http over https\n');
res.end();
res.end();
}).listen(9009);
//

View File

@ -1,7 +1,7 @@
/*
proxy-https-to-https.js: Basic example of proxying over HTTPS to a target HTTPS server
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -36,18 +36,18 @@ var https = require('https'),
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
};
//
// Create the target HTTPS server
// Create the target HTTPS server
//
https.createServer(httpsOpts, function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('hello https\n');
res.end();
res.end();
}).listen(9010);
//
// Create the proxy server listening on port 443
// Create the proxy server listening on port 8010
//
httpProxy.createServer({
ssl: httpsOpts,

View File

@ -0,0 +1,55 @@
/*
reverse-proxy.js: Example of reverse proxying (with HTTPS support)
Copyright (c) 2015 Alberto Pose <albertopose@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var http = require('http'),
net = require('net'),
httpProxy = require('../../lib/http-proxy'),
url = require('url'),
util = require('util');
var proxy = httpProxy.createServer();
var server = http.createServer(function (req, res) {
util.puts('Receiving reverse proxy request for:' + req.url);
var parsedUrl = url.parse(req.url);
var target = parsedUrl.protocol + '//' + parsedUrl.hostname;
proxy.web(req, res, {target: target, secure: false});
}).listen(8213);
server.on('connect', function (req, socket) {
util.puts('Receiving reverse proxy request for:' + req.url);
var serverUrl = url.parse('https://' + req.url);
var srvSocket = net.connect(serverUrl.port, serverUrl.hostname, function() {
socket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node-Proxy\r\n' +
'\r\n');
srvSocket.pipe(socket);
socket.pipe(srvSocket);
});
});
// Test with:
// curl -vv -x http://127.0.0.1:8213 https://www.google.com
// curl -vv -x http://127.0.0.1:8213 http://www.google.com

67
examples/http/sse.js Normal file
View File

@ -0,0 +1,67 @@
/*
sse.js: Basic example of proxying over HTTP
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var util = require('util'),
colors = require('colors'),
http = require('http'),
httpProxy = require('../../lib/http-proxy'),
SSE = require('sse');
//
// Basic Http Proxy Server
//
var proxy = new httpProxy.createProxyServer();
http.createServer(function (req, res) {
proxy.web(req, res, {
target: 'http://localhost:9003'
});
}).listen(8003);
//
// Target Http Server
//
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
});
//
// Use SSE
//
var sse = new SSE(server, {path: '/'});
sse.on('connection', function(client) {
var count = 0;
setInterval(function(){
client.send('message #' + count++);
}, 1500);
});
server.listen(9003);
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8003'.yellow);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9003 '.yellow);

View File

@ -1,7 +1,7 @@
/*
standalone-proxy.js: Example of proxying over HTTP with a standalone HTTP server.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -51,4 +51,4 @@ http.createServer(function (req, res) {
}).listen(9002);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxy.web() handler'.cyan.underline + ' and latency'.magenta);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9001 '.yellow);
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9002 '.yellow);

View File

@ -1,7 +1,7 @@
/*
bodyDecoder-middleware.js: Basic example of `connect.bodyParser()` middleware in node-http-proxy
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -29,33 +29,42 @@ var http = require('http'),
request = require('request'),
colors = require('colors'),
util = require('util'),
queryString = require('querystring'),
bodyParser = require('body-parser'),
httpProxy = require('../../lib/http-proxy'),
proxy = httpProxy.createProxyServer({});
//restreame
var restreamer = function (){
return function (req, res, next) { //restreame
req.removeAllListeners('data')
req.removeAllListeners('end')
next()
process.nextTick(function () {
if(req.body) {
req.emit('data', JSON.stringify(req.body))
}
req.emit('end')
})
//restream parsed body before proxying
proxy.on('proxyReq', function(proxyReq, req, res, options) {
if (!req.body || !Object.keys(req.body).length) {
return;
}
}
var contentType = proxyReq.getHeader('Content-Type');
var bodyData;
if (contentType === 'application/json') {
bodyData = JSON.stringify(req.body);
}
if (contentType === 'application/x-www-form-urlencoded') {
bodyData = queryString.stringify(req.body);
}
if (bodyData) {
proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
proxyReq.write(bodyData);
}
});
//
// Basic Http Proxy Server
//
var app = connect()
.use(bodyParser.json())//json
.use(restreamer())//restreame
.use(bodyParser.json())//json parser
.use(bodyParser.urlencoded())//urlencoded parser
.use(function(req, res){
// modify body here,
// eg: req.body = {a: 1}.
@ -84,10 +93,16 @@ http.createServer(app1).listen(9013, function(){
//request to 8013 to proxy
request.post({//
url: 'http://127.0.0.1:8013',
json: {content: 123, type: "greeting"}
json: {content: 123, type: "greeting from json request"}
},function(err, res,data){
console.log('return:' ,err, data)
console.log('return for json request:' ,err, data)
})
// application/x-www-form-urlencoded request
request.post({//
url: 'http://127.0.0.1:8013',
form: {content: 123, type: "greeting from urlencoded request"}
},function(err, res,data){
console.log('return for urlencoded request:' ,err, data)
})
});

View File

@ -1,7 +1,7 @@
/*
gzip-middleware.js: Basic example of `connect-gzip` middleware in node-http-proxy
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -27,7 +27,7 @@
var util = require('util'),
colors = require('colors'),
http = require('http'),
connect = require('connect')
connect = require('connect'),
httpProxy = require('../../lib/http-proxy');
//

View File

@ -1,7 +1,7 @@
/*
modifyBody-middleware.js: Example of middleware which modifies response
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -28,24 +28,26 @@ var util = require('util'),
colors = require('colors'),
http = require('http'),
connect = require('connect'),
app = connect(),
httpProxy = require('../../lib/http-proxy');
//
// Basic Connect App
//
connect.createServer(
function (req, res, next) {
var _write = res.write;
app.use(function (req, res, next) {
var _write = res.write;
res.write = function (data) {
_write.call(res, data.toString().replace("Ruby", "nodejitsu"));
}
next();
},
function (req, res) {
proxy.web(req, res);
res.write = function (data) {
_write.call(res, data.toString().replace("Ruby", "http-party"));
}
).listen(8013);
next();
});
app.use(function (req, res) {
proxy.web(req, res)
});
http.createServer(app).listen(8013);
//
// Basic Http Proxy Server
@ -59,7 +61,7 @@ var proxy = httpProxy.createProxyServer({
//
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, I know Ruby\n');
res.end('Hello, I love Ruby\n');
}).listen(9013);
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8013'.yellow);

View File

@ -3,11 +3,12 @@
"description": "packages required to run the examples",
"version": "0.0.0",
"dependencies": {
"colors": "~0.6.2",
"agentkeepalive": "^4.0.0",
"colors": "~1.3.0",
"connect-restreamer": "~1.0.0",
"request": "~2.88.0",
"socket.io": "~0.9.16",
"socket.io-client": "~0.9.16",
"connect": "~2.11.0",
"request": "~2.27.0",
"connect-restreamer": "~1.0.0"
"sse": "0.0.8"
}
}

View File

@ -1,7 +1,7 @@
/*
standalone-websocket-proxy.js: Example of proxying websockets over HTTP with a standalone HTTP server.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -59,7 +59,7 @@ server.sockets.on('connection', function (client) {
//
var proxy = new httpProxy.createProxyServer({
target: {
host: 'localhost',
host: 'localhost',
port: 9016
}
});
@ -69,7 +69,7 @@ var proxyServer = http.createServer(function (req, res) {
});
//
// Listen to the `upgrade` event and proxy the
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {

View File

@ -1,7 +1,7 @@
/*
standalone-websocket-proxy.js: Example of proxying websockets over HTTP with a standalone HTTP server.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -68,7 +68,7 @@ var proxyServer = http.createServer(function (req, res) {
});
//
// Listen to the `upgrade` event and proxy the
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head) {

View File

@ -1,7 +1,7 @@
/*
web-socket-proxy.js: Example of proxying over HTTP and WebSockets.
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@ -1,12 +1,6 @@
var http = require('http'),
https = require('https'),
url = require('url'),
httpProxy = require('./http-proxy/');
// Use explicit /index.js to help browserify negociation in require '/lib/http-proxy' (!)
var ProxyServer = require('./http-proxy/index.js').Server;
/**
* Export the proxy "Server" as the main export.
*/
module.exports = httpProxy.Server;
/**
* Creates the proxy server.
@ -23,9 +17,8 @@ module.exports = httpProxy.Server;
* @api public
*/
module.exports.createProxyServer =
module.exports.createServer =
module.exports.createProxy = function createProxyServer(options) {
function createProxyServer(options) {
/*
* `options` is needed and it must have the following layout:
*
@ -39,8 +32,14 @@ module.exports.createProxyServer =
* secure : <true/false, verify SSL certificate>
* toProxy: <true/false, explicitly specify if we are proxying to another proxy>
* prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
* ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
* localAddress : <Local interface string to bind for outgoing connections>
* changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
* preserveHeaderKeyCase: <true/false, Default: false - specify whether you want to keep letter case of response header key >
* auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
* hostRewrite: rewrites the location hostname on (201/301/302/307/308) redirects, Default: null.
* autoRewrite: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
* protocolRewrite: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.
* }
*
* NOTE: `options.ws` and `options.ssl` are optional.
@ -49,6 +48,19 @@ module.exports.createProxyServer =
* }
*/
return new httpProxy.Server(options);
};
return new ProxyServer(options);
}
ProxyServer.createProxyServer = createProxyServer;
ProxyServer.createServer = createProxyServer;
ProxyServer.createProxy = createProxyServer;
/**
* Export the proxy "Server" as the main export.
*/
module.exports = ProxyServer;

View File

@ -1,8 +1,15 @@
var common = exports,
url = require('url'),
extend = require('util')._extend;
var common = exports,
url = require('url'),
extend = require('util')._extend,
required = require('requires-port');
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
isSSL = /^https|wss/;
/**
* Simple Regex for testing if protocol is https
*/
common.isSSL = isSSL;
/**
* Copies the right headers from `options` and `req` to
* `outgoing` which is then used to fire the proxied
@ -25,21 +32,29 @@ var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i;
common.setupOutgoing = function(outgoing, options, req, forward) {
outgoing.port = options[forward || 'target'].port ||
(~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol) ? 443 : 80);
(isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
['host', 'hostname', 'socketPath'].forEach(
['host', 'hostname', 'socketPath', 'pfx', 'key',
'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
function(e) { outgoing[e] = options[forward || 'target'][e]; }
);
['method', 'headers'].forEach(
function(e) { outgoing[e] = req[e]; }
);
outgoing.method = options.method || req.method;
outgoing.headers = extend({}, req.headers);
if (options.headers){
extend(outgoing.headers, options.headers);
}
if (~['https:', 'wss:'].indexOf(options[forward || 'target'].protocol)) {
if (options.auth) {
outgoing.auth = options.auth;
}
if (options.ca) {
outgoing.ca = options.ca;
}
if (isSSL.test(options[forward || 'target'].protocol)) {
outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
}
@ -69,15 +84,24 @@ common.setupOutgoing = function(outgoing, options, req, forward) {
// Remark: Can we somehow not use url.parse as a perf optimization?
//
var outgoingPath = !options.toProxy
? url.parse(req.url).path
? (url.parse(req.url).path || '')
: req.url;
//
// Remark: ignorePath will just straight up ignore whatever the request's
// path is. This can be labeled as FOOT-GUN material if you do not know what
// you are doing and are using conflicting options.
//
outgoingPath = !options.ignorePath ? outgoingPath : '';
outgoing.path = common.urlJoin(targetPath, outgoingPath);
if (options.changeOrigin) {
outgoing.headers.host = outgoing.host;
outgoing.headers.host =
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
? outgoing.host + ':' + outgoing.port
: outgoing.host;
}
return outgoing;
};
@ -121,7 +145,20 @@ common.getPort = function(req) {
return res ?
res[1] :
req.connection.pair ? '443' : '80';
common.hasEncryptedConnection(req) ? '443' : '80';
};
/**
* Check if the request has an encrypted connection.
*
* @param {Request} req Incoming HTTP request.
*
* @return {Boolean} Whether the connection is encrypted or not.
*
* @api private
*/
common.hasEncryptedConnection = function(req) {
return Boolean(req.connection.encrypted || req.connection.pair);
};
/**
@ -141,22 +178,71 @@ common.urlJoin = function() {
last = args[lastIndex],
lastSegs = last.split('?'),
retSegs;
args[lastIndex] = lastSegs[0];
args[lastIndex] = lastSegs.shift();
//
// Join all strings, but remove empty strings so we don't get extra slashes from
// joining e.g. ['', 'am']
//
retSegs = [
args.filter(function filter(a) {
return !!a;
}).join('/').replace(/\/+/g, '/')
args.filter(Boolean).join('/')
.replace(/\/+/g, '/')
.replace('http:/', 'http://')
.replace('https:/', 'https://')
];
// Only join the query string if it exists so we don't have trailing a '?'
// on every request
lastSegs[1] && retSegs.push(lastSegs[1]);
// Handle case where there could be multiple ? in the URL.
retSegs.push.apply(retSegs, lastSegs);
return retSegs.join('?')
};
/**
* Rewrites or removes the domain of a cookie header
*
* @param {String|Array} Header
* @param {Object} Config, mapping of domain to rewritten domain.
* '*' key to match any domain, null value to remove the domain.
*
* @api private
*/
common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
if (Array.isArray(header)) {
return header.map(function (headerElement) {
return rewriteCookieProperty(headerElement, config, property);
});
}
return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
var newValue;
if (previousValue in config) {
newValue = config[previousValue];
} else if ('*' in config) {
newValue = config['*'];
} else {
//no match, return previous value
return match;
}
if (newValue) {
//replace value
return prefix + newValue;
} else {
//remove value
return '';
}
});
};
/**
* Check the host and see if it potentially has a port in it (keep it simple)
*
* @returns {Boolean} Whether we have one or not
*
* @api private
*/
function hasPort(host) {
return !!~host.indexOf(':');
};

View File

@ -1,7 +1,7 @@
var httpProxy = exports,
var httpProxy = module.exports,
extend = require('util')._extend,
parse_url = require('url').parse,
EE3 = require('eventemitter3').EventEmitter,
EE3 = require('eventemitter3'),
http = require('http'),
https = require('https'),
web = require('./passes/web-incoming'),
@ -41,14 +41,15 @@ function createRightProxy(type) {
cntr--;
}
var requestOptions = options;
if(
!(args[cntr] instanceof Buffer) &&
args[cntr] !== res
) {
//Copy global options
options = extend({}, options);
requestOptions = extend({}, options);
//Overwrite with request options
extend(options, args[cntr]);
extend(requestOptions, args[cntr]);
cntr--;
}
@ -60,11 +61,11 @@ function createRightProxy(type) {
/* optional args parse end */
['target', 'forward'].forEach(function(e) {
if (typeof options[e] === 'string')
options[e] = parse_url(options[e]);
if (typeof requestOptions[e] === 'string')
requestOptions[e] = parse_url(requestOptions[e]);
});
if (!options.target && !options.forward) {
if (!requestOptions.target && !requestOptions.forward) {
return this.emit('error', new Error('Must provide a proper URL as target'));
}
@ -77,7 +78,7 @@ function createRightProxy(type) {
* refer to the connection socket
* pass(req, socket, options, head)
*/
if(passes[i](req, res, options, head, cbl ? false : this, cbl)) { // passes can return a truthy value to halt the loop
if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
break;
}
}
@ -104,7 +105,7 @@ function ProxyServer(options) {
return ws[pass];
});
this.on('error', this.onError.bind(this));
this.on('error', this.onError, this);
}

View File

@ -1,13 +1,15 @@
var http = require('http'),
https = require('https'),
var httpNative = require('http'),
httpsNative = require('https'),
web_o = require('./web-outgoing'),
common = require('../common'),
passes = exports;
followRedirects = require('follow-redirects');
web_o = Object.keys(web_o).map(function(pass) {
return web_o[pass];
});
var nativeAgents = { http: httpNative, https: httpsNative };
/*!
* Array of passes.
*
@ -16,7 +18,8 @@ web_o = Object.keys(web_o).map(function(pass) {
* flexible.
*/
[ // <--
module.exports = {
/**
* Sets `content-length` to '0' if request is of DELETE type.
@ -28,9 +31,11 @@ web_o = Object.keys(web_o).map(function(pass) {
* @api private
*/
function deleteLength(req, res, options) {
if(req.method === 'DELETE' && !req.headers['content-length']) {
deleteLength: function deleteLength(req, res, options) {
if((req.method === 'DELETE' || req.method === 'OPTIONS')
&& !req.headers['content-length']) {
req.headers['content-length'] = '0';
delete req.headers['transfer-encoding'];
}
},
@ -44,7 +49,7 @@ web_o = Object.keys(web_o).map(function(pass) {
* @api private
*/
function timeout(req, res, options) {
timeout: function timeout(req, res, options) {
if(options.timeout) {
req.socket.setTimeout(options.timeout);
}
@ -60,13 +65,14 @@ web_o = Object.keys(web_o).map(function(pass) {
* @api private
*/
function XHeaders(req, res, options) {
XHeaders: function XHeaders(req, res, options) {
if(!options.xfwd) return;
var encrypted = req.isSpdy || common.hasEncryptedConnection(req);
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : common.getPort(req),
proto: req.isSpdy ? 'https' : (req.connection.pair ? 'https' : 'http')
proto: encrypted ? 'https' : 'http'
};
['for', 'port', 'proto'].forEach(function(header) {
@ -75,6 +81,8 @@ web_o = Object.keys(web_o).map(function(pass) {
(req.headers['x-forwarded-' + header] ? ',' : '') +
values[header];
});
req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
},
/**
@ -89,17 +97,27 @@ web_o = Object.keys(web_o).map(function(pass) {
* @api private
*/
function stream(req, res, options, _, server, clb) {
stream: function stream(req, res, options, _, server, clb) {
// And we begin!
if (!clb) {
server.emit('start', req, res, options.target)
}
server.emit('start', req, res, options.target || options.forward);
var agents = options.followRedirects ? followRedirects : nativeAgents;
var http = agents.http;
var https = agents.https;
if(options.forward) {
// If forward enable, so just pipe the request
var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req, 'forward')
);
// error handler (e.g. ECONNRESET, ECONNREFUSED)
// Handle errors on incoming request as well as it makes sense to
var forwardError = createErrorHandler(forwardReq, options.forward);
req.on('error', forwardError);
forwardReq.on('error', forwardError);
(options.buffer || req).pipe(forwardReq);
if(!options.target) { return res.end(); }
}
@ -111,7 +129,9 @@ web_o = Object.keys(web_o).map(function(pass) {
// Enable developers to modify the proxyReq before headers are sent
proxyReq.on('socket', function(socket) {
if(server) { server.emit('proxyReq', proxyReq, req, res, options); }
if(server && !proxyReq.getHeader('expect')) {
server.emit('proxyReq', proxyReq, req, res, options);
}
});
// allow outgoing socket to timeout so that we could
@ -127,17 +147,23 @@ web_o = Object.keys(web_o).map(function(pass) {
proxyReq.abort();
});
// Handle errors on incoming request as well as it makes sense to
// handle errors in proxy and incoming request, just like for forward proxy
var proxyError = createErrorHandler(proxyReq, options.target);
req.on('error', proxyError);
// Error Handler
proxyReq.on('error', proxyError);
function proxyError (err){
if (clb) {
clb(err, req, res, options.target);
} else {
server.emit('error', err, req, res, options.target);
function createErrorHandler(proxyReq, url) {
return function proxyError(err) {
if (req.socket.destroyed && err.code === 'ECONNRESET') {
server.emit('econnreset', err, req, res, url);
return proxyReq.abort();
}
if (clb) {
clb(err, req, res, url);
} else {
server.emit('error', err, req, res, url);
}
}
}
@ -145,24 +171,24 @@ web_o = Object.keys(web_o).map(function(pass) {
proxyReq.on('response', function(proxyRes) {
if(server) { server.emit('proxyRes', proxyRes, req, res); }
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes)) { break; }
if(!res.headersSent && !options.selfHandleResponse) {
for(var i=0; i < web_o.length; i++) {
if(web_o[i](req, res, proxyRes, options)) { break; }
}
}
// Allow us to listen when the proxy has completed
proxyRes.on('end', function () {
if (!clb) {
server.emit('end', req, res, proxyRes);
}
})
proxyRes.pipe(res);
if (!res.finished) {
// Allow us to listen when the proxy has completed
proxyRes.on('end', function () {
if (server) server.emit('end', req, res, proxyRes);
});
// We pipe to the response unless its expected to be handled by the user
if (!options.selfHandleResponse) proxyRes.pipe(res);
} else {
if (server) server.emit('end', req, res, proxyRes);
}
});
//proxyReq.end();
}
] // <--
.forEach(function(func) {
passes[func.name] = func;
});
};

View File

@ -1,4 +1,8 @@
var passes = exports;
var url = require('url'),
common = require('../common');
var redirectRegex = /^201|30(1|2|7|8)$/;
/*!
* Array of passes.
@ -8,7 +12,7 @@ var passes = exports;
* flexible.
*/
[ // <--
module.exports = { // <--
/**
* If is a HTTP 1.0 request, remove chunk headers
@ -19,8 +23,8 @@ var passes = exports;
*
* @api private
*/
function removeChunked(req, res, proxyRes) {
if(req.httpVersion === '1.0') {
removeChunked: function removeChunked(req, res, proxyRes) {
if (req.httpVersion === '1.0') {
delete proxyRes.headers['transfer-encoding'];
}
},
@ -35,14 +39,38 @@ var passes = exports;
*
* @api private
*/
function setConnection(req, res, proxyRes) {
setConnection: function setConnection(req, res, proxyRes) {
if (req.httpVersion === '1.0') {
proxyRes.headers.connection = req.headers.connection || 'close';
} else if (!proxyRes.headers.connection) {
} else if (req.httpVersion !== '2.0' && !proxyRes.headers.connection) {
proxyRes.headers.connection = req.headers.connection || 'keep-alive';
}
},
setRedirectHostRewrite: function setRedirectHostRewrite(req, res, proxyRes, options) {
if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
&& proxyRes.headers['location']
&& redirectRegex.test(proxyRes.statusCode)) {
var target = url.parse(options.target);
var u = url.parse(proxyRes.headers['location']);
// make sure the redirected host matches the target host before rewriting
if (target.host != u.host) {
return;
}
if (options.hostRewrite) {
u.host = options.hostRewrite;
} else if (options.autoRewrite) {
u.host = req.headers['host'];
}
if (options.protocolRewrite) {
u.protocol = options.protocolRewrite;
}
proxyRes.headers['location'] = u.format();
}
},
/**
* Copy headers from proxyResponse to response
* set each header in response object.
@ -50,12 +78,50 @@ var passes = exports;
* @param {ClientRequest} Req Request object
* @param {IncomingMessage} Res Response object
* @param {proxyResponse} Res Response object from the proxy request
* @param {Object} Options options.cookieDomainRewrite: Config to rewrite cookie domain
*
* @api private
*/
function writeHeaders(req, res, proxyRes) {
writeHeaders: function writeHeaders(req, res, proxyRes, options) {
var rewriteCookieDomainConfig = options.cookieDomainRewrite,
rewriteCookiePathConfig = options.cookiePathRewrite,
preserveHeaderKeyCase = options.preserveHeaderKeyCase,
rawHeaderKeyMap,
setHeader = function(key, header) {
if (header == undefined) return;
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain');
}
if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
}
res.setHeader(String(key).trim(), header);
};
if (typeof rewriteCookieDomainConfig === 'string') { //also test for ''
rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
}
if (typeof rewriteCookiePathConfig === 'string') { //also test for ''
rewriteCookiePathConfig = { '*': rewriteCookiePathConfig };
}
// message.rawHeaders is added in: v0.11.6
// https://nodejs.org/api/http.html#http_message_rawheaders
if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
rawHeaderKeyMap = {};
for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) {
var key = proxyRes.rawHeaders[i];
rawHeaderKeyMap[key.toLowerCase()] = key;
}
}
Object.keys(proxyRes.headers).forEach(function(key) {
res.setHeader(key, proxyRes.headers[key]);
var header = proxyRes.headers[key];
if (preserveHeaderKeyCase && rawHeaderKeyMap) {
key = rawHeaderKeyMap[key] || key;
}
setHeader(key, header);
});
},
@ -68,11 +134,14 @@ var passes = exports;
*
* @api private
*/
function writeStatusCode(req, res, proxyRes) {
res.writeHead(proxyRes.statusCode);
writeStatusCode: function writeStatusCode(req, res, proxyRes) {
// From Node.js docs: response.writeHead(statusCode[, statusMessage][, headers])
if(proxyRes.statusMessage) {
res.statusCode = proxyRes.statusCode;
res.statusMessage = proxyRes.statusMessage;
} else {
res.statusCode = proxyRes.statusCode;
}
}
] // <--
.forEach(function(func) {
passes[func.name] = func;
});
};

View File

@ -1,7 +1,6 @@
var http = require('http'),
https = require('https'),
common = require('../common'),
passes = exports;
common = require('../common');
/*!
* Array of passes.
@ -16,9 +15,8 @@ var http = require('http'),
*
*/
var passes = exports;
[
module.exports = {
/**
* WebSocket requests must have the `GET` method and
* the `upgrade:websocket` header
@ -29,7 +27,7 @@ var passes = exports;
* @api private
*/
function checkMethodAndHeader (req, socket) {
checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
if (req.method !== 'GET' || !req.headers.upgrade) {
socket.destroy();
return true;
@ -51,13 +49,13 @@ var passes = exports;
* @api private
*/
function XHeaders(req, socket, options) {
XHeaders : function XHeaders(req, socket, options) {
if(!options.xfwd) return;
var values = {
for : req.connection.remoteAddress || req.socket.remoteAddress,
port : common.getPort(req),
proto: req.connection.pair ? 'wss' : 'ws'
proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
};
['for', 'port', 'proto'].forEach(function(header) {
@ -78,24 +76,55 @@ var passes = exports;
*
* @api private
*/
function stream(req, socket, options, head, server, clb) {
stream : function stream(req, socket, options, head, server, clb) {
var createHttpHeader = function(line, headers) {
return Object.keys(headers).reduce(function (head, key) {
var value = headers[key];
if (!Array.isArray(value)) {
head.push(key + ': ' + value);
return head;
}
for (var i = 0; i < value.length; i++) {
head.push(key + ': ' + value[i]);
}
return head;
}, [line])
.join('\r\n') + '\r\n\r\n';
}
common.setupSocket(socket);
if (head && head.length) socket.unshift(head);
var proxyReq = (~['https:', 'wss:'].indexOf(options.target.protocol) ? https : http).request(
var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, req)
);
// Enable developers to modify the proxyReq before headers are sent
if (server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }
// Error Handler
proxyReq.on('error', onOutgoingError);
proxyReq.on('response', function (res) {
// if upgrade event isn't going to happen, close the socket
if (!res.upgrade) socket.end();
if (!res.upgrade) {
socket.write(createHttpHeader('HTTP/' + res.httpVersion + ' ' + res.statusCode + ' ' + res.statusMessage, res.headers));
res.pipe(socket);
}
});
proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
proxySocket.on('error', onOutgoingError);
// Allow us to listen when the websocket has completed
proxySocket.on('end', function () {
server.emit('close', proxyRes, proxySocket, proxyHead);
});
// The pipe below will end proxySocket if socket closes cleanly, but not
// if it errors (eg, vanishes from the net and starts returning
// EHOSTUNREACH). We need to do that explicitly.
@ -107,13 +136,16 @@ var passes = exports;
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
socket.write('HTTP/1.1 101 Switching Protocols\r\n');
socket.write(Object.keys(proxyRes.headers).map(function(i) {
return i + ": " + proxyRes.headers[i];
}).join('\r\n') + '\r\n\r\n');
//
// Remark: Handle writing the headers to the socket when switching protocols
// Also handles when a header is an array
//
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
proxySocket.pipe(socket).pipe(proxySocket);
// Make sure server exists before we try to emit
server && server.emit('proxySocket', proxySocket);
server.emit('open', proxySocket);
server.emit('proxySocket', proxySocket); //DEPRECATED.
});
return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
@ -127,8 +159,4 @@ var passes = exports;
socket.end();
}
}
] // <--
.forEach(function(func) {
passes[func.name] = func;
});
};

2028
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,46 +1,41 @@
{
"name" : "http-proxy",
"version" : "1.6.2",
"repository" : {
"type" : "git",
"url" : "https://github.com/nodejitsu/node-http-proxy.git"
"name": "http-proxy",
"version": "1.18.1",
"repository": {
"type": "git",
"url": "https://github.com/http-party/node-http-proxy.git"
},
"description" : "HTTP proxying for the masses",
"author": "Nodejitsu Inc. <info@nodejitsu.com>",
"maintainers" : [
"yawnt <yawnt@nodejitsu.com>",
"indexzero <charlie@nodejitsu.com>"
"description": "HTTP proxying for the masses",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"maintainers": [
"jcrugzz <jcrugzz@gmail.com>"
],
"main" : "index.js",
"dependencies" : {
"eventemitter3" : "*"
"main": "index.js",
"dependencies": {
"eventemitter3": "^4.0.0",
"requires-port": "^1.0.0",
"follow-redirects": "^1.0.0"
},
"devDependencies": {
"mocha" : "*",
"expect.js" : "*",
"dox" : "*",
"coveralls" : "*",
"mocha-lcov-reporter": "*",
"blanket" : "*",
"ws" : "*",
"socket.io" : "*",
"socket.io-client" : "*",
"async" : "*"
"async": "^3.0.0",
"auto-changelog": "^1.15.0",
"concat-stream": "^2.0.0",
"expect.js": "~0.3.1",
"mocha": "^3.5.3",
"nyc": "^14.0.0",
"semver": "^5.0.3",
"socket.io": "^2.1.0",
"socket.io-client": "^2.1.0",
"sse": "0.0.8",
"ws": "^3.0.0"
},
"scripts" : {
"coveralls" : "mocha --require blanket --reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js",
"blanket" : { "pattern": "lib/http-proxy" },
"test" : "mocha -R landing test/*-test.js",
"test-cov" : "mocha --require blanket -R html-cov > cov/coverage.html"
"scripts": {
"mocha": "mocha test/*-test.js",
"test": "nyc --reporter=text --reporter=lcov npm run mocha",
"version": "auto-changelog -p && git add CHANGELOG.md"
},
"engines" : {
"node" : ">=0.10.0"
"engines": {
"node": ">=8.0.0"
},
"license" : "MIT"
"license": "MIT"
}

19
renovate.json Normal file
View File

@ -0,0 +1,19 @@
{
"platform": "github",
"autodiscover": false,
"requireConfig": true,
"ignoreNpmrcFile": true,
"rangeStrategy": "replace",
"packageRules": [
{
"packagePatterns": [
"*"
],
"minor": {
"groupName": "all non-major dependencies",
"groupSlug": "all-minor-patch"
}
}
],
"commitMessagePrefix": "[dist]"
}

View File

@ -1,7 +1,7 @@
/*
examples-test.js: Test to run all the examples
Copyright (c) Nodejitsu 2013
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
*/
var path = require('path'),

View File

@ -1,13 +1,25 @@
-----BEGIN CERTIFICATE-----
MIIB7DCCAZYCCQC7gs0MDNn6MTANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQGEwJV
UzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYDVQQKEwZKb3llbnQxEDAO
BgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEgMB4GCSqGSIb3DQEJARYR
cnlAdGlueWNsb3Vkcy5vcmcwHhcNMTEwMzE0MTgyOTEyWhcNMzgwNzI5MTgyOTEy
WjB9MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExCzAJBgNVBAcTAlNGMQ8wDQYD
VQQKEwZKb3llbnQxEDAOBgNVBAsTB05vZGUuanMxDzANBgNVBAMTBmFnZW50MjEg
MB4GCSqGSIb3DQEJARYRcnlAdGlueWNsb3Vkcy5vcmcwXDANBgkqhkiG9w0BAQEF
AANLADBIAkEAyXb8FrRdKbhrKLgLSsn61i1C7w7fVVVd7OQsmV/7p9WB2lWFiDlC
WKGU9SiIz/A6wNZDUAuc2E+VwtpCT561AQIDAQABMA0GCSqGSIb3DQEBBQUAA0EA
C8HzpuNhFLCI3A5KkBS5zHAQax6TFUOhbpBCR0aTDbJ6F1liDTK1lmU/BjvPoj+9
1LHwrmh29rK8kBPEjmymCQ==
MIIEIDCCAggCCQChRDh/XiBF+zANBgkqhkiG9w0BAQsFADBUMQswCQYDVQQGEwJ1
czETMBEGA1UECAwKV2FzaGluZ3RvbjEQMA4GA1UEBwwHU2VhdHRsZTEeMBwGA1UE
AwwVRHVtbXkgSW50ZXJtZWRpYXRlIENBMB4XDTE4MDYyMjIwMzEwNFoXDTMyMDIy
OTIwMzEwNFowUDELMAkGA1UEBhMCdXMxEzARBgNVBAgMCldhc2hpbmd0b24xEDAO
BgNVBAcMB1NlYXR0bGUxGjAYBgNVBAMMEWR1bW15LmV4YW1wbGUuY29tMIIBIjAN
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvSQq3d8AeZMTvtqZ13jWCckikyXJ
SACvkGCQUCJqOceESbg6IHdRzQdoccE4P3sbvNsf9BlbdJKM+neCxabqKaU1PPje
4P0tHT57t6yJrMuUh9NxEz3Bgh1srNHVS7saKvwHmcKm79jc+wxlioPmEQvQagjn
y7oTkyLt0sn4LGxBjrcv2JoHOC9f1pxX7l47MaiN0/ctRau7Nr3PFn+pkB4Yf6Z0
VyicVJbaUSz39Qo4HQWl1L2hiBP3CS1oKs2Yk0O1aOCMExWrhZQan+ZgHqL1rhgm
kPpw2/zwwPt5Vf9CSakvHwg198EXuTTXtkzYduuIJAm8yp969iEIiG2xTwIDAQAB
MA0GCSqGSIb3DQEBCwUAA4ICAQBnMSIo+kujkeXPh+iErFBmNtu/7EA+i/QnFPbN
lSLngclYYBJAGQI+DhirJI8ghDi6vmlHB2THewDaOJXEKvC1czE8064wioIcA9HJ
l3QJ3YYOFRctYdSHBU4TWdJbPgkLWDzYP5smjOfw8nDdr4WO/5jh9qRFcFpTFmQf
DyU3xgWLsQnNK3qXLdJjWG75pEhHR+7TGo+Ob/RUho/1RX/P89Ux7/oVbzdKqqFu
SErXAsjEIEFzWOM2uDOt6hrxDF6q+8/zudwQNEo422poEcTT9tDEFxMQ391CzZRi
nozBm4igRn1f5S3YZzLI6VEUns0s76BNy2CzvFWn40DziTqNBExAMfFFj76wiMsX
6fTIdcvkaTBa0S9SZB0vN99qahBdcG17rt4RssMHVRH1Wn7NXMwe476L0yXZ6gO7
Z4uNAPxgaI3BRP75EPfslLutCLZ+BC4Zzu6MY0Salbpfl0Go462EhsKCxvYhE2Dg
T477pICLfETZfA499Fd1tOaIsoLCrILAia/+Yd76uf94MuXUIqykDng/4H7xCc47
BZhNFJiPC6XHaXzN7NYSEUNX9VOwY8ncxKwtP6TXga96PdMUy/p98KIM8RZlDoxB
Xy9dcZBFNn/zrqjW7R0CCWCUriDIFSmEP0wDZ91YYa6BVuJMb5uL/USkTLpjZS4/
HNGvug==
-----END CERTIFICATE-----

View File

@ -1,10 +0,0 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIBXTCCAQcCAQAwfTELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMQswCQYDVQQH
EwJTRjEPMA0GA1UEChMGSm95ZW50MRAwDgYDVQQLEwdOb2RlLmpzMQ8wDQYDVQQD
EwZhZ2VudDIxIDAeBgkqhkiG9w0BCQEWEXJ5QHRpbnljbG91ZHMub3JnMFwwDQYJ
KoZIhvcNAQEBBQADSwAwSAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf
+6fVgdpVhYg5QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAaAlMCMGCSqG
SIb3DQEJBzEWExRBIGNoYWxsZW5nZSBwYXNzd29yZDANBgkqhkiG9w0BAQUFAANB
AJnll2pt5l0pzskQSpjjLVTlFDFmJr/AZ3UK8v0WxBjYjCe5Jx4YehkChpxIyDUm
U3J9q9MDUf0+Y2+EGkssFfk=
-----END CERTIFICATE REQUEST-----

View File

@ -1,9 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIBOgIBAAJBAMl2/Ba0XSm4ayi4C0rJ+tYtQu8O31VVXezkLJlf+6fVgdpVhYg5
QlihlPUoiM/wOsDWQ1ALnNhPlcLaQk+etQECAwEAAQJBAMT6Bf34+UHKY1ObpsbH
9u2jsVblFq1rWvs8GPMY6oertzvwm3DpuSUp7PTgOB1nLTLYtCERbQ4ovtN8tn3p
OHUCIQDzIEGsoCr5vlxXvy2zJwu+fxYuhTZWMVuo1397L0VyhwIhANQh+yzqUgaf
WRtSB4T2W7ADtJI35ET61jKBty3CqJY3AiAIwju7dVW3A5WeD6Qc1SZGKZvp9yCb
AFI2BfVwwaY11wIgXF3PeGcvACMyMWsuSv7aPXHfliswAbkWuzcwA4TW01ECIGWa
cgsDvVFxmfM5NPSuT/UDTa6R5BFISB5ea0N0AR3I
MIIEpQIBAAKCAQEAvSQq3d8AeZMTvtqZ13jWCckikyXJSACvkGCQUCJqOceESbg6
IHdRzQdoccE4P3sbvNsf9BlbdJKM+neCxabqKaU1PPje4P0tHT57t6yJrMuUh9Nx
Ez3Bgh1srNHVS7saKvwHmcKm79jc+wxlioPmEQvQagjny7oTkyLt0sn4LGxBjrcv
2JoHOC9f1pxX7l47MaiN0/ctRau7Nr3PFn+pkB4Yf6Z0VyicVJbaUSz39Qo4HQWl
1L2hiBP3CS1oKs2Yk0O1aOCMExWrhZQan+ZgHqL1rhgmkPpw2/zwwPt5Vf9CSakv
Hwg198EXuTTXtkzYduuIJAm8yp969iEIiG2xTwIDAQABAoIBAGPIw/C/qJF7HYyv
6T+7GTiaa2o0IiehbP3/Y8NTFLWc49a8obXlHTvMr7Zr2I/tE+ojtIzkH9K1SjkN
eelqsNj9tsOPDI6oIvftsflpxkxqLtclnt8m0oMhoObf4OaONDT/N8dP4SBiSdsM
ZDmacnMFx5NZVWiup4sVf2CYexx7qks9FhyN2K5PArCQ4S9LHjFhSJVH4DSEpv7E
Ykbp30rhpqV7wSwjgUsm8ZYvI2NOlmffzLSiPdt3vy2K5Q25S/MVEAicg83rfDgK
6EluHjeygRI1xU6DJ0hU7tnU7zE9KURoHPUycO3BKzZnzUH26AA36I58Pu4fXWw/
Cgmbv2ECgYEA+og9E4ziKCEi3p8gqjIfwTRgWZxDLjEzooB/K0UhEearn/xiX29A
FiSzEHKfCB4uSrw5OENg2ckDs8uy08Qmxx7xFXL7AtufAl5fIYaWa0sNSqCaIk7p
ebbUmPcaYhKiLzIEd1EYEL38sXVZ62wvSVMRSWvEMq44g1qnoRlDa/8CgYEAwUTt
talYNwVmR9ZdkVEWm9ZxirdzoM6NaM6u4Tf34ygptpapdmIFSUhfq4iOiEnRGNg/
tuNqhNCIb3LNpJbhRPEzqN7E7qiF/mp7AcJgbuxLZBm12QuLuJdG3nrisKPFXcY1
lA4A7CFmNgH3E4THFfgwzyDXsBOxVLXleTqn+rECgYEA9up1P6J3dtOJuV2d5P/3
ugRz/X173LfTSxJXw36jZDAy8D/feG19/RT4gnplcKvGNhQiVOhbOOnbw0U8n2fQ
TCmbs+cZqyxnH/+AxNsPvvk+RVHZ93xMsY/XIldP4l65B8jFDA+Zp06IESI2mEeM
pzi+bd1Phh+dRSCA2865W2MCgYEAlxYsgmQ1WyX0dFpHYU+zzfXRYzDQyrhOYc2Z
duVK+yCto1iad7pfCY/zgmRJkI+sT7DV9kJIRjXDQuTLkEyHJF8vFGe6KhxCS8aw
DIsI2g4NTd6vg1J8UryoIUqNpqsQoqNNxUVBQVdG0ReuMGsPO8R/W50AIFz0txVP
o/rP0LECgYEA7e/mOwCnR+ovmS/CAksmos3oIqvkRkXNKpKe513FVmp3TpTU38ex
cBkFNU3hEO31FyrX1hGIKp3N5mHYSQ1lyODHM6teHW0OLWWTwIe8rIGvR2jfRLe0
bbkdj40atYVkfeFmpz9uHHG24CUYxJdPc360jbXTVp4i3q8zqgL5aMY=
-----END RSA PRIVATE KEY-----

View File

@ -1,19 +0,0 @@
[ req ]
default_bits = 1024
days = 999
distinguished_name = req_distinguished_name
attributes = req_attributes
prompt = no
[ req_distinguished_name ]
C = US
ST = CA
L = SF
O = Joyent
OU = Node.js
CN = agent2
emailAddress = ry@tinyclouds.org
[ req_attributes ]
challengePassword = A challenge password

View File

@ -1,4 +1,5 @@
var common = require('../lib/http-proxy/common'),
url = require('url'),
expect = require('expect.js');
describe('lib/http-proxy/common.js', function () {
@ -16,6 +17,7 @@ describe('lib/http-proxy/common.js', function () {
},
headers: {'fizz': 'bang', 'overwritten':true},
localAddress: 'local.address',
auth:'username:pass'
},
{
method : 'i',
@ -36,6 +38,7 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.headers.fizz).to.eql('bang');
expect(outgoing.headers.overwritten).to.eql(true);
expect(outgoing.localAddress).to.eql('local.address');
expect(outgoing.auth).to.eql('username:pass');
});
it('should not override agentless upgrade header', function () {
@ -146,7 +149,7 @@ describe('lib/http-proxy/common.js', function () {
{
method : 'i',
url : 'am',
headers : 'proxy'
headers : {pro:'xy'}
});
expect(outgoing.host).to.eql('how');
@ -156,7 +159,7 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.method).to.eql('i');
expect(outgoing.path).to.eql('am');
expect(outgoing.headers).to.eql('proxy')
expect(outgoing.headers.pro).to.eql('xy');
expect(outgoing.port).to.eql(443);
});
@ -184,6 +187,18 @@ describe('lib/http-proxy/common.js', function () {
expect(outgoing.path).to.eql('some-path/am');
});
it('should properly detect https/wss protocol without the colon', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: {
protocol: 'https',
host: 'whatever.com'
}
}, { url: '/' });
expect(outgoing.port).to.eql(443);
});
it('should not prepend the target path to the outgoing path with prependPath = false', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
@ -207,10 +222,151 @@ describe('lib/http-proxy/common.js', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: { path: '/forward' },
}, { url: '/?foo=bar//&target=http://foobar.com/' });
}, { url: '/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2' });
expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/');
expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/?a=1%26b=2&other=2');
})
//
// This is the proper failing test case for the common.join problem
//
it('should correctly format the toProxy URL', function () {
var outgoing = {};
var google = 'https://google.com'
common.setupOutgoing(outgoing, {
target: url.parse('http://sometarget.com:80'),
toProxy: true,
}, { url: google });
expect(outgoing.path).to.eql('/' + google);
});
it('should not replace :\ to :\\ when no https word before', function () {
var outgoing = {};
var google = 'https://google.com:/join/join.js'
common.setupOutgoing(outgoing, {
target: url.parse('http://sometarget.com:80'),
toProxy: true,
}, { url: google });
expect(outgoing.path).to.eql('/' + google);
});
it('should not replace :\ to :\\ when no http word before', function () {
var outgoing = {};
var google = 'http://google.com:/join/join.js'
common.setupOutgoing(outgoing, {
target: url.parse('http://sometarget.com:80'),
toProxy: true,
}, { url: google });
expect(outgoing.path).to.eql('/' + google);
});
describe('when using ignorePath', function () {
it('should ignore the path of the `req.url` passed in but use the target path', function () {
var outgoing = {};
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
common.setupOutgoing(outgoing, {
target: url.parse(myEndpoint),
ignorePath: true
}, { url: '/more/crazy/pathness' });
expect(outgoing.path).to.eql('/some/crazy/path/whoooo');
});
it('and prependPath: false, it should ignore path of target and incoming request', function () {
var outgoing = {};
var myEndpoint = 'https://whatever.com/some/crazy/path/whoooo';
common.setupOutgoing(outgoing, {
target: url.parse(myEndpoint),
ignorePath: true,
prependPath: false
}, { url: '/more/crazy/pathness' });
expect(outgoing.path).to.eql('');
});
});
describe('when using changeOrigin', function () {
it('should correctly set the port to the host when it is a non-standard port using url.parse', function () {
var outgoing = {};
var myEndpoint = 'https://myCouch.com:6984';
common.setupOutgoing(outgoing, {
target: url.parse(myEndpoint),
changeOrigin: true
}, { url: '/' });
expect(outgoing.headers.host).to.eql('mycouch.com:6984');
});
it('should correctly set the port to the host when it is a non-standard port when setting host and port manually (which ignores port)', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: {
protocol: 'https:',
host: 'mycouch.com',
port: 6984
},
changeOrigin: true
}, { url: '/' });
expect(outgoing.headers.host).to.eql('mycouch.com:6984');
})
});
it('should pass through https client parameters', function () {
var outgoing = {};
common.setupOutgoing(outgoing,
{
agent : '?',
target: {
host : 'how',
hostname : 'are',
socketPath: 'you',
protocol: 'https:',
pfx: 'my-pfx',
key: 'my-key',
passphrase: 'my-passphrase',
cert: 'my-cert',
ca: 'my-ca',
ciphers: 'my-ciphers',
secureProtocol: 'my-secure-protocol'
}
},
{
method : 'i',
url : 'am'
});
expect(outgoing.pfx).eql('my-pfx');
expect(outgoing.key).eql('my-key');
expect(outgoing.passphrase).eql('my-passphrase');
expect(outgoing.cert).eql('my-cert');
expect(outgoing.ca).eql('my-ca');
expect(outgoing.ciphers).eql('my-ciphers');
expect(outgoing.secureProtocol).eql('my-secure-protocol');
});
it('should handle overriding the `method` of the http request', function () {
var outgoing = {};
common.setupOutgoing(outgoing, {
target: url.parse('https://whooooo.com'),
method: 'POST' ,
}, { method: 'GET', url: '' });
expect(outgoing.method).eql('POST');
});
// url.parse('').path => null
it('should not pass null as last arg to #urlJoin', function(){
var outgoing = {};
common.setupOutgoing(outgoing, {target:
{ path: '' }
}, { url : '' });
expect(outgoing.path).to.be('');
});
});
describe('#setupSocket', function () {

View File

@ -1,18 +1,42 @@
var webPasses = require('../lib/http-proxy/passes/web-incoming'),
httpProxy = require('../lib/http-proxy'),
expect = require('expect.js'),
concat = require('concat-stream'),
async = require('async'),
url = require('url'),
http = require('http');
describe('lib/http-proxy/passes/web.js', function() {
describe('#deleteLength', function() {
it('should change `content-length`', function() {
it('should change `content-length` for DELETE requests', function() {
var stubRequest = {
method: 'DELETE',
headers: {}
};
webPasses.deleteLength(stubRequest, {}, {});
expect(stubRequest.headers['content-length']).to.eql('0');
})
});
it('should change `content-length` for OPTIONS requests', function() {
var stubRequest = {
method: 'OPTIONS',
headers: {}
};
webPasses.deleteLength(stubRequest, {}, {});
expect(stubRequest.headers['content-length']).to.eql('0');
});
it('should remove `transfer-encoding` from empty DELETE requests', function() {
var stubRequest = {
method: 'DELETE',
headers: {
'transfer-encoding': 'chunked'
}
};
webPasses.deleteLength(stubRequest, {}, {});
expect(stubRequest.headers['content-length']).to.eql('0');
expect(stubRequest.headers).to.not.have.key('transfer-encoding');
});
});
describe('#timeout', function() {
@ -102,6 +126,50 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8081', function() {}).end();
});
it('should skip proxyReq event when handling a request with header "expect: 100-continue" [https://www.npmjs.com/advisories/1486]', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
});
proxy.on('proxyReq', function(proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
function requestHandler(req, res) {
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
source.close();
proxyServer.close();
expect(req.headers['x-special-proxy-header']).to.not.eql('foobar');
done();
});
proxyServer.listen('8081');
source.listen('8080');
const postData = ''.padStart(1025, 'x');
const postOptions = {
hostname: '127.0.0.1',
port: 8081,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData),
'expect': '100-continue'
}
};
const req = http.request(postOptions, function() {});
req.write(postData);
req.end();
});
it('should proxy the request and handle error via callback', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080'
@ -156,6 +224,35 @@ describe('#createProxyServer.web() using own http server', function () {
}, function() {}).end();
});
it('should forward the request and handle error via event listener', function(done) {
var proxy = httpProxy.createProxyServer({
forward: 'http://127.0.0.1:8080'
});
var proxyServer = http.createServer(requestHandler);
function requestHandler(req, res) {
proxy.once('error', function (err, errReq, errRes) {
proxyServer.close();
expect(err).to.be.an(Error);
expect(errReq).to.be.equal(req);
expect(errRes).to.be.equal(res);
expect(err.code).to.be('ECONNREFUSED');
done();
});
proxy.web(req, res);
}
proxyServer.listen('8083');
http.request({
hostname: '127.0.0.1',
port: '8083',
method: 'GET',
}, function() {}).end();
});
it('should proxy the request and handle timeout error (proxyTimeout)', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:45000',
@ -208,7 +305,7 @@ describe('#createProxyServer.web() using own http server', function () {
var started = new Date().getTime();
function requestHandler(req, res) {
proxy.once('error', function (err, errReq, errRes) {
proxy.once('econnreset', function (err, errReq, errRes) {
proxyServer.close();
expect(err).to.be.an(Error);
expect(errReq).to.be.equal(req);
@ -265,6 +362,44 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8086', function() {}).end();
});
it('should proxy the request and provide and respond to manual user response when using modifyResponse', function(done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
selfHandleResponse: true
});
function requestHandler(req, res) {
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
proxyRes.pipe(concat(function (body) {
expect(body.toString('utf8')).eql('Response');
pRes.end(Buffer.from('my-custom-response'));
}))
});
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
res.end('Response');
});
async.parallel([
next => proxyServer.listen(8086, next),
next => source.listen(8080, next)
], function (err) {
http.get('http://127.0.0.1:8086', function(res) {
res.pipe(concat(function(body) {
expect(body.toString('utf8')).eql('my-custom-response');
source.close();
proxyServer.close();
done();
}));
}).once('error', done);
})
});
it('should proxy the request and handle changeOrigin option', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
@ -290,4 +425,112 @@ describe('#createProxyServer.web() using own http server', function () {
http.request('http://127.0.0.1:8081', function() {}).end();
});
});
it('should proxy the request with the Authorization header set', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
auth: 'user:pass'
});
function requestHandler(req, res) {
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
source.close();
proxyServer.close();
var auth = new Buffer(req.headers.authorization.split(' ')[1], 'base64');
expect(req.method).to.eql('GET');
expect(auth.toString()).to.eql('user:pass');
done();
});
proxyServer.listen('8081');
source.listen('8080');
http.request('http://127.0.0.1:8081', function() {}).end();
});
it('should proxy requests to multiple servers with different options', function (done) {
var proxy = httpProxy.createProxyServer();
// proxies to two servers depending on url, rewriting the url as well
// http://127.0.0.1:8080/s1/ -> http://127.0.0.1:8081/
// http://127.0.0.1:8080/ -> http://127.0.0.1:8082/
function requestHandler(req, res) {
if (req.url.indexOf('/s1/') === 0) {
proxy.web(req, res, {
ignorePath: true,
target: 'http://127.0.0.1:8081' + req.url.substring(3)
});
} else {
proxy.web(req, res, {
target: 'http://127.0.0.1:8082'
});
}
}
var proxyServer = http.createServer(requestHandler);
var source1 = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8080');
expect(req.url).to.eql('/test1');
});
var source2 = http.createServer(function(req, res) {
source1.close();
source2.close();
proxyServer.close();
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql('8080');
expect(req.url).to.eql('/test2');
done();
});
proxyServer.listen('8080');
source1.listen('8081');
source2.listen('8082');
http.request('http://127.0.0.1:8080/s1/test1', function() {}).end();
http.request('http://127.0.0.1:8080/test2', function() {}).end();
});
});
describe('#followRedirects', function () {
it('should proxy the request follow redirects', function (done) {
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:8080',
followRedirects: true
});
function requestHandler(req, res) {
proxy.web(req, res);
}
var proxyServer = http.createServer(requestHandler);
var source = http.createServer(function(req, res) {
if (url.parse(req.url).pathname === '/redirect') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('ok');
}
res.writeHead(301, { 'Location': '/redirect' });
res.end();
});
proxyServer.listen('8081');
source.listen('8080');
http.request('http://127.0.0.1:8081', function(res) {
source.close();
proxyServer.close();
expect(res.statusCode).to.eql(200);
done();
}).end();
});
});

View File

@ -2,6 +2,146 @@ var httpProxy = require('../lib/http-proxy/passes/web-outgoing'),
expect = require('expect.js');
describe('lib/http-proxy/passes/web-outgoing.js', function () {
describe('#setRedirectHostRewrite', function () {
beforeEach(function() {
this.req = {
headers: {
host: 'ext-auto.com'
}
};
this.proxyRes = {
statusCode: 301,
headers: {
location: 'http://backend.com/'
}
};
this.options = {
target: 'http://backend.com'
};
});
context('rewrites location host with hostRewrite', function() {
beforeEach(function() {
this.options.hostRewrite = 'ext-manual.com';
});
[201, 301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
});
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when hostRewrite is unset', function() {
delete this.options.hostRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('takes precedence over autoRewrite', function() {
this.options.autoRewrite = true;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
});
it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = 'http://some-other/';
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});
it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = 'http://backend.com:8080/';
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});
context('rewrites location host with autoRewrite', function() {
beforeEach(function() {
this.options.autoRewrite = true;
});
[201, 301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-auto.com/');
});
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when autoRewrite is unset', function() {
delete this.options.autoRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = 'http://some-other/';
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});
it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = 'http://backend.com:8080/';
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});
context('rewrites location protocol with protocolRewrite', function() {
beforeEach(function() {
this.options.protocolRewrite = 'https';
});
[201, 301, 302, 307, 308].forEach(function(code) {
it('on ' + code, function() {
this.proxyRes.statusCode = code;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://backend.com/');
});
});
it('not on 200', function() {
this.proxyRes.statusCode = 200;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('not when protocolRewrite is unset', function() {
delete this.options.protocolRewrite;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});
it('works together with hostRewrite', function() {
this.options.hostRewrite = 'ext-manual.com';
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://ext-manual.com/');
});
it('works together with autoRewrite', function() {
this.options.autoRewrite = true;
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('https://ext-auto.com/');
});
});
});
describe('#setConnection', function () {
it('set the right connection with 1.0 - `close`', function() {
var proxyRes = { headers: {} };
@ -12,7 +152,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql('close');
expect(proxyRes.headers.connection).to.eql('close');
});
it('set the right connection with 1.0 - req.connection', function() {
@ -24,7 +164,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql('hey');
expect(proxyRes.headers.connection).to.eql('hey');
});
it('set the right connection - req.connection', function() {
@ -36,7 +176,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql('hola');
expect(proxyRes.headers.connection).to.eql('hola');
});
it('set the right connection - `keep-alive`', function() {
@ -48,9 +188,31 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql('keep-alive');
expect(proxyRes.headers.connection).to.eql('keep-alive');
});
it('don`t set connection with 2.0 if exist', function() {
var proxyRes = { headers: {} };
httpProxy.setConnection({
httpVersion: '2.0',
headers: {
connection: 'namstey'
}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql(undefined);
});
it('don`t set connection with 2.0 if doesn`t exist', function() {
var proxyRes = { headers: {} };
httpProxy.setConnection({
httpVersion: '2.0',
headers: {}
}, {}, proxyRes);
expect(proxyRes.headers.connection).to.eql(undefined);
})
});
describe('#writeStatusCode', function () {
@ -59,31 +221,189 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
writeHead: function(n) {
expect(n).to.eql(200);
}
}
};
httpProxy.writeStatusCode({}, res, { statusCode: 200 });
});
});
describe('#writeHeaders', function() {
var proxyRes = {
headers: {
hey: 'hello',
how: 'are you?'
}
};
beforeEach(function() {
this.proxyRes = {
headers: {
hey: 'hello',
how: 'are you?',
'set-cookie': [
'hello; domain=my.domain; path=/',
'there; domain=my.domain; path=/'
]
}
};
this.rawProxyRes = {
headers: {
hey: 'hello',
how: 'are you?',
'set-cookie': [
'hello; domain=my.domain; path=/',
'there; domain=my.domain; path=/'
]
},
rawHeaders: [
'Hey', 'hello',
'How', 'are you?',
'Set-Cookie', 'hello; domain=my.domain; path=/',
'Set-Cookie', 'there; domain=my.domain; path=/'
]
};
this.res = {
setHeader: function(k, v) {
// https://nodejs.org/api/http.html#http_message_headers
// Header names are lower-cased
this.headers[k.toLowerCase()] = v;
},
headers: {}
};
});
var res = {
setHeader: function(k, v) {
this.headers[k] = v;
},
headers: {}
};
it('writes headers', function() {
var options = {};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
httpProxy.writeHeaders({}, res, proxyRes);
expect(this.res.headers.hey).to.eql('hello');
expect(this.res.headers.how).to.eql('are you?');
expect(res.headers.hey).to.eql('hello');
expect(res.headers.how).to.eql('are you?');
expect(this.res.headers).to.have.key('set-cookie');
expect(this.res.headers['set-cookie']).to.be.an(Array);
expect(this.res.headers['set-cookie']).to.have.length(2);
});
it('writes raw headers', function() {
var options = {};
httpProxy.writeHeaders({}, this.res, this.rawProxyRes, options);
expect(this.res.headers.hey).to.eql('hello');
expect(this.res.headers.how).to.eql('are you?');
expect(this.res.headers).to.have.key('set-cookie');
expect(this.res.headers['set-cookie']).to.be.an(Array);
expect(this.res.headers['set-cookie']).to.have.length(2);
});
it('rewrites path', function() {
var options = {
cookiePathRewrite: '/dummyPath'
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain; path=/dummyPath');
});
it('does not rewrite path', function() {
var options = {};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain; path=/');
});
it('removes path', function() {
var options = {
cookiePathRewrite: ''
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain');
});
it('does not rewrite domain', function() {
var options = {};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.domain; path=/');
});
it('rewrites domain', function() {
var options = {
cookieDomainRewrite: 'my.new.domain'
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; domain=my.new.domain; path=/');
});
it('removes domain', function() {
var options = {
cookieDomainRewrite: ''
};
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello; path=/');
});
it('rewrites headers with advanced configuration', function() {
var options = {
cookieDomainRewrite: {
'*': '',
'my.old.domain': 'my.new.domain',
'my.special.domain': 'my.special.domain'
}
};
this.proxyRes.headers['set-cookie'] = [
'hello-on-my.domain; domain=my.domain; path=/',
'hello-on-my.old.domain; domain=my.old.domain; path=/',
'hello-on-my.special.domain; domain=my.special.domain; path=/'
];
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.domain; path=/');
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.old.domain; domain=my.new.domain; path=/');
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
});
it('rewrites raw headers with advanced configuration', function() {
var options = {
cookieDomainRewrite: {
'*': '',
'my.old.domain': 'my.new.domain',
'my.special.domain': 'my.special.domain'
}
};
this.rawProxyRes.headers['set-cookie'] = [
'hello-on-my.domain; domain=my.domain; path=/',
'hello-on-my.old.domain; domain=my.old.domain; path=/',
'hello-on-my.special.domain; domain=my.special.domain; path=/'
];
this.rawProxyRes.rawHeaders = this.rawProxyRes.rawHeaders.concat([
'Set-Cookie',
'hello-on-my.domain; domain=my.domain; path=/',
'Set-Cookie',
'hello-on-my.old.domain; domain=my.old.domain; path=/',
'Set-Cookie',
'hello-on-my.special.domain; domain=my.special.domain; path=/'
]);
httpProxy.writeHeaders({}, this.res, this.rawProxyRes, options);
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.domain; path=/');
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.old.domain; domain=my.new.domain; path=/');
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
});
});
@ -101,4 +421,3 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
});
});

View File

@ -1,8 +1,10 @@
var httpProxy = require('../lib/http-proxy'),
expect = require('expect.js'),
http = require('http'),
ws = require('ws')
net = require('net'),
ws = require('ws'),
io = require('socket.io'),
SSE = require('sse'),
ioClient = require('socket.io-client');
//
@ -16,7 +18,6 @@ Object.defineProperty(gen, 'port', {
}
});
describe('lib/http-proxy.js', function() {
describe('#createProxyServer', function() {
it.skip('should throw without options', function() {
@ -62,6 +63,41 @@ describe('lib/http-proxy.js', function() {
});
describe('#createProxyServer using the web-incoming passes', function () {
it('should proxy sse', function(done){
var ports = { source: gen.port, proxy: gen.port },
proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + ports.source,
}),
proxyServer = proxy.listen(ports.proxy),
source = http.createServer(),
sse = new SSE(source, {path: '/'});
sse.on('connection', function(client) {
client.send('Hello over SSE');
client.close();
});
source.listen(ports.source);
var options = {
hostname: 'localhost',
port: ports.proxy,
};
var req = http.request(options, function(res) {
var streamData = '';
res.on('data', function (chunk) {
streamData += chunk.toString('utf8');
});
res.on('end', function (chunk) {
expect(streamData).to.equal(':ok\n\ndata: Hello over SSE\n\n');
source.close();
proxy.close();
done();
});
}).end();
});
it('should make the request on pipe and finish it', function(done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
@ -94,7 +130,8 @@ describe('lib/http-proxy.js', function() {
it('should make the request, handle response and finish it', function(done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'http://127.0.0.1:' + ports.source
target: 'http://127.0.0.1:' + ports.source,
preserveHeaderKeyCase: true
}).listen(ports.proxy);
var source = http.createServer(function(req, res) {
@ -112,6 +149,11 @@ describe('lib/http-proxy.js', function() {
method: 'GET'
}, function(res) {
expect(res.statusCode).to.eql(200);
expect(res.headers['content-type']).to.eql('text/plain');
if (res.rawHeaders != undefined) {
expect(res.rawHeaders.indexOf('Content-Type')).not.to.eql(-1);
expect(res.rawHeaders.indexOf('text/plain')).not.to.eql(-1);
}
res.on('data', function (data) {
expect(data.toString()).to.eql('Hello from ' + ports.source);
@ -187,11 +229,54 @@ describe('lib/http-proxy.js', function() {
});
testReq.end();
});
});
describe('#createProxyServer with xfwd option', function () {
it('should not throw on empty http host header', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
forward: 'http://127.0.0.1:' + ports.source,
xfwd: true
}).listen(ports.proxy);
var source = http.createServer(function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.source);
source.close();
proxy.close();
done();
});
source.listen(ports.source);
var socket = net.connect({port: ports.proxy}, function()
{
socket.write('GET / HTTP/1.0\r\n\r\n');
});
// handle errors
socket.on('error', function()
{
expect.fail('Unexpected socket error');
});
socket.on('data', function(data)
{
socket.end();
});
socket.on('end', function()
{
expect('Socket to finish').to.be.ok();
});
// http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
})
})
});
// describe('#createProxyServer using the web-incoming passes', function () {
// it('should emit events correclty', function(done) {
// it('should emit events correctly', function(done) {
// var proxy = httpProxy.createProxyServer({
// target: 'http://127.0.0.1:8080'
// }),
@ -280,7 +365,7 @@ describe('lib/http-proxy.js', function() {
client.on('open', function () {
client.send('hello there');
});
var count = 0;
function maybe_done () {
count += 1;
@ -330,21 +415,21 @@ describe('lib/http-proxy.js', function() {
client.on('error', function (err) {
expect(err).to.be.an(Error);
expect(err.code).to.be('ECONNRESET');
proxyServer.close();
done();
});
});
it('should proxy a socket.io stream', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
var ports = { source: gen.port, proxy: gen.port },
proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
});
proxyServer = proxy.listen(ports.proxy);
var server = http.createServer();
}),
proxyServer = proxy.listen(ports.proxy),
server = http.createServer(),
destiny = io.listen(server);
function startSocketIo() {
var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);
@ -369,5 +454,186 @@ describe('lib/http-proxy.js', function() {
});
})
});
})
it('should emit open and close events when socket.io client connects and disconnects', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
});
var proxyServer = proxy.listen(ports.proxy);
var server = http.createServer();
var destiny = io.listen(server);
function startSocketIo() {
var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy, {rejectUnauthorized: null});
client.on('connect', function () {
client.disconnect();
});
}
var count = 0;
proxyServer.on('open', function() {
count += 1;
});
proxyServer.on('close', function() {
proxyServer.close();
server.close();
destiny.close();
if (count == 1) { done(); }
});
server.listen(ports.source);
server.on('listening', startSocketIo);
});
it('should pass all set-cookie headers to client', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
}),
proxyServer = proxy.listen(ports.proxy),
destiny = new ws.Server({ port: ports.source }, function () {
var key = new Buffer(Math.random().toString()).toString('base64');
var requestOptions = {
port: ports.proxy,
host: '127.0.0.1',
headers: {
'Connection': 'Upgrade',
'Upgrade': 'websocket',
'Host': 'ws://127.0.0.1',
'Sec-WebSocket-Version': 13,
'Sec-WebSocket-Key': key
}
};
var req = http.request(requestOptions);
req.on('upgrade', function (res, socket, upgradeHead) {
expect(res.headers['set-cookie'].length).to.be(2);
done();
});
req.end();
});
destiny.on('headers', function (headers) {
headers.push('Set-Cookie: test1=test1');
headers.push('Set-Cookie: test2=test2');
});
});
it('should detect a proxyReq event and modify headers', function (done) {
var ports = { source: gen.port, proxy: gen.port },
proxy,
proxyServer,
destiny;
proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
});
proxy.on('proxyReqWs', function(proxyReq, req, socket, options, head) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
proxyServer = proxy.listen(ports.proxy);
destiny = new ws.Server({ port: ports.source }, function () {
var client = new ws('ws://127.0.0.1:' + ports.proxy);
client.on('open', function () {
client.send('hello there');
});
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
destiny.close();
done();
});
});
destiny.on('connection', function (socket, upgradeReq) {
expect(upgradeReq.headers['x-special-proxy-header']).to.eql('foobar');
socket.on('message', function (msg) {
expect(msg).to.be('hello there');
socket.send('Hello over websockets');
});
});
});
it('should forward frames with single frame payload (including on node 4.x)', function (done) {
var payload = Array(65529).join('0');
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
}),
proxyServer = proxy.listen(ports.proxy),
destiny = new ws.Server({ port: ports.source }, function () {
var client = new ws('ws://127.0.0.1:' + ports.proxy);
client.on('open', function () {
client.send(payload);
});
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
destiny.close();
done();
});
});
destiny.on('connection', function (socket) {
socket.on('message', function (msg) {
expect(msg).to.be(payload);
socket.send('Hello over websockets');
});
});
});
it('should forward continuation frames with big payload (including on node 4.x)', function (done) {
var payload = Array(65530).join('0');
var ports = { source: gen.port, proxy: gen.port };
var proxy = httpProxy.createProxyServer({
target: 'ws://127.0.0.1:' + ports.source,
ws: true
}),
proxyServer = proxy.listen(ports.proxy),
destiny = new ws.Server({ port: ports.source }, function () {
var client = new ws('ws://127.0.0.1:' + ports.proxy);
client.on('open', function () {
client.send(payload);
});
client.on('message', function (msg) {
expect(msg).to.be('Hello over websockets');
client.close();
proxyServer.close();
destiny.close();
done();
});
});
destiny.on('connection', function (socket) {
socket.on('message', function (msg) {
expect(msg).to.be(payload);
socket.send('Hello over websockets');
});
});
});
});
});

View File

@ -1,4 +1,5 @@
var httpProxy = require('../lib/http-proxy'),
semver = require('semver'),
expect = require('expect.js'),
http = require('http')
https = require('https'),
@ -18,7 +19,7 @@ Object.defineProperty(gen, 'port', {
describe('lib/http-proxy.js', function() {
describe('HTTPS #createProxyServer', function() {
describe('HTTPS to HTTP', function () {
describe('HTTPS to HTTP', function () {
it('should proxy the request en send back the response', function (done) {
var ports = { source: gen.port, proxy: gen.port };
var source = http.createServer(function(req, res) {
@ -35,6 +36,7 @@ describe('lib/http-proxy.js', function() {
ssl: {
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
}
}).listen(ports.proxy);
@ -65,6 +67,7 @@ describe('lib/http-proxy.js', function() {
var source = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
}, function (req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
@ -105,6 +108,7 @@ describe('lib/http-proxy.js', function() {
var source = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
}, function(req, res) {
expect(req.method).to.eql('GET');
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
@ -119,6 +123,7 @@ describe('lib/http-proxy.js', function() {
ssl: {
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
},
secure: false
}).listen(ports.proxy);
@ -150,6 +155,7 @@ describe('lib/http-proxy.js', function() {
var source = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
}).listen(ports.source);
var proxy = httpProxy.createProxyServer({
@ -161,7 +167,11 @@ describe('lib/http-proxy.js', function() {
proxy.on('error', function (err, req, res) {
expect(err).to.be.an(Error);
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
if (semver.gt(process.versions.node, '0.12.0')) {
expect(err.toString()).to.be('Error: unable to verify the first certificate')
} else {
expect(err.toString()).to.be('Error: DEPTH_ZERO_SELF_SIGNED_CERT')
}
done();
})
@ -191,6 +201,7 @@ describe('lib/http-proxy.js', function() {
var ownServer = https.createServer({
key: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'fixtures', 'agent2-cert.pem')),
ciphers: 'AES128-GCM-SHA256',
}, function (req, res) {
proxy.web(req, res, {
target: 'http://127.0.0.1:' + ports.source