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; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 ProxyServer() : server = new HttpServer(), client = new HttpClient(); | 66 ProxyServer() : server = new HttpServer(), client = new HttpClient(); |
67 | 67 |
68 void start() { | 68 void start() { |
69 server.listen("127.0.0.1", 0); | 69 server.listen("127.0.0.1", 0); |
70 server.defaultRequestHandler = | 70 server.defaultRequestHandler = |
71 (HttpRequest request, HttpResponse response) { | 71 (HttpRequest request, HttpResponse response) { |
72 requestCount++; | 72 requestCount++; |
73 // Open the connection from the proxy. | 73 // Open the connection from the proxy. |
74 HttpClientConnection conn = | 74 HttpClientConnection conn = |
75 client.openUrl(request.method, new Uri.fromString(request.path)); | 75 client.openUrl(request.method, Uri.parse(request.path)); |
76 conn.onRequest = (HttpClientRequest clientRequest) { | 76 conn.onRequest = (HttpClientRequest clientRequest) { |
77 // Forward all headers. | 77 // Forward all headers. |
78 request.headers.forEach((String name, List<String> values) { | 78 request.headers.forEach((String name, List<String> values) { |
79 values.forEach((String value) { | 79 values.forEach((String value) { |
80 if (name != "content-length" && name != "via") { | 80 if (name != "content-length" && name != "via") { |
81 clientRequest.headers.add(name, value); | 81 clientRequest.headers.add(name, value); |
82 } | 82 } |
83 }); | 83 }); |
84 }); | 84 }); |
85 // Special handling of Content-Length and Via. | 85 // Special handling of Content-Length and Via. |
(...skipping 25 matching lines...) Expand all Loading... |
111 return proxyServer; | 111 return proxyServer; |
112 } | 112 } |
113 | 113 |
114 testInvalidProxy() { | 114 testInvalidProxy() { |
115 HttpClient client = new HttpClient(); | 115 HttpClient client = new HttpClient(); |
116 | 116 |
117 // TODO(sgjesse): This should not throw errors, but call | 117 // TODO(sgjesse): This should not throw errors, but call |
118 // HttpClientConnection onError. | 118 // HttpClientConnection onError. |
119 client.findProxy = (Uri uri) => "XXX"; | 119 client.findProxy = (Uri uri) => "XXX"; |
120 Expect.throws( | 120 Expect.throws( |
121 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | 121 () => client.getUrl(Uri.parse("http://www.google.com/test")), |
122 (e) => e is HttpException); | 122 (e) => e is HttpException); |
123 | 123 |
124 client.findProxy = (Uri uri) => "PROXY www.google.com"; | 124 client.findProxy = (Uri uri) => "PROXY www.google.com"; |
125 Expect.throws( | 125 Expect.throws( |
126 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | 126 () => client.getUrl(Uri.parse("http://www.google.com/test")), |
127 (e) => e is HttpException); | 127 (e) => e is HttpException); |
128 | 128 |
129 client.findProxy = (Uri uri) => "PROXY www.google.com:http"; | 129 client.findProxy = (Uri uri) => "PROXY www.google.com:http"; |
130 Expect.throws( | 130 Expect.throws( |
131 () => client.getUrl(new Uri.fromString("http://www.google.com/test")), | 131 () => client.getUrl(Uri.parse("http://www.google.com/test")), |
132 (e) => e is HttpException); | 132 (e) => e is HttpException); |
133 } | 133 } |
134 | 134 |
135 int testDirectDoneCount = 0; | 135 int testDirectDoneCount = 0; |
136 void testDirectProxy() { | 136 void testDirectProxy() { |
137 Server server = setupServer(0); | 137 Server server = setupServer(0); |
138 HttpClient client = new HttpClient(); | 138 HttpClient client = new HttpClient(); |
139 List<String> proxy = | 139 List<String> proxy = |
140 ["DIRECT", " DIRECT ", "DIRECT ;", " DIRECT ; ", | 140 ["DIRECT", " DIRECT ", "DIRECT ;", " DIRECT ; ", |
141 ";DIRECT", " ; DIRECT ", ";;DIRECT;;"]; | 141 ";DIRECT", " ; DIRECT ", ";;DIRECT;;"]; |
142 | 142 |
143 client.findProxy = (Uri uri) { | 143 client.findProxy = (Uri uri) { |
144 int index = int.parse(uri.path.substring(1)); | 144 int index = int.parse(uri.path.substring(1)); |
145 return proxy[index]; | 145 return proxy[index]; |
146 }; | 146 }; |
147 | 147 |
148 for (int i = 0; i < proxy.length; i++) { | 148 for (int i = 0; i < proxy.length; i++) { |
149 HttpClientConnection conn = | 149 HttpClientConnection conn = |
150 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | 150 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")); |
151 conn.onRequest = (HttpClientRequest clientRequest) { | 151 conn.onRequest = (HttpClientRequest clientRequest) { |
152 String content = "$i$i$i"; | 152 String content = "$i$i$i"; |
153 clientRequest.contentLength = content.length; | 153 clientRequest.contentLength = content.length; |
154 clientRequest.outputStream.writeString(content); | 154 clientRequest.outputStream.writeString(content); |
155 clientRequest.outputStream.close(); | 155 clientRequest.outputStream.close(); |
156 }; | 156 }; |
157 conn.onResponse = (HttpClientResponse response) { | 157 conn.onResponse = (HttpClientResponse response) { |
158 response.inputStream.onData = () => response.inputStream.read(); | 158 response.inputStream.onData = () => response.inputStream.read(); |
159 response.inputStream.onClosed = () { | 159 response.inputStream.onClosed = () { |
160 testDirectDoneCount++; | 160 testDirectDoneCount++; |
(...skipping 23 matching lines...) Expand all Loading... |
184 | 184 |
185 client.findProxy = (Uri uri) { | 185 client.findProxy = (Uri uri) { |
186 // Pick the proxy configuration based on the request path. | 186 // Pick the proxy configuration based on the request path. |
187 int index = int.parse(uri.path.substring(1)); | 187 int index = int.parse(uri.path.substring(1)); |
188 return proxy[index]; | 188 return proxy[index]; |
189 }; | 189 }; |
190 | 190 |
191 for (int i = 0; i < proxy.length; i++) { | 191 for (int i = 0; i < proxy.length; i++) { |
192 HttpClientConnection conn = | 192 HttpClientConnection conn = |
193 client.postUrl( | 193 client.postUrl( |
194 new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | 194 Uri.parse("http://127.0.0.1:${server.port}/$i")); |
195 conn.onRequest = (HttpClientRequest clientRequest) { | 195 conn.onRequest = (HttpClientRequest clientRequest) { |
196 String content = "$i$i$i"; | 196 String content = "$i$i$i"; |
197 clientRequest.outputStream.writeString(content); | 197 clientRequest.outputStream.writeString(content); |
198 clientRequest.outputStream.close(); | 198 clientRequest.outputStream.close(); |
199 }; | 199 }; |
200 conn.onResponse = (HttpClientResponse response) { | 200 conn.onResponse = (HttpClientResponse response) { |
201 response.inputStream.onData = () => response.inputStream.read(); | 201 response.inputStream.onData = () => response.inputStream.read(); |
202 response.inputStream.onClosed = () { | 202 response.inputStream.onClosed = () { |
203 testProxyDoneCount++; | 203 testProxyDoneCount++; |
204 if (testProxyDoneCount == proxy.length) { | 204 if (testProxyDoneCount == proxy.length) { |
(...skipping 26 matching lines...) Expand all Loading... |
231 "PROXY localhost:${proxyServer1.port}; DIRECT"]; | 231 "PROXY localhost:${proxyServer1.port}; DIRECT"]; |
232 | 232 |
233 client.findProxy = (Uri uri) { | 233 client.findProxy = (Uri uri) { |
234 // Pick the proxy configuration based on the request path. | 234 // Pick the proxy configuration based on the request path. |
235 int index = int.parse(uri.path.substring(1)); | 235 int index = int.parse(uri.path.substring(1)); |
236 return proxy[index]; | 236 return proxy[index]; |
237 }; | 237 }; |
238 | 238 |
239 for (int i = 0; i < proxy.length; i++) { | 239 for (int i = 0; i < proxy.length; i++) { |
240 HttpClientConnection conn = | 240 HttpClientConnection conn = |
241 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | 241 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")); |
242 conn.onRequest = (HttpClientRequest clientRequest) { | 242 conn.onRequest = (HttpClientRequest clientRequest) { |
243 String content = "$i$i$i"; | 243 String content = "$i$i$i"; |
244 clientRequest.contentLength = content.length; | 244 clientRequest.contentLength = content.length; |
245 clientRequest.outputStream.writeString(content); | 245 clientRequest.outputStream.writeString(content); |
246 clientRequest.outputStream.close(); | 246 clientRequest.outputStream.close(); |
247 }; | 247 }; |
248 conn.onResponse = (HttpClientResponse response) { | 248 conn.onResponse = (HttpClientResponse response) { |
249 response.inputStream.onData = () => response.inputStream.read(); | 249 response.inputStream.onData = () => response.inputStream.read(); |
250 response.inputStream.onClosed = () { | 250 response.inputStream.onClosed = () { |
251 testProxyChainDoneCount++; | 251 testProxyChainDoneCount++; |
(...skipping 21 matching lines...) Expand all Loading... |
273 "PROXY localhost:8080; DIRECT"]; | 273 "PROXY localhost:8080; DIRECT"]; |
274 | 274 |
275 client.findProxy = (Uri uri) { | 275 client.findProxy = (Uri uri) { |
276 // Pick the proxy configuration based on the request path. | 276 // Pick the proxy configuration based on the request path. |
277 int index = int.parse(uri.path.substring(1)); | 277 int index = int.parse(uri.path.substring(1)); |
278 return proxy[index]; | 278 return proxy[index]; |
279 }; | 279 }; |
280 | 280 |
281 for (int i = 0; i < proxy.length; i++) { | 281 for (int i = 0; i < proxy.length; i++) { |
282 HttpClientConnection conn = | 282 HttpClientConnection conn = |
283 client.getUrl(new Uri.fromString("http://127.0.0.1:${server.port}/$i")); | 283 client.getUrl(Uri.parse("http://127.0.0.1:${server.port}/$i")); |
284 conn.onRequest = (HttpClientRequest clientRequest) { | 284 conn.onRequest = (HttpClientRequest clientRequest) { |
285 String content = "$i$i$i"; | 285 String content = "$i$i$i"; |
286 clientRequest.contentLength = content.length; | 286 clientRequest.contentLength = content.length; |
287 clientRequest.outputStream.writeString(content); | 287 clientRequest.outputStream.writeString(content); |
288 clientRequest.outputStream.close(); | 288 clientRequest.outputStream.close(); |
289 }; | 289 }; |
290 conn.onResponse = (HttpClientResponse response) { | 290 conn.onResponse = (HttpClientResponse response) { |
291 response.inputStream.onData = () => response.inputStream.read(); | 291 response.inputStream.onData = () => response.inputStream.read(); |
292 response.inputStream.onClosed = () { | 292 response.inputStream.onClosed = () { |
293 testRealProxyDoneCount++; | 293 testRealProxyDoneCount++; |
294 if (testRealProxyDoneCount == proxy.length) { | 294 if (testRealProxyDoneCount == proxy.length) { |
295 Expect.equals(proxy.length, server.requestCount); | 295 Expect.equals(proxy.length, server.requestCount); |
296 server.shutdown(); | 296 server.shutdown(); |
297 client.shutdown(); | 297 client.shutdown(); |
298 } | 298 } |
299 }; | 299 }; |
300 }; | 300 }; |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
304 main() { | 304 main() { |
305 testInvalidProxy(); | 305 testInvalidProxy(); |
306 testDirectProxy(); | 306 testDirectProxy(); |
307 testProxy(); | 307 testProxy(); |
308 testProxyChain(); | 308 testProxyChain(); |
309 // 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 |
310 // with a real proxy server (e.g. Apache). | 310 // with a real proxy server (e.g. Apache). |
311 // testRealProxy(); | 311 // testRealProxy(); |
312 } | 312 } |
OLD | NEW |