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" |
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 = client.postUrl(Uri.parse(url)); |
198 clientRequest.outputStream.close(); | 203 conn.onRequest = (HttpClientRequest clientRequest) { |
199 }; | 204 String content = "$i$i$i"; |
200 conn.onResponse = (HttpClientResponse response) { | 205 clientRequest.outputStream.writeString(content); |
201 response.inputStream.onData = () => response.inputStream.read(); | 206 clientRequest.outputStream.close(); |
202 response.inputStream.onClosed = () { | |
203 testProxyDoneCount++; | |
204 if (testProxyDoneCount == proxy.length) { | |
205 Expect.equals(proxy.length, server.requestCount); | |
206 proxyServer.shutdown(); | |
207 server.shutdown(); | |
208 client.shutdown(); | |
209 } | |
210 }; | 207 }; |
211 }; | 208 conn.onResponse = (HttpClientResponse response) { |
| 209 response.inputStream.onData = () => response.inputStream.read(); |
| 210 response.inputStream.onClosed = () { |
| 211 testProxyDoneCount++; |
| 212 if (testProxyDoneCount == proxy.length * 2) { |
| 213 Expect.equals(proxy.length, server.requestCount); |
| 214 proxyServer.shutdown(); |
| 215 server.shutdown(); |
| 216 secureServer.shutdown(); |
| 217 client.shutdown(); |
| 218 } |
| 219 }; |
| 220 }; |
| 221 } |
| 222 |
| 223 test(false); |
| 224 test(true); |
212 } | 225 } |
213 } | 226 } |
214 | 227 |
215 int testProxyChainDoneCount = 0; | 228 int testProxyChainDoneCount = 0; |
216 void testProxyChain() { | 229 void testProxyChain() { |
217 // Setup two proxy servers having the first using the second as its proxy. | 230 // Setup two proxy servers having the first using the second as its proxy. |
218 ProxyServer proxyServer1 = setupProxyServer(); | 231 ProxyServer proxyServer1 = setupProxyServer(); |
219 ProxyServer proxyServer2 = setupProxyServer(); | 232 ProxyServer proxyServer2 = setupProxyServer(); |
220 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; | 233 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; |
221 | 234 |
222 Server server = setupServer(2, ["/4"]); | 235 Server server = setupServer(2, directRequestPaths: ["/4"]); |
223 HttpClient client = new HttpClient(); | 236 HttpClient client = new HttpClient(); |
224 | 237 |
225 List<String> proxy = | 238 List<String> proxy = |
226 ["PROXY localhost:${proxyServer1.port}", | 239 ["PROXY localhost:${proxyServer1.port}", |
227 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", | 240 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", |
228 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer1.port}", | 241 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer1.port}", |
229 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; PROXY localhost:${
proxyServer1.port}", | 242 "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", | 243 "PROXY hede.hule.hest:8080; PROXY hede.hule.hest:8181; DIRECT", |
231 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | 244 "PROXY localhost:${proxyServer1.port}; DIRECT"]; |
232 | 245 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 if (testRealProxyDoneCount == proxy.length) { | 307 if (testRealProxyDoneCount == proxy.length) { |
295 Expect.equals(proxy.length, server.requestCount); | 308 Expect.equals(proxy.length, server.requestCount); |
296 server.shutdown(); | 309 server.shutdown(); |
297 client.shutdown(); | 310 client.shutdown(); |
298 } | 311 } |
299 }; | 312 }; |
300 }; | 313 }; |
301 } | 314 } |
302 } | 315 } |
303 | 316 |
| 317 void InitializeSSL() { |
| 318 var testPkcertDatabase = |
| 319 new Path(new Options().script).directoryPath.append('pkcert/'); |
| 320 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), |
| 321 password: 'dartdart'); |
| 322 } |
| 323 |
304 main() { | 324 main() { |
| 325 InitializeSSL(); |
305 testInvalidProxy(); | 326 testInvalidProxy(); |
306 testDirectProxy(); | 327 testDirectProxy(); |
307 testProxy(); | 328 testProxy(); |
308 testProxyChain(); | 329 testProxyChain(); |
309 // This test is not normally run. It can be used for locally testing | 330 // This test is not normally run. It can be used for locally testing |
310 // with a real proxy server (e.g. Apache). | 331 // with a real proxy server (e.g. Apache). |
311 // testRealProxy(); | 332 // testRealProxy(); |
312 } | 333 } |
OLD | NEW |