| 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'; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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); |
| 37 if (!direct && proxyHops > 0) { | 37 if (!secure && !direct && proxyHops > 0) { |
| 38 Expect.isNotNull(request.headers[HttpHeaders.VIA]); | 38 Expect.isNotNull(request.headers[HttpHeaders.VIA]); |
| 39 Expect.equals(1, request.headers[HttpHeaders.VIA].length); | 39 Expect.equals(1, request.headers[HttpHeaders.VIA].length); |
| 40 Expect.equals( | 40 Expect.equals( |
| 41 proxyHops, | 41 proxyHops, |
| 42 request.headers[HttpHeaders.VIA][0].split(",").length); | 42 request.headers[HttpHeaders.VIA][0].split(",").length); |
| 43 } else { | 43 } else { |
| 44 Expect.isNull(request.headers[HttpHeaders.VIA]); | 44 Expect.isNull(request.headers[HttpHeaders.VIA]); |
| 45 } | 45 } |
| 46 var body = new StringBuffer(); | 46 var body = new StringBuffer(); |
| 47 request.listen( | 47 request.listen( |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 Expect.equals("Basic", tokens[0]); | 115 Expect.equals("Basic", tokens[0]); |
| 116 String auth = | 116 String auth = |
| 117 CryptoUtils.bytesToBase64(encodeUtf8("$username:$password")); | 117 CryptoUtils.bytesToBase64(encodeUtf8("$username:$password")); |
| 118 if (auth != tokens[1]) { | 118 if (auth != tokens[1]) { |
| 119 authenticationRequired(request); | 119 authenticationRequired(request); |
| 120 return; | 120 return; |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 } | 123 } |
| 124 // Open the connection from the proxy. | 124 // Open the connection from the proxy. |
| 125 client.openUrl(request.method, request.uri) | 125 if (request.method == "CONNECT") { |
| 126 .then((HttpClientRequest clientRequest) { | 126 var tmp = request.uri.toString().split(":"); |
| 127 // Forward all headers. | 127 Socket.connect(tmp[0], int.parse(tmp[1])) |
| 128 request.headers.forEach((String name, List<String> values) { | 128 .then((socket) { |
| 129 values.forEach((String value) { | 129 request.response.reasonPhrase = "Connection established"; |
| 130 if (name != "content-length" && name != "via") { | 130 request.response.detachSocket() |
| 131 clientRequest.headers.add(name, value); | 131 .then((detached) { |
| 132 } | 132 socket.pipe(detached); |
| 133 detached.pipe(socket); |
| 134 }); |
| 133 }); | 135 }); |
| 136 } else { |
| 137 client.openUrl(request.method, request.uri) |
| 138 .then((HttpClientRequest clientRequest) { |
| 139 // Forward all headers. |
| 140 request.headers.forEach((String name, List<String> values) { |
| 141 values.forEach((String value) { |
| 142 if (name != "content-length" && name != "via") { |
| 143 clientRequest.headers.add(name, value); |
| 144 } |
| 145 }); |
| 146 }); |
| 147 // Special handling of Content-Length and Via. |
| 148 clientRequest.contentLength = request.contentLength; |
| 149 List<String> via = request.headers[HttpHeaders.VIA]; |
| 150 String viaPrefix = via == null ? "" : "${via[0]}, "; |
| 151 clientRequest.headers.add( |
| 152 HttpHeaders.VIA, "${viaPrefix}1.1 localhost:$port"); |
| 153 // Copy all content. |
| 154 return request.pipe(clientRequest); |
| 155 }) |
| 156 .then((HttpClientResponse clientResponse) { |
| 157 clientResponse.pipe(request.response); |
| 134 }); | 158 }); |
| 135 // Special handling of Content-Length and Via. | 159 } |
| 136 clientRequest.contentLength = request.contentLength; | |
| 137 List<String> via = request.headers[HttpHeaders.VIA]; | |
| 138 String viaPrefix = via == null ? "" : "${via[0]}, "; | |
| 139 clientRequest.headers.add( | |
| 140 HttpHeaders.VIA, "${viaPrefix}1.1 localhost:$port"); | |
| 141 // Copy all content. | |
| 142 return request.pipe(clientRequest); | |
| 143 }) | |
| 144 .then((HttpClientResponse clientResponse) { | |
| 145 clientResponse.pipe(request.response); | |
| 146 }); | |
| 147 }); | 160 }); |
| 148 }); | 161 }); |
| 149 return x.future; | 162 return x.future; |
| 150 } | 163 } |
| 151 | 164 |
| 152 void shutdown() { | 165 void shutdown() { |
| 153 server.close(); | 166 server.close(); |
| 154 client.close(); | 167 client.close(); |
| 155 } | 168 } |
| 156 | 169 |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 ? "https://localhost:${secureServer.port}/$i" | 435 ? "https://localhost:${secureServer.port}/$i" |
| 423 : "http://localhost:${server.port}/$i"; | 436 : "http://localhost:${server.port}/$i"; |
| 424 | 437 |
| 425 client.postUrl(Uri.parse(url)) | 438 client.postUrl(Uri.parse(url)) |
| 426 .then((HttpClientRequest clientRequest) { | 439 .then((HttpClientRequest clientRequest) { |
| 427 String content = "$i$i$i"; | 440 String content = "$i$i$i"; |
| 428 clientRequest.write(content); | 441 clientRequest.write(content); |
| 429 return clientRequest.close(); | 442 return clientRequest.close(); |
| 430 }) | 443 }) |
| 431 .then((HttpClientResponse response) { | 444 .then((HttpClientResponse response) { |
| 432 response.listen((_) {}, onDone: () { | 445 Expect.fail("No response expected"); |
| 433 testProxyAuthenticateCount++; | 446 }). |
| 434 Expect.equals(HttpStatus.PROXY_AUTHENTICATION_REQUIRED, | 447 catchError((e) { |
| 435 response.statusCode); | 448 testProxyAuthenticateCount++; |
| 436 if (testProxyAuthenticateCount == loopCount * 2) { | 449 if (testProxyAuthenticateCount == loopCount * 2) { |
| 437 Expect.equals(0, server.requestCount); | 450 Expect.equals(0, server.requestCount); |
| 438 Expect.equals(0, secureServer.requestCount); | 451 Expect.equals(0, secureServer.requestCount); |
| 439 step1.complete(null); | 452 step1.complete(null); |
| 440 } | 453 } |
| 441 }); | 454 }); |
| 442 }); | |
| 443 } | 455 } |
| 444 | 456 |
| 445 test(false); | 457 test(false); |
| 446 test(true); | 458 test(true); |
| 447 } | 459 } |
| 448 | |
| 449 step1.future.then((_) { | 460 step1.future.then((_) { |
| 450 testProxyAuthenticateCount = 0; | 461 testProxyAuthenticateCount = 0; |
| 451 client.findProxy = (Uri uri) { | 462 client.findProxy = (Uri uri) { |
| 452 return "PROXY test:test@localhost:${proxyServer.port}"; | 463 return "PROXY test:test@localhost:${proxyServer.port}"; |
| 453 }; | 464 }; |
| 454 | 465 |
| 455 for (int i = 0; i < loopCount; i++) { | 466 for (int i = 0; i < loopCount; i++) { |
| 456 test(bool secure) { | 467 test(bool secure) { |
| 457 String url = secure | 468 String url = secure |
| 458 ? "https://localhost:${secureServer.port}/$i" | 469 ? "https://localhost:${secureServer.port}/$i" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 testDirectProxy(); | 638 testDirectProxy(); |
| 628 testProxy(); | 639 testProxy(); |
| 629 testProxyChain(); | 640 testProxyChain(); |
| 630 testProxyFromEnviroment(); | 641 testProxyFromEnviroment(); |
| 631 testProxyAuthenticate(); | 642 testProxyAuthenticate(); |
| 632 // This test is not normally run. It can be used for locally testing | 643 // This test is not normally run. It can be used for locally testing |
| 633 // with a real proxy server (e.g. Apache). | 644 // with a real proxy server (e.g. Apache). |
| 634 //testRealProxy(); | 645 //testRealProxy(); |
| 635 //testRealProxyAuth(); | 646 //testRealProxyAuth(); |
| 636 } | 647 } |
| OLD | NEW |