1 | var common = exports,
|
2 | url = require('url'),
|
3 | extend = require('util')._extend,
|
4 | required = require('requires-port');
|
5 |
|
6 | var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
|
7 | isSSL = /^https|wss/,
|
8 | cookieDomainRegex = /(;\s*domain=)([^;]+)/i;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | common.isSSL = isSSL;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | common.setupOutgoing = function(outgoing, options, req) {
|
34 | outgoing.port = options['target'].port ||
|
35 | (isSSL.test(options['target'].protocol) ? 443 : 80);
|
36 |
|
37 | ['host', 'hostname', 'socketPath', 'pfx', 'key',
|
38 | 'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
|
39 | function(e) { outgoing[e] = options['target'][e]; }
|
40 | );
|
41 |
|
42 | outgoing.method = req.method;
|
43 | outgoing.headers = extend({}, req.headers);
|
44 |
|
45 | if (options.headers){
|
46 | extend(outgoing.headers, options.headers);
|
47 | }
|
48 |
|
49 | if (options.auth) {
|
50 | outgoing.auth = options.auth;
|
51 | }
|
52 |
|
53 | if (options.ca) {
|
54 | outgoing.ca = options.ca;
|
55 | }
|
56 |
|
57 | if (isSSL.test(options['target'].protocol)) {
|
58 | outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
|
59 | }
|
60 |
|
61 |
|
62 | outgoing.agent = options.agent || false;
|
63 | outgoing.localAddress = options.localAddress;
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | if (!outgoing.agent) {
|
70 | outgoing.headers = outgoing.headers || {};
|
71 | if (typeof outgoing.headers.connection !== 'string'
|
72 | || !upgradeHeader.test(outgoing.headers.connection)
|
73 | ) { outgoing.headers.connection = 'close'; }
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | var target = options['target'];
|
79 | var targetPath = target && options.prependPath !== false
|
80 | ? (target.path || '')
|
81 | : '';
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | var outgoingPath = !options.toProxy
|
87 | ? (url.parse(req.url).path || '')
|
88 | : req.url;
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | outgoingPath = !options.ignorePath ? outgoingPath : '';
|
96 |
|
97 | outgoing.path = common.urlJoin(targetPath, outgoingPath);
|
98 |
|
99 | if (options.changeOrigin) {
|
100 | outgoing.headers.host =
|
101 | required(outgoing.port, options['target'].protocol) && !hasPort(outgoing.host)
|
102 | ? outgoing.host + ':' + outgoing.port
|
103 | : outgoing.host;
|
104 | }
|
105 | return outgoing;
|
106 | };
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | common.setupSocket = function(socket) {
|
126 | socket.setTimeout(0);
|
127 | socket.setNoDelay(true);
|
128 |
|
129 | socket.setKeepAlive(true, 0);
|
130 |
|
131 | return socket;
|
132 | };
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | common.getPort = function(req) {
|
144 | var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
|
145 |
|
146 | return res ?
|
147 | res[1] :
|
148 | common.hasEncryptedConnection(req) ? '443' : '80';
|
149 | };
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | common.hasEncryptedConnection = function(req) {
|
161 | return Boolean(req.connection.encrypted || req.connection.pair);
|
162 | };
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | common.urlJoin = function() {
|
173 |
|
174 |
|
175 |
|
176 | var args = Array.prototype.slice.call(arguments),
|
177 | lastIndex = args.length - 1,
|
178 | last = args[lastIndex],
|
179 | lastSegs = last.split('?'),
|
180 | retSegs;
|
181 |
|
182 | args[lastIndex] = lastSegs.shift();
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | retSegs = [
|
189 | args.filter(Boolean).join('/')
|
190 | .replace(/\/+/g, '/')
|
191 | .replace('http:/', 'http://')
|
192 | .replace('https:/', 'https://')
|
193 | ];
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | retSegs.push.apply(retSegs, lastSegs);
|
200 |
|
201 | return retSegs.join('?')
|
202 | };
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | common.rewriteCookieDomain = function rewriteCookieDomain(header, config) {
|
214 | if (Array.isArray(header)) {
|
215 | return header.map(function (headerElement) {
|
216 | return rewriteCookieDomain(headerElement, config);
|
217 | });
|
218 | }
|
219 | return header.replace(cookieDomainRegex, function(match, prefix, previousDomain) {
|
220 | var newDomain;
|
221 | if (previousDomain in config) {
|
222 | newDomain = config[previousDomain];
|
223 | } else if ('*' in config) {
|
224 | newDomain = config['*'];
|
225 | } else {
|
226 |
|
227 | return match;
|
228 | }
|
229 | if (newDomain) {
|
230 |
|
231 | return prefix + newDomain;
|
232 | } else {
|
233 |
|
234 | return '';
|
235 | }
|
236 | });
|
237 | };
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | function hasPort(host) {
|
247 | return !!~host.indexOf(':');
|
248 | };
|