OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import "dart:io"; | 5 import "dart:io"; |
6 import "dart:uri"; | 6 import "dart:uri"; |
7 | 7 |
8 class Server { | 8 class Server { |
9 HttpServer server; | 9 HttpServer server; |
10 bool secure; | |
10 int proxyHops; | 11 int proxyHops; |
11 List<String> directRequestPaths; | 12 List<String> directRequestPaths; |
12 int requestCount = 0; | 13 int requestCount = 0; |
13 | 14 |
14 Server(this.proxyHops, this.directRequestPaths) : server = new HttpServer(); | 15 Server(this.proxyHops, this.directRequestPaths, this.secure) { |
16 server = secure ? new HttpsServer() : new HttpServer(); | |
17 } | |
15 | 18 |
16 void start() { | 19 void start() { |
17 server.listen("127.0.0.1", 0); | 20 server.listen("127.0.0.1", 0, certificate_name: 'CN=localhost'); |
18 server.defaultRequestHandler = | 21 server.defaultRequestHandler = |
19 (HttpRequest request, HttpResponse response) { | 22 (HttpRequest request, HttpResponse response) { |
20 requestCount++; | 23 requestCount++; |
21 // Check whether a proxy or direct connection is expected. | 24 // Check whether a proxy or direct connection is expected. |
22 bool direct = directRequestPaths.reduce( | 25 bool direct = directRequestPaths.reduce( |
23 false, | 26 false, |
24 (prev, path) => prev ? prev : path == request.path); | 27 (prev, path) => prev ? prev : path == request.path); |
25 if (!direct && proxyHops > 0) { | 28 if (!direct && proxyHops > 0) { |
26 Expect.isNotNull(request.headers[HttpHeaders.VIA]); | 29 Expect.isNotNull(request.headers[HttpHeaders.VIA]); |
27 Expect.equals(1, request.headers[HttpHeaders.VIA].length); | 30 Expect.equals(1, request.headers[HttpHeaders.VIA].length); |
(...skipping 17 matching lines...) Expand all Loading... | |
45 } | 48 } |
46 | 49 |
47 void shutdown() { | 50 void shutdown() { |
48 server.close(); | 51 server.close(); |
49 } | 52 } |
50 | 53 |
51 int get port => server.port; | 54 int get port => server.port; |
52 } | 55 } |
53 | 56 |
54 Server setupServer(int proxyHops, | 57 Server setupServer(int proxyHops, |
55 [List<String> directRequestPaths = const <String>[]]) { | 58 {List<String> directRequestPaths: const <String>[], |
56 Server server = new Server(proxyHops, directRequestPaths); | 59 secure: false}) { |
60 Server server = new Server(proxyHops, directRequestPaths, secure); | |
57 server.start(); | 61 server.start(); |
58 return server; | 62 return server; |
59 } | 63 } |
60 | 64 |
61 class ProxyServer { | 65 class ProxyServer { |
62 HttpServer server; | 66 HttpServer server; |
63 HttpClient client; | 67 HttpClient client; |
64 int requestCount = 0; | 68 int requestCount = 0; |
65 | 69 |
66 ProxyServer() : server = new HttpServer(), client = new HttpClient(); | 70 ProxyServer() : server = new HttpServer(), client = new HttpClient(); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 client.shutdown(); | 168 client.shutdown(); |
165 } | 169 } |
166 }; | 170 }; |
167 }; | 171 }; |
168 } | 172 } |
169 } | 173 } |
170 | 174 |
171 int testProxyDoneCount = 0; | 175 int testProxyDoneCount = 0; |
172 void testProxy() { | 176 void testProxy() { |
173 ProxyServer proxyServer = setupProxyServer(); | 177 ProxyServer proxyServer = setupProxyServer(); |
174 Server server = setupServer(1, ["/4"]); | 178 Server server = setupServer(1, directRequestPaths: ["/4"]); |
179 Server secureServer = setupServer(1, directRequestPaths: ["/4"], secure: true) ; | |
175 HttpClient client = new HttpClient(); | 180 HttpClient client = new HttpClient(); |
176 | 181 |
177 List<String> proxy = | 182 List<String> proxy = |
178 ["PROXY localhost:${proxyServer.port}", | 183 ["PROXY localhost:${proxyServer.port}", |
179 "PROXY localhost:${proxyServer.port}; PROXY hede.hule.hest:8080", | 184 "PROXY localhost:${proxyServer.port}; PROXY hede.hule.hest:8080", |
180 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer.port}", | 185 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer.port}", |
181 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; PROXY localhost:${ proxyServer.port}", | 186 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; PROXY localhost:${ proxyServer.port}", |
182 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; DIRECT", | 187 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; DIRECT", |
183 "PROXY localhost:${proxyServer.port}; DIRECT"]; | 188 "PROXY localhost:${proxyServer.port}; DIRECT"]; |
184 | 189 |
185 client.findProxy = (Uri uri) { | 190 client.findProxy = (Uri uri) { |
186 // Pick the proxy configuration based on the request path. | 191 // Pick the proxy configuration based on the request path. |
187 int index = int.parse(uri.path.substring(1)); | 192 int index = int.parse(uri.path.substring(1)); |
188 return proxy[index]; | 193 return proxy[index]; |
189 }; | 194 }; |
190 | 195 |
191 for (int i = 0; i < proxy.length; i++) { | 196 for (int i = 0; i < proxy.length; i++) { |
192 HttpClientConnection conn = | 197 test(bool secure) { |
193 client.postUrl( | 198 String url = secure |
194 Uri.parse("http://127.0.0.1:${server.port}/$i")); | 199 ? "https://localhost:${secureServer.port}/$i" |
Anders Johnsen
2013/02/20 14:24:20
localhost vs 127.0.0.1?
Søren Gjesse
2013/02/20 14:35:08
For HTTPS the hostname needs to be there.
| |
195 conn.onRequest = (HttpClientRequest clientRequest) { | 200 : "http://127.0.0.1:${server.port}/$i"; |
196 String content = "$i$i$i"; | 201 |
197 clientRequest.outputStream.writeString(content); | 202 HttpClientConnection conn = |
Anders Johnsen
2013/02/20 14:24:20
One line?
Søren Gjesse
2013/02/20 14:35:08
Done.
| |
198 clientRequest.outputStream.close(); | 203 client.postUrl( |
199 }; | 204 Uri.parse(url)); |
200 conn.onResponse = (HttpClientResponse response) { | 205 conn.onRequest = (HttpClientRequest clientRequest) { |
201 response.inputStream.onData = () => response.inputStream.read(); | 206 String content = "$i$i$i"; |
202 response.inputStream.onClosed = () { | 207 clientRequest.outputStream.writeString(content); |
203 testProxyDoneCount++; | 208 clientRequest.outputStream.close(); |
204 if (testProxyDoneCount == proxy.length) { | |
205 Expect.equals(proxy.length, server.requestCount); | |
206 proxyServer.shutdown(); | |
207 server.shutdown(); | |
208 client.shutdown(); | |
209 } | |
210 }; | 209 }; |
211 }; | 210 conn.onResponse = (HttpClientResponse response) { |
211 response.inputStream.onData = () => response.inputStream.read(); | |
212 response.inputStream.onClosed = () { | |
213 testProxyDoneCount++; | |
214 if (testProxyDoneCount == proxy.length * 2) { | |
215 Expect.equals(proxy.length, server.requestCount); | |
216 proxyServer.shutdown(); | |
217 server.shutdown(); | |
Anders Johnsen
2013/02/20 14:24:20
Don't you only have to shutdown the server matchin
Søren Gjesse
2013/02/20 14:35:08
No, both servers are used in the test. And both ar
| |
218 secureServer.shutdown(); | |
219 client.shutdown(); | |
220 } | |
221 }; | |
222 }; | |
223 } | |
224 | |
225 test(false); | |
226 test(true); | |
212 } | 227 } |
213 } | 228 } |
214 | 229 |
215 int testProxyChainDoneCount = 0; | 230 int testProxyChainDoneCount = 0; |
216 void testProxyChain() { | 231 void testProxyChain() { |
217 // Setup two proxy servers having the first using the second as its proxy. | 232 // Setup two proxy servers having the first using the second as its proxy. |
218 ProxyServer proxyServer1 = setupProxyServer(); | 233 ProxyServer proxyServer1 = setupProxyServer(); |
219 ProxyServer proxyServer2 = setupProxyServer(); | 234 ProxyServer proxyServer2 = setupProxyServer(); |
220 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; | 235 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; |
221 | 236 |
222 Server server = setupServer(2, ["/4"]); | 237 Server server = setupServer(2, directRequestPaths: ["/4"]); |
223 HttpClient client = new HttpClient(); | 238 HttpClient client = new HttpClient(); |
224 | 239 |
225 List<String> proxy = | 240 List<String> proxy = |
226 ["PROXY localhost:${proxyServer1.port}", | 241 ["PROXY localhost:${proxyServer1.port}", |
227 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", | 242 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", |
228 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer1.port}", | 243 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer1.port}", |
229 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; PROXY localhost:${ proxyServer1.port}", | 244 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; PROXY localhost:${ proxyServer1.port}", |
230 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; DIRECT", | 245 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; DIRECT", |
231 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | 246 "PROXY localhost:${proxyServer1.port}; DIRECT"]; |
232 | 247 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 if (testRealProxyDoneCount == proxy.length) { | 309 if (testRealProxyDoneCount == proxy.length) { |
295 Expect.equals(proxy.length, server.requestCount); | 310 Expect.equals(proxy.length, server.requestCount); |
296 server.shutdown(); | 311 server.shutdown(); |
297 client.shutdown(); | 312 client.shutdown(); |
298 } | 313 } |
299 }; | 314 }; |
300 }; | 315 }; |
301 } | 316 } |
302 } | 317 } |
303 | 318 |
319 void InitializeSSL() { | |
320 var testPkcertDatabase = | |
321 new Path(new Options().script).directoryPath.append('pkcert/'); | |
322 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | |
323 password: 'dartdart'); | |
324 } | |
325 | |
304 main() { | 326 main() { |
327 InitializeSSL(); | |
305 testInvalidProxy(); | 328 testInvalidProxy(); |
306 testDirectProxy(); | 329 testDirectProxy(); |
307 testProxy(); | 330 testProxy(); |
308 testProxyChain(); | 331 testProxyChain(); |
309 // This test is not normally run. It can be used for locally testing | 332 // This test is not normally run. It can be used for locally testing |
310 // with a real proxy server (e.g. Apache). | 333 // with a real proxy server (e.g. Apache). |
311 // testRealProxy(); | 334 // testRealProxy(); |
312 } | 335 } |
OLD | NEW |