mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
Compare commits
1157 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b96cd7251 | ||
|
|
335aeeba2f | ||
|
|
dba39668ba | ||
|
|
9bbe486c5e | ||
|
|
6e4bef4d1c | ||
|
|
d05624167c | ||
|
|
244303b994 | ||
|
|
b4028ba78b | ||
|
|
77a98159d2 | ||
|
|
c662f9ebcd | ||
|
|
806e4927c9 | ||
|
|
c8fa599983 | ||
|
|
0d9ed366b1 | ||
|
|
9d75b981a1 | ||
|
|
192b2b980b | ||
|
|
4a657a7126 | ||
|
|
7a154f81d1 | ||
|
|
749eec65c3 | ||
|
|
e588213644 | ||
|
|
59c4403e9d | ||
|
|
dd1d08b631 | ||
|
|
16d4f8a951 | ||
|
|
fc93520d74 | ||
|
|
7e4a0e511b | ||
|
|
a9b09cce43 | ||
|
|
b00911c937 | ||
|
|
36bfe566a7 | ||
|
|
91fee3e943 | ||
|
|
235f0aa047 | ||
|
|
acdbec09c6 | ||
|
|
569e2ac4fb | ||
|
|
cb3171abfa | ||
|
|
a3fe02d651 | ||
|
|
42e8e1e099 | ||
|
|
e94d52973a | ||
|
|
4a37175a52 | ||
|
|
e5c02b8a8a | ||
|
|
2c44039a7c | ||
|
|
8097ae237e | ||
|
|
abf882e03c | ||
|
|
bab02e909e | ||
|
|
c9a556cfa5 | ||
|
|
6f88caf6e4 | ||
|
|
d2f9db8241 | ||
|
|
8231984fb0 | ||
|
|
81d58c531b | ||
|
|
d533a1be43 | ||
|
|
89f9ef87e0 | ||
|
|
c5d8466483 | ||
|
|
107c18720c | ||
|
|
de1b80851a | ||
|
|
bc6a23709c | ||
|
|
f5c2381395 | ||
|
|
50f58b4cd9 | ||
|
|
2c98416ac2 | ||
|
|
543636d0f6 | ||
|
|
a4bccc332d | ||
|
|
f4ff1006b9 | ||
|
|
09dcb98456 | ||
|
|
812757541d | ||
|
|
c979ba9f2c | ||
|
|
e6f24ba617 | ||
|
|
d73f1ee873 | ||
|
|
a539f3cbc1 | ||
|
|
d4d85ac5c4 | ||
|
|
c1fb596b85 | ||
|
|
961f457622 | ||
|
|
ac1a01b1f3 | ||
|
|
8cb451f20c | ||
|
|
c252b32f6c | ||
|
|
927357bedc | ||
|
|
4edbb62cc5 | ||
|
|
69cf892519 | ||
|
|
2f7f03778c | ||
|
|
d8223884f6 | ||
|
|
d0f1dfeb82 | ||
|
|
220f5fb795 | ||
|
|
d48f67eb90 | ||
|
|
61c2889109 | ||
|
|
fbc266809c | ||
|
|
8eddf45f2a | ||
|
|
f5217d6c20 | ||
|
|
2d01edc5a5 | ||
|
|
d0a1588639 | ||
|
|
912cd3acae | ||
|
|
84087208dd | ||
|
|
b98c75b1ff | ||
|
|
b781af641a | ||
|
|
cbd5777060 | ||
|
|
d8fb344715 | ||
|
|
1e52f660f0 | ||
|
|
9d06ca99d3 | ||
|
|
d0e000e1f9 | ||
|
|
183b5bb4fc | ||
|
|
3a347af543 | ||
|
|
fcfb0b37f6 | ||
|
|
42df703830 | ||
|
|
3e966361bc | ||
|
|
5082acc067 | ||
|
|
433a7408cf | ||
|
|
6baf1498cb | ||
|
|
284903d379 | ||
|
|
b223275ebb | ||
|
|
ecdfff408f | ||
|
|
28cecb5a96 | ||
|
|
bdb3492b21 | ||
|
|
a4b2857216 | ||
|
|
820fc5987c | ||
|
|
f345a1ac2d | ||
|
|
018352955e | ||
|
|
f8d605d53f | ||
|
|
c1440b58f1 | ||
|
|
ef86b50427 | ||
|
|
e1b2f4c31b | ||
|
|
820f198729 | ||
|
|
d2df053677 | ||
|
|
7357200334 | ||
|
|
d19be83858 | ||
|
|
570cf3b4f9 | ||
|
|
5c46e4b754 | ||
|
|
25e56d0182 | ||
|
|
9d9fa940cf | ||
|
|
2831d9a4f9 | ||
|
|
a379d160f3 | ||
|
|
7aed1f4559 | ||
|
|
bfcab93d8d | ||
|
|
7bad3fbca4 | ||
|
|
268994ea45 | ||
|
|
58c0fdc761 | ||
|
|
d73b0d312a | ||
|
|
a76e226221 | ||
|
|
db576d75c1 | ||
|
|
e017fa8189 | ||
|
|
646a7493a4 | ||
|
|
3b39d2c3dc | ||
|
|
0cb1d3c68e | ||
|
|
f9540de7b1 | ||
|
|
f2093b5313 | ||
|
|
41414a56a1 | ||
|
|
6371231086 | ||
|
|
6c83980f29 | ||
|
|
e4760727f1 | ||
|
|
222a4d0fa1 | ||
|
|
6106d4c32f | ||
|
|
cd1d7776e8 | ||
|
|
642e9cc2cb | ||
|
|
eea79cab53 | ||
|
|
f82ce18d2f | ||
|
|
470475b896 | ||
|
|
2a5e69d48c | ||
|
|
9ea1e89a2f | ||
|
|
ab42124d54 | ||
|
|
5d593e8ef1 | ||
|
|
64fa520789 | ||
|
|
b5a6d0e583 | ||
|
|
0bc4c783ca | ||
|
|
9752652e76 | ||
|
|
a05fc2d169 | ||
|
|
60baca5aed | ||
|
|
6b65c428b3 | ||
|
|
eb97bf5423 | ||
|
|
ec64e4f597 | ||
|
|
e666a4e07d | ||
|
|
c86ae51bb9 | ||
|
|
302d981dd2 | ||
|
|
30e3b371de | ||
|
|
cea0e8676b | ||
|
|
3d2350c54f | ||
|
|
ca73208749 | ||
|
|
da674ec4df | ||
|
|
855cebdac4 | ||
|
|
8bfd90c4d9 | ||
|
|
931f73dc98 | ||
|
|
349b843731 | ||
|
|
2e3eb0950d | ||
|
|
1f4fccd2c6 | ||
|
|
ecbba1a008 | ||
|
|
fe3dd8363f | ||
|
|
38864d0167 | ||
|
|
76051032e7 | ||
|
|
7e82a04a16 | ||
|
|
1912b62ddb | ||
|
|
3f997b9289 | ||
|
|
7e6c66a7e4 | ||
|
|
d26ef56e1b | ||
|
|
607f96c00c | ||
|
|
18c77cafc7 | ||
|
|
934e6c4d54 | ||
|
|
0db8f195d7 | ||
|
|
0bd446c680 | ||
|
|
a6ae6c4997 | ||
|
|
1b89bc9a76 | ||
|
|
c6dfb04a67 | ||
|
|
6201ac76f7 | ||
|
|
5f14bcaa70 | ||
|
|
0ee314c436 | ||
|
|
c33d1616cd | ||
|
|
1dabda241f | ||
|
|
5a969d077b | ||
|
|
21b30b754d | ||
|
|
aa8f3e9a6e | ||
|
|
ab37a224aa | ||
|
|
d145152655 | ||
|
|
87a92a7280 | ||
|
|
507f818df5 | ||
|
|
e907d7bb2a | ||
|
|
7298510e91 | ||
|
|
63c9262df5 | ||
|
|
ff1626f071 | ||
|
|
df158bfc53 | ||
|
|
f55ffa356a | ||
|
|
ab5c3e5c81 | ||
|
|
245d73ae6c | ||
|
|
6d074eff47 | ||
|
|
26029ba7ac | ||
|
|
14415a5074 | ||
|
|
62e4b75101 | ||
|
|
7f2f3ac35c | ||
|
|
9eefd4678e | ||
|
|
9ece52fac4 | ||
|
|
32aa10dfe2 | ||
|
|
c82c9ece8a | ||
|
|
3311106c2c | ||
|
|
402ab05734 | ||
|
|
f0db5b3f70 | ||
|
|
f30486195c | ||
|
|
ea0a4ded80 | ||
|
|
8a8a894092 | ||
|
|
c62610e8e4 | ||
|
|
05d18a4e1b | ||
|
|
8bff3ddc12 | ||
|
|
26537866b3 | ||
|
|
f92a1b6839 | ||
|
|
c1a94176a8 | ||
|
|
099eb86948 | ||
|
|
4bd3c4671f | ||
|
|
6a330ff904 | ||
|
|
89f9ca1e89 | ||
|
|
c04485671a | ||
|
|
2086e4917c | ||
|
|
71a06aab02 | ||
|
|
501e8c2a9b | ||
|
|
81874f795b | ||
|
|
75a9a2de61 | ||
|
|
aeb42a3614 | ||
|
|
56a7b77645 | ||
|
|
9c0b8697bc | ||
|
|
410a8ce94c | ||
|
|
d98d9516ea | ||
|
|
70ed1c4273 | ||
|
|
361d4e3b00 | ||
|
|
276f65a3b8 | ||
|
|
8d68ac0e0f | ||
|
|
48ae5d828c | ||
|
|
95a588706b | ||
|
|
3194d819b4 | ||
|
|
8a24a1e18f | ||
|
|
add81338a9 | ||
|
|
daf66a7a88 | ||
|
|
aba505d159 | ||
|
|
84036e9ddd | ||
|
|
daa2ce0ee3 | ||
|
|
69a693034e | ||
|
|
9ba8311343 | ||
|
|
709b3e9656 | ||
|
|
3f19e6e178 | ||
|
|
7c5e40a429 | ||
|
|
eb95660822 | ||
|
|
4a2b870cc9 | ||
|
|
fa797fca90 | ||
|
|
57329b6327 | ||
|
|
d1eabccf93 | ||
|
|
60bd697051 | ||
|
|
1af8224cc1 | ||
|
|
43641b00b3 | ||
|
|
70fa8ad58c | ||
|
|
796ab0bcc5 | ||
|
|
f70015f001 | ||
|
|
f64954a08b | ||
|
|
9f684d0439 | ||
|
|
3768cce4ca | ||
|
|
d46e876e27 | ||
|
|
0378b03e53 | ||
|
|
04c45a022c | ||
|
|
9577a0faf2 | ||
|
|
fcdbf46e4d | ||
|
|
c62766391e | ||
|
|
43c6f0c7c0 | ||
|
|
b065d92908 | ||
|
|
bcd8a564a8 | ||
|
|
77305489d9 | ||
|
|
f0bf741815 | ||
|
|
10a294af4d | ||
|
|
232258b6ec | ||
|
|
9210b56c9e | ||
|
|
000eb533de | ||
|
|
2aa3b84d7b | ||
|
|
a4ca578b44 | ||
|
|
e7d50b1a37 | ||
|
|
c0a796b3e3 | ||
|
|
90d40d6a6a | ||
|
|
45cf95a82e | ||
|
|
42c35aedca | ||
|
|
ec683b924b | ||
|
|
65a21bce6d | ||
|
|
51eeebef68 | ||
|
|
554f59c518 | ||
|
|
73e8a4cdd5 | ||
|
|
3ab6e9591e | ||
|
|
a934cb6a46 | ||
|
|
107b9da256 | ||
|
|
73d865bc9f | ||
|
|
df12aeb12d | ||
|
|
ed73f06ed3 | ||
|
|
d5c656bceb | ||
|
|
dceef407a1 | ||
|
|
e44fabe58a | ||
|
|
9a534c6ff6 | ||
|
|
d1facd52c3 | ||
|
|
5568cb5575 | ||
|
|
6ac05259ca | ||
|
|
0e64568b4e | ||
|
|
fc73828035 | ||
|
|
814fbd254d | ||
|
|
a65021d52b | ||
|
|
511b7b3d47 | ||
|
|
6b83ae47bb | ||
|
|
d16062bba2 | ||
|
|
49a0de1e7c | ||
|
|
05f0b891a6 | ||
|
|
261742a429 | ||
|
|
f92f7aea9b | ||
|
|
8be9d945d0 | ||
|
|
0a6b424e2c | ||
|
|
37036dd325 | ||
|
|
a3d02196c5 | ||
|
|
63c53a1772 | ||
|
|
3e8ee042fb | ||
|
|
db5f2954b2 | ||
|
|
ed9e12b0ed | ||
|
|
5f838541cb | ||
|
|
e336b52629 | ||
|
|
7104a7c023 | ||
|
|
d1baa3684e | ||
|
|
e50846b967 | ||
|
|
9df4bc1e12 | ||
|
|
f6bac7b257 | ||
|
|
48d46e6750 | ||
|
|
7b79a7409a | ||
|
|
4193d3bd74 | ||
|
|
159ca83652 | ||
|
|
0f243516e1 | ||
|
|
431aba79d8 | ||
|
|
7cb98a4e41 | ||
|
|
5ebf9833c8 | ||
|
|
1385635e18 | ||
|
|
1213e46b1b | ||
|
|
c472527ea6 | ||
|
|
ccad177954 | ||
|
|
896ee7c9c3 | ||
|
|
8aa7c519b1 | ||
|
|
d637b96420 | ||
|
|
4947484806 | ||
|
|
c54278bd3b | ||
|
|
61c8734e8b | ||
|
|
77a1cff9bc | ||
|
|
d908e2ad61 | ||
|
|
4f07dc220d | ||
|
|
97ceeb37d0 | ||
|
|
8b48a9fdab | ||
|
|
c6b7a7919f | ||
|
|
7e5feec34f | ||
|
|
d658c9fa39 | ||
|
|
eca765a856 | ||
|
|
07fceb7c7a | ||
|
|
ece85b4e1b | ||
|
|
5251a238e7 | ||
|
|
89a22bc003 | ||
|
|
a7b16eb136 | ||
|
|
c22610af75 | ||
|
|
99f757251b | ||
|
|
e633b0f7e4 | ||
|
|
10670540ac | ||
|
|
68fa17bbca | ||
|
|
7923cf3c6b | ||
|
|
81daf26a43 | ||
|
|
80f645c7c0 | ||
|
|
4bdc3e4f45 | ||
|
|
4c3ba74c4e | ||
|
|
daad4703f3 | ||
|
|
b60673522b | ||
|
|
4ecc6e26ce | ||
|
|
8004f4e5fc | ||
|
|
3b2178d1f7 | ||
|
|
0b223abb65 | ||
|
|
7a53ec2fd6 | ||
|
|
96ea631fb6 | ||
|
|
3e47c2485c | ||
|
|
c5ec1836b2 | ||
|
|
e936d186b6 | ||
|
|
68c5512303 | ||
|
|
689459fe46 | ||
|
|
53a2653b5e | ||
|
|
3330e125aa | ||
|
|
d6d2d0c882 | ||
|
|
d4942e52e7 | ||
|
|
d23353d980 | ||
|
|
4351ed1c86 | ||
|
|
0ba4fa8e5e | ||
|
|
969a623542 | ||
|
|
0b642d4cf7 | ||
|
|
a4ee8f9d82 | ||
|
|
2c8edc170d | ||
|
|
c47adac391 | ||
|
|
c17b591b7d | ||
|
|
6fa23e11f6 | ||
|
|
182c76cd23 | ||
|
|
e5991519db | ||
|
|
840f6d8d29 | ||
|
|
a81dd8d53e | ||
|
|
8eb6780f87 | ||
|
|
1333c0cc62 | ||
|
|
2d42709c32 | ||
|
|
d4e91ebc33 | ||
|
|
7e8041d2b6 | ||
|
|
8332e74420 | ||
|
|
145798062e | ||
|
|
7c8ecc8ea8 | ||
|
|
25bb3bfa70 | ||
|
|
bc12ca3939 | ||
|
|
781c038f2b | ||
|
|
d60f1a9353 | ||
|
|
162a42f58f | ||
|
|
9243444ac0 | ||
|
|
db12f6c24e | ||
|
|
16828a9915 | ||
|
|
e2a5d513ca | ||
|
|
d83fdf69a1 | ||
|
|
bc236d7e95 | ||
|
|
c82ff2c3c0 | ||
|
|
d7064f2e1e | ||
|
|
de3ff11656 | ||
|
|
2142c506e0 | ||
|
|
e592c53d1a | ||
|
|
d85ccdd333 | ||
|
|
831a44b3c8 | ||
|
|
ed8c9eeba9 | ||
|
|
588327c2c4 | ||
|
|
e02317ce86 | ||
|
|
b726116134 | ||
|
|
7e44d3669b | ||
|
|
bdeabb767a | ||
|
|
cfd417de23 | ||
|
|
c4d56a5faf | ||
|
|
4d65280ea3 | ||
|
|
36389384cb | ||
|
|
9e74a633a7 | ||
|
|
97e4600e94 | ||
|
|
03880d8d06 | ||
|
|
0393b5da99 | ||
|
|
ae0faef5aa | ||
|
|
ef8150af95 | ||
|
|
584ce76e75 | ||
|
|
10c0f11b68 | ||
|
|
83367e7e91 | ||
|
|
cb7af4f4d7 | ||
|
|
dcb873ad99 | ||
|
|
54eceb4a86 | ||
|
|
7a3f6dfbcc | ||
|
|
04c10113f7 | ||
|
|
961d2f9400 | ||
|
|
bbe3bfdf98 | ||
|
|
31d919b0a3 | ||
|
|
6a6dfbb79d | ||
|
|
a467b7b4a9 | ||
|
|
fd42dcef01 | ||
|
|
a2b1f0a4c9 | ||
|
|
d0862aff0c | ||
|
|
cde08fb2ee | ||
|
|
590bb604da | ||
|
|
b8c6397a94 | ||
|
|
dda6f7a45a | ||
|
|
8085178dc2 | ||
|
|
1204a35e46 | ||
|
|
f720e36b42 | ||
|
|
33a2462d28 | ||
|
|
e3f8d5fdbe | ||
|
|
a1b25a123b | ||
|
|
920f1e7707 | ||
|
|
c75d06c5f9 | ||
|
|
d60353f80b | ||
|
|
02df9a33c5 | ||
|
|
881c7e62e0 | ||
|
|
cc09ae6a34 | ||
|
|
7c72f3b407 | ||
|
|
0bfb9be418 | ||
|
|
5d66ce11bb | ||
|
|
5e130de854 | ||
|
|
babdf531fe | ||
|
|
2bf20d61d5 | ||
|
|
52ecd52ee5 | ||
|
|
9b3e1eb247 | ||
|
|
90fb01d38a | ||
|
|
1d1ee88582 | ||
|
|
e4450132e2 | ||
|
|
bbe2b2788a | ||
|
|
86750c7e59 | ||
|
|
c65ffbb976 | ||
|
|
a7042132c8 | ||
|
|
b333e63648 | ||
|
|
c9f5772fc1 | ||
|
|
a9f9e21eda | ||
|
|
3d8e5383cd | ||
|
|
5a1504f076 | ||
|
|
c7924e01f9 | ||
|
|
601dbcbfe9 | ||
|
|
b79bd29d5e | ||
|
|
a51b062278 | ||
|
|
8269eca2bb | ||
|
|
1436b715ae | ||
|
|
bb0d28c587 | ||
|
|
f7f5fa727e | ||
|
|
455f97e14c | ||
|
|
17399e7c3e | ||
|
|
0aeaba7fe6 | ||
|
|
94ec6fa5ce | ||
|
|
32a40889ce | ||
|
|
32dcb0449c | ||
|
|
4a517fbe6e | ||
|
|
5dcdf2b36c | ||
|
|
7ad5c0f993 | ||
|
|
39b0c46a69 | ||
|
|
a350fadea6 | ||
|
|
1b5fb1d8fc | ||
|
|
12cda561af | ||
|
|
f566a42e51 | ||
|
|
4ee96ddd66 | ||
|
|
1c7ace26c5 | ||
|
|
7d840d3515 | ||
|
|
427d8d8536 | ||
|
|
13741a823f | ||
|
|
69f126b34c | ||
|
|
72a89eab8b | ||
|
|
1ceea3e5f9 | ||
|
|
f36cb4d5a1 | ||
|
|
ca092635e7 | ||
|
|
8663ac1c43 | ||
|
|
afc4d0931f | ||
|
|
6b61878759 | ||
|
|
16a4d9da11 | ||
|
|
2c10f256b6 | ||
|
|
611a1b1961 | ||
|
|
031452e400 | ||
|
|
9efa40a9d2 | ||
|
|
8eff1a1f26 | ||
|
|
10a0db4f0d | ||
|
|
0602500230 | ||
|
|
02007ed0fb | ||
|
|
40902506af | ||
|
|
7e25bded27 | ||
|
|
c01fd2c54e | ||
|
|
dc9d7e5452 | ||
|
|
7b9169c8c5 | ||
|
|
1cb967b90a | ||
|
|
5bb83b967e | ||
|
|
275a5192fa | ||
|
|
f1aeb0500c | ||
|
|
da9de7034a | ||
|
|
38e6d7cd54 | ||
|
|
469b0d4e9f | ||
|
|
9f3508805a | ||
|
|
dfdedf23b7 | ||
|
|
bd106d69f0 | ||
|
|
07091b5077 | ||
|
|
edd8e2f04e | ||
|
|
ef946a7697 | ||
|
|
268afe34bb | ||
|
|
1b867a7f59 | ||
|
|
29afab4488 | ||
|
|
e08d4edad3 | ||
|
|
60de543d04 | ||
|
|
adc5be020c | ||
|
|
0637322d96 | ||
|
|
4d3a4e1ee7 | ||
|
|
dd0f7b8876 | ||
|
|
d7078e2fdd | ||
|
|
ee3cc38066 | ||
|
|
aaff1966e4 | ||
|
|
57abb7f26c | ||
|
|
8b05626eed | ||
|
|
98f29bdcfc | ||
|
|
4c2f2f3b9a | ||
|
|
c9945dd370 | ||
|
|
5823842194 | ||
|
|
d1663549ec | ||
|
|
18341d5597 | ||
|
|
26c4c43a06 | ||
|
|
3c91ed3d26 | ||
|
|
46fe81e11a | ||
|
|
92de6a6f95 | ||
|
|
6e77cd3909 | ||
|
|
7599cee3fd | ||
|
|
dad211e71c | ||
|
|
63b016c8ef | ||
|
|
ec981c5b74 | ||
|
|
c4ddc4edd3 | ||
|
|
886a870707 | ||
|
|
8c8c455541 | ||
|
|
031aa0fbf3 | ||
|
|
07cfa6b981 | ||
|
|
7d71a867a8 | ||
|
|
6a03e5f7cf | ||
|
|
2a593664a5 | ||
|
|
893100972c | ||
|
|
79a14acfd2 | ||
|
|
f97c0c6167 | ||
|
|
1a7bef0cda | ||
|
|
e45bfd66a2 | ||
|
|
a74cd85c8a | ||
|
|
4a4607d075 | ||
|
|
0fb33810f5 | ||
|
|
c9cd6d2ad3 | ||
|
|
8b3fe32f6a | ||
|
|
e0faaaf811 | ||
|
|
1993faf8a4 | ||
|
|
3a39e444ff | ||
|
|
07551c63e4 | ||
|
|
79f7f99528 | ||
|
|
4480699d3a | ||
|
|
a6256cac1d | ||
|
|
b85aa16e75 | ||
|
|
27df8d72ad | ||
|
|
abf1d90fdf | ||
|
|
c9612798f1 | ||
|
|
8fc3389367 | ||
|
|
f4e9945856 | ||
|
|
2fac7b9b00 | ||
|
|
c02b721321 | ||
|
|
d40e4beb62 | ||
|
|
356f43d719 | ||
|
|
d4f0da898e | ||
|
|
9ab8749a9b | ||
|
|
bd3df45010 | ||
|
|
2e7343d728 | ||
|
|
6a4294cbdf | ||
|
|
4f24664e8a | ||
|
|
cedc5c4bd2 | ||
|
|
335af81d02 | ||
|
|
a255f984fe | ||
|
|
16eacfa961 | ||
|
|
004a46c09d | ||
|
|
34f16e7464 | ||
|
|
d05af4af60 | ||
|
|
8273cb6461 | ||
|
|
4d13156721 | ||
|
|
ebbba73eda | ||
|
|
2fd748fb61 | ||
|
|
e1d384e769 | ||
|
|
5ff2b6e1a1 | ||
|
|
f5e1844abd | ||
|
|
0c753234c0 | ||
|
|
de0928f616 | ||
|
|
7fc39d77f4 | ||
|
|
59b71c033f | ||
|
|
985025c90f | ||
|
|
98f5c462ea | ||
|
|
daf53bd753 | ||
|
|
a29b5e8e28 | ||
|
|
9c13ad46e4 | ||
|
|
3763dc935f | ||
|
|
71183bf30b | ||
|
|
8665f3cc60 | ||
|
|
4335f49b0e | ||
|
|
ea10bb22d9 | ||
|
|
2620f06e2d | ||
|
|
3ebc795c33 | ||
|
|
4e42354e77 | ||
|
|
35ba0db554 | ||
|
|
c9b6895c5e | ||
|
|
701dc698e3 | ||
|
|
ea5e214522 | ||
|
|
c78356e9cf | ||
|
|
c68e038912 | ||
|
|
133115c976 | ||
|
|
ad213106d0 | ||
|
|
ea0587a8f9 | ||
|
|
5d515e4728 | ||
|
|
5e6be6ccf5 | ||
|
|
a1607c1684 | ||
|
|
46b078a98d | ||
|
|
10f6b05775 | ||
|
|
2c3650746c | ||
|
|
89d43c20dd | ||
|
|
476cbe741f | ||
|
|
9be0af3166 | ||
|
|
a89e2f2688 | ||
|
|
6a278b3dd8 | ||
|
|
3130665d9f | ||
|
|
4c1a2c1416 | ||
|
|
b1c4bd61e8 | ||
|
|
3b86a7aae3 | ||
|
|
4c130f5dac | ||
|
|
9cecd97153 | ||
|
|
440013c263 | ||
|
|
ba65a485fc | ||
|
|
ef66833c4d | ||
|
|
af9eb06e47 | ||
|
|
28a3b0a6f0 | ||
|
|
ecb547223f | ||
|
|
ffe74ed299 | ||
|
|
eee6babc98 | ||
|
|
c6da760ca9 | ||
|
|
a681493371 | ||
|
|
2055d0c8ec | ||
|
|
013cb2e0c2 | ||
|
|
deca7565c5 | ||
|
|
64efa7f929 | ||
|
|
83fbd42506 | ||
|
|
9672b99271 | ||
|
|
8d8739999f | ||
|
|
31fc94aa5e | ||
|
|
133240937d | ||
|
|
916d44e3d2 | ||
|
|
025adc2912 | ||
|
|
8b38c994a9 | ||
|
|
c686ac7b01 | ||
|
|
603106a13d | ||
|
|
ee6bbe0024 | ||
|
|
3b84e27ab4 | ||
|
|
43e5f33320 | ||
|
|
d2888c83f5 | ||
|
|
15afc23a27 | ||
|
|
26d3646ff2 | ||
|
|
223eacda85 | ||
|
|
edfe869159 | ||
|
|
6cd78f6af9 | ||
|
|
2a61ec85bd | ||
|
|
7bc1a628fe | ||
|
|
b81d9b71da | ||
|
|
6797a2705a | ||
|
|
2bd9cd9adb | ||
|
|
8b3cfdaaea | ||
|
|
0fd5884a0f | ||
|
|
22639b3781 | ||
|
|
03dbe115c2 | ||
|
|
8a88774ecf | ||
|
|
b8c27ed565 | ||
|
|
886a395429 | ||
|
|
9042665ea9 | ||
|
|
41c9a9caad | ||
|
|
74ec175715 | ||
|
|
3531fd609a | ||
|
|
fefbf04ac0 | ||
|
|
3d618777ea | ||
|
|
a487dc9b58 | ||
|
|
4d7e8a808d | ||
|
|
ebfdbede31 | ||
|
|
5df6e7bdb8 | ||
|
|
1df2b30e84 | ||
|
|
213e03c998 | ||
|
|
d4cb9dad6c | ||
|
|
4c8e1d96a3 | ||
|
|
2e7d8a88f4 | ||
|
|
eafdc744b6 | ||
|
|
e925e4928f | ||
|
|
a89a5b8088 | ||
|
|
698b01da8e | ||
|
|
a64cec0be9 | ||
|
|
bf04bbde8f | ||
|
|
1db9542723 | ||
|
|
850171cdc4 | ||
|
|
4f6387c17f | ||
|
|
cee27feedd | ||
|
|
812868ddfc | ||
|
|
1783ab0625 | ||
|
|
a454666e7a | ||
|
|
c4a7b15843 | ||
|
|
fd648a5290 | ||
|
|
82da8535c5 | ||
|
|
89459bfd32 | ||
|
|
24b84068ea | ||
|
|
0d00b06af3 | ||
|
|
13c34d09b2 | ||
|
|
67539519fa | ||
|
|
29e6e748f7 | ||
|
|
2d75510d82 | ||
|
|
36226daa2e | ||
|
|
46c184cb08 | ||
|
|
4999b20b84 | ||
|
|
02b914d609 | ||
|
|
f28c62c815 | ||
|
|
0273958b0a | ||
|
|
04ce49c5b2 | ||
|
|
1cc1f90527 | ||
|
|
54f83717b2 | ||
|
|
7e854d778b | ||
|
|
828dbebcaa | ||
|
|
55286a7c49 | ||
|
|
e2dc7f9693 | ||
|
|
4ae7a5b840 | ||
|
|
0b6c7f5f7e | ||
|
|
fd16aeb4f8 | ||
|
|
bf7e328fb8 | ||
|
|
415d4ed908 | ||
|
|
06e78f2747 | ||
|
|
ccd8f840b2 | ||
|
|
0933f1c598 | ||
|
|
81f6095cf0 | ||
|
|
45d67f42cb | ||
|
|
27fa708449 | ||
|
|
e9a3a3012c | ||
|
|
64c974755d | ||
|
|
df650d11dd | ||
|
|
b5847733cd | ||
|
|
ca37ad7436 | ||
|
|
0532995dfa | ||
|
|
f223ce8b4e | ||
|
|
27316e22e8 | ||
|
|
c4d185dca9 | ||
|
|
5d839dd5f8 | ||
|
|
ed06af97ef | ||
|
|
4d7eaff65b | ||
|
|
4fc1ee85d3 | ||
|
|
843901eeeb | ||
|
|
b26b434e9f | ||
|
|
11315e63fa | ||
|
|
4358a4c122 | ||
|
|
1a014f96dd | ||
|
|
5842d0ee7d | ||
|
|
62013281b8 | ||
|
|
f1611ec8dc | ||
|
|
fd7fcd8dec | ||
|
|
f20b3740b1 | ||
|
|
4f2bc58431 | ||
|
|
99ee54259e | ||
|
|
e9fd3f43d7 | ||
|
|
a088efdb04 | ||
|
|
7fa6599f4f | ||
|
|
b979cb32a1 | ||
|
|
e15db4fb50 | ||
|
|
91bb2a9106 | ||
|
|
73e415a226 | ||
|
|
868f7e7a28 | ||
|
|
5055689a11 | ||
|
|
c81bae2fdd | ||
|
|
6f8ad3b8c3 | ||
|
|
3828616816 | ||
|
|
ea7fea6272 | ||
|
|
8358ef8a2b | ||
|
|
c4124da4f2 | ||
|
|
bc98c0dbce | ||
|
|
feb324b0d4 | ||
|
|
275109b2f8 | ||
|
|
c26ab5e46f | ||
|
|
d7f15d02f7 | ||
|
|
35d2088c96 | ||
|
|
48d4a8b263 | ||
|
|
5c3d41bf4e | ||
|
|
38bd906f2b | ||
|
|
b76680b045 | ||
|
|
68cebbe0e7 | ||
|
|
e109eba972 | ||
|
|
219b0ff8f8 | ||
|
|
a4079c6a1c | ||
|
|
2ccc5c73ea | ||
|
|
004be38048 | ||
|
|
4e1ca6e618 | ||
|
|
13389db1be | ||
|
|
f79f3adf02 | ||
|
|
f298411f76 | ||
|
|
5ac987857c | ||
|
|
f1c0be3f0b | ||
|
|
7bf8d4a7be | ||
|
|
4d43d81e5c | ||
|
|
80c216df0c | ||
|
|
a6353897cd | ||
|
|
7648fe50c1 | ||
|
|
25a9e2d217 | ||
|
|
60ff181af9 | ||
|
|
98bbe541e4 | ||
|
|
d7461f3206 | ||
|
|
c0857f2d59 | ||
|
|
b3b5cce3ae | ||
|
|
82060a5343 | ||
|
|
31a8c6800d | ||
|
|
87999d0288 | ||
|
|
543f214361 | ||
|
|
8ca5d83497 | ||
|
|
63dfc7f175 | ||
|
|
9f05e6c567 | ||
|
|
d1b19a1205 | ||
|
|
5b52c89694 | ||
|
|
86b4122323 | ||
|
|
6655e01642 | ||
|
|
8d701bb20b | ||
|
|
e2dccbca48 | ||
|
|
f086692619 | ||
|
|
411bb51cc6 | ||
|
|
553e7fbc33 | ||
|
|
621f9b425a | ||
|
|
1e33434fcc | ||
|
|
30dac898f3 | ||
|
|
3ab02f3ad7 | ||
|
|
c03a450d9b | ||
|
|
2061c71366 | ||
|
|
d6ea3a425c | ||
|
|
152d258ea0 | ||
|
|
6a9751eb0d | ||
|
|
c98ccb40e9 | ||
|
|
c5dc9295c7 | ||
|
|
f27d26f451 | ||
|
|
b4d41c3628 | ||
|
|
65b7872e6a | ||
|
|
f004788098 | ||
|
|
dd8319972c | ||
|
|
c60c6a27f6 | ||
|
|
3dfba2ba45 | ||
|
|
182dcd3455 | ||
|
|
6e65c20017 | ||
|
|
c83d88ee88 | ||
|
|
9f923325d0 | ||
|
|
9e630daf81 | ||
|
|
63ac925260 | ||
|
|
cdb45247ca | ||
|
|
7feee194f8 | ||
|
|
a72696533b | ||
|
|
729496d289 | ||
|
|
91e9bb9070 | ||
|
|
4d50915373 | ||
|
|
f188f4ffd8 | ||
|
|
db185bb303 | ||
|
|
b7adf866b5 | ||
|
|
ccccc45f11 | ||
|
|
45ef87e71b | ||
|
|
6c6fec094e | ||
|
|
2e8d4c6e49 | ||
|
|
5ad77395c0 | ||
|
|
2b2c4abb2c | ||
|
|
549360a462 | ||
|
|
8fc8d966c4 | ||
|
|
e5693d2b5b | ||
|
|
24ef919495 | ||
|
|
1f33943b23 | ||
|
|
66e982060c | ||
|
|
2677bb6c44 | ||
|
|
787370ee87 | ||
|
|
5ba25aa345 | ||
|
|
bdf48bea36 | ||
|
|
00e34a10bd | ||
|
|
f7010e5169 | ||
|
|
0182ba37cd | ||
|
|
0eb4917ded | ||
|
|
d6c543691b | ||
|
|
13eaec55dc | ||
|
|
2cd8256c4d | ||
|
|
6c1c554451 | ||
|
|
0ba5023e82 | ||
|
|
6e1ade0bb8 | ||
|
|
734769fa9b | ||
|
|
f765f90ec3 | ||
|
|
598fe2e38d | ||
|
|
5927ecd62a | ||
|
|
2937229820 | ||
|
|
3a4d312eda | ||
|
|
81d6c31875 | ||
|
|
0eae2a913a | ||
|
|
ec03d72c5d | ||
|
|
0e36912906 | ||
|
|
0c71119ee5 | ||
|
|
37e2541891 | ||
|
|
6a7fd14bfa | ||
|
|
ca1d12cf1b | ||
|
|
38315f6b1f | ||
|
|
07c8d2ee60 | ||
|
|
5575bcf60c | ||
|
|
558a8a4f79 | ||
|
|
84be9f2c3a | ||
|
|
2b9e09b00a | ||
|
|
967884c5de | ||
|
|
f4fcf93403 | ||
|
|
e1c41d0694 | ||
|
|
de4a6fe8a5 | ||
|
|
7beead5465 | ||
|
|
f6dc12a971 | ||
|
|
216d46dc81 | ||
|
|
7bda25b1c6 | ||
|
|
62201a0917 | ||
|
|
340be42797 | ||
|
|
daf9231a66 | ||
|
|
be4562da9f | ||
|
|
d2b0e4399e | ||
|
|
db10c4af91 | ||
|
|
1389b706b5 | ||
|
|
5d6e6b9f78 | ||
|
|
be3a0d84a1 | ||
|
|
5d0bbb38c3 | ||
|
|
d8068a832d | ||
|
|
b8f84994b0 | ||
|
|
2626308cd8 | ||
|
|
fea371dc0a | ||
|
|
604ed2878d | ||
|
|
e6ff8d6597 | ||
|
|
0f8fe8e246 | ||
|
|
5ba0f89aa3 | ||
|
|
20125889b3 | ||
|
|
6ec8d6caac | ||
|
|
5bf2d59241 | ||
|
|
2cf4e0a9e6 | ||
|
|
020290a162 | ||
|
|
f7452bc42d | ||
|
|
f0917a3f97 | ||
|
|
8eaec35074 | ||
|
|
549bfeac23 | ||
|
|
4cc18f4217 | ||
|
|
8b48b7e0af | ||
|
|
caa1f494ab | ||
|
|
f6484de411 | ||
|
|
45f3df8093 | ||
|
|
b5d5eaabab | ||
|
|
d3c06973a1 | ||
|
|
7976de1121 | ||
|
|
c773eedeb6 | ||
|
|
2caa5d2b0d | ||
|
|
b54666ff69 | ||
|
|
25c06a3a95 | ||
|
|
03475a5944 | ||
|
|
e3d95ecab2 | ||
|
|
5d33ad7118 | ||
|
|
76aa982181 | ||
|
|
efa17ef6cf | ||
|
|
ee3506a8e7 | ||
|
|
404818b1dc | ||
|
|
baf0b9e25a | ||
|
|
f0649d8d6a | ||
|
|
eb6f9a6354 | ||
|
|
9d9509f791 | ||
|
|
4f85ca04e4 | ||
|
|
fcfe84626f | ||
|
|
1ee8ae7104 | ||
|
|
b608a029f8 | ||
|
|
4cdbf0e872 | ||
|
|
7b574d3d3e | ||
|
|
653c6ca1af | ||
|
|
887c5808c9 | ||
|
|
a1cdf005b9 | ||
|
|
b4ac4d441f | ||
|
|
57ca62c878 | ||
|
|
028d2044e7 | ||
|
|
76ecb51e7b | ||
|
|
74120d8988 | ||
|
|
a865fe662f | ||
|
|
a86d18bc7f | ||
|
|
6fd272ac18 | ||
|
|
c5fd368a8d | ||
|
|
0911c1719e | ||
|
|
421895fa30 | ||
|
|
e9b3ec9b1d | ||
|
|
f1c0f641aa | ||
|
|
5d2192e654 | ||
|
|
652cca37ea | ||
|
|
bd45216bc9 | ||
|
|
76580c292a | ||
|
|
acacc0561f | ||
|
|
7bf0caef9f | ||
|
|
32a15dd79d | ||
|
|
521fe27185 | ||
|
|
c04eec1c37 | ||
|
|
abc01bce29 | ||
|
|
faf2618cf3 | ||
|
|
91737fadb6 | ||
|
|
895f577744 | ||
|
|
d9fa261cdc | ||
|
|
03b9087446 | ||
|
|
9e36d2d2e6 | ||
|
|
9c6c4b908b | ||
|
|
44a85664a8 | ||
|
|
9ab54ab47f | ||
|
|
6e679c8019 | ||
|
|
360e79a005 | ||
|
|
a5e1e3e70d | ||
|
|
d08c2bb525 | ||
|
|
1ee6beff6a | ||
|
|
ff829467d3 | ||
|
|
e6c52d431f | ||
|
|
e90cbd6f14 | ||
|
|
4a14bc0f29 | ||
|
|
ab8c264e6d | ||
|
|
d5b9ba7180 | ||
|
|
28f6dc153a | ||
|
|
332d2d780a | ||
|
|
85223ea080 | ||
|
|
6c80177340 | ||
|
|
40dc9dee2d | ||
|
|
57127a3671 | ||
|
|
ac425d70ef | ||
|
|
642e15805d | ||
|
|
ddf31b22ec | ||
|
|
12064d8e5d | ||
|
|
bd6a2622ad | ||
|
|
40c51a703b | ||
|
|
212009df6b | ||
|
|
bf68dc30a5 | ||
|
|
4d18ac1ae6 | ||
|
|
7cbf447320 | ||
|
|
cfddd12e82 | ||
|
|
6e4bf6a9cb | ||
|
|
b0b0183c2b | ||
|
|
5681fc1a28 | ||
|
|
a3cb527be5 | ||
|
|
c485c8742c | ||
|
|
a89b3976b2 | ||
|
|
a5d88aaacc | ||
|
|
0d1a3fe995 | ||
|
|
cbb5fbccd0 | ||
|
|
6c42f04524 | ||
|
|
1dd9b3b150 | ||
|
|
3fd3c96fa0 | ||
|
|
3588687874 | ||
|
|
571531820e | ||
|
|
4110448046 | ||
|
|
3bc7d16adc | ||
|
|
973f19fd5a | ||
|
|
d8c54063dc | ||
|
|
389159da1b | ||
|
|
e39a9f93d2 | ||
|
|
9faa924a29 | ||
|
|
34cba38c29 | ||
|
|
8ef2e1fe33 | ||
|
|
0e7f362671 | ||
|
|
de53d5eb2c | ||
|
|
8251296d7f | ||
|
|
00014d624c | ||
|
|
c06f4bf7fe | ||
|
|
bedc7a3ae5 | ||
|
|
8c3e993833 | ||
|
|
f84880fcd9 | ||
|
|
73381cf71a | ||
|
|
266e5246ea | ||
|
|
7b0ea85e2a | ||
|
|
32aaf74e95 | ||
|
|
60791f361f | ||
|
|
9128a8c5a1 | ||
|
|
dd1918dc36 | ||
|
|
7c2eb5de35 | ||
|
|
3bb458e115 | ||
|
|
7e61f0cf57 | ||
|
|
56003b5276 | ||
|
|
cd78af5fea | ||
|
|
7249ef3ee7 | ||
|
|
5d54ea58c9 | ||
|
|
4069a7e98c | ||
|
|
711258ef46 | ||
|
|
f8bff4c618 | ||
|
|
6d47d98f53 | ||
|
|
a9084b923a | ||
|
|
9f0aeacab1 | ||
|
|
66afb2a2a3 |
6
.auto-changelog
Normal file
6
.auto-changelog
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"output": "CHANGELOG.md",
|
||||
"template": "keepachangelog",
|
||||
"unreleased": true,
|
||||
"commitLimit": false
|
||||
}
|
||||
1
.gitattributes
vendored
Normal file
1
.gitattributes
vendored
Normal file
@ -0,0 +1 @@
|
||||
package-lock.json binary
|
||||
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
node_modules
|
||||
*.swp
|
||||
cov
|
||||
atest.js
|
||||
notes
|
||||
primus-proxy.js
|
||||
tes.js
|
||||
npm-debug.log
|
||||
.nyc_output
|
||||
coverage
|
||||
9
.npmignore
Normal file
9
.npmignore
Normal file
@ -0,0 +1,9 @@
|
||||
test
|
||||
examples
|
||||
doc
|
||||
benchmark
|
||||
coverage
|
||||
.nyc_output
|
||||
.travis.yml
|
||||
CHANGELOG.md
|
||||
UPGRADING.md
|
||||
12
.travis.yml
Normal file
12
.travis.yml
Normal file
@ -0,0 +1,12 @@
|
||||
sudo: false
|
||||
language: node_js
|
||||
node_js:
|
||||
- "8"
|
||||
- "10"
|
||||
- "12"
|
||||
script:
|
||||
- npm test
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
matrix:
|
||||
fast_finish: true
|
||||
1872
CHANGELOG.md
Normal file
1872
CHANGELOG.md
Normal file
File diff suppressed because it is too large
Load Diff
74
CODE_OF_CONDUCT.md
Normal file
74
CODE_OF_CONDUCT.md
Normal 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/
|
||||
4
LICENSE
4
LICENSE
@ -1,7 +1,7 @@
|
||||
|
||||
node-http-proxy
|
||||
|
||||
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires
|
||||
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.
|
||||
|
||||
630
README.md
630
README.md
@ -1,110 +1,568 @@
|
||||
# node-http-proxy - v0.2.0
|
||||
<p align="center">
|
||||
<img src="https://raw.github.com/http-party/node-http-proxy/master/doc/logo.png"/>
|
||||
</p>
|
||||
|
||||
<img src = "http://i.imgur.com/dSSUX.png"/>
|
||||
# node-http-proxy [](https://travis-ci.org/http-party/node-http-proxy) [](https://codecov.io/gh/http-party/node-http-proxy)
|
||||
|
||||
## Battle-hardened node.js http proxy
|
||||
`node-http-proxy` is an HTTP programmable proxying library that supports
|
||||
websockets. It is suitable for implementing components such as reverse
|
||||
proxies and load balancers.
|
||||
|
||||
### Features
|
||||
### 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)
|
||||
|
||||
- reverse-proxies incoming http.Server requests
|
||||
- can be used as a CommonJS module in node.js
|
||||
- uses event buffering to support application latency in proxied requests
|
||||
- minimal request overhead and latency
|
||||
- fully-tested
|
||||
- battled-hardened through production usage @ [nodejitsu.com][0]
|
||||
- written entirely in javascript
|
||||
- easy to use api
|
||||
### Installation
|
||||
|
||||
### When to use node-http-proxy
|
||||
`npm install http-proxy --save`
|
||||
|
||||
Let's suppose you were running multiple http application servers, but you only wanted to expose one machine to the internet. You could setup node-http-proxy on that one machine and then reverse-proxy the incoming http requests to locally running services which were not exposed to the outside network.
|
||||
**[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#L26-L42))
|
||||
|
||||
```javascript
|
||||
var httpProxy = require('http-proxy');
|
||||
|
||||
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 methods:
|
||||
|
||||
* web `req, res, [options]` (used for proxying regular HTTP(S) requests)
|
||||
* ws `req, socket, head, [options]` (used for proxying WS(S) requests)
|
||||
* listen `port` (a function that wraps the object in a webserver, for your convenience)
|
||||
* close `[callback]` (a function that closes the inner webserver and stops listening on given port)
|
||||
|
||||
It is then possible to proxy requests by calling these functions
|
||||
|
||||
```javascript
|
||||
http.createServer(function(req, res) {
|
||||
proxy.web(req, res, { target: 'http://mytarget.com:8080' });
|
||||
});
|
||||
```
|
||||
|
||||
Errors can be listened on either using the Event Emitter API
|
||||
|
||||
```javascript
|
||||
proxy.on('error', function(e) {
|
||||
...
|
||||
});
|
||||
```
|
||||
|
||||
or using the callback API
|
||||
|
||||
```javascript
|
||||
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 (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
|
||||
|
||||
```js
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
//
|
||||
// Create your proxy server and set the target in the options.
|
||||
//
|
||||
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000); // See (†)
|
||||
|
||||
//
|
||||
// Create your target server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
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 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
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
|
||||
//
|
||||
// Create a proxy server with custom application logic
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer({});
|
||||
|
||||
//
|
||||
// Create your custom server and just call `proxy.web()` to proxy
|
||||
// a web request to the target passed in the options
|
||||
// also you can use `proxy.ws()` to proxy a websockets request
|
||||
//
|
||||
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: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.
|
||||
|
||||
```js
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
|
||||
//
|
||||
// Create a proxy server with custom application logic
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer({});
|
||||
|
||||
// To modify the proxy connection before data is sent, you can listen
|
||||
// for the 'proxyReq' event. When the event is fired, you will receive
|
||||
// the following arguments:
|
||||
// (http.ClientRequest proxyReq, http.IncomingMessage req,
|
||||
// http.ServerResponse res, Object options). This mechanism is useful when
|
||||
// you need to modify the proxy request before the proxy connection
|
||||
// is made to the target.
|
||||
//
|
||||
proxy.on('proxyReq', function(proxyReq, req, res, options) {
|
||||
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
|
||||
});
|
||||
|
||||
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:5050'
|
||||
});
|
||||
});
|
||||
|
||||
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
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
|
||||
//
|
||||
// Create a proxy server with latency
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer();
|
||||
|
||||
//
|
||||
// Create your server that makes an operation that waits a while
|
||||
// and then proxies the request
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
// This simulates an operation that takes 500ms to execute
|
||||
setTimeout(function () {
|
||||
proxy.web(req, res, {
|
||||
target: 'http://localhost:9008'
|
||||
});
|
||||
}, 500);
|
||||
}).listen(8008);
|
||||
|
||||
//
|
||||
// Create your target server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9008);
|
||||
```
|
||||
|
||||
**[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.
|
||||
|
||||
##### HTTPS -> HTTP
|
||||
|
||||
```js
|
||||
//
|
||||
// Create the HTTPS proxy server in front of a HTTP server
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9009
|
||||
},
|
||||
ssl: {
|
||||
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
|
||||
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
|
||||
}
|
||||
}).listen(8009);
|
||||
```
|
||||
|
||||
##### HTTPS -> HTTPS
|
||||
|
||||
```js
|
||||
//
|
||||
// Create the proxy server listening on port 443
|
||||
//
|
||||
httpProxy.createServer({
|
||||
ssl: {
|
||||
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
|
||||
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
|
||||
},
|
||||
target: 'https://localhost:9010',
|
||||
secure: true // Depends on your needs, could be false.
|
||||
}).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.
|
||||
|
||||
```js
|
||||
//
|
||||
// Create a proxy server for websockets
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target: 'ws://localhost:9014',
|
||||
ws: true
|
||||
}).listen(8014);
|
||||
```
|
||||
|
||||
Also you can proxy the websocket requests just calling the `ws(req, socket, head)` method.
|
||||
|
||||
```js
|
||||
//
|
||||
// Setup our server to proxy standard HTTP requests
|
||||
//
|
||||
var proxy = new httpProxy.createProxyServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9015
|
||||
}
|
||||
});
|
||||
var proxyServer = http.createServer(function (req, res) {
|
||||
proxy.web(req, res);
|
||||
});
|
||||
|
||||
//
|
||||
// Listen to the `upgrade` event and proxy the
|
||||
// WebSocket requests as well.
|
||||
//
|
||||
proxyServer.on('upgrade', function (req, socket, head) {
|
||||
proxy.ws(req, socket, head);
|
||||
});
|
||||
|
||||
proxyServer.listen(8015);
|
||||
```
|
||||
|
||||
**[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)
|
||||
* **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
|
||||
|
||||
|
||||
### Installing npm (node package manager)
|
||||
<pre>
|
||||
curl http://npmjs.org/install.sh | sh
|
||||
</pre>
|
||||
**[Back to top](#table-of-contents)**
|
||||
|
||||
### Installing node-http-proxy
|
||||
<pre>
|
||||
npm install http-proxy
|
||||
</pre>
|
||||
### Listening for proxy events
|
||||
|
||||
### How to setup a basic proxy server
|
||||
<pre>
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
* `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`.
|
||||
|
||||
httpProxy.createServer(9000, 'localhost').listen(8000);
|
||||
```js
|
||||
var httpProxy = require('http-proxy');
|
||||
// Error example
|
||||
//
|
||||
// Http Proxy Server with bad target
|
||||
//
|
||||
var proxy = httpProxy.createServer({
|
||||
target:'http://localhost:9005'
|
||||
});
|
||||
|
||||
http.createServer(function (req, res){
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9000);
|
||||
</pre>
|
||||
proxy.listen(8005);
|
||||
|
||||
see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
|
||||
//
|
||||
// Listen for the `error` event on `proxy`.
|
||||
proxy.on('error', function (err, req, res) {
|
||||
res.writeHead(500, {
|
||||
'Content-Type': 'text/plain'
|
||||
});
|
||||
|
||||
### How to setup a proxy server with custom server logic
|
||||
<pre>
|
||||
var http = require('http'),
|
||||
httpProxy = require('http-proxy');
|
||||
res.end('Something went wrong. And we are reporting a custom error message.');
|
||||
});
|
||||
|
||||
// create a proxy server with custom application logic
|
||||
httpProxy.createServer(function (req, res, proxyRequest) {
|
||||
// Put your custom server logic here
|
||||
proxyRequest(9000, 'localhost');
|
||||
}).listen(8000);
|
||||
//
|
||||
// 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));
|
||||
});
|
||||
|
||||
http.createServer(function (req, res){
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9000);
|
||||
|
||||
</pre>
|
||||
//
|
||||
// Listen for the `open` event on `proxy`.
|
||||
//
|
||||
proxy.on('open', function (proxySocket) {
|
||||
// listen for messages coming FROM the target here
|
||||
proxySocket.on('data', hybiParseAndLogMessage);
|
||||
});
|
||||
|
||||
### How to proxy requests with latent operations (IO, etc.)
|
||||
//
|
||||
// Listen for the `close` event on `proxy`.
|
||||
//
|
||||
proxy.on('close', function (res, socket, head) {
|
||||
// view disconnected websocket connections
|
||||
console.log('Client disconnected');
|
||||
});
|
||||
```
|
||||
|
||||
node-http-proxy supports event buffering, that means if an event (like 'data', or 'end') is raised by the incoming request before you have a chance to perform your custom server logic, those events will be captured and re-raised when you later proxy the request. Here's a simple example:
|
||||
**[Back to top](#table-of-contents)**
|
||||
|
||||
<pre>
|
||||
httpProxy.createServer(function (req, res, proxyRequest) {
|
||||
setTimeout(function () {
|
||||
proxyRequest(port, server);
|
||||
}, latency);
|
||||
}).listen(8081);
|
||||
</pre>
|
||||
### Shutdown
|
||||
|
||||
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
|
||||
* When testing or running server within another program it may be necessary to close the proxy.
|
||||
* This will stop the proxy from accepting new connections.
|
||||
|
||||
If you have a suggestion for a feature currently not supported, feel free to open a [support issue](http://github.com/nodejitsu/node-http-proxy/issues). node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.
|
||||
```js
|
||||
var proxy = new httpProxy.createProxyServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 1337
|
||||
}
|
||||
});
|
||||
|
||||
proxy.close();
|
||||
```
|
||||
|
||||
**[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 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)**
|
||||
|
||||
<br/>
|
||||
### License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers & Marak Squires
|
||||
|
||||
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.
|
||||
|
||||
[0]:http://nodejitsu.com "nodejitsu.com"
|
||||
>The MIT License (MIT)
|
||||
>
|
||||
>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
|
||||
>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.
|
||||
|
||||
97
UPGRADING.md
Normal file
97
UPGRADING.md
Normal file
@ -0,0 +1,97 @@
|
||||
Looking to upgrade from `http-proxy@0.x.x` to `http-proxy@1.0`? You've come to the right place!
|
||||
`http-proxy@1.0` is a from-scratch implementation of `http-proxy` and, as such
|
||||
brings some breaking changes to APIs.
|
||||
|
||||
## Server creation
|
||||
|
||||
Available through `.createServer()` or `.createProxyServer()`.
|
||||
|
||||
```javascript
|
||||
httpProxy.createServer({
|
||||
target:'http://localhost:9003'
|
||||
}).listen(8003);
|
||||
```
|
||||
|
||||
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/http-party/node-http-proxy/tree/caronte/examples/http)
|
||||
|
||||
```javascript
|
||||
//
|
||||
// Create a HTTP Proxy server with a HTTPS target
|
||||
//
|
||||
httpProxy.createProxyServer({
|
||||
target: 'https://google.com',
|
||||
agent : https.globalAgent,
|
||||
headers: {
|
||||
host: 'google.com'
|
||||
}
|
||||
}).listen(8011);
|
||||
|
||||
```
|
||||
|
||||
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({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9015
|
||||
}
|
||||
});
|
||||
var proxyServer = http.createServer(function (req, res) {
|
||||
proxy.web(req, res);
|
||||
});
|
||||
|
||||
//
|
||||
// Listen to the `upgrade` event and proxy the
|
||||
// WebSocket requests as well.
|
||||
//
|
||||
proxyServer.on('upgrade', function (req, socket, head) {
|
||||
proxy.ws(req, socket, head);
|
||||
});
|
||||
```
|
||||
|
||||
## Error Handling
|
||||
|
||||
It is possible to listen globally on the `error` event on the server. In alternative, a
|
||||
callback passed to `.web()` or `.ws()` as last parameter is also accepted.
|
||||
|
||||
```javascript
|
||||
var proxy = httpProxy.createServer({
|
||||
target:'http://localhost:9005'
|
||||
});
|
||||
|
||||
//
|
||||
// Tell the proxy to listen on port 8000
|
||||
//
|
||||
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.');
|
||||
});
|
||||
```
|
||||
|
||||
## Dropped
|
||||
|
||||
Since the API was rewritten to be extremely flexible we decided to drop some features
|
||||
which were in the core and delegate them to eventual "userland" modules.
|
||||
|
||||
- Middleware API
|
||||
- ProxyTable API
|
||||
|
||||
### 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/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.
|
||||
37
benchmark/README.md
Normal file
37
benchmark/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# Benchmarking `node-http-proxy`
|
||||
|
||||
The long-term goal of these scripts and documentation is to provide a consistent and well understood benchmarking process for `node-http-proxy` so that performance does not degrade over time. They were initially created to compare the performance of `v0.10.3` and `v1.0.0` (which was a significant rewrite).
|
||||
|
||||
## Pre-requisites
|
||||
|
||||
All benchmarking shall be done with [wrk](https://github.com/wg/wrk) which _is the same tool used for performance testing by the node.js core team._ **Make sure you have `wrk` installed before continuing**.
|
||||
|
||||
```
|
||||
$ wrk
|
||||
Usage: wrk <options> <url>
|
||||
Options:
|
||||
-c, --connections <N> Connections to keep open
|
||||
-d, --duration <T> Duration of test
|
||||
-t, --threads <N> Number of threads to use
|
||||
|
||||
-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 (1k, 1M, 1G)
|
||||
Time arguments may include a time unit (2s, 2m, 2h)
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
1. [Simple HTTP benchmark](#simple-http)
|
||||
|
||||
### Simple HTTP
|
||||
|
||||
_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 -d5m -t 2 http://127.0.0.1:8000`
|
||||
3
benchmark/scripts/hello.js
Normal file
3
benchmark/scripts/hello.js
Normal file
@ -0,0 +1,3 @@
|
||||
require('http').createServer(function(req, res) {
|
||||
res.end('Hello world!');
|
||||
}).listen(9000);
|
||||
6
benchmark/scripts/proxy.js
Normal file
6
benchmark/scripts/proxy.js
Normal file
@ -0,0 +1,6 @@
|
||||
var http = require('http'),
|
||||
httpProxy = require('../../');
|
||||
//
|
||||
// Create your proxy server
|
||||
//
|
||||
httpProxy.createProxyServer({ target: 'http://localhost:9000' }).listen(8000);
|
||||
88
benchmark/scripts/websockets-throughput.js
Normal file
88
benchmark/scripts/websockets-throughput.js
Normal file
@ -0,0 +1,88 @@
|
||||
var crypto = require('crypto'),
|
||||
WebSocket = require('ws'),
|
||||
async = require('async'),
|
||||
httpProxy = require('../../');
|
||||
|
||||
var SERVER_PORT = 8415,
|
||||
PROXY_PORT = 8514;
|
||||
|
||||
var testSets = [
|
||||
{
|
||||
size: 1024 * 1024, // 1 MB
|
||||
count: 128 // 128 MB
|
||||
},
|
||||
{
|
||||
size: 1024, // 1 KB,
|
||||
count: 1024 // 1 MB
|
||||
},
|
||||
{
|
||||
size: 128, // 128 B
|
||||
count: 1024 * 8 // 1 MB
|
||||
}
|
||||
];
|
||||
|
||||
testSets.forEach(function (set) {
|
||||
set.buffer = new Buffer(crypto.randomBytes(set.size));
|
||||
|
||||
set.buffers = [];
|
||||
for (var i = 0; i < set.count; i++) {
|
||||
set.buffers.push(set.buffer);
|
||||
}
|
||||
});
|
||||
|
||||
function runSet(set, callback) {
|
||||
function runAgainst(port, callback) {
|
||||
function send(sock) {
|
||||
sock.send(set.buffers[got++]);
|
||||
if (got === set.count) {
|
||||
t = new Date() - t;
|
||||
|
||||
server.close();
|
||||
proxy.close();
|
||||
|
||||
callback(null, t);
|
||||
}
|
||||
}
|
||||
|
||||
var server = new WebSocket.Server({ port: SERVER_PORT }),
|
||||
proxy = httpProxy.createServer(SERVER_PORT, 'localhost').listen(PROXY_PORT),
|
||||
client = new WebSocket('ws://localhost:' + port),
|
||||
got = 0,
|
||||
t = new Date();
|
||||
|
||||
server.on('connection', function (ws) {
|
||||
send(ws);
|
||||
|
||||
ws.on('message', function (msg) {
|
||||
send(ws);
|
||||
});
|
||||
});
|
||||
|
||||
client.on('message', function () {
|
||||
send(client);
|
||||
});
|
||||
}
|
||||
|
||||
async.series({
|
||||
server: async.apply(runAgainst, SERVER_PORT),
|
||||
proxy: async.apply(runAgainst, PROXY_PORT)
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
var mb = (set.size * set.count) / (1024 * 1024);
|
||||
console.log(set.size / (1024) + ' KB * ' + set.count + ' (' + mb + ' MB)');
|
||||
|
||||
Object.keys(results).forEach(function (key) {
|
||||
var t = results[key],
|
||||
throughput = mb / (t / 1000);
|
||||
|
||||
console.log(' ' + key + ' took ' + t + ' ms (' + throughput + ' MB/s)');
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
async.forEachLimit(testSets, 1, runSet);
|
||||
@ -1,3 +0,0 @@
|
||||
Benchmarks are being run on a development laptop using apache ab (everything running locally, on one machine)
|
||||
|
||||
These benchmarks should not be considered very accurate, or indicative of actual results. There are numerous factors that could cause these numbers to be inaccurate. These benchmarks should only be considered a very basic baseline to determine changes in performance as we iterate on this project.
|
||||
@ -1,56 +0,0 @@
|
||||
[Marak@Maraks-MacBook-Pro ~/dev/zalgo.js]$ ab -c 50 -n 1000 -k http://127.0.0.1:9000/
|
||||
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
|
||||
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
|
||||
Licensed to The Apache Software Foundation, http://www.apache.org/
|
||||
|
||||
Benchmarking 127.0.0.1 (be patient)
|
||||
Completed 100 requests
|
||||
Completed 200 requests
|
||||
Completed 300 requests
|
||||
Completed 400 requests
|
||||
Completed 500 requests
|
||||
Completed 600 requests
|
||||
Completed 700 requests
|
||||
Completed 800 requests
|
||||
Completed 900 requests
|
||||
Completed 1000 requests
|
||||
Finished 1000 requests
|
||||
|
||||
|
||||
Server Software:
|
||||
Server Hostname: 127.0.0.1
|
||||
Server Port: 9000
|
||||
|
||||
Document Path: /
|
||||
Document Length: 149 bytes
|
||||
|
||||
Concurrency Level: 50
|
||||
Time taken for tests: 0.178 seconds
|
||||
Complete requests: 1000
|
||||
Failed requests: 0
|
||||
Write errors: 0
|
||||
Keep-Alive requests: 0
|
||||
Total transferred: 213213 bytes
|
||||
HTML transferred: 149149 bytes
|
||||
Requests per second: 5627.84 [#/sec] (mean)
|
||||
Time per request: 8.884 [ms] (mean)
|
||||
Time per request: 0.178 [ms] (mean, across all concurrent requests)
|
||||
Transfer rate: 1171.81 [Kbytes/sec] received
|
||||
|
||||
Connection Times (ms)
|
||||
min mean[+/-sd] median max
|
||||
Connect: 0 1 0.9 1 5
|
||||
Processing: 3 8 2.6 7 17
|
||||
Waiting: 1 7 2.4 7 17
|
||||
Total: 5 9 2.4 8 17
|
||||
|
||||
Percentage of the requests served within a certain time (ms)
|
||||
50% 8
|
||||
66% 9
|
||||
75% 9
|
||||
80% 10
|
||||
90% 13
|
||||
95% 14
|
||||
98% 15
|
||||
99% 16
|
||||
100% 17 (longest request)
|
||||
@ -1,56 +0,0 @@
|
||||
[Marak@Maraks-MacBook-Pro ~]$ ab -c 5 -n 1000 http://127.0.0.1:8000/
|
||||
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
|
||||
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
|
||||
Licensed to The Apache Software Foundation, http://www.apache.org/
|
||||
|
||||
Benchmarking 127.0.0.1 (be patient)
|
||||
Completed 100 requests
|
||||
Completed 200 requests
|
||||
Completed 300 requests
|
||||
Completed 400 requests
|
||||
Completed 500 requests
|
||||
Completed 600 requests
|
||||
Completed 700 requests
|
||||
Completed 800 requests
|
||||
Completed 900 requests
|
||||
Completed 1000 requests
|
||||
Finished 1000 requests
|
||||
|
||||
|
||||
Server Software:
|
||||
Server Hostname: 127.0.0.1
|
||||
Server Port: 8000
|
||||
|
||||
Document Path: /
|
||||
Document Length: 155 bytes
|
||||
|
||||
Concurrency Level: 5
|
||||
Time taken for tests: 0.602 seconds
|
||||
Complete requests: 1000
|
||||
Failed requests: 5
|
||||
(Connect: 0, Receive: 0, Length: 5, Exceptions: 0)
|
||||
Write errors: 0
|
||||
Total transferred: 247030 bytes
|
||||
HTML transferred: 155030 bytes
|
||||
Requests per second: 1660.64 [#/sec] (mean)
|
||||
Time per request: 3.011 [ms] (mean)
|
||||
Time per request: 0.602 [ms] (mean, across all concurrent requests)
|
||||
Transfer rate: 400.61 [Kbytes/sec] received
|
||||
|
||||
Connection Times (ms)
|
||||
min mean[+/-sd] median max
|
||||
Connect: 0 0 0.1 0 1
|
||||
Processing: 2 3 0.8 3 10
|
||||
Waiting: 2 3 0.8 3 10
|
||||
Total: 2 3 0.8 3 10
|
||||
|
||||
Percentage of the requests served within a certain time (ms)
|
||||
50% 3
|
||||
66% 3
|
||||
75% 3
|
||||
80% 3
|
||||
90% 3
|
||||
95% 4
|
||||
98% 5
|
||||
99% 9
|
||||
100% 10 (longest request)
|
||||
10
codecov.yml
Normal file
10
codecov.yml
Normal file
@ -0,0 +1,10 @@
|
||||
coverage:
|
||||
parsers:
|
||||
javascript:
|
||||
enable_partials: yes
|
||||
status:
|
||||
project:
|
||||
default:
|
||||
target: "70%"
|
||||
patch:
|
||||
enabled: false
|
||||
61
demo.js
61
demo.js
@ -1,61 +0,0 @@
|
||||
/*
|
||||
demo.js: http proxy for node.js
|
||||
|
||||
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
|
||||
|
||||
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 sys = require('sys'),
|
||||
colors = require('colors')
|
||||
http = require('http'),
|
||||
httpProxy = require('./lib/node-http-proxy');
|
||||
|
||||
// ascii art from http://github.com/marak/asciimo
|
||||
var welcome = '\
|
||||
# # ##### ##### ##### ##### ##### #### # # # # \n\
|
||||
# # # # # # # # # # # # # # # # \n\
|
||||
###### # # # # ##### # # # # # # ## # \n\
|
||||
# # # # ##### ##### ##### # # ## # \n\
|
||||
# # # # # # # # # # # # # \n\
|
||||
# # # # # # # # #### # # # \n';
|
||||
sys.puts(welcome.rainbow.bold);
|
||||
|
||||
|
||||
/****** basic http proxy server ******/
|
||||
httpProxy.createServer(9000, 'localhost').listen(8000);
|
||||
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
||||
|
||||
/****** http proxy server with latency******/
|
||||
httpProxy.createServer(function (req, res, proxyRequest){
|
||||
setTimeout(function(){
|
||||
proxyRequest(9000, 'localhost', req, res);
|
||||
}, 2000)
|
||||
}).listen(8001);
|
||||
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );
|
||||
|
||||
/****** regular http server ******/
|
||||
http.createServer(function (req, res){
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9000);
|
||||
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
|
||||
BIN
doc/logo.png
Normal file
BIN
doc/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 46 KiB |
83
examples/balancer/simple-balancer-with-websockets.js
Normal file
83
examples/balancer/simple-balancer-with-websockets.js
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
simple-balancer.js: Example of a simple round robin balancer for websockets
|
||||
|
||||
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 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 = [
|
||||
{
|
||||
host: 'ws1.0.0.0',
|
||||
port: 80
|
||||
},
|
||||
{
|
||||
host: 'ws2.0.0.0',
|
||||
port: 80
|
||||
}
|
||||
];
|
||||
|
||||
//
|
||||
// Create a HttpProxy object for each target
|
||||
//
|
||||
|
||||
var proxies = addresses.map(function (target) {
|
||||
return new httpProxy.createProxyServer({
|
||||
target: target
|
||||
});
|
||||
});
|
||||
|
||||
//
|
||||
// Get the proxy at the front of the array, put it at the end and return it
|
||||
// If you want a fancier balancer, put your code here
|
||||
//
|
||||
|
||||
function nextProxy() {
|
||||
var proxy = proxies.shift();
|
||||
proxies.push(proxy);
|
||||
return proxy;
|
||||
}
|
||||
|
||||
//
|
||||
// Get the 'next' proxy and send the http request
|
||||
//
|
||||
|
||||
var server = http.createServer(function (req, res) {
|
||||
nextProxy().web(req, res);
|
||||
});
|
||||
|
||||
//
|
||||
// Get the 'next' proxy and send the upgrade request
|
||||
//
|
||||
|
||||
server.on('upgrade', function (req, socket, head) {
|
||||
nextProxy().ws(req, socket, head);
|
||||
});
|
||||
|
||||
server.listen(8001);
|
||||
64
examples/balancer/simple-balancer.js
Normal file
64
examples/balancer/simple-balancer.js
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
simple-balancer.js: Example of a simple round robin balancer
|
||||
|
||||
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 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 = [
|
||||
{
|
||||
host: 'ws1.0.0.0',
|
||||
port: 80
|
||||
},
|
||||
{
|
||||
host: 'ws2.0.0.0',
|
||||
port: 80
|
||||
}
|
||||
];
|
||||
var proxy = httpProxy.createServer();
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
//
|
||||
// On each request, get the first location from the list...
|
||||
//
|
||||
var target = { target: addresses.shift() };
|
||||
|
||||
//
|
||||
// ...then proxy to the server whose 'turn' it is...
|
||||
//
|
||||
console.log('balancing request to: ', target);
|
||||
proxy.web(req, res, target);
|
||||
|
||||
//
|
||||
// ...and then the server you just used becomes the last item in the list.
|
||||
//
|
||||
addresses.push(target.target);
|
||||
}).listen(8021);
|
||||
|
||||
// Rinse; repeat; enjoy.
|
||||
64
examples/helpers/store.js
Normal file
64
examples/helpers/store.js
Normal file
@ -0,0 +1,64 @@
|
||||
|
||||
//
|
||||
// just to make these example a little bit interesting,
|
||||
// make a little key value store with an http interface
|
||||
// (see couchdb for a grown-up version of this)
|
||||
//
|
||||
// API:
|
||||
// GET /
|
||||
// retrive list of keys
|
||||
//
|
||||
// GET /[url]
|
||||
// retrive object stored at [url]
|
||||
// will respond with 404 if there is nothing stored at [url]
|
||||
//
|
||||
// POST /[url]
|
||||
//
|
||||
// JSON.parse the body and store it under [url]
|
||||
// will respond 400 (bad request) if body is not valid json.
|
||||
//
|
||||
// TODO: cached map-reduce views and auto-magic sharding.
|
||||
//
|
||||
var Store = module.exports = function Store () {
|
||||
this.store = {};
|
||||
};
|
||||
|
||||
Store.prototype = {
|
||||
get: function (key) {
|
||||
return this.store[key]
|
||||
},
|
||||
set: function (key, value) {
|
||||
return this.store[key] = value
|
||||
},
|
||||
handler:function () {
|
||||
var store = this
|
||||
return function (req, res) {
|
||||
function send (obj, status) {
|
||||
res.writeHead(200 || status,{'Content-Type': 'application/json'})
|
||||
res.write(JSON.stringify(obj) + '\n')
|
||||
res.end()
|
||||
}
|
||||
var url = req.url.split('?').shift()
|
||||
if (url === '/') {
|
||||
console.log('get index')
|
||||
return send(Object.keys(store.store))
|
||||
} else if (req.method == 'GET') {
|
||||
var obj = store.get (url)
|
||||
send(obj || {error: 'not_found', url: url}, obj ? 200 : 404)
|
||||
} else {
|
||||
//post: buffer body, and parse.
|
||||
var body = '', obj
|
||||
req.on('data', function (c) { body += c})
|
||||
req.on('end', function (c) {
|
||||
try {
|
||||
obj = JSON.parse(body)
|
||||
} catch (err) {
|
||||
return send (err, 400)
|
||||
}
|
||||
store.set(url, obj)
|
||||
send({ok: true})
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
60
examples/http/basic-proxy.js
Normal file
60
examples/http/basic-proxy.js
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
basic-proxy.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');
|
||||
|
||||
var welcome = [
|
||||
'# # ##### ##### ##### ##### ##### #### # # # #',
|
||||
'# # # # # # # # # # # # # # # # ',
|
||||
'###### # # # # ##### # # # # # # ## # ',
|
||||
'# # # # ##### ##### ##### # # ## # ',
|
||||
'# # # # # # # # # # # # # ',
|
||||
'# # # # # # # # #### # # # '
|
||||
].join('\n');
|
||||
|
||||
util.puts(welcome.rainbow.bold);
|
||||
|
||||
//
|
||||
// Basic Http Proxy Server
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target:'http://localhost:9003'
|
||||
}).listen(8003);
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).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);
|
||||
68
examples/http/concurrent-proxy.js
Normal file
68
examples/http/concurrent-proxy.js
Normal file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
concurrent-proxy.js: check levelof concurrency through proxy.
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Basic Http Proxy Server
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target:'http://localhost:9004'
|
||||
}).listen(8004);
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
// to check apparent problems with concurrent connections
|
||||
// make a server which only responds when there is a given nubmer on connections
|
||||
//
|
||||
|
||||
|
||||
var connections = [],
|
||||
go;
|
||||
|
||||
http.createServer(function (req, res) {
|
||||
connections.push(function () {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
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) {
|
||||
connections.shift()();
|
||||
}
|
||||
}
|
||||
}).listen(9004);
|
||||
|
||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8004'.yellow);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9004 '.yellow);
|
||||
55
examples/http/custom-proxy-error.js
Normal file
55
examples/http/custom-proxy-error.js
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
custom-proxy-error.js: Example of using the custom `proxyError` event.
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Http Proxy Server with bad target
|
||||
//
|
||||
var proxy = httpProxy.createServer({
|
||||
target:'http://localhost:9005'
|
||||
});
|
||||
|
||||
//
|
||||
// Tell the proxy to listen on port 8000
|
||||
//
|
||||
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.');
|
||||
});
|
||||
|
||||
|
||||
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8005 '.yellow + 'with custom error message'.magenta.underline);
|
||||
63
examples/http/error-handling.js
Normal file
63
examples/http/error-handling.js
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
error-handling.js: Example of handle erros for HTTP and WebSockets
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// HTTP Proxy Server
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer({target:'http://localhost:9000', ws:true});
|
||||
|
||||
//
|
||||
// Example of error handling
|
||||
//
|
||||
function requestHandler(req, res) {
|
||||
// Pass a callback to the web proxy method
|
||||
// and catch the error there.
|
||||
proxy.web(req, res, function (err) {
|
||||
// Now you can get the err
|
||||
// and handle it by your self
|
||||
// if (err) throw err;
|
||||
res.writeHead(502);
|
||||
res.end("There was an error proxying your request");
|
||||
});
|
||||
|
||||
// In a websocket request case
|
||||
req.on('upgrade', function (req, socket, head) {
|
||||
proxy.ws(req, socket, head, function (err) {
|
||||
// Now you can get the err
|
||||
// and handle it by your self
|
||||
// if (err) throw err;
|
||||
socket.close();
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
http.createServer(requestHandler).listen(8000);
|
||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
|
||||
67
examples/http/forward-and-target-proxy.js
Normal file
67
examples/http/forward-and-target-proxy.js
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
forward-and-target-proxy.js: Example of proxying over HTTP with additional forward proxy
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Setup proxy server with forwarding
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target: {
|
||||
port: 9006,
|
||||
host: 'localhost'
|
||||
},
|
||||
forward: {
|
||||
port: 9007,
|
||||
host: 'localhost'
|
||||
}
|
||||
}).listen(8006);
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9006);
|
||||
|
||||
//
|
||||
// Target Http Forwarding Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
util.puts('Receiving forward for: ' + req.url);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9007);
|
||||
|
||||
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8006 '.yellow + 'with forward proxy'.magenta.underline);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9006 '.yellow);
|
||||
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9007 '.yellow);
|
||||
53
examples/http/forward-proxy.js
Normal file
53
examples/http/forward-proxy.js
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
forward-proxy.js: Example of proxying over HTTP with additional forward proxy
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Setup proxy server with forwarding
|
||||
//
|
||||
httpProxy.createServer({
|
||||
forward: {
|
||||
port: 9019,
|
||||
host: 'localhost'
|
||||
}
|
||||
}).listen(8019);
|
||||
|
||||
//
|
||||
// Target Http Forwarding Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
util.puts('Receiving forward for: ' + req.url);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully forwarded to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9019);
|
||||
|
||||
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8019 '.yellow + 'with forward proxy'.magenta.underline);
|
||||
util.puts('http forward server '.blue + 'started '.green.bold + 'on port '.blue + '9019 '.yellow);
|
||||
54
examples/http/latent-proxy.js
Normal file
54
examples/http/latent-proxy.js
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
latent-proxy.js: Example of proxying over HTTP with latency
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Http Proxy Server with Latency
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer();
|
||||
http.createServer(function (req, res) {
|
||||
setTimeout(function () {
|
||||
proxy.web(req, res, {
|
||||
target: 'http://localhost:9008'
|
||||
});
|
||||
}, 500);
|
||||
}).listen(8008);
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9008);
|
||||
|
||||
util.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8008 '.yellow + 'with latency'.magenta.underline);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9008 '.yellow);
|
||||
26
examples/http/ntlm-authentication.js
Normal file
26
examples/http/ntlm-authentication.js
Normal 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);
|
||||
46
examples/http/proxy-http-to-https.js
Normal file
46
examples/http/proxy-http-to-https.js
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
proxy-http-to-https.js: Basic example of proxying over HTTP to a target HTTPS server
|
||||
|
||||
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 https = require('https'),
|
||||
http = require('http'),
|
||||
util = require('util'),
|
||||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
//
|
||||
// Create a HTTP Proxy server with a HTTPS target
|
||||
//
|
||||
httpProxy.createProxyServer({
|
||||
target: 'https://google.com',
|
||||
agent : https.globalAgent,
|
||||
headers: {
|
||||
host: 'google.com'
|
||||
}
|
||||
}).listen(8011);
|
||||
|
||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8011'.yellow);
|
||||
60
examples/http/proxy-https-to-http.js
Normal file
60
examples/http/proxy-https-to-http.js
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
proxy-https-to-http.js: Basic example of proxying over HTTPS to a target HTTP server
|
||||
|
||||
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 https = require('https'),
|
||||
http = require('http'),
|
||||
util = require('util'),
|
||||
path = require('path'),
|
||||
fs = require('fs'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy'),
|
||||
fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures');
|
||||
|
||||
//
|
||||
// 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();
|
||||
}).listen(9009);
|
||||
|
||||
//
|
||||
// Create the HTTPS proxy server listening on port 8000
|
||||
//
|
||||
httpProxy.createServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9009
|
||||
},
|
||||
ssl: {
|
||||
key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
|
||||
cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
|
||||
}
|
||||
}).listen(8009);
|
||||
|
||||
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8009'.yellow);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9009 '.yellow);
|
||||
59
examples/http/proxy-https-to-https.js
Normal file
59
examples/http/proxy-https-to-https.js
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
proxy-https-to-https.js: Basic example of proxying over HTTPS to a target HTTPS server
|
||||
|
||||
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 https = require('https'),
|
||||
http = require('http'),
|
||||
util = require('util'),
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy'),
|
||||
fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures'),
|
||||
httpsOpts = {
|
||||
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
|
||||
//
|
||||
https.createServer(httpsOpts, function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('hello https\n');
|
||||
res.end();
|
||||
}).listen(9010);
|
||||
|
||||
//
|
||||
// Create the proxy server listening on port 8010
|
||||
//
|
||||
httpProxy.createServer({
|
||||
ssl: httpsOpts,
|
||||
target: 'https://localhost:9010',
|
||||
secure: false
|
||||
}).listen(8010);
|
||||
|
||||
util.puts('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8010'.yellow);
|
||||
util.puts('https server '.blue + 'started '.green.bold + 'on port '.blue + '9010 '.yellow);
|
||||
55
examples/http/reverse-proxy.js
Normal file
55
examples/http/reverse-proxy.js
Normal 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
67
examples/http/sse.js
Normal 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);
|
||||
54
examples/http/standalone-proxy.js
Normal file
54
examples/http/standalone-proxy.js
Normal file
@ -0,0 +1,54 @@
|
||||
/*
|
||||
standalone-proxy.js: Example of proxying over HTTP with a standalone HTTP server.
|
||||
|
||||
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');
|
||||
|
||||
//
|
||||
// Http Server with proxyRequest Handler and Latency
|
||||
//
|
||||
var proxy = new httpProxy.createProxyServer();
|
||||
http.createServer(function (req, res) {
|
||||
setTimeout(function () {
|
||||
proxy.web(req, res, {
|
||||
target: 'http://localhost:9002'
|
||||
});
|
||||
}, 200);
|
||||
}).listen(8002);
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).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 + '9002 '.yellow);
|
||||
108
examples/middleware/bodyDecoder-middleware.js
Normal file
108
examples/middleware/bodyDecoder-middleware.js
Normal file
@ -0,0 +1,108 @@
|
||||
/*
|
||||
bodyDecoder-middleware.js: Basic example of `connect.bodyParser()` middleware in node-http-proxy
|
||||
|
||||
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 http = require('http'),
|
||||
connect = require('connect'),
|
||||
request = require('request'),
|
||||
colors = require('colors'),
|
||||
util = require('util'),
|
||||
queryString = require('querystring'),
|
||||
bodyParser = require('body-parser'),
|
||||
httpProxy = require('../../lib/http-proxy'),
|
||||
proxy = httpProxy.createProxyServer({});
|
||||
|
||||
|
||||
//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 parser
|
||||
.use(bodyParser.urlencoded())//urlencoded parser
|
||||
.use(function(req, res){
|
||||
// modify body here,
|
||||
// eg: req.body = {a: 1}.
|
||||
console.log('proxy body:',req.body)
|
||||
proxy.web(req, res, {
|
||||
target: 'http://127.0.0.1:9013'
|
||||
})
|
||||
});
|
||||
|
||||
http.createServer(app).listen(8013, function(){
|
||||
console.log('proxy linsten 8013');
|
||||
});
|
||||
|
||||
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
var app1 = connect()
|
||||
.use(bodyParser.json())
|
||||
.use(function(req, res){
|
||||
console.log('app1:',req.body)
|
||||
res.end('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
});
|
||||
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 from json request"}
|
||||
},function(err, res,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)
|
||||
})
|
||||
});
|
||||
65
examples/middleware/gzip-middleware.js
Normal file
65
examples/middleware/gzip-middleware.js
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
gzip-middleware.js: Basic example of `connect-gzip` middleware in node-http-proxy
|
||||
|
||||
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'),
|
||||
connect = require('connect'),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
//
|
||||
// Basic Connect App
|
||||
//
|
||||
connect.createServer(
|
||||
connect.compress({
|
||||
// Pass to connect.compress() the options
|
||||
// that you need, just for show the example
|
||||
// we use threshold to 1
|
||||
threshold: 1
|
||||
}),
|
||||
function (req, res) {
|
||||
proxy.web(req, res);
|
||||
}
|
||||
).listen(8012);
|
||||
|
||||
//
|
||||
// Basic Http Proxy Server
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://localhost:9012'
|
||||
});
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
|
||||
res.end();
|
||||
}).listen(9012);
|
||||
|
||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8012'.yellow);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9012 '.yellow);
|
||||
69
examples/middleware/modifyResponse-middleware.js
Normal file
69
examples/middleware/modifyResponse-middleware.js
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
modifyBody-middleware.js: Example of middleware which modifies response
|
||||
|
||||
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'),
|
||||
connect = require('connect'),
|
||||
app = connect(),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
//
|
||||
// Basic Connect App
|
||||
//
|
||||
app.use(function (req, res, next) {
|
||||
var _write = res.write;
|
||||
|
||||
res.write = function (data) {
|
||||
_write.call(res, data.toString().replace("Ruby", "http-party"));
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(function (req, res) {
|
||||
proxy.web(req, res)
|
||||
});
|
||||
|
||||
http.createServer(app).listen(8013);
|
||||
|
||||
//
|
||||
// Basic Http Proxy Server
|
||||
//
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://localhost:9013'
|
||||
});
|
||||
|
||||
//
|
||||
// Target Http Server
|
||||
//
|
||||
http.createServer(function (req, res) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Hello, I love Ruby\n');
|
||||
}).listen(9013);
|
||||
|
||||
util.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8013'.yellow);
|
||||
util.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9013 '.yellow);
|
||||
|
||||
14
examples/package.json
Normal file
14
examples/package.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "http-proxy-examples",
|
||||
"description": "packages required to run the examples",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"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",
|
||||
"sse": "0.0.8"
|
||||
}
|
||||
}
|
||||
91
examples/websocket/latent-websocket-proxy.js
Normal file
91
examples/websocket/latent-websocket-proxy.js
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
standalone-websocket-proxy.js: Example of proxying websockets over HTTP with a standalone HTTP server.
|
||||
|
||||
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'),
|
||||
http = require('http'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
try {
|
||||
var io = require('socket.io'),
|
||||
client = require('socket.io-client');
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('Socket.io is required for this example:');
|
||||
console.error('npm ' + 'install'.green);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
// Create the target HTTP server and setup
|
||||
// socket.io on it.
|
||||
//
|
||||
var server = io.listen(9016);
|
||||
server.sockets.on('connection', function (client) {
|
||||
util.debug('Got websocket connection');
|
||||
|
||||
client.on('message', function (msg) {
|
||||
util.debug('Got message from client: ' + msg);
|
||||
});
|
||||
|
||||
client.send('from server');
|
||||
});
|
||||
|
||||
//
|
||||
// Setup our server to proxy standard HTTP requests
|
||||
//
|
||||
var proxy = new httpProxy.createProxyServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9016
|
||||
}
|
||||
});
|
||||
|
||||
var proxyServer = http.createServer(function (req, res) {
|
||||
proxy.web(req, res);
|
||||
});
|
||||
|
||||
//
|
||||
// Listen to the `upgrade` event and proxy the
|
||||
// WebSocket requests as well.
|
||||
//
|
||||
proxyServer.on('upgrade', function (req, socket, head) {
|
||||
setTimeout(function () {
|
||||
proxy.ws(req, socket, head);
|
||||
}, 1000);
|
||||
});
|
||||
|
||||
proxyServer.listen(8016);
|
||||
|
||||
//
|
||||
// Setup the socket.io client against our proxy
|
||||
//
|
||||
var ws = client.connect('ws://localhost:8016');
|
||||
|
||||
ws.on('message', function (msg) {
|
||||
util.debug('Got message: ' + msg);
|
||||
ws.send('I am the client');
|
||||
});
|
||||
88
examples/websocket/standalone-websocket-proxy.js
Normal file
88
examples/websocket/standalone-websocket-proxy.js
Normal file
@ -0,0 +1,88 @@
|
||||
/*
|
||||
standalone-websocket-proxy.js: Example of proxying websockets over HTTP with a standalone HTTP server.
|
||||
|
||||
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'),
|
||||
http = require('http'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
try {
|
||||
var io = require('socket.io'),
|
||||
client = require('socket.io-client');
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('Socket.io is required for this example:');
|
||||
console.error('npm ' + 'install'.green);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
// Create the target HTTP server and setup
|
||||
// socket.io on it.
|
||||
//
|
||||
var server = io.listen(9015);
|
||||
server.sockets.on('connection', function (client) {
|
||||
util.debug('Got websocket connection');
|
||||
|
||||
client.on('message', function (msg) {
|
||||
util.debug('Got message from client: ' + msg);
|
||||
});
|
||||
|
||||
client.send('from server');
|
||||
});
|
||||
|
||||
//
|
||||
// Setup our server to proxy standard HTTP requests
|
||||
//
|
||||
var proxy = new httpProxy.createProxyServer({
|
||||
target: {
|
||||
host: 'localhost',
|
||||
port: 9015
|
||||
}
|
||||
});
|
||||
var proxyServer = http.createServer(function (req, res) {
|
||||
proxy.web(req, res);
|
||||
});
|
||||
|
||||
//
|
||||
// Listen to the `upgrade` event and proxy the
|
||||
// WebSocket requests as well.
|
||||
//
|
||||
proxyServer.on('upgrade', function (req, socket, head) {
|
||||
proxy.ws(req, socket, head);
|
||||
});
|
||||
|
||||
proxyServer.listen(8015);
|
||||
|
||||
//
|
||||
// Setup the socket.io client against our proxy
|
||||
//
|
||||
var ws = client.connect('ws://localhost:8015');
|
||||
|
||||
ws.on('message', function (msg) {
|
||||
util.debug('Got message: ' + msg);
|
||||
ws.send('I am the client');
|
||||
});
|
||||
70
examples/websocket/websocket-proxy.js
Normal file
70
examples/websocket/websocket-proxy.js
Normal file
@ -0,0 +1,70 @@
|
||||
/*
|
||||
web-socket-proxy.js: Example of proxying over HTTP and WebSockets.
|
||||
|
||||
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'),
|
||||
http = require('http'),
|
||||
colors = require('colors'),
|
||||
httpProxy = require('../../lib/http-proxy');
|
||||
|
||||
try {
|
||||
var io = require('socket.io'),
|
||||
client = require('socket.io-client');
|
||||
}
|
||||
catch (ex) {
|
||||
console.error('Socket.io is required for this example:');
|
||||
console.error('npm ' + 'install'.green);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
//
|
||||
// Create the target HTTP server and setup
|
||||
// socket.io on it.
|
||||
//
|
||||
var server = io.listen(9014);
|
||||
server.sockets.on('connection', function (client) {
|
||||
util.debug('Got websocket connection');
|
||||
|
||||
client.on('message', function (msg) {
|
||||
util.debug('Got message from client: ' + msg);
|
||||
});
|
||||
|
||||
client.send('from server');
|
||||
});
|
||||
|
||||
//
|
||||
// Create a proxy server with node-http-proxy
|
||||
//
|
||||
httpProxy.createServer({ target: 'ws://localhost:9014', ws: true }).listen(8014);
|
||||
|
||||
//
|
||||
// Setup the socket.io client against our proxy
|
||||
//
|
||||
var ws = client.connect('ws://localhost:8014');
|
||||
|
||||
ws.on('message', function (msg) {
|
||||
util.debug('Got message: ' + msg);
|
||||
ws.send('I am the client');
|
||||
});
|
||||
13
index.js
Normal file
13
index.js
Normal file
@ -0,0 +1,13 @@
|
||||
/*!
|
||||
* Caron dimonio, con occhi di bragia
|
||||
* loro accennando, tutte le raccoglie;
|
||||
* batte col remo qualunque s’adagia
|
||||
*
|
||||
* Charon the demon, with the eyes of glede,
|
||||
* Beckoning to them, collects them all together,
|
||||
* Beats with his oar whoever lags behind
|
||||
*
|
||||
* Dante - The Divine Comedy (Canto III)
|
||||
*/
|
||||
|
||||
module.exports = require('./lib/http-proxy');
|
||||
66
lib/http-proxy.js
Normal file
66
lib/http-proxy.js
Normal file
@ -0,0 +1,66 @@
|
||||
// Use explicit /index.js to help browserify negociation in require '/lib/http-proxy' (!)
|
||||
var ProxyServer = require('./http-proxy/index.js').Server;
|
||||
|
||||
|
||||
/**
|
||||
* Creates the proxy server.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* httpProxy.createProxyServer({ .. }, 8000)
|
||||
* // => '{ web: [Function], ws: [Function] ... }'
|
||||
*
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
function createProxyServer(options) {
|
||||
/*
|
||||
* `options` is needed and it must have the following layout:
|
||||
*
|
||||
* {
|
||||
* 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>
|
||||
* 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, 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.
|
||||
* `options.target and `options.forward` cannot be
|
||||
* both missing
|
||||
* }
|
||||
*/
|
||||
|
||||
return new ProxyServer(options);
|
||||
}
|
||||
|
||||
|
||||
ProxyServer.createProxyServer = createProxyServer;
|
||||
ProxyServer.createServer = createProxyServer;
|
||||
ProxyServer.createProxy = createProxyServer;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Export the proxy "Server" as the main export.
|
||||
*/
|
||||
module.exports = ProxyServer;
|
||||
|
||||
248
lib/http-proxy/common.js
Normal file
248
lib/http-proxy/common.js
Normal file
@ -0,0 +1,248 @@
|
||||
var common = exports,
|
||||
url = require('url'),
|
||||
extend = require('util')._extend,
|
||||
required = require('requires-port');
|
||||
|
||||
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
|
||||
* request.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* common.setupOutgoing(outgoing, options, req)
|
||||
* // => { host: ..., hostname: ...}
|
||||
*
|
||||
* @param {Object} Outgoing Base object to be filled with required properties
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
* @param {ClientRequest} Req Request Object
|
||||
* @param {String} Forward String to select forward or target
|
||||
*
|
||||
* @return {Object} Outgoing Object with all required properties set
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
common.setupOutgoing = function(outgoing, options, req, forward) {
|
||||
outgoing.port = options[forward || 'target'].port ||
|
||||
(isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
|
||||
|
||||
['host', 'hostname', 'socketPath', 'pfx', 'key',
|
||||
'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
|
||||
function(e) { outgoing[e] = options[forward || 'target'][e]; }
|
||||
);
|
||||
|
||||
outgoing.method = options.method || req.method;
|
||||
outgoing.headers = extend({}, req.headers);
|
||||
|
||||
if (options.headers){
|
||||
extend(outgoing.headers, options.headers);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
outgoing.agent = options.agent || false;
|
||||
outgoing.localAddress = options.localAddress;
|
||||
|
||||
//
|
||||
// Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
|
||||
// as node core doesn't handle this COMPLETELY properly yet.
|
||||
//
|
||||
if (!outgoing.agent) {
|
||||
outgoing.headers = outgoing.headers || {};
|
||||
if (typeof outgoing.headers.connection !== 'string'
|
||||
|| !upgradeHeader.test(outgoing.headers.connection)
|
||||
) { outgoing.headers.connection = 'close'; }
|
||||
}
|
||||
|
||||
|
||||
// the final path is target path + relative path requested by user:
|
||||
var target = options[forward || 'target'];
|
||||
var targetPath = target && options.prependPath !== false
|
||||
? (target.path || '')
|
||||
: '';
|
||||
|
||||
//
|
||||
// Remark: Can we somehow not use url.parse as a perf optimization?
|
||||
//
|
||||
var outgoingPath = !options.toProxy
|
||||
? (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 =
|
||||
required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
|
||||
? outgoing.host + ':' + outgoing.port
|
||||
: outgoing.host;
|
||||
}
|
||||
return outgoing;
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the proper configuration for sockets,
|
||||
* set no delay and set keep alive, also set
|
||||
* the timeout to 0.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* common.setupSocket(socket)
|
||||
* // => Socket
|
||||
*
|
||||
* @param {Socket} Socket instance to setup
|
||||
*
|
||||
* @return {Socket} Return the configured socket.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
common.setupSocket = function(socket) {
|
||||
socket.setTimeout(0);
|
||||
socket.setNoDelay(true);
|
||||
|
||||
socket.setKeepAlive(true, 0);
|
||||
|
||||
return socket;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the port number from the host. Or guess it based on the connection type.
|
||||
*
|
||||
* @param {Request} req Incoming HTTP request.
|
||||
*
|
||||
* @return {String} The port number.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
common.getPort = function(req) {
|
||||
var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
|
||||
|
||||
return res ?
|
||||
res[1] :
|
||||
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);
|
||||
};
|
||||
|
||||
/**
|
||||
* OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
|
||||
*
|
||||
* @return {String} The generated path.
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
common.urlJoin = function() {
|
||||
//
|
||||
// We do not want to mess with the query string. All we want to touch is the path.
|
||||
//
|
||||
var args = Array.prototype.slice.call(arguments),
|
||||
lastIndex = args.length - 1,
|
||||
last = args[lastIndex],
|
||||
lastSegs = last.split('?'),
|
||||
retSegs;
|
||||
|
||||
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(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
|
||||
|
||||
// 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(':');
|
||||
};
|
||||
185
lib/http-proxy/index.js
Normal file
185
lib/http-proxy/index.js
Normal file
@ -0,0 +1,185 @@
|
||||
var httpProxy = module.exports,
|
||||
extend = require('util')._extend,
|
||||
parse_url = require('url').parse,
|
||||
EE3 = require('eventemitter3'),
|
||||
http = require('http'),
|
||||
https = require('https'),
|
||||
web = require('./passes/web-incoming'),
|
||||
ws = require('./passes/ws-incoming');
|
||||
|
||||
httpProxy.Server = ProxyServer;
|
||||
|
||||
/**
|
||||
* Returns a function that creates the loader for
|
||||
* either `ws` or `web`'s passes.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* httpProxy.createRightProxy('ws')
|
||||
* // => [Function]
|
||||
*
|
||||
* @param {String} Type Either 'ws' or 'web'
|
||||
*
|
||||
* @return {Function} Loader Function that when called returns an iterator for the right passes
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function createRightProxy(type) {
|
||||
|
||||
return function(options) {
|
||||
return function(req, res /*, [head], [opts] */) {
|
||||
var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
||||
args = [].slice.call(arguments),
|
||||
cntr = args.length - 1,
|
||||
head, cbl;
|
||||
|
||||
/* optional args parse begin */
|
||||
if(typeof args[cntr] === 'function') {
|
||||
cbl = args[cntr];
|
||||
|
||||
cntr--;
|
||||
}
|
||||
|
||||
var requestOptions = options;
|
||||
if(
|
||||
!(args[cntr] instanceof Buffer) &&
|
||||
args[cntr] !== res
|
||||
) {
|
||||
//Copy global options
|
||||
requestOptions = extend({}, options);
|
||||
//Overwrite with request options
|
||||
extend(requestOptions, args[cntr]);
|
||||
|
||||
cntr--;
|
||||
}
|
||||
|
||||
if(args[cntr] instanceof Buffer) {
|
||||
head = args[cntr];
|
||||
}
|
||||
|
||||
/* optional args parse end */
|
||||
|
||||
['target', 'forward'].forEach(function(e) {
|
||||
if (typeof requestOptions[e] === 'string')
|
||||
requestOptions[e] = parse_url(requestOptions[e]);
|
||||
});
|
||||
|
||||
if (!requestOptions.target && !requestOptions.forward) {
|
||||
return this.emit('error', new Error('Must provide a proper URL as target'));
|
||||
}
|
||||
|
||||
for(var i=0; i < passes.length; i++) {
|
||||
/**
|
||||
* Call of passes functions
|
||||
* pass(req, res, options, head)
|
||||
*
|
||||
* In WebSockets case the `res` variable
|
||||
* refer to the connection socket
|
||||
* pass(req, socket, options, head)
|
||||
*/
|
||||
if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
httpProxy.createRightProxy = createRightProxy;
|
||||
|
||||
function ProxyServer(options) {
|
||||
EE3.call(this);
|
||||
|
||||
options = options || {};
|
||||
options.prependPath = options.prependPath === false ? false : true;
|
||||
|
||||
this.web = this.proxyRequest = createRightProxy('web')(options);
|
||||
this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options);
|
||||
this.options = options;
|
||||
|
||||
this.webPasses = Object.keys(web).map(function(pass) {
|
||||
return web[pass];
|
||||
});
|
||||
|
||||
this.wsPasses = Object.keys(ws).map(function(pass) {
|
||||
return ws[pass];
|
||||
});
|
||||
|
||||
this.on('error', this.onError, this);
|
||||
|
||||
}
|
||||
|
||||
require('util').inherits(ProxyServer, EE3);
|
||||
|
||||
ProxyServer.prototype.onError = function (err) {
|
||||
//
|
||||
// Remark: Replicate node core behavior using EE3
|
||||
// so we force people to handle their own errors
|
||||
//
|
||||
if(this.listeners('error').length === 1) {
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
ProxyServer.prototype.listen = function(port, hostname) {
|
||||
var self = this,
|
||||
closure = function(req, res) { self.web(req, res); };
|
||||
|
||||
this._server = this.options.ssl ?
|
||||
https.createServer(this.options.ssl, closure) :
|
||||
http.createServer(closure);
|
||||
|
||||
if(this.options.ws) {
|
||||
this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
|
||||
}
|
||||
|
||||
this._server.listen(port, hostname);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
ProxyServer.prototype.close = function(callback) {
|
||||
var self = this;
|
||||
if (this._server) {
|
||||
this._server.close(done);
|
||||
}
|
||||
|
||||
// Wrap callback to nullify server after all open connections are closed.
|
||||
function done() {
|
||||
self._server = null;
|
||||
if (callback) {
|
||||
callback.apply(null, arguments);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
ProxyServer.prototype.before = function(type, passName, callback) {
|
||||
if (type !== 'ws' && type !== 'web') {
|
||||
throw new Error('type must be `web` or `ws`');
|
||||
}
|
||||
var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
||||
i = false;
|
||||
|
||||
passes.forEach(function(v, idx) {
|
||||
if(v.name === passName) i = idx;
|
||||
})
|
||||
|
||||
if(i === false) throw new Error('No such pass');
|
||||
|
||||
passes.splice(i, 0, callback);
|
||||
};
|
||||
ProxyServer.prototype.after = function(type, passName, callback) {
|
||||
if (type !== 'ws' && type !== 'web') {
|
||||
throw new Error('type must be `web` or `ws`');
|
||||
}
|
||||
var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
|
||||
i = false;
|
||||
|
||||
passes.forEach(function(v, idx) {
|
||||
if(v.name === passName) i = idx;
|
||||
})
|
||||
|
||||
if(i === false) throw new Error('No such pass');
|
||||
|
||||
passes.splice(i++, 0, callback);
|
||||
};
|
||||
194
lib/http-proxy/passes/web-incoming.js
Normal file
194
lib/http-proxy/passes/web-incoming.js
Normal file
@ -0,0 +1,194 @@
|
||||
var httpNative = require('http'),
|
||||
httpsNative = require('https'),
|
||||
web_o = require('./web-outgoing'),
|
||||
common = require('../common'),
|
||||
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.
|
||||
*
|
||||
* A `pass` is just a function that is executed on `req, res, options`
|
||||
* so that you can easily add new checks while still keeping the base
|
||||
* flexible.
|
||||
*/
|
||||
|
||||
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* Sets `content-length` to '0' if request is of DELETE type.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
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'];
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets timeout in request socket if it was specified in options.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
timeout: function timeout(req, res, options) {
|
||||
if(options.timeout) {
|
||||
req.socket.setTimeout(options.timeout);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
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: encrypted ? 'https' : 'http'
|
||||
};
|
||||
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header];
|
||||
});
|
||||
|
||||
req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. If `forward` is enabled fires up
|
||||
* a ForwardStream, same happens for ProxyStream. The request
|
||||
* just dies otherwise.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
stream: function stream(req, res, options, _, server, clb) {
|
||||
|
||||
// And we begin!
|
||||
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(); }
|
||||
}
|
||||
|
||||
// Request initalization
|
||||
var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
|
||||
common.setupOutgoing(options.ssl || {}, options, req)
|
||||
);
|
||||
|
||||
// Enable developers to modify the proxyReq before headers are sent
|
||||
proxyReq.on('socket', function(socket) {
|
||||
if(server && !proxyReq.getHeader('expect')) {
|
||||
server.emit('proxyReq', proxyReq, req, res, options);
|
||||
}
|
||||
});
|
||||
|
||||
// allow outgoing socket to timeout so that we could
|
||||
// show an error page at the initial request
|
||||
if(options.proxyTimeout) {
|
||||
proxyReq.setTimeout(options.proxyTimeout, function() {
|
||||
proxyReq.abort();
|
||||
});
|
||||
}
|
||||
|
||||
// Ensure we abort proxy if request is aborted
|
||||
req.on('aborted', function () {
|
||||
proxyReq.abort();
|
||||
});
|
||||
|
||||
// handle errors in proxy and incoming request, just like for forward proxy
|
||||
var proxyError = createErrorHandler(proxyReq, options.target);
|
||||
req.on('error', proxyError);
|
||||
proxyReq.on('error', proxyError);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(options.buffer || req).pipe(proxyReq);
|
||||
|
||||
proxyReq.on('response', function(proxyRes) {
|
||||
if(server) { server.emit('proxyRes', proxyRes, req, res); }
|
||||
|
||||
if(!res.headersSent && !options.selfHandleResponse) {
|
||||
for(var i=0; i < web_o.length; i++) {
|
||||
if(web_o[i](req, res, proxyRes, options)) { break; }
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
147
lib/http-proxy/passes/web-outgoing.js
Normal file
147
lib/http-proxy/passes/web-outgoing.js
Normal file
@ -0,0 +1,147 @@
|
||||
var url = require('url'),
|
||||
common = require('../common');
|
||||
|
||||
|
||||
var redirectRegex = /^201|30(1|2|7|8)$/;
|
||||
|
||||
/*!
|
||||
* Array of passes.
|
||||
*
|
||||
* A `pass` is just a function that is executed on `req, res, options`
|
||||
* so that you can easily add new checks while still keeping the base
|
||||
* flexible.
|
||||
*/
|
||||
|
||||
module.exports = { // <--
|
||||
|
||||
/**
|
||||
* If is a HTTP 1.0 request, remove chunk headers
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {proxyResponse} Res Response object from the proxy request
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
removeChunked: function removeChunked(req, res, proxyRes) {
|
||||
if (req.httpVersion === '1.0') {
|
||||
delete proxyRes.headers['transfer-encoding'];
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* If is a HTTP 1.0 request, set the correct connection header
|
||||
* or if connection header not present, then use `keep-alive`
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {proxyResponse} Res Response object from the proxy request
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
setConnection: function setConnection(req, res, proxyRes) {
|
||||
if (req.httpVersion === '1.0') {
|
||||
proxyRes.headers.connection = req.headers.connection || 'close';
|
||||
} 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.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
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) {
|
||||
var header = proxyRes.headers[key];
|
||||
if (preserveHeaderKeyCase && rawHeaderKeyMap) {
|
||||
key = rawHeaderKeyMap[key] || key;
|
||||
}
|
||||
setHeader(key, header);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Set the statusCode from the proxyResponse
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {IncomingMessage} Res Response object
|
||||
* @param {proxyResponse} Res Response object from the proxy request
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
162
lib/http-proxy/passes/ws-incoming.js
Normal file
162
lib/http-proxy/passes/ws-incoming.js
Normal file
@ -0,0 +1,162 @@
|
||||
var http = require('http'),
|
||||
https = require('https'),
|
||||
common = require('../common');
|
||||
|
||||
/*!
|
||||
* Array of passes.
|
||||
*
|
||||
* A `pass` is just a function that is executed on `req, socket, options`
|
||||
* so that you can easily add new checks while still keeping the base
|
||||
* flexible.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Websockets Passes
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* WebSocket requests must have the `GET` method and
|
||||
* the `upgrade:websocket` header
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
|
||||
if (req.method !== 'GET' || !req.headers.upgrade) {
|
||||
socket.destroy();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (req.headers.upgrade.toLowerCase() !== 'websocket') {
|
||||
socket.destroy();
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets `x-forwarded-*` headers if specified in config.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
|
||||
XHeaders : function XHeaders(req, socket, options) {
|
||||
if(!options.xfwd) return;
|
||||
|
||||
var values = {
|
||||
for : req.connection.remoteAddress || req.socket.remoteAddress,
|
||||
port : common.getPort(req),
|
||||
proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
|
||||
};
|
||||
|
||||
['for', 'port', 'proto'].forEach(function(header) {
|
||||
req.headers['x-forwarded-' + header] =
|
||||
(req.headers['x-forwarded-' + header] || '') +
|
||||
(req.headers['x-forwarded-' + header] ? ',' : '') +
|
||||
values[header];
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Does the actual proxying. Make the request and upgrade it
|
||||
* send the Switching Protocols request and pipe the sockets.
|
||||
*
|
||||
* @param {ClientRequest} Req Request object
|
||||
* @param {Socket} Websocket
|
||||
* @param {Object} Options Config object passed to the proxy
|
||||
*
|
||||
* @api private
|
||||
*/
|
||||
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 = (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.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.
|
||||
socket.on('error', function () {
|
||||
proxySocket.end();
|
||||
});
|
||||
|
||||
common.setupSocket(proxySocket);
|
||||
|
||||
if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
|
||||
|
||||
//
|
||||
// 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);
|
||||
|
||||
server.emit('open', proxySocket);
|
||||
server.emit('proxySocket', proxySocket); //DEPRECATED.
|
||||
});
|
||||
|
||||
return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
|
||||
|
||||
function onOutgoingError(err) {
|
||||
if (clb) {
|
||||
clb(err, req, socket);
|
||||
} else {
|
||||
server.emit('error', err, req, socket);
|
||||
}
|
||||
socket.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1,139 +0,0 @@
|
||||
/*
|
||||
node-http-proxy.js: http proxy for node.js with pooling and event buffering
|
||||
|
||||
Copyright (c) 2010 Mikeal Rogers, Charlie Robbins
|
||||
|
||||
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 sys = require('sys'),
|
||||
http = require('http'),
|
||||
pool = require('pool'),
|
||||
url = require('url'),
|
||||
events = require('events'),
|
||||
min = 0,
|
||||
max = 100;
|
||||
|
||||
// Setup the PoolManager
|
||||
var manager = pool.createPoolManager();
|
||||
manager.setMinClients(min);
|
||||
manager.setMaxClients(max);
|
||||
|
||||
exports.createServer = function () {
|
||||
var args, action, port, host;
|
||||
args = Array.prototype.slice.call(arguments);
|
||||
action = typeof args[args.length - 1] === 'function' && args.pop();
|
||||
if (args[0]) port = args[0];
|
||||
if (args[1]) host = args[1];
|
||||
|
||||
var proxy = createProxy();
|
||||
proxy.on('route', function (req, res, callback) {
|
||||
var uri = url.parse(req.url);
|
||||
if (action) {
|
||||
action(req, res, callback);
|
||||
}
|
||||
else {
|
||||
port = port ? port : uri.port ? uri.port : 80;
|
||||
host = host ? host : uri.hostname;
|
||||
callback(port, host);
|
||||
}
|
||||
});
|
||||
return proxy;
|
||||
};
|
||||
|
||||
exports.setMin = function (value) {
|
||||
min = value;
|
||||
manager.setMinClients(min);
|
||||
};
|
||||
|
||||
exports.setMax = function (value) {
|
||||
max = value;
|
||||
manager.setMaxClients(max);
|
||||
}
|
||||
|
||||
var createProxy = function () {
|
||||
var server = http.createServer(function (req, res) {
|
||||
var buffers = [],
|
||||
b = function (chunk) { buffers.push(chunk) },
|
||||
e = function () { e = false };
|
||||
|
||||
req.on('data', b);
|
||||
req.on('end', e);
|
||||
|
||||
server.emit('route', req, res, function (port, hostname) {
|
||||
var p = manager.getPool(port, hostname);
|
||||
|
||||
p.request(req.method, req.url, req.headers, function (reverse_proxy) {
|
||||
var data = '';
|
||||
reverse_proxy.on('error', function (err) {
|
||||
res.writeHead(500, {'Content-Type': 'text/plain'});
|
||||
|
||||
if(req.method !== 'HEAD') {
|
||||
res.write('An error has occurred: ' + sys.puts(JSON.stringify(err)));
|
||||
}
|
||||
|
||||
res.end();
|
||||
});
|
||||
|
||||
buffers.forEach(function (c) {
|
||||
data += c;
|
||||
reverse_proxy.write(c);
|
||||
});
|
||||
|
||||
buffers = null;
|
||||
req.removeListener('data', b);
|
||||
sys.pump(req, reverse_proxy);
|
||||
|
||||
if (e) {
|
||||
req.removeListener('end', e);
|
||||
req.addListener('end', function () { reverse_proxy.end() });
|
||||
}
|
||||
else {
|
||||
reverse_proxy.end();
|
||||
}
|
||||
|
||||
// Add a listener for the reverse_proxy response event
|
||||
reverse_proxy.addListener('response', function (response) {
|
||||
if (response.headers.connection) {
|
||||
if (req.headers.connection) response.headers.connection = req.headers.connection;
|
||||
else response.headers.connection = 'close';
|
||||
}
|
||||
|
||||
// These two listeners are for testability and observation
|
||||
// of what's passed back from the target server
|
||||
response.addListener('data', function (chunk) {
|
||||
data += chunk;
|
||||
});
|
||||
|
||||
response.addListener('end', function() {
|
||||
server.emit('proxy', null, data);
|
||||
});
|
||||
|
||||
// Set the response headers of the client response
|
||||
res.writeHead(response.statusCode, response.headers);
|
||||
|
||||
sys.pump(response, res);
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
return server;
|
||||
};
|
||||
2028
package-lock.json
generated
Normal file
2028
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
51
package.json
51
package.json
@ -1,22 +1,41 @@
|
||||
{
|
||||
"name": "http-proxy",
|
||||
"description": "A full-featured http reverse proxy for node.js",
|
||||
"version": "0.2.0",
|
||||
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
|
||||
"contributors": [
|
||||
{ "name": "Mikeal Rogers", "email": "mikeal.rogers@gmail.com" },
|
||||
{ "name": "Marak Squires", "email": "marak.squires@gmail.com" },
|
||||
],
|
||||
"version": "1.18.1",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/nodejitsu/node-http-proxy.git"
|
||||
"type": "git",
|
||||
"url": "https://github.com/http-party/node-http-proxy.git"
|
||||
},
|
||||
"keywords": ["reverse", "proxy", "http"],
|
||||
"description": "HTTP proxying for the masses",
|
||||
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
|
||||
"maintainers": [
|
||||
"jcrugzz <jcrugzz@gmail.com>"
|
||||
],
|
||||
"main": "index.js",
|
||||
"dependencies": {
|
||||
"colors": ">= 0.3.0",
|
||||
"pool": ">= 0.4.1"
|
||||
"eventemitter3": "^4.0.0",
|
||||
"requires-port": "^1.0.0",
|
||||
"follow-redirects": "^1.0.0"
|
||||
},
|
||||
"main": "./lib/node-http-proxy",
|
||||
"scripts": { "test": "vows" },
|
||||
"engines": { "node": ">= 0.2.0" }
|
||||
}
|
||||
"devDependencies": {
|
||||
"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": {
|
||||
"mocha": "mocha test/*-test.js",
|
||||
"test": "nyc --reporter=text --reporter=lcov npm run mocha",
|
||||
"version": "auto-changelog -p && git add CHANGELOG.md"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
|
||||
19
renovate.json
Normal file
19
renovate.json
Normal 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]"
|
||||
}
|
||||
71
test/examples-test.js
Normal file
71
test/examples-test.js
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
examples-test.js: Test to run all the examples
|
||||
|
||||
Copyright (c) 2013 - 2016 Charlie Robbins, Jarrett Cruger & the Contributors.
|
||||
|
||||
*/
|
||||
var path = require('path'),
|
||||
fs = require('fs'),
|
||||
spawn = require('child_process').spawn,
|
||||
expect = require('expect.js'),
|
||||
async = require('async');
|
||||
|
||||
var rootDir = path.join(__dirname, '..'),
|
||||
examplesDir = path.join(rootDir, 'examples');
|
||||
|
||||
describe.skip('http-proxy examples', function () {
|
||||
describe('Before testing examples', function () {
|
||||
// Set a timeout to avoid this error
|
||||
this.timeout(30 * 1000);
|
||||
it('should have installed dependencies', function (done) {
|
||||
async.waterfall([
|
||||
//
|
||||
// 1. Read files in examples dir
|
||||
//
|
||||
async.apply(fs.readdir, examplesDir),
|
||||
//
|
||||
// 2. If node_modules exists, continue. Otherwise
|
||||
// exec `npm` to install them
|
||||
//
|
||||
function checkNodeModules(files, next) {
|
||||
if (files.indexOf('node_modules') !== -1) {
|
||||
return next();
|
||||
}
|
||||
|
||||
console.log('Warning: installing dependencies, this operation could take a while');
|
||||
|
||||
var child = spawn('npm', ['install', '-f'], {
|
||||
cwd: examplesDir
|
||||
});
|
||||
|
||||
child.on('exit', function (code) {
|
||||
return code
|
||||
? next(new Error('npm install exited with non-zero exit code'))
|
||||
: next();
|
||||
});
|
||||
},
|
||||
//
|
||||
// 3. Read files in examples dir again to ensure the install
|
||||
// worked as expected.
|
||||
//
|
||||
async.apply(fs.readdir, examplesDir),
|
||||
], done);
|
||||
})
|
||||
});
|
||||
|
||||
describe('Requiring all the examples', function () {
|
||||
it('should have no errors', function (done) {
|
||||
async.each(['balancer', 'http', 'middleware', 'websocket'], function (dir, cb) {
|
||||
var name = 'examples/' + dir,
|
||||
files = fs.readdirSync(path.join(rootDir, 'examples', dir));
|
||||
|
||||
async.each(files, function (file, callback) {
|
||||
var example;
|
||||
expect(function () { example = require(path.join(examplesDir, dir, file)); }).to.not.throwException();
|
||||
expect(example).to.be.an('object');
|
||||
callback();
|
||||
}, cb);
|
||||
}, done);
|
||||
})
|
||||
})
|
||||
})
|
||||
25
test/fixtures/agent2-cert.pem
vendored
Normal file
25
test/fixtures/agent2-cert.pem
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
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-----
|
||||
27
test/fixtures/agent2-key.pem
vendored
Normal file
27
test/fixtures/agent2-key.pem
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
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-----
|
||||
397
test/lib-http-proxy-common-test.js
Normal file
397
test/lib-http-proxy-common-test.js
Normal file
@ -0,0 +1,397 @@
|
||||
var common = require('../lib/http-proxy/common'),
|
||||
url = require('url'),
|
||||
expect = require('expect.js');
|
||||
|
||||
describe('lib/http-proxy/common.js', function () {
|
||||
describe('#setupOutgoing', function () {
|
||||
it('should setup the correct headers', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent : '?',
|
||||
target: {
|
||||
host : 'hey',
|
||||
hostname : 'how',
|
||||
socketPath: 'are',
|
||||
port : 'you',
|
||||
},
|
||||
headers: {'fizz': 'bang', 'overwritten':true},
|
||||
localAddress: 'local.address',
|
||||
auth:'username:pass'
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {'pro':'xy','overwritten':false}
|
||||
});
|
||||
|
||||
expect(outgoing.host).to.eql('hey');
|
||||
expect(outgoing.hostname).to.eql('how');
|
||||
expect(outgoing.socketPath).to.eql('are');
|
||||
expect(outgoing.port).to.eql('you');
|
||||
expect(outgoing.agent).to.eql('?');
|
||||
|
||||
expect(outgoing.method).to.eql('i');
|
||||
expect(outgoing.path).to.eql('am');
|
||||
|
||||
expect(outgoing.headers.pro).to.eql('xy');
|
||||
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 () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent: undefined,
|
||||
target: {
|
||||
host : 'hey',
|
||||
hostname : 'how',
|
||||
socketPath: 'are',
|
||||
port : 'you',
|
||||
},
|
||||
headers: {'connection': 'upgrade'},
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {'pro':'xy','overwritten':false}
|
||||
});
|
||||
expect(outgoing.headers.connection).to.eql('upgrade');
|
||||
});
|
||||
|
||||
it('should not override agentless connection: contains upgrade', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent: undefined,
|
||||
target: {
|
||||
host : 'hey',
|
||||
hostname : 'how',
|
||||
socketPath: 'are',
|
||||
port : 'you',
|
||||
},
|
||||
headers: {'connection': 'keep-alive, upgrade'}, // this is what Firefox sets
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {'pro':'xy','overwritten':false}
|
||||
});
|
||||
expect(outgoing.headers.connection).to.eql('keep-alive, upgrade');
|
||||
});
|
||||
|
||||
it('should override agentless connection: contains improper upgrade', function () {
|
||||
// sanity check on upgrade regex
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent: undefined,
|
||||
target: {
|
||||
host : 'hey',
|
||||
hostname : 'how',
|
||||
socketPath: 'are',
|
||||
port : 'you',
|
||||
},
|
||||
headers: {'connection': 'keep-alive, not upgrade'},
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {'pro':'xy','overwritten':false}
|
||||
});
|
||||
expect(outgoing.headers.connection).to.eql('close');
|
||||
});
|
||||
|
||||
it('should override agentless non-upgrade header to close', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent: undefined,
|
||||
target: {
|
||||
host : 'hey',
|
||||
hostname : 'how',
|
||||
socketPath: 'are',
|
||||
port : 'you',
|
||||
},
|
||||
headers: {'connection': 'xyz'},
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {'pro':'xy','overwritten':false}
|
||||
});
|
||||
expect(outgoing.headers.connection).to.eql('close');
|
||||
});
|
||||
|
||||
it('should set the agent to false if none is given', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing, {target:
|
||||
'http://localhost'
|
||||
}, { url: '/' });
|
||||
expect(outgoing.agent).to.eql(false);
|
||||
});
|
||||
|
||||
it('set the port according to the protocol', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing,
|
||||
{
|
||||
agent : '?',
|
||||
target: {
|
||||
host : 'how',
|
||||
hostname : 'are',
|
||||
socketPath: 'you',
|
||||
protocol: 'https:'
|
||||
}
|
||||
},
|
||||
{
|
||||
method : 'i',
|
||||
url : 'am',
|
||||
headers : {pro:'xy'}
|
||||
});
|
||||
|
||||
expect(outgoing.host).to.eql('how');
|
||||
expect(outgoing.hostname).to.eql('are');
|
||||
expect(outgoing.socketPath).to.eql('you');
|
||||
expect(outgoing.agent).to.eql('?');
|
||||
|
||||
expect(outgoing.method).to.eql('i');
|
||||
expect(outgoing.path).to.eql('am');
|
||||
expect(outgoing.headers.pro).to.eql('xy');
|
||||
|
||||
expect(outgoing.port).to.eql(443);
|
||||
});
|
||||
|
||||
it('should keep the original target path in the outgoing path', function(){
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing, {target:
|
||||
{ path: 'some-path' }
|
||||
}, { url : 'am' });
|
||||
|
||||
expect(outgoing.path).to.eql('some-path/am');
|
||||
});
|
||||
|
||||
it('should keep the original forward path in the outgoing path', function(){
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing, {
|
||||
target: {},
|
||||
forward: {
|
||||
path: 'some-path'
|
||||
}
|
||||
}, {
|
||||
url : 'am'
|
||||
}, 'forward');
|
||||
|
||||
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, {
|
||||
target: { path: 'hellothere' },
|
||||
prependPath: false
|
||||
}, { url: 'hi' });
|
||||
|
||||
expect(outgoing.path).to.eql('hi');
|
||||
})
|
||||
|
||||
it('should properly join paths', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing, {
|
||||
target: { path: '/forward' },
|
||||
}, { url: '/static/path' });
|
||||
|
||||
expect(outgoing.path).to.eql('/forward/static/path');
|
||||
})
|
||||
|
||||
it('should not modify the query string', function () {
|
||||
var outgoing = {};
|
||||
common.setupOutgoing(outgoing, {
|
||||
target: { path: '/forward' },
|
||||
}, { 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/?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 () {
|
||||
it('should setup a socket', function () {
|
||||
var socketConfig = {
|
||||
timeout: null,
|
||||
nodelay: false,
|
||||
keepalive: false
|
||||
},
|
||||
stubSocket = {
|
||||
setTimeout: function (num) {
|
||||
socketConfig.timeout = num;
|
||||
},
|
||||
setNoDelay: function (bol) {
|
||||
socketConfig.nodelay = bol;
|
||||
},
|
||||
setKeepAlive: function (bol) {
|
||||
socketConfig.keepalive = bol;
|
||||
}
|
||||
}
|
||||
returnValue = common.setupSocket(stubSocket);
|
||||
|
||||
expect(socketConfig.timeout).to.eql(0);
|
||||
expect(socketConfig.nodelay).to.eql(true);
|
||||
expect(socketConfig.keepalive).to.eql(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
536
test/lib-http-proxy-passes-web-incoming-test.js
Normal file
536
test/lib-http-proxy-passes-web-incoming-test.js
Normal file
@ -0,0 +1,536 @@
|
||||
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` 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() {
|
||||
it('should set timeout on the socket', function() {
|
||||
var done = false, stubRequest = {
|
||||
socket: {
|
||||
setTimeout: function(value) { done = value; }
|
||||
}
|
||||
}
|
||||
|
||||
webPasses.timeout(stubRequest, {}, { timeout: 5000});
|
||||
expect(done).to.eql(5000);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#XHeaders', function () {
|
||||
var stubRequest = {
|
||||
connection: {
|
||||
remoteAddress: '192.168.1.2',
|
||||
remotePort: '8080'
|
||||
},
|
||||
headers: {
|
||||
host: '192.168.1.2:8080'
|
||||
}
|
||||
}
|
||||
|
||||
it('set the correct x-forwarded-* headers', function () {
|
||||
webPasses.XHeaders(stubRequest, {}, { xfwd: true });
|
||||
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
|
||||
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
|
||||
expect(stubRequest.headers['x-forwarded-proto']).to.be('http');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createProxyServer.web() using own http server', function () {
|
||||
it('should proxy the request using the web proxy handler', function (done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:8080'
|
||||
});
|
||||
|
||||
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.method).to.eql('GET');
|
||||
expect(req.headers.host.split(':')[1]).to.eql('8081');
|
||||
done();
|
||||
});
|
||||
|
||||
proxyServer.listen('8081');
|
||||
source.listen('8080');
|
||||
|
||||
http.request('http://127.0.0.1:8081', function() {}).end();
|
||||
});
|
||||
|
||||
it('should detect a proxyReq event and modify headers', 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.eql('foobar');
|
||||
done();
|
||||
});
|
||||
|
||||
proxyServer.listen('8081');
|
||||
source.listen('8080');
|
||||
|
||||
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'
|
||||
});
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
function requestHandler(req, res) {
|
||||
proxy.web(req, res, function (err) {
|
||||
proxyServer.close();
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNREFUSED');
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
proxyServer.listen('8082');
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: '8082',
|
||||
method: 'GET',
|
||||
}, function() {}).end();
|
||||
});
|
||||
|
||||
it('should proxy the request and handle error via event listener', function(done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: '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 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',
|
||||
proxyTimeout: 100
|
||||
});
|
||||
|
||||
require('net').createServer().listen(45000);
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
var started = new Date().getTime();
|
||||
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(new Date().getTime() - started).to.be.greaterThan(99);
|
||||
expect(err.code).to.be('ECONNRESET');
|
||||
done();
|
||||
});
|
||||
|
||||
proxy.web(req, res);
|
||||
}
|
||||
|
||||
proxyServer.listen('8084');
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: '8084',
|
||||
method: 'GET',
|
||||
}, function() {}).end();
|
||||
});
|
||||
|
||||
it('should proxy the request and handle timeout error', function(done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:45001',
|
||||
timeout: 100
|
||||
});
|
||||
|
||||
require('net').createServer().listen(45001);
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
var cnt = 0;
|
||||
var doneOne = function() {
|
||||
cnt += 1;
|
||||
if(cnt === 2) done();
|
||||
}
|
||||
|
||||
var started = new Date().getTime();
|
||||
function requestHandler(req, res) {
|
||||
proxy.once('econnreset', 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('ECONNRESET');
|
||||
doneOne();
|
||||
});
|
||||
|
||||
proxy.web(req, res);
|
||||
}
|
||||
|
||||
proxyServer.listen('8085');
|
||||
|
||||
var req = http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: '8085',
|
||||
method: 'GET',
|
||||
}, function() {});
|
||||
|
||||
req.on('error', function(err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNRESET');
|
||||
expect(new Date().getTime() - started).to.be.greaterThan(99);
|
||||
doneOne();
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
|
||||
it('should proxy the request and provide a proxyRes event with the request and response parameters', function(done) {
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:8080'
|
||||
});
|
||||
|
||||
function requestHandler(req, res) {
|
||||
proxy.once('proxyRes', function (proxyRes, pReq, pRes) {
|
||||
source.close();
|
||||
proxyServer.close();
|
||||
expect(pReq).to.be.equal(req);
|
||||
expect(pRes).to.be.equal(res);
|
||||
done();
|
||||
});
|
||||
|
||||
proxy.web(req, res);
|
||||
}
|
||||
|
||||
var proxyServer = http.createServer(requestHandler);
|
||||
|
||||
var source = http.createServer(function(req, res) {
|
||||
res.end('Response');
|
||||
});
|
||||
|
||||
proxyServer.listen('8086');
|
||||
source.listen('8080');
|
||||
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',
|
||||
changeOrigin: true
|
||||
});
|
||||
|
||||
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.method).to.eql('GET');
|
||||
expect(req.headers.host.split(':')[1]).to.eql('8080');
|
||||
done();
|
||||
});
|
||||
|
||||
proxyServer.listen('8081');
|
||||
source.listen('8080');
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
423
test/lib-http-proxy-passes-web-outgoing-test.js
Normal file
423
test/lib-http-proxy-passes-web-outgoing-test.js
Normal file
@ -0,0 +1,423 @@
|
||||
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: {} };
|
||||
httpProxy.setConnection({
|
||||
httpVersion: '1.0',
|
||||
headers: {
|
||||
connection: null
|
||||
}
|
||||
}, {}, proxyRes);
|
||||
|
||||
expect(proxyRes.headers.connection).to.eql('close');
|
||||
});
|
||||
|
||||
it('set the right connection with 1.0 - req.connection', function() {
|
||||
var proxyRes = { headers: {} };
|
||||
httpProxy.setConnection({
|
||||
httpVersion: '1.0',
|
||||
headers: {
|
||||
connection: 'hey'
|
||||
}
|
||||
}, {}, proxyRes);
|
||||
|
||||
expect(proxyRes.headers.connection).to.eql('hey');
|
||||
});
|
||||
|
||||
it('set the right connection - req.connection', function() {
|
||||
var proxyRes = { headers: {} };
|
||||
httpProxy.setConnection({
|
||||
httpVersion: null,
|
||||
headers: {
|
||||
connection: 'hola'
|
||||
}
|
||||
}, {}, proxyRes);
|
||||
|
||||
expect(proxyRes.headers.connection).to.eql('hola');
|
||||
});
|
||||
|
||||
it('set the right connection - `keep-alive`', function() {
|
||||
var proxyRes = { headers: {} };
|
||||
httpProxy.setConnection({
|
||||
httpVersion: null,
|
||||
headers: {
|
||||
connection: null
|
||||
}
|
||||
}, {}, proxyRes);
|
||||
|
||||
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 () {
|
||||
it('should write status code', function() {
|
||||
var res = {
|
||||
writeHead: function(n) {
|
||||
expect(n).to.eql(200);
|
||||
}
|
||||
};
|
||||
|
||||
httpProxy.writeStatusCode({}, res, { statusCode: 200 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('#writeHeaders', function() {
|
||||
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: {}
|
||||
};
|
||||
});
|
||||
|
||||
it('writes headers', function() {
|
||||
var options = {};
|
||||
httpProxy.writeHeaders({}, this.res, this.proxyRes, 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('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=/');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#removeChunked', function() {
|
||||
var proxyRes = {
|
||||
headers: {
|
||||
'transfer-encoding': 'hello'
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
httpProxy.removeChunked({ httpVersion: '1.0' }, {}, proxyRes);
|
||||
|
||||
expect(proxyRes.headers['transfer-encoding']).to.eql(undefined);
|
||||
});
|
||||
|
||||
});
|
||||
120
test/lib-http-proxy-passes-ws-incoming-test.js
Normal file
120
test/lib-http-proxy-passes-ws-incoming-test.js
Normal file
@ -0,0 +1,120 @@
|
||||
var httpProxy = require('../lib/http-proxy/passes/ws-incoming'),
|
||||
expect = require('expect.js');
|
||||
|
||||
describe('lib/http-proxy/passes/ws-incoming.js', function () {
|
||||
describe('#checkMethodAndHeader', function () {
|
||||
it('should drop non-GET connections', function () {
|
||||
var destroyCalled = false,
|
||||
stubRequest = {
|
||||
method: 'DELETE',
|
||||
headers: {}
|
||||
},
|
||||
stubSocket = {
|
||||
destroy: function () {
|
||||
// Simulate Socket.destroy() method when call
|
||||
destroyCalled = true;
|
||||
}
|
||||
}
|
||||
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
|
||||
expect(returnValue).to.be(true);
|
||||
expect(destroyCalled).to.be(true);
|
||||
})
|
||||
|
||||
it('should drop connections when no upgrade header', function () {
|
||||
var destroyCalled = false,
|
||||
stubRequest = {
|
||||
method: 'GET',
|
||||
headers: {}
|
||||
},
|
||||
stubSocket = {
|
||||
destroy: function () {
|
||||
// Simulate Socket.destroy() method when call
|
||||
destroyCalled = true;
|
||||
}
|
||||
}
|
||||
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
|
||||
expect(returnValue).to.be(true);
|
||||
expect(destroyCalled).to.be(true);
|
||||
})
|
||||
|
||||
it('should drop connections when upgrade header is different of `websocket`', function () {
|
||||
var destroyCalled = false,
|
||||
stubRequest = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
upgrade: 'anotherprotocol'
|
||||
}
|
||||
},
|
||||
stubSocket = {
|
||||
destroy: function () {
|
||||
// Simulate Socket.destroy() method when call
|
||||
destroyCalled = true;
|
||||
}
|
||||
}
|
||||
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
|
||||
expect(returnValue).to.be(true);
|
||||
expect(destroyCalled).to.be(true);
|
||||
})
|
||||
|
||||
it('should return nothing when all is ok', function () {
|
||||
var destroyCalled = false,
|
||||
stubRequest = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
upgrade: 'websocket'
|
||||
}
|
||||
},
|
||||
stubSocket = {
|
||||
destroy: function () {
|
||||
// Simulate Socket.destroy() method when call
|
||||
destroyCalled = true;
|
||||
}
|
||||
}
|
||||
returnValue = httpProxy.checkMethodAndHeader(stubRequest, stubSocket);
|
||||
expect(returnValue).to.be(undefined);
|
||||
expect(destroyCalled).to.be(false);
|
||||
})
|
||||
});
|
||||
|
||||
describe('#XHeaders', function () {
|
||||
it('return if no forward request', function () {
|
||||
var returnValue = httpProxy.XHeaders({}, {}, {});
|
||||
expect(returnValue).to.be(undefined);
|
||||
});
|
||||
|
||||
it('set the correct x-forwarded-* headers from req.connection', function () {
|
||||
var stubRequest = {
|
||||
connection: {
|
||||
remoteAddress: '192.168.1.2',
|
||||
remotePort: '8080'
|
||||
},
|
||||
headers: {
|
||||
host: '192.168.1.2:8080'
|
||||
}
|
||||
}
|
||||
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
|
||||
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.2');
|
||||
expect(stubRequest.headers['x-forwarded-port']).to.be('8080');
|
||||
expect(stubRequest.headers['x-forwarded-proto']).to.be('ws');
|
||||
});
|
||||
|
||||
it('set the correct x-forwarded-* headers from req.socket', function () {
|
||||
var stubRequest = {
|
||||
socket: {
|
||||
remoteAddress: '192.168.1.3',
|
||||
remotePort: '8181'
|
||||
},
|
||||
connection: {
|
||||
pair: true
|
||||
},
|
||||
headers: {
|
||||
host: '192.168.1.3:8181'
|
||||
}
|
||||
};
|
||||
httpProxy.XHeaders(stubRequest, {}, { xfwd: true });
|
||||
expect(stubRequest.headers['x-forwarded-for']).to.be('192.168.1.3');
|
||||
expect(stubRequest.headers['x-forwarded-port']).to.be('8181');
|
||||
expect(stubRequest.headers['x-forwarded-proto']).to.be('wss');
|
||||
});
|
||||
});
|
||||
});
|
||||
639
test/lib-http-proxy-test.js
Normal file
639
test/lib-http-proxy-test.js
Normal file
@ -0,0 +1,639 @@
|
||||
var httpProxy = require('../lib/http-proxy'),
|
||||
expect = require('expect.js'),
|
||||
http = require('http'),
|
||||
net = require('net'),
|
||||
ws = require('ws'),
|
||||
io = require('socket.io'),
|
||||
SSE = require('sse'),
|
||||
ioClient = require('socket.io-client');
|
||||
|
||||
//
|
||||
// Expose a port number generator.
|
||||
// thanks to @3rd-Eden
|
||||
//
|
||||
var initialPort = 1024, gen = {};
|
||||
Object.defineProperty(gen, 'port', {
|
||||
get: function get() {
|
||||
return initialPort++;
|
||||
}
|
||||
});
|
||||
|
||||
describe('lib/http-proxy.js', function() {
|
||||
describe('#createProxyServer', function() {
|
||||
it.skip('should throw without options', function() {
|
||||
var error;
|
||||
try {
|
||||
httpProxy.createProxyServer();
|
||||
} catch(e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).to.be.an(Error);
|
||||
})
|
||||
|
||||
it('should return an object otherwise', function() {
|
||||
var obj = httpProxy.createProxyServer({
|
||||
target: 'http://www.google.com:80'
|
||||
});
|
||||
|
||||
expect(obj.web).to.be.a(Function);
|
||||
expect(obj.ws).to.be.a(Function);
|
||||
expect(obj.listen).to.be.a(Function);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createProxyServer with forward options and using web-incoming passes', function () {
|
||||
it('should pipe the request using web-incoming#stream method', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
forward: 'http://127.0.0.1:' + ports.source
|
||||
}).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.proxy);
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();
|
||||
})
|
||||
});
|
||||
|
||||
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({
|
||||
target: 'http://127.0.0.1:' + ports.source
|
||||
}).listen(ports.proxy);
|
||||
|
||||
var source = http.createServer(function(req, res) {
|
||||
expect(req.method).to.eql('POST');
|
||||
expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');
|
||||
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'x-forwarded-for': '127.0.0.1'
|
||||
}
|
||||
}, function() {}).end();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createProxyServer using the web-incoming passes', 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,
|
||||
preserveHeaderKeyCase: 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.proxy);
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'})
|
||||
res.end('Hello from ' + source.address().port);
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
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);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
});
|
||||
}).end();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createProxyServer() method with error response', function () {
|
||||
it('should make the request and emit the error event', function(done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:' + ports.source
|
||||
});
|
||||
|
||||
proxy.on('error', function (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNREFUSED');
|
||||
proxy.close();
|
||||
done();
|
||||
})
|
||||
|
||||
proxy.listen(ports.proxy);
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
method: 'GET',
|
||||
}, function() {}).end();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#createProxyServer setting the correct timeout value', function () {
|
||||
it('should hang up the socket at the timeout', function (done) {
|
||||
this.timeout(30);
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:' + ports.source,
|
||||
timeout: 3
|
||||
}).listen(ports.proxy);
|
||||
|
||||
proxy.on('error', function (e) {
|
||||
expect(e).to.be.an(Error);
|
||||
expect(e.code).to.be.eql('ECONNRESET');
|
||||
});
|
||||
|
||||
var source = http.createServer(function(req, res) {
|
||||
setTimeout(function () {
|
||||
res.end('At this point the socket should be closed');
|
||||
}, 5)
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
var testReq = http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
method: 'GET',
|
||||
}, function() {});
|
||||
|
||||
testReq.on('error', function (e) {
|
||||
expect(e).to.be.an(Error);
|
||||
expect(e.code).to.be.eql('ECONNRESET');
|
||||
proxy.close();
|
||||
source.close();
|
||||
done();
|
||||
});
|
||||
|
||||
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 correctly', function(done) {
|
||||
// var proxy = httpProxy.createProxyServer({
|
||||
// target: 'http://127.0.0.1:8080'
|
||||
// }),
|
||||
|
||||
// proxyServer = proxy.listen('8081'),
|
||||
|
||||
// source = http.createServer(function(req, res) {
|
||||
// expect(req.method).to.eql('GET');
|
||||
// expect(req.headers.host.split(':')[1]).to.eql('8081');
|
||||
// res.writeHead(200, {'Content-Type': 'text/plain'})
|
||||
// res.end('Hello from ' + source.address().port);
|
||||
// }),
|
||||
|
||||
// events = [];
|
||||
|
||||
// source.listen('8080');
|
||||
|
||||
// proxy.ee.on('http-proxy:**', function (uno, dos, tres) {
|
||||
// events.push(this.event);
|
||||
// })
|
||||
|
||||
// http.request({
|
||||
// hostname: '127.0.0.1',
|
||||
// port: '8081',
|
||||
// method: 'GET',
|
||||
// }, function(res) {
|
||||
// expect(res.statusCode).to.eql(200);
|
||||
|
||||
// res.on('data', function (data) {
|
||||
// expect(data.toString()).to.eql('Hello from 8080');
|
||||
// });
|
||||
|
||||
// res.on('end', function () {
|
||||
// expect(events).to.contain('http-proxy:outgoing:web:begin');
|
||||
// expect(events).to.contain('http-proxy:outgoing:web:end');
|
||||
// source.close();
|
||||
// proxyServer.close();
|
||||
// done();
|
||||
// });
|
||||
// }).end();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#createProxyServer using the ws-incoming passes', function () {
|
||||
it('should proxy the websockets stream', 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 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) {
|
||||
socket.on('message', function (msg) {
|
||||
expect(msg).to.be('hello there');
|
||||
socket.send('Hello over websockets');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should emit error on proxy error', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
// note: we don't ever listen on this port
|
||||
target: 'ws://127.0.0.1:' + ports.source,
|
||||
ws: true
|
||||
}),
|
||||
proxyServer = proxy.listen(ports.proxy),
|
||||
client = new ws('ws://127.0.0.1:' + ports.proxy);
|
||||
|
||||
client.on('open', function () {
|
||||
client.send('hello there');
|
||||
});
|
||||
|
||||
var count = 0;
|
||||
function maybe_done () {
|
||||
count += 1;
|
||||
if (count === 2) done();
|
||||
}
|
||||
|
||||
client.on('error', function (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNRESET');
|
||||
maybe_done();
|
||||
});
|
||||
|
||||
proxy.on('error', function (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
expect(err.code).to.be('ECONNREFUSED');
|
||||
proxyServer.close();
|
||||
maybe_done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should close client socket if upstream is closed before upgrade', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
var server = http.createServer();
|
||||
server.on('upgrade', function (req, socket, head) {
|
||||
var response = [
|
||||
'HTTP/1.1 404 Not Found',
|
||||
'Content-type: text/html',
|
||||
'',
|
||||
''
|
||||
];
|
||||
socket.write(response.join('\r\n'));
|
||||
socket.end();
|
||||
});
|
||||
server.listen(ports.source);
|
||||
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
// note: we don't ever listen on this port
|
||||
target: 'ws://127.0.0.1:' + ports.source,
|
||||
ws: true
|
||||
}),
|
||||
proxyServer = proxy.listen(ports.proxy),
|
||||
client = new ws('ws://127.0.0.1:' + ports.proxy);
|
||||
|
||||
client.on('open', function () {
|
||||
client.send('hello there');
|
||||
});
|
||||
|
||||
client.on('error', function (err) {
|
||||
expect(err).to.be.an(Error);
|
||||
proxyServer.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should proxy a socket.io stream', function (done) {
|
||||
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),
|
||||
server = http.createServer(),
|
||||
destiny = io.listen(server);
|
||||
|
||||
function startSocketIo() {
|
||||
var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);
|
||||
|
||||
client.on('connect', function () {
|
||||
client.emit('incoming', 'hello there');
|
||||
});
|
||||
|
||||
client.on('outgoing', function (data) {
|
||||
expect(data).to.be('Hello over websockets');
|
||||
proxyServer.close();
|
||||
server.close();
|
||||
done();
|
||||
});
|
||||
}
|
||||
server.listen(ports.source);
|
||||
server.on('listening', startSocketIo);
|
||||
|
||||
destiny.sockets.on('connection', function (socket) {
|
||||
socket.on('incoming', function (msg) {
|
||||
expect(msg).to.be('hello there');
|
||||
socket.emit('outgoing', 'Hello over websockets');
|
||||
});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
233
test/lib-https-proxy-test.js
Normal file
233
test/lib-https-proxy-test.js
Normal file
@ -0,0 +1,233 @@
|
||||
var httpProxy = require('../lib/http-proxy'),
|
||||
semver = require('semver'),
|
||||
expect = require('expect.js'),
|
||||
http = require('http')
|
||||
https = require('https'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
//
|
||||
// Expose a port number generator.
|
||||
// thanks to @3rd-Eden
|
||||
//
|
||||
var initialPort = 1024, gen = {};
|
||||
Object.defineProperty(gen, 'port', {
|
||||
get: function get() {
|
||||
return initialPort++;
|
||||
}
|
||||
});
|
||||
|
||||
describe('lib/http-proxy.js', function() {
|
||||
describe('HTTPS #createProxyServer', 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) {
|
||||
expect(req.method).to.eql('GET');
|
||||
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'http://127.0.0.1:' + ports.source,
|
||||
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);
|
||||
|
||||
https.request({
|
||||
host: 'localhost',
|
||||
port: ports.proxy,
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
rejectUnauthorized: false
|
||||
}, function(res) {
|
||||
expect(res.statusCode).to.eql(200);
|
||||
|
||||
res.on('data', function (data) {
|
||||
expect(data.toString()).to.eql('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
})
|
||||
}).end();
|
||||
})
|
||||
});
|
||||
describe('HTTP to HTTPS', function () {
|
||||
it('should proxy the request en send back the response', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
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);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'https://127.0.0.1:' + ports.source,
|
||||
// Allow to use SSL self signed
|
||||
secure: false
|
||||
}).listen(ports.proxy);
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
method: 'GET'
|
||||
}, function(res) {
|
||||
expect(res.statusCode).to.eql(200);
|
||||
|
||||
res.on('data', function (data) {
|
||||
expect(data.toString()).to.eql('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
});
|
||||
}).end();
|
||||
})
|
||||
})
|
||||
describe('HTTPS to HTTPS', function () {
|
||||
it('should proxy the request en send back the response', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
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);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
var proxy = httpProxy.createProxyServer({
|
||||
target: 'https://127.0.0.1:' + ports.source,
|
||||
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);
|
||||
|
||||
https.request({
|
||||
host: 'localhost',
|
||||
port: ports.proxy,
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
rejectUnauthorized: false
|
||||
}, function(res) {
|
||||
expect(res.statusCode).to.eql(200);
|
||||
|
||||
res.on('data', function (data) {
|
||||
expect(data.toString()).to.eql('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
source.close();
|
||||
proxy.close();
|
||||
done();
|
||||
})
|
||||
}).end();
|
||||
})
|
||||
});
|
||||
describe('HTTPS not allow SSL self signed', function () {
|
||||
it('should fail with error', function (done) {
|
||||
var ports = { source: gen.port, proxy: gen.port };
|
||||
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({
|
||||
target: 'https://127.0.0.1:' + ports.source,
|
||||
secure: true
|
||||
});
|
||||
|
||||
proxy.listen(ports.proxy);
|
||||
|
||||
proxy.on('error', function (err, req, res) {
|
||||
expect(err).to.be.an(Error);
|
||||
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();
|
||||
})
|
||||
|
||||
http.request({
|
||||
hostname: '127.0.0.1',
|
||||
port: ports.proxy,
|
||||
method: 'GET'
|
||||
}).end();
|
||||
})
|
||||
})
|
||||
describe('HTTPS to HTTP using own server', 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) {
|
||||
expect(req.method).to.eql('GET');
|
||||
expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
source.listen(ports.source);
|
||||
|
||||
var proxy = httpProxy.createServer({
|
||||
agent: new http.Agent({ maxSockets: 2 })
|
||||
});
|
||||
|
||||
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
|
||||
})
|
||||
}).listen(ports.proxy);
|
||||
|
||||
https.request({
|
||||
host: 'localhost',
|
||||
port: ports.proxy,
|
||||
path: '/',
|
||||
method: 'GET',
|
||||
rejectUnauthorized: false
|
||||
}, function(res) {
|
||||
expect(res.statusCode).to.eql(200);
|
||||
|
||||
res.on('data', function (data) {
|
||||
expect(data.toString()).to.eql('Hello from ' + ports.source);
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
source.close();
|
||||
ownServer.close();
|
||||
done();
|
||||
})
|
||||
}).end();
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
@ -1,147 +0,0 @@
|
||||
/*
|
||||
node-http-proxy-test.js: http proxy for node.js
|
||||
|
||||
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
|
||||
|
||||
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 vows = require('vows'),
|
||||
sys = require('sys'),
|
||||
assert = require('assert'),
|
||||
http = require('http');
|
||||
|
||||
var httpProxy = require('./../lib/node-http-proxy');
|
||||
var testServers = {};
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server
|
||||
//
|
||||
var startProxyServer = function (port, server) {
|
||||
var proxyServer = httpProxy.createServer(port, server);
|
||||
proxyServer.listen(8080);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// Creates the reverse proxy server with a specified latency
|
||||
//
|
||||
var startLatentProxyServer = function (port, server, latency) {
|
||||
// Initialize the nodeProxy and start proxying the request
|
||||
var proxyServer = httpProxy.createServer(function (req, res, proxy) {
|
||||
setTimeout(function () {
|
||||
proxy(port, server);
|
||||
}, latency);
|
||||
});
|
||||
|
||||
proxyServer.listen(8081);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// Creates the 'hellonode' server
|
||||
//
|
||||
var startTargetServer = function (port) {
|
||||
var targetServer = http.createServer(function (req, res) {
|
||||
res.writeHead(200, {'Content-Type': 'text/plain'});
|
||||
res.write('hello world')
|
||||
res.end();
|
||||
});
|
||||
|
||||
targetServer.listen(port);
|
||||
return targetServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The default test bootstrapper with no latency
|
||||
//
|
||||
var startTest = function (port) {
|
||||
var proxyServer = startProxyServer(port, 'localhost'),
|
||||
targetServer = startTargetServer(port);
|
||||
|
||||
testServers.noLatency = [];
|
||||
testServers.noLatency.push(proxyServer);
|
||||
testServers.noLatency.push(targetServer);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//
|
||||
// The test bootstrapper with some latency
|
||||
//
|
||||
var startTestWithLatency = function (port) {
|
||||
var proxyServer = startLatentProxyServer(port, 'localhost', 2000),
|
||||
targetServer = startTargetServer(port);
|
||||
|
||||
testServers.latency = [];
|
||||
testServers.latency.push(proxyServer);
|
||||
testServers.latency.push(targetServer);
|
||||
return proxyServer;
|
||||
};
|
||||
|
||||
//var proxy = startTest(8082);
|
||||
//var latent = startTestWithLatency(8083);
|
||||
|
||||
vows.describe('node-http-proxy').addBatch({
|
||||
"A node-http-proxy": {
|
||||
"when instantiated directly": {
|
||||
"and an incoming request is proxied to the helloNode server" : {
|
||||
"with no latency" : {
|
||||
topic: function () {
|
||||
var proxyServer = startTest(8082);
|
||||
proxyServer.on('proxy', this.callback);
|
||||
|
||||
var client = http.createClient(8080, 'localhost');
|
||||
var request = client.request('GET', '/');
|
||||
request.end();
|
||||
},
|
||||
teardown: function () {
|
||||
|
||||
},
|
||||
"it should received 'hello world'": function (err, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
testServers.noLatency.forEach(function (server) {
|
||||
server.close();
|
||||
});
|
||||
}
|
||||
},
|
||||
"with latency": {
|
||||
topic: function () {
|
||||
var proxyServer = startTestWithLatency(8083);
|
||||
proxyServer.on('proxy', this.callback);
|
||||
|
||||
var client = http.createClient(8081, 'localhost');
|
||||
var request = client.request('GET', '/');
|
||||
request.end();
|
||||
},
|
||||
teardown: function () {
|
||||
|
||||
},
|
||||
"it should receive 'hello world'": function (err, body) {
|
||||
assert.equal(body, 'hello world');
|
||||
testServers.latency.forEach(function (server) {
|
||||
server.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}).export(module);
|
||||
Loading…
x
Reference in New Issue
Block a user