OLD | NEW |
1 // Copyright (c) 2013, 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import "dart:async"; | 6 import "dart:async"; |
7 import 'dart:crypto'; | 7 import 'dart:crypto'; |
8 import "dart:io"; | 8 import "dart:io"; |
9 import "dart:uri"; | 9 import "dart:uri"; |
10 import 'dart:utf'; | 10 import 'dart:utf'; |
11 | 11 |
12 class Server { | 12 class Server { |
13 HttpServer server; | 13 HttpServer server; |
14 bool secure; | 14 bool secure; |
15 int proxyHops; | 15 int proxyHops; |
16 List<String> directRequestPaths; | 16 List<String> directRequestPaths; |
17 int requestCount = 0; | 17 int requestCount = 0; |
18 | 18 |
19 Server(this.proxyHops, this.directRequestPaths, this.secure); | 19 Server(this.proxyHops, this.directRequestPaths, this.secure); |
20 | 20 |
21 Future<Server> start() { | 21 Future<Server> start() { |
22 var x = new Completer(); | 22 var x = new Completer(); |
23 Future f = secure | 23 Future f = secure |
24 ? HttpServer.bindSecure( | 24 ? HttpServer.bindSecure( |
25 "127.0.0.1", 0, certificateName: 'localhost_cert') | 25 "localhost", 0, certificateName: 'localhost_cert') |
26 : HttpServer.bind(); | 26 : HttpServer.bind("localhost"); |
27 return f.then((s) { | 27 return f.then((s) { |
28 server = s; | 28 server = s; |
29 x.complete(this); | 29 x.complete(this); |
30 server.listen((request) { | 30 server.listen((request) { |
31 var response = request.response; | 31 var response = request.response; |
32 requestCount++; | 32 requestCount++; |
33 // Check whether a proxy or direct connection is expected. | 33 // Check whether a proxy or direct connection is expected. |
34 bool direct = directRequestPaths.fold( | 34 bool direct = directRequestPaths.fold( |
35 false, | 35 false, |
36 (prev, path) => prev ? prev : path == request.uri.path); | 36 (prev, path) => prev ? prev : path == request.uri.path); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 var response = request.response; | 90 var response = request.response; |
91 response.headers.set(HttpHeaders.PROXY_AUTHENTICATE, | 91 response.headers.set(HttpHeaders.PROXY_AUTHENTICATE, |
92 "Basic, realm=realm"); | 92 "Basic, realm=realm"); |
93 response.statusCode = HttpStatus.PROXY_AUTHENTICATION_REQUIRED; | 93 response.statusCode = HttpStatus.PROXY_AUTHENTICATION_REQUIRED; |
94 response.close(); | 94 response.close(); |
95 }); | 95 }); |
96 } | 96 } |
97 | 97 |
98 Future<ProxyServer> start() { | 98 Future<ProxyServer> start() { |
99 var x = new Completer(); | 99 var x = new Completer(); |
100 HttpServer.bind().then((s) { | 100 HttpServer.bind("localhost").then((s) { |
101 server = s; | 101 server = s; |
102 x.complete(this); | 102 x.complete(this); |
103 server.listen((HttpRequest request) { | 103 server.listen((HttpRequest request) { |
104 requestCount++; | 104 requestCount++; |
105 if (username != null && password != null) { | 105 if (username != null && password != null) { |
106 if (request.headers[HttpHeaders.PROXY_AUTHORIZATION] == null) { | 106 if (request.headers[HttpHeaders.PROXY_AUTHORIZATION] == null) { |
107 authenticationRequired(request); | 107 authenticationRequired(request); |
108 return; | 108 return; |
109 } else { | 109 } else { |
110 Expect.equals( | 110 Expect.equals( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 List<String> proxy = | 189 List<String> proxy = |
190 ["DIRECT", " DIRECT ", "DIRECT ;", " DIRECT ; ", | 190 ["DIRECT", " DIRECT ", "DIRECT ;", " DIRECT ; ", |
191 ";DIRECT", " ; DIRECT ", ";;DIRECT;;"]; | 191 ";DIRECT", " ; DIRECT ", ";;DIRECT;;"]; |
192 | 192 |
193 client.findProxy = (Uri uri) { | 193 client.findProxy = (Uri uri) { |
194 int index = int.parse(uri.path.substring(1)); | 194 int index = int.parse(uri.path.substring(1)); |
195 return proxy[index]; | 195 return proxy[index]; |
196 }; | 196 }; |
197 | 197 |
198 for (int i = 0; i < proxy.length; i++) { | 198 for (int i = 0; i < proxy.length; i++) { |
199 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")) | 199 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) |
200 .then((HttpClientRequest clientRequest) { | 200 .then((HttpClientRequest clientRequest) { |
201 String content = "$i$i$i"; | 201 String content = "$i$i$i"; |
202 clientRequest.contentLength = content.length; | 202 clientRequest.contentLength = content.length; |
203 clientRequest.write(content); | 203 clientRequest.write(content); |
204 return clientRequest.close(); | 204 return clientRequest.close(); |
205 }) | 205 }) |
206 .then((HttpClientResponse response) { | 206 .then((HttpClientResponse response) { |
207 response.listen((_) {}, onDone: () { | 207 response.listen((_) {}, onDone: () { |
208 testDirectDoneCount++; | 208 testDirectDoneCount++; |
209 if (testDirectDoneCount == proxy.length) { | 209 if (testDirectDoneCount == proxy.length) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 client.findProxy = (Uri uri) { | 247 client.findProxy = (Uri uri) { |
248 // Pick the proxy configuration based on the request path. | 248 // Pick the proxy configuration based on the request path. |
249 int index = int.parse(uri.path.substring(1)); | 249 int index = int.parse(uri.path.substring(1)); |
250 return proxy[index]; | 250 return proxy[index]; |
251 }; | 251 }; |
252 | 252 |
253 for (int i = 0; i < proxy.length; i++) { | 253 for (int i = 0; i < proxy.length; i++) { |
254 test(bool secure) { | 254 test(bool secure) { |
255 String url = secure | 255 String url = secure |
256 ? "https://localhost:${secureServer.port}/$i" | 256 ? "https://localhost:${secureServer.port}/$i" |
257 : "http://127.0.0.1:${server.port}/$i"; | 257 : "http://localhost:${server.port}/$i"; |
258 | 258 |
259 client.postUrl(Uri.parse(url)) | 259 client.postUrl(Uri.parse(url)) |
260 .then((HttpClientRequest clientRequest) { | 260 .then((HttpClientRequest clientRequest) { |
261 String content = "$i$i$i"; | 261 String content = "$i$i$i"; |
262 clientRequest.write(content); | 262 clientRequest.write(content); |
263 return clientRequest.close(); | 263 return clientRequest.close(); |
264 }) | 264 }) |
265 .then((HttpClientResponse response) { | 265 .then((HttpClientResponse response) { |
266 response.listen((_) {}, onDone: () { | 266 response.listen((_) {}, onDone: () { |
267 testProxyDoneCount++; | 267 testProxyDoneCount++; |
(...skipping 15 matching lines...) Expand all Loading... |
283 }); | 283 }); |
284 }); | 284 }); |
285 }); | 285 }); |
286 } | 286 } |
287 | 287 |
288 int testProxyChainDoneCount = 0; | 288 int testProxyChainDoneCount = 0; |
289 void testProxyChain() { | 289 void testProxyChain() { |
290 // Setup two proxy servers having the first using the second as its proxy. | 290 // Setup two proxy servers having the first using the second as its proxy. |
291 setupProxyServer().then((proxyServer1) { | 291 setupProxyServer().then((proxyServer1) { |
292 setupProxyServer().then((proxyServer2) { | 292 setupProxyServer().then((proxyServer2) { |
293 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; | 293 proxyServer1.client.findProxy = (_) => "PROXY localhost:${proxyServer2.port}"; |
294 | 294 |
295 setupServer(2, directRequestPaths: ["/4"]).then((server) { | 295 setupServer(2, directRequestPaths: ["/4"]).then((server) { |
296 HttpClient client = new HttpClient(); | 296 HttpClient client = new HttpClient(); |
297 | 297 |
298 List<String> proxy; | 298 List<String> proxy; |
299 if (Platform.operatingSystem == "windows") { | 299 if (Platform.operatingSystem == "windows") { |
300 proxy = | 300 proxy = |
301 ["PROXY localhost:${proxyServer1.port}", | 301 ["PROXY localhost:${proxyServer1.port}", |
302 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", | 302 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", |
303 "PROXY localhost:${proxyServer1.port}", | 303 "PROXY localhost:${proxyServer1.port}", |
(...skipping 11 matching lines...) Expand all Loading... |
315 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | 315 "PROXY localhost:${proxyServer1.port}; DIRECT"]; |
316 } | 316 } |
317 | 317 |
318 client.findProxy = (Uri uri) { | 318 client.findProxy = (Uri uri) { |
319 // Pick the proxy configuration based on the request path. | 319 // Pick the proxy configuration based on the request path. |
320 int index = int.parse(uri.path.substring(1)); | 320 int index = int.parse(uri.path.substring(1)); |
321 return proxy[index]; | 321 return proxy[index]; |
322 }; | 322 }; |
323 | 323 |
324 for (int i = 0; i < proxy.length; i++) { | 324 for (int i = 0; i < proxy.length; i++) { |
325 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")) | 325 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) |
326 .then((HttpClientRequest clientRequest) { | 326 .then((HttpClientRequest clientRequest) { |
327 String content = "$i$i$i"; | 327 String content = "$i$i$i"; |
328 clientRequest.contentLength = content.length; | 328 clientRequest.contentLength = content.length; |
329 clientRequest.write(content); | 329 clientRequest.write(content); |
330 return clientRequest.close(); | 330 return clientRequest.close(); |
331 }) | 331 }) |
332 .then((HttpClientResponse response) { | 332 .then((HttpClientResponse response) { |
333 response.listen((_) {}, onDone: () { | 333 response.listen((_) {}, onDone: () { |
334 testProxyChainDoneCount++; | 334 testProxyChainDoneCount++; |
335 if (testProxyChainDoneCount == proxy.length) { | 335 if (testProxyChainDoneCount == proxy.length) { |
(...skipping 23 matching lines...) Expand all Loading... |
359 uri, | 359 uri, |
360 environment: {"http_proxy": "localhost:${proxyServer.port}", | 360 environment: {"http_proxy": "localhost:${proxyServer.port}", |
361 "https_proxy": "localhost:${proxyServer.port}"}); | 361 "https_proxy": "localhost:${proxyServer.port}"}); |
362 }; | 362 }; |
363 | 363 |
364 const int loopCount = 5; | 364 const int loopCount = 5; |
365 for (int i = 0; i < loopCount; i++) { | 365 for (int i = 0; i < loopCount; i++) { |
366 test(bool secure) { | 366 test(bool secure) { |
367 String url = secure | 367 String url = secure |
368 ? "https://localhost:${secureServer.port}/$i" | 368 ? "https://localhost:${secureServer.port}/$i" |
369 : "http://127.0.0.1:${server.port}/$i"; | 369 : "http://localhost:${server.port}/$i"; |
370 | 370 |
371 client.postUrl(Uri.parse(url)) | 371 client.postUrl(Uri.parse(url)) |
372 .then((HttpClientRequest clientRequest) { | 372 .then((HttpClientRequest clientRequest) { |
373 String content = "$i$i$i"; | 373 String content = "$i$i$i"; |
374 clientRequest.write(content); | 374 clientRequest.write(content); |
375 return clientRequest.close(); | 375 return clientRequest.close(); |
376 }) | 376 }) |
377 .then((HttpClientResponse response) { | 377 .then((HttpClientResponse response) { |
378 response.listen((_) {}, onDone: () { | 378 response.listen((_) {}, onDone: () { |
379 testProxyFromEnviromentDoneCount++; | 379 testProxyFromEnviromentDoneCount++; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 // Test with no authentication. | 413 // Test with no authentication. |
414 client.findProxy = (Uri uri) { | 414 client.findProxy = (Uri uri) { |
415 return "PROXY localhost:${proxyServer.port}"; | 415 return "PROXY localhost:${proxyServer.port}"; |
416 }; | 416 }; |
417 | 417 |
418 const int loopCount = 2; | 418 const int loopCount = 2; |
419 for (int i = 0; i < loopCount; i++) { | 419 for (int i = 0; i < loopCount; i++) { |
420 test(bool secure) { | 420 test(bool secure) { |
421 String url = secure | 421 String url = secure |
422 ? "https://localhost:${secureServer.port}/$i" | 422 ? "https://localhost:${secureServer.port}/$i" |
423 : "http://127.0.0.1:${server.port}/$i"; | 423 : "http://localhost:${server.port}/$i"; |
424 | 424 |
425 client.postUrl(Uri.parse(url)) | 425 client.postUrl(Uri.parse(url)) |
426 .then((HttpClientRequest clientRequest) { | 426 .then((HttpClientRequest clientRequest) { |
427 String content = "$i$i$i"; | 427 String content = "$i$i$i"; |
428 clientRequest.write(content); | 428 clientRequest.write(content); |
429 return clientRequest.close(); | 429 return clientRequest.close(); |
430 }) | 430 }) |
431 .then((HttpClientResponse response) { | 431 .then((HttpClientResponse response) { |
432 response.listen((_) {}, onDone: () { | 432 response.listen((_) {}, onDone: () { |
433 testProxyAuthenticateCount++; | 433 testProxyAuthenticateCount++; |
(...skipping 15 matching lines...) Expand all Loading... |
449 step1.future.then((_) { | 449 step1.future.then((_) { |
450 testProxyAuthenticateCount = 0; | 450 testProxyAuthenticateCount = 0; |
451 client.findProxy = (Uri uri) { | 451 client.findProxy = (Uri uri) { |
452 return "PROXY test:test@localhost:${proxyServer.port}"; | 452 return "PROXY test:test@localhost:${proxyServer.port}"; |
453 }; | 453 }; |
454 | 454 |
455 for (int i = 0; i < loopCount; i++) { | 455 for (int i = 0; i < loopCount; i++) { |
456 test(bool secure) { | 456 test(bool secure) { |
457 String url = secure | 457 String url = secure |
458 ? "https://localhost:${secureServer.port}/$i" | 458 ? "https://localhost:${secureServer.port}/$i" |
459 : "http://127.0.0.1:${server.port}/$i"; | 459 : "http://localhost:${server.port}/$i"; |
460 | 460 |
461 client.postUrl(Uri.parse(url)) | 461 client.postUrl(Uri.parse(url)) |
462 .then((HttpClientRequest clientRequest) { | 462 .then((HttpClientRequest clientRequest) { |
463 String content = "$i$i$i"; | 463 String content = "$i$i$i"; |
464 clientRequest.write(content); | 464 clientRequest.write(content); |
465 return clientRequest.close(); | 465 return clientRequest.close(); |
466 }) | 466 }) |
467 .then((HttpClientResponse response) { | 467 .then((HttpClientResponse response) { |
468 response.listen((_) {}, onDone: () { | 468 response.listen((_) {}, onDone: () { |
469 testProxyAuthenticateCount++; | 469 testProxyAuthenticateCount++; |
(...skipping 24 matching lines...) Expand all Loading... |
494 proxyServer.port, | 494 proxyServer.port, |
495 "realm", | 495 "realm", |
496 new HttpClientBasicCredentials("test", "test")); | 496 new HttpClientBasicCredentials("test", "test")); |
497 return new Future.value(true); | 497 return new Future.value(true); |
498 }; | 498 }; |
499 | 499 |
500 for (int i = 0; i < loopCount; i++) { | 500 for (int i = 0; i < loopCount; i++) { |
501 test(bool secure) { | 501 test(bool secure) { |
502 String url = secure | 502 String url = secure |
503 ? "https://localhost:${secureServer.port}/A" | 503 ? "https://localhost:${secureServer.port}/A" |
504 : "http://127.0.0.1:${server.port}/A"; | 504 : "http://localhost:${server.port}/A"; |
505 | 505 |
506 client.postUrl(Uri.parse(url)) | 506 client.postUrl(Uri.parse(url)) |
507 .then((HttpClientRequest clientRequest) { | 507 .then((HttpClientRequest clientRequest) { |
508 String content = "$i$i$i"; | 508 String content = "$i$i$i"; |
509 clientRequest.write(content); | 509 clientRequest.write(content); |
510 return clientRequest.close(); | 510 return clientRequest.close(); |
511 }) | 511 }) |
512 .then((HttpClientResponse response) { | 512 .then((HttpClientResponse response) { |
513 response.listen((_) {}, onDone: () { | 513 response.listen((_) {}, onDone: () { |
514 testProxyAuthenticateCount++; | 514 testProxyAuthenticateCount++; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 "PROXY hede.hule.hest:8080; PROXY localhost:8080", | 549 "PROXY hede.hule.hest:8080; PROXY localhost:8080", |
550 "PROXY localhost:8080; DIRECT"]; | 550 "PROXY localhost:8080; DIRECT"]; |
551 | 551 |
552 client.findProxy = (Uri uri) { | 552 client.findProxy = (Uri uri) { |
553 // Pick the proxy configuration based on the request path. | 553 // Pick the proxy configuration based on the request path. |
554 int index = int.parse(uri.path.substring(1)); | 554 int index = int.parse(uri.path.substring(1)); |
555 return proxy[index]; | 555 return proxy[index]; |
556 }; | 556 }; |
557 | 557 |
558 for (int i = 0; i < proxy.length; i++) { | 558 for (int i = 0; i < proxy.length; i++) { |
559 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")) | 559 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) |
560 .then((HttpClientRequest clientRequest) { | 560 .then((HttpClientRequest clientRequest) { |
561 String content = "$i$i$i"; | 561 String content = "$i$i$i"; |
562 clientRequest.contentLength = content.length; | 562 clientRequest.contentLength = content.length; |
563 clientRequest.write(content); | 563 clientRequest.write(content); |
564 return clientRequest.close(); | 564 return clientRequest.close(); |
565 }) | 565 }) |
566 .then((HttpClientResponse response) { | 566 .then((HttpClientResponse response) { |
567 response.listen((_) {}, onDone: () { | 567 response.listen((_) {}, onDone: () { |
568 if (++testRealProxyDoneCount == proxy.length) { | 568 if (++testRealProxyDoneCount == proxy.length) { |
569 Expect.equals(proxy.length, server.requestCount); | 569 Expect.equals(proxy.length, server.requestCount); |
(...skipping 17 matching lines...) Expand all Loading... |
587 "PROXY hede.hule.hest:8080; PROXY test:test@localhost:8080", | 587 "PROXY hede.hule.hest:8080; PROXY test:test@localhost:8080", |
588 "PROXY test:test@localhost:8080; DIRECT"]; | 588 "PROXY test:test@localhost:8080; DIRECT"]; |
589 | 589 |
590 client.findProxy = (Uri uri) { | 590 client.findProxy = (Uri uri) { |
591 // Pick the proxy configuration based on the request path. | 591 // Pick the proxy configuration based on the request path. |
592 int index = int.parse(uri.path.substring(1)); | 592 int index = int.parse(uri.path.substring(1)); |
593 return proxy[index]; | 593 return proxy[index]; |
594 }; | 594 }; |
595 | 595 |
596 for (int i = 0; i < proxy.length; i++) { | 596 for (int i = 0; i < proxy.length; i++) { |
597 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")) | 597 client.getUrl(Uri.parse("http://localhost:${server.port}/$i")) |
598 .then((HttpClientRequest clientRequest) { | 598 .then((HttpClientRequest clientRequest) { |
599 String content = "$i$i$i"; | 599 String content = "$i$i$i"; |
600 clientRequest.contentLength = content.length; | 600 clientRequest.contentLength = content.length; |
601 clientRequest.write(content); | 601 clientRequest.write(content); |
602 return clientRequest.close(); | 602 return clientRequest.close(); |
603 }) | 603 }) |
604 .then((HttpClientResponse response) { | 604 .then((HttpClientResponse response) { |
605 response.listen((_) {}, onDone: () { | 605 response.listen((_) {}, onDone: () { |
606 if (++testRealProxyAuthDoneCount == proxy.length) { | 606 if (++testRealProxyAuthDoneCount == proxy.length) { |
607 Expect.equals(proxy.length, server.requestCount); | 607 Expect.equals(proxy.length, server.requestCount); |
(...skipping 19 matching lines...) Expand all Loading... |
627 testDirectProxy(); | 627 testDirectProxy(); |
628 testProxy(); | 628 testProxy(); |
629 testProxyChain(); | 629 testProxyChain(); |
630 testProxyFromEnviroment(); | 630 testProxyFromEnviroment(); |
631 testProxyAuthenticate(); | 631 testProxyAuthenticate(); |
632 // This test is not normally run. It can be used for locally testing | 632 // This test is not normally run. It can be used for locally testing |
633 // with a real proxy server (e.g. Apache). | 633 // with a real proxy server (e.g. Apache). |
634 //testRealProxy(); | 634 //testRealProxy(); |
635 //testRealProxyAuth(); | 635 //testRealProxyAuth(); |
636 } | 636 } |
OLD | NEW |