| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 int proxyHops; | 10 int proxyHops; |
| 11 List<String> directRequestPaths; |
| 11 int requestCount = 0; | 12 int requestCount = 0; |
| 12 | 13 |
| 13 Server(this.proxyHops) : server = new HttpServer(); | 14 Server(this.proxyHops, this.directRequestPaths) : server = new HttpServer(); |
| 14 | 15 |
| 15 void start() { | 16 void start() { |
| 16 server.listen("127.0.0.1", 0); | 17 server.listen("127.0.0.1", 0); |
| 17 server.defaultRequestHandler = | 18 server.defaultRequestHandler = |
| 18 (HttpRequest request, HttpResponse response) { | 19 (HttpRequest request, HttpResponse response) { |
| 19 requestCount++; | 20 requestCount++; |
| 20 if (proxyHops > 0) { | 21 // Check whether a proxy or direct connection is expected. |
| 22 bool direct = directRequestPaths.reduce( |
| 23 false, |
| 24 (prev, path) => prev ? prev : path == request.path); |
| 25 if (!direct && proxyHops > 0) { |
| 21 Expect.isNotNull(request.headers[HttpHeaders.VIA]); | 26 Expect.isNotNull(request.headers[HttpHeaders.VIA]); |
| 22 Expect.equals(1, request.headers[HttpHeaders.VIA].length); | 27 Expect.equals(1, request.headers[HttpHeaders.VIA].length); |
| 23 Expect.equals( | 28 Expect.equals( |
| 24 proxyHops, | 29 proxyHops, |
| 25 request.headers[HttpHeaders.VIA][0].split(",").length); | 30 request.headers[HttpHeaders.VIA][0].split(",").length); |
| 26 } else { | 31 } else { |
| 27 Expect.isNull(request.headers[HttpHeaders.VIA]); | 32 Expect.isNull(request.headers[HttpHeaders.VIA]); |
| 28 } | 33 } |
| 29 StringInputStream stream = new StringInputStream(request.inputStream); | 34 StringInputStream stream = new StringInputStream(request.inputStream); |
| 30 StringBuffer body = new StringBuffer(); | 35 StringBuffer body = new StringBuffer(); |
| 31 stream.onData = () => body.add(stream.read()); | 36 stream.onData = () => body.add(stream.read()); |
| 32 stream.onClosed = () { | 37 stream.onClosed = () { |
| 33 String path = request.path.substring(1); | 38 String path = request.path.substring(1); |
| 34 String content = "$path$path$path"; | 39 String content = "$path$path$path"; |
| 35 Expect.equals(content, body.toString()); | 40 Expect.equals(content, body.toString()); |
| 36 response.outputStream.writeString(request.path); | 41 response.outputStream.writeString(request.path); |
| 37 response.outputStream.close(); | 42 response.outputStream.close(); |
| 38 }; | 43 }; |
| 39 }; | 44 }; |
| 40 } | 45 } |
| 41 | 46 |
| 42 void shutdown() { | 47 void shutdown() { |
| 43 server.close(); | 48 server.close(); |
| 44 } | 49 } |
| 45 | 50 |
| 46 int get port => server.port; | 51 int get port => server.port; |
| 47 } | 52 } |
| 48 | 53 |
| 49 Server setupServer(int proxyHops) { | 54 Server setupServer(int proxyHops, |
| 50 Server server = new Server(proxyHops); | 55 [List<String> directRequestPaths = const <String>[]]) { |
| 56 Server server = new Server(proxyHops, directRequestPaths); |
| 51 server.start(); | 57 server.start(); |
| 52 return server; | 58 return server; |
| 53 } | 59 } |
| 54 | 60 |
| 55 class ProxyServer { | 61 class ProxyServer { |
| 56 HttpServer server; | 62 HttpServer server; |
| 57 HttpClient client; | 63 HttpClient client; |
| 58 int requestCount = 0; | 64 int requestCount = 0; |
| 59 | 65 |
| 60 ProxyServer() : server = new HttpServer(), client = new HttpClient(); | 66 ProxyServer() : server = new HttpServer(), client = new HttpClient(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 client.shutdown(); | 164 client.shutdown(); |
| 159 } | 165 } |
| 160 }; | 166 }; |
| 161 }; | 167 }; |
| 162 } | 168 } |
| 163 } | 169 } |
| 164 | 170 |
| 165 int testProxyDoneCount = 0; | 171 int testProxyDoneCount = 0; |
| 166 void testProxy() { | 172 void testProxy() { |
| 167 ProxyServer proxyServer = setupProxyServer(); | 173 ProxyServer proxyServer = setupProxyServer(); |
| 168 Server server = setupServer(1); | 174 Server server = setupServer(1, ["/4"]); |
| 169 HttpClient client = new HttpClient(); | 175 HttpClient client = new HttpClient(); |
| 170 | 176 |
| 171 List<String> proxy = | 177 List<String> proxy = |
| 172 ["PROXY localhost:${proxyServer.port}", | 178 ["PROXY localhost:${proxyServer.port}", |
| 173 "PROXY localhost:${proxyServer.port}; PROXY hede.hule.hest:8080", | 179 "PROXY localhost:${proxyServer.port}; PROXY hede.hule.hest:8080", |
| 180 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer.port}", |
| 181 "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", |
| 174 "PROXY localhost:${proxyServer.port}; DIRECT"]; | 183 "PROXY localhost:${proxyServer.port}; DIRECT"]; |
| 175 | 184 |
| 176 client.findProxy = (Uri uri) { | 185 client.findProxy = (Uri uri) { |
| 177 // Pick the proxy configuration based on the request path. | 186 // Pick the proxy configuration based on the request path. |
| 178 int index = int.parse(uri.path.substring(1)); | 187 int index = int.parse(uri.path.substring(1)); |
| 179 return proxy[index]; | 188 return proxy[index]; |
| 180 }; | 189 }; |
| 181 | 190 |
| 182 for (int i = 0; i < proxy.length; i++) { | 191 for (int i = 0; i < proxy.length; i++) { |
| 183 HttpClientConnection conn = | 192 HttpClientConnection conn = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 203 } | 212 } |
| 204 } | 213 } |
| 205 | 214 |
| 206 int testProxyChainDoneCount = 0; | 215 int testProxyChainDoneCount = 0; |
| 207 void testProxyChain() { | 216 void testProxyChain() { |
| 208 // Setup two proxy servers having the first using the second as its proxy. | 217 // Setup two proxy servers having the first using the second as its proxy. |
| 209 ProxyServer proxyServer1 = setupProxyServer(); | 218 ProxyServer proxyServer1 = setupProxyServer(); |
| 210 ProxyServer proxyServer2 = setupProxyServer(); | 219 ProxyServer proxyServer2 = setupProxyServer(); |
| 211 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; | 220 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; |
| 212 | 221 |
| 213 Server server = setupServer(2); | 222 Server server = setupServer(2, ["/4"]); |
| 214 HttpClient client = new HttpClient(); | 223 HttpClient client = new HttpClient(); |
| 215 | 224 |
| 216 List<String> proxy = | 225 List<String> proxy = |
| 217 ["PROXY localhost:${proxyServer1.port}", | 226 ["PROXY localhost:${proxyServer1.port}", |
| 218 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", | 227 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", |
| 228 "PROXY hede.hule.hest:8080; PROXY localhost:${proxyServer1.port}", |
| 229 "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", |
| 219 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | 231 "PROXY localhost:${proxyServer1.port}; DIRECT"]; |
| 220 | 232 |
| 221 client.findProxy = (Uri uri) { | 233 client.findProxy = (Uri uri) { |
| 222 // Pick the proxy configuration based on the request path. | 234 // Pick the proxy configuration based on the request path. |
| 223 int index = int.parse(uri.path.substring(1)); | 235 int index = int.parse(uri.path.substring(1)); |
| 224 return proxy[index]; | 236 return proxy[index]; |
| 225 }; | 237 }; |
| 226 | 238 |
| 227 for (int i = 0; i < proxy.length; i++) { | 239 for (int i = 0; i < proxy.length; i++) { |
| 228 HttpClientConnection conn = | 240 HttpClientConnection conn = |
| (...skipping 21 matching lines...) Expand all Loading... |
| 250 } | 262 } |
| 251 | 263 |
| 252 int testRealProxyDoneCount = 0; | 264 int testRealProxyDoneCount = 0; |
| 253 void testRealProxy() { | 265 void testRealProxy() { |
| 254 Server server = setupServer(1); | 266 Server server = setupServer(1); |
| 255 HttpClient client = new HttpClient(); | 267 HttpClient client = new HttpClient(); |
| 256 | 268 |
| 257 List<String> proxy = | 269 List<String> proxy = |
| 258 ["PROXY localhost:8080", | 270 ["PROXY localhost:8080", |
| 259 "PROXY localhost:8080; PROXY hede.hule.hest:8080", | 271 "PROXY localhost:8080; PROXY hede.hule.hest:8080", |
| 272 "PROXY hede.hule.hest:8080; PROXY localhost:8080", |
| 260 "PROXY localhost:8080; DIRECT"]; | 273 "PROXY localhost:8080; DIRECT"]; |
| 261 | 274 |
| 262 client.findProxy = (Uri uri) { | 275 client.findProxy = (Uri uri) { |
| 263 // Pick the proxy configuration based on the request path. | 276 // Pick the proxy configuration based on the request path. |
| 264 int index = int.parse(uri.path.substring(1)); | 277 int index = int.parse(uri.path.substring(1)); |
| 265 return proxy[index]; | 278 return proxy[index]; |
| 266 }; | 279 }; |
| 267 | 280 |
| 268 for (int i = 0; i < proxy.length; i++) { | 281 for (int i = 0; i < proxy.length; i++) { |
| 269 HttpClientConnection conn = | 282 HttpClientConnection conn = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 290 | 303 |
| 291 main() { | 304 main() { |
| 292 testInvalidProxy(); | 305 testInvalidProxy(); |
| 293 testDirectProxy(); | 306 testDirectProxy(); |
| 294 testProxy(); | 307 testProxy(); |
| 295 testProxyChain(); | 308 testProxyChain(); |
| 296 // This test is not normally run. It can be used for locally testing | 309 // This test is not normally run. It can be used for locally testing |
| 297 // with a real proxy server (e.g. Apache). | 310 // with a real proxy server (e.g. Apache). |
| 298 // testRealProxy(); | 311 // testRealProxy(); |
| 299 } | 312 } |
| OLD | NEW |