Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #import("dart:io"); | |
| 6 #import("dart:uri"); | |
| 7 | |
| 8 class Server { | |
| 9 HttpServer server; | |
| 10 int proxyHops; | |
| 11 int requestCount = 0; | |
| 12 | |
| 13 Server(this.proxyHops) : server = new HttpServer(); | |
| 14 | |
| 15 void start() { | |
| 16 server.listen("127.0.0.1", 0, 5); | |
|
Mads Ager (google)
2012/10/08 16:59:39
Maybe leave out the backlog parameter and just use
Søren Gjesse
2012/10/09 14:05:22
Done.
| |
| 17 server.defaultRequestHandler = | |
| 18 (HttpRequest request, HttpResponse response) { | |
|
Mads Ager (google)
2012/10/08 16:59:39
Funky indentation. See the
if (...) {
} else {
Søren Gjesse
2012/10/09 14:05:22
Done.
| |
| 19 requestCount++; | |
| 20 if (proxyHops > 0) { | |
| 21 Expect.isNotNull(request.headers[HttpHeaders.VIA]); | |
| 22 Expect.equals(1, request.headers[HttpHeaders.VIA].length); | |
| 23 Expect.equals( | |
| 24 proxyHops, | |
| 25 request.headers[HttpHeaders.VIA][0].split(",").length); | |
| 26 } else { | |
| 27 Expect.isNull(request.headers[HttpHeaders.VIA]); | |
| 28 } | |
| 29 StringInputStream stream = new StringInputStream(request.inputStream); | |
| 30 StringBuffer body = new StringBuffer(); | |
| 31 stream.onData = () => body.add(stream.read()); | |
| 32 stream.onClosed = () { | |
| 33 String path = request.path.substring(1); | |
| 34 String content = "$path$path$path"; | |
| 35 Expect.equals(content, body.toString()); | |
| 36 response.outputStream.writeString(request.path); | |
| 37 response.outputStream.close(); | |
| 38 }; | |
| 39 }; | |
| 40 } | |
| 41 | |
| 42 void shutdown() { | |
| 43 server.close(); | |
| 44 } | |
| 45 | |
| 46 int get port => server.port; | |
| 47 } | |
| 48 | |
| 49 Server setupServer(int proxyHops) { | |
| 50 Server server = new Server(proxyHops); | |
| 51 server.start(); | |
| 52 return server; | |
| 53 } | |
| 54 | |
| 55 class ProxyServer { | |
| 56 HttpServer server; | |
| 57 HttpClient client; | |
| 58 int requestCount = 0; | |
| 59 | |
| 60 ProxyServer() : server = new HttpServer(), client = new HttpClient(); | |
| 61 | |
| 62 void start() { | |
| 63 server.listen("127.0.0.1", 0, 5); | |
|
Mads Ager (google)
2012/10/08 16:59:39
Ditto for the backlog param?
Søren Gjesse
2012/10/09 14:05:22
Done.
| |
| 64 server.defaultRequestHandler = | |
| 65 (HttpRequest request, HttpResponse response) { | |
| 66 requestCount++; | |
| 67 // Open the connection from the proxy. | |
| 68 HttpClientConnection conn = | |
| 69 client.openUrl(request.method, new Uri.fromString(request.path)); | |
| 70 conn.onRequest = (HttpClientRequest clientRequest) { | |
| 71 // Forward all headers. | |
| 72 request.headers.forEach((String name, List<String> values) { | |
| 73 values.forEach((String value) { | |
| 74 if (name != "content-length" && name != "via") { | |
| 75 clientRequest.headers.add(name, value); | |
| 76 } | |
| 77 }); | |
| 78 }); | |
| 79 // Special handling of Content-Length and Via. | |
| 80 clientRequest.contentLength = request.contentLength; | |
| 81 List<String> via = request.headers[HttpHeaders.VIA]; | |
| 82 String viaPrefix = via == null ? "" : "${via[0]}, "; | |
| 83 clientRequest.headers.add( | |
| 84 HttpHeaders.VIA, "${viaPrefix}1.1 localhost:$port"); | |
| 85 // Copy all content. | |
| 86 request.inputStream.pipe(clientRequest.outputStream); | |
| 87 }; | |
| 88 conn.onResponse = (HttpClientResponse clientResponse) { | |
| 89 clientResponse.inputStream.pipe(response.outputStream); | |
| 90 }; | |
| 91 }; | |
| 92 } | |
| 93 | |
| 94 void shutdown() { | |
| 95 server.close(); | |
| 96 client.shutdown(); | |
| 97 } | |
| 98 | |
| 99 int get port => server.port; | |
| 100 } | |
| 101 | |
| 102 ProxyServer setupProxyServer() { | |
| 103 ProxyServer proxyServer = new ProxyServer(); | |
| 104 proxyServer.start(); | |
| 105 return proxyServer; | |
| 106 } | |
| 107 | |
| 108 testInvalidProxy() { | |
| 109 HttpClient client = new HttpClient(); | |
| 110 | |
| 111 // TODO(sgjesse): This should not throw errors, but call | |
| 112 // HttpClientConnection onError. | |
| 113 client.findProxy = (Uri uri) => "XXX"; | |
| 114 Expect.throws( | |
| 115 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | |
| 116 (e) => e is HttpException); | |
| 117 | |
| 118 client.findProxy = (Uri uri) => "PROXY www.google.com"; | |
| 119 Expect.throws( | |
| 120 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | |
| 121 (e) => e is HttpException); | |
| 122 | |
| 123 client.findProxy = (Uri uri) => "PROXY www.google.com:http"; | |
| 124 Expect.throws( | |
| 125 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | |
| 126 (e) => e is HttpException); | |
| 127 } | |
| 128 | |
| 129 int testDirectDoneCount = 0; | |
| 130 void testDirectProxy() { | |
| 131 Server server = setupServer(0); | |
| 132 HttpClient client = new HttpClient(); | |
| 133 List<String> proxy = | |
| 134 ["DIRECT", " DIRECT ", "DIRECT ;", " DIRECT ; ", | |
| 135 ";DIRECT", " ; DIRECT ", ";;DIRECT;;"]; | |
| 136 | |
| 137 client.findProxy = (Uri uri) { | |
| 138 int index = int.parse(uri.path.substring(1)); | |
| 139 return proxy[index]; | |
| 140 }; | |
| 141 | |
| 142 for (int i = 0; i < proxy.length; i++) { | |
| 143 HttpClientConnection conn = | |
| 144 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | |
| 145 conn.onRequest = (HttpClientRequest clientRequest) { | |
| 146 String content = "$i$i$i"; | |
| 147 clientRequest.contentLength = content.length; | |
| 148 clientRequest.outputStream.writeString(content); | |
| 149 clientRequest.outputStream.close(); | |
| 150 }; | |
| 151 conn.onResponse = (HttpClientResponse response) { | |
| 152 response.inputStream.onData = () => response.inputStream.read(); | |
| 153 response.inputStream.onClosed = () { | |
| 154 testDirectDoneCount++; | |
| 155 if (testDirectDoneCount == proxy.length) { | |
| 156 Expect.equals(proxy.length, server.requestCount); | |
| 157 server.shutdown(); | |
| 158 client.shutdown(); | |
| 159 } | |
| 160 }; | |
| 161 }; | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 int testProxyDoneCount = 0; | |
| 166 void testProxy() { | |
| 167 ProxyServer proxyServer = setupProxyServer(); | |
| 168 Server server = setupServer(1); | |
| 169 HttpClient client = new HttpClient(); | |
| 170 | |
| 171 List<String> proxy = | |
| 172 ["PROXY localhost:${proxyServer.port}", | |
| 173 "PROXY localhost:${proxyServer.port}; PROXY hede.hule.hest:8080", | |
| 174 "PROXY localhost:${proxyServer.port}; DIRECT"]; | |
| 175 | |
| 176 client.findProxy = (Uri uri) { | |
| 177 // Pick the proxy configuration based on the request path. | |
| 178 int index = int.parse(uri.path.substring(1)); | |
| 179 return proxy[index]; | |
| 180 }; | |
| 181 | |
| 182 for (int i = 0; i < proxy.length; i++) { | |
| 183 HttpClientConnection conn = | |
| 184 client.postUrl( | |
| 185 new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | |
| 186 conn.onRequest = (HttpClientRequest clientRequest) { | |
| 187 String content = "$i$i$i"; | |
| 188 clientRequest.outputStream.writeString(content); | |
| 189 clientRequest.outputStream.close(); | |
| 190 }; | |
| 191 conn.onResponse = (HttpClientResponse response) { | |
| 192 response.inputStream.onData = () => response.inputStream.read(); | |
| 193 response.inputStream.onClosed = () { | |
| 194 testProxyDoneCount++; | |
| 195 if (testProxyDoneCount == proxy.length) { | |
| 196 Expect.equals(proxy.length, server.requestCount); | |
| 197 proxyServer.shutdown(); | |
| 198 server.shutdown(); | |
| 199 client.shutdown(); | |
| 200 } | |
| 201 }; | |
| 202 }; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 int testProxyChainDoneCount = 0; | |
| 207 void testProxyChain() { | |
| 208 // Setup two proxy servers having the first using the second as its proxy. | |
| 209 ProxyServer proxyServer1 = setupProxyServer(); | |
| 210 ProxyServer proxyServer2 = setupProxyServer(); | |
| 211 proxyServer1.client.findProxy = (_) => "PROXY 127.0.0.1:${proxyServer2.port}"; | |
| 212 | |
| 213 Server server = setupServer(2); | |
| 214 HttpClient client = new HttpClient(); | |
| 215 | |
| 216 List<String> proxy = | |
| 217 ["PROXY localhost:${proxyServer1.port}", | |
| 218 "PROXY localhost:${proxyServer1.port}; PROXY hede.hule.hest:8080", | |
| 219 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | |
| 220 | |
| 221 client.findProxy = (Uri uri) { | |
| 222 // Pick the proxy configuration based on the request path. | |
| 223 int index = int.parse(uri.path.substring(1)); | |
| 224 return proxy[index]; | |
| 225 }; | |
| 226 | |
| 227 for (int i = 0; i < proxy.length; i++) { | |
| 228 HttpClientConnection conn = | |
| 229 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | |
| 230 conn.onRequest = (HttpClientRequest clientRequest) { | |
| 231 String content = "$i$i$i"; | |
| 232 clientRequest.contentLength = content.length; | |
| 233 clientRequest.outputStream.writeString(content); | |
| 234 clientRequest.outputStream.close(); | |
| 235 }; | |
| 236 conn.onResponse = (HttpClientResponse response) { | |
| 237 response.inputStream.onData = () => response.inputStream.read(); | |
| 238 response.inputStream.onClosed = () { | |
| 239 testProxyChainDoneCount++; | |
| 240 if (testProxyChainDoneCount == proxy.length) { | |
| 241 Expect.equals(proxy.length, server.requestCount); | |
| 242 proxyServer1.shutdown(); | |
| 243 proxyServer2.shutdown(); | |
| 244 server.shutdown(); | |
| 245 client.shutdown(); | |
| 246 } | |
| 247 }; | |
| 248 }; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 int testRealProxyDoneCount = 0; | |
| 253 void testRealProxy() { | |
| 254 Server server = setupServer(1); | |
| 255 HttpClient client = new HttpClient(); | |
| 256 | |
| 257 List<String> proxy = | |
| 258 ["PROXY localhost:8080", | |
| 259 "PROXY localhost:8080; PROXY hede.hule.hest:8080", | |
| 260 "PROXY localhost:8080; DIRECT"]; | |
| 261 | |
| 262 client.findProxy = (Uri uri) { | |
| 263 // Pick the proxy configuration based on the request path. | |
| 264 int index = int.parse(uri.path.substring(1)); | |
| 265 return proxy[index]; | |
| 266 }; | |
| 267 | |
| 268 for (int i = 0; i < proxy.length; i++) { | |
| 269 HttpClientConnection conn = | |
| 270 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | |
| 271 conn.onRequest = (HttpClientRequest clientRequest) { | |
| 272 String content = "$i$i$i"; | |
| 273 clientRequest.contentLength = content.length; | |
| 274 clientRequest.outputStream.writeString(content); | |
| 275 clientRequest.outputStream.close(); | |
| 276 }; | |
| 277 conn.onResponse = (HttpClientResponse response) { | |
| 278 response.inputStream.onData = () => response.inputStream.read(); | |
| 279 response.inputStream.onClosed = () { | |
| 280 testRealProxyDoneCount++; | |
| 281 if (testRealProxyDoneCount == proxy.length) { | |
| 282 Expect.equals(proxy.length, server.requestCount); | |
| 283 server.shutdown(); | |
| 284 client.shutdown(); | |
| 285 } | |
| 286 }; | |
| 287 }; | |
| 288 } | |
| 289 } | |
| 290 | |
| 291 main() { | |
| 292 testInvalidProxy(); | |
| 293 testDirectProxy(); | |
| 294 testProxy(); | |
| 295 testProxyChain(); | |
| 296 // This test is not normally run. It can be used for locally testing | |
| 297 // with a real proxy server (e.g. Apache). | |
| 298 // testRealProxy(); | |
| 299 } | |
| OLD | NEW |