Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(531)

Side by Side Diff: runtime/bin/http_impl.dart

Issue 11092044: Add support for multiple HTTP proxies (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/standalone/io/http_proxy_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 class _HttpHeaders implements HttpHeaders { 5 class _HttpHeaders implements HttpHeaders {
6 _HttpHeaders() : _headers = new Map<String, List<String>>(); 6 _HttpHeaders() : _headers = new Map<String, List<String>>();
7 7
8 List<String> operator[](String name) { 8 List<String> operator[](String name) {
9 name = name.toLowerCase(); 9 name = name.toLowerCase();
10 return _headers[name]; 10 return _headers[name];
(...skipping 2087 matching lines...) Expand 10 before | Expand all | Expand 10 after
2098 void _cancelEvictionTimer() { 2098 void _cancelEvictionTimer() {
2099 _evictionTimer.cancel(); 2099 _evictionTimer.cancel();
2100 _evictionTimer = null; 2100 _evictionTimer = null;
2101 } 2101 }
2102 2102
2103 String _connectionKey(String host, int port) { 2103 String _connectionKey(String host, int port) {
2104 return "$host:$port"; 2104 return "$host:$port";
2105 } 2105 }
2106 2106
2107 HttpClientConnection _prepareHttpClientConnection( 2107 HttpClientConnection _prepareHttpClientConnection(
2108 String method, 2108 String method,
2109 Uri url, 2109 Uri url,
2110 [_HttpClientConnection connection]) { 2110 [_HttpClientConnection connection]) {
2111 2111
2112 void _establishConnection(String host,
Anders Johnsen 2012/10/12 06:40:14 Should we consider moving this outside of _prepare
Søren Gjesse 2012/10/12 08:15:37 I was also considering that, but then the argument
2113 int port,
2114 _ProxyConfiguration proxyConfiguration,
2115 int proxyIndex) {
2116
2117 void _connectionOpened(_SocketConnection socketConn,
2118 _HttpClientConnection connection,
2119 bool usingProxy) {
2120 connection._usingProxy = usingProxy;
2121 connection._connectionEstablished(socketConn);
2122 HttpClientRequest request = connection.open(method, url);
2123 request.headers.host = host;
2124 request.headers.port = port;
2125 if (connection._onRequest != null) {
2126 connection._onRequest(request);
2127 } else {
2128 request.outputStream.close();
2129 }
2130 }
2131
2132 // Determine the actual host to connect to.
2133 String connectHost;
2134 int connectPort;
2135 _Proxy proxy = proxyConfiguration.proxies[proxyIndex];
2136 if (proxy.isDirect) {
2137 connectHost = host;
2138 connectPort = port;
2139 } else {
2140 connectHost = proxy.host;
2141 connectPort = proxy.port;
2142 }
2143
2144 // If there are active connections for this key get the first one
2145 // otherwise create a new one.
2146 String key = _connectionKey(connectHost, connectPort);
2147 Queue socketConnections = _openSockets[key];
2148 if (socketConnections == null || socketConnections.isEmpty()) {
2149 Socket socket = new Socket(connectHost, connectPort);
2150 // Until the connection is established handle connection errors
2151 // here as the HttpClientConnection object is not yet associated
2152 // with the socket.
2153 socket.onError = (e) {
2154 proxyIndex++;
2155 if (proxyIndex < proxyConfiguration.proxies.length) {
Anders Johnsen 2012/10/12 06:40:14 An alternative would be to move this line to the s
Søren Gjesse 2012/10/12 08:15:37 The problem with that approach is that the actual
2156 // Try the next proxy in the list.
2157 _establishConnection(host, port, proxyConfiguration, proxyIndex);
2158 } else {
2159 // Report the error through the HttpClientConnection object to
2160 // the client.
2161 connection._onError(e);
2162 }
2163 };
2164 socket.onConnect = () {
2165 // When the connection is established, clear the error
2166 // callback as it will now be handled by the
2167 // HttpClientConnection object which will be associated with
2168 // the connected socket.
2169 socket.onError = null;
Anders Johnsen 2012/10/12 06:40:14 Not knowing much about http proxy, is the spec say
Søren Gjesse 2012/10/12 08:15:37 There is no "real" standard here. The page http://
2170 _SocketConnection socketConn =
2171 new _SocketConnection(connectHost, connectPort, socket);
2172 _activeSockets.add(socketConn);
2173 _connectionOpened(socketConn, connection, !proxy.isDirect);
2174 };
2175 } else {
2176 _SocketConnection socketConn = socketConnections.removeFirst();
2177 _activeSockets.add(socketConn);
2178 new Timer(0, (ignored) =>
2179 _connectionOpened(socketConn, connection, !proxy.isDirect));
2180
2181 // Get rid of eviction timer if there are no more active connections.
2182 if (socketConnections.isEmpty()) _openSockets.remove(key);
2183 if (_openSockets.isEmpty()) _cancelEvictionTimer();
2184 }
2185 }
2186
2187 // Find the TCP host and port.
2112 String host = url.domain; 2188 String host = url.domain;
2113 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port; 2189 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port;
2114 2190
2115 void _connectionOpened(_SocketConnection socketConn, 2191 // Create a new connection object if we are not re-using an existing one.
2116 _HttpClientConnection connection,
2117 bool usingProxy) {
2118 connection._usingProxy = usingProxy;
2119 connection._connectionEstablished(socketConn);
2120 HttpClientRequest request = connection.open(method, url);
2121 request.headers.host = host;
2122 request.headers.port = port;
2123 if (connection._onRequest != null) {
2124 connection._onRequest(request);
2125 } else {
2126 request.outputStream.close();
2127 }
2128 }
2129
2130 // Create a new connection if we are not re-using an existing one.
2131 if (connection == null) { 2192 if (connection == null) {
2132 connection = new _HttpClientConnection(this); 2193 connection = new _HttpClientConnection(this);
2133 } 2194 }
2134 connection.onDetach = () => _activeSockets.remove(connection._socketConn); 2195 connection.onDetach = () => _activeSockets.remove(connection._socketConn);
2135 2196
2136 // Check to see if a proxy server should be used for this connection. 2197 // Check to see if a proxy server should be used for this connection.
2137 _ProxyConfiguration proxyConfiguration = const _ProxyConfiguration.direct(); 2198 _ProxyConfiguration proxyConfiguration = const _ProxyConfiguration.direct();
2138 if (_findProxy != null) { 2199 if (_findProxy != null) {
2139 // TODO(sgjesse): Keep a map of these as normally only a few 2200 // TODO(sgjesse): Keep a map of these as normally only a few
2140 // configuration strings will be used. 2201 // configuration strings will be used.
2141 proxyConfiguration = new _ProxyConfiguration(_findProxy(url)); 2202 proxyConfiguration = new _ProxyConfiguration(_findProxy(url));
2142 } 2203 }
2143 2204
2144 // Determine the actual host to connect to. 2205 // Establish the connection starting with the first proxy configured.
2145 String connectHost; 2206 _establishConnection(host, port, proxyConfiguration, 0);
2146 int connectPort;
2147 _Proxy proxy = proxyConfiguration.proxies[0];
2148 if (proxy.isDirect) {
2149 connectHost = host;
2150 connectPort = port;
2151 } else {
2152 connectHost = proxy.host;
2153 connectPort = proxy.port;
2154 }
2155
2156 // If there are active connections for this key get the first one
2157 // otherwise create a new one.
2158 String key = _connectionKey(connectHost, connectPort);
2159 Queue socketConnections = _openSockets[key];
2160 if (socketConnections == null || socketConnections.isEmpty()) {
2161 Socket socket = new Socket(connectHost, connectPort);
2162 // Until the connection is established handle connection errors
2163 // here as the HttpClientConnection object is not yet associated
2164 // with the socket.
2165 socket.onError = (e) {
2166 // Report the error through the HttpClientConnection object to
2167 // the client.
2168 connection._onError(e);
2169 };
2170 socket.onConnect = () {
2171 // When the connection is established, clear the error
2172 // callback as it will now be handled by the
2173 // HttpClientConnection object which will be associated with
2174 // the connected socket.
2175 socket.onError = null;
2176 _SocketConnection socketConn =
2177 new _SocketConnection(connectHost, connectPort, socket);
2178 _activeSockets.add(socketConn);
2179 _connectionOpened(socketConn, connection, !proxy.isDirect);
2180 };
2181 } else {
2182 _SocketConnection socketConn = socketConnections.removeFirst();
2183 _activeSockets.add(socketConn);
2184 new Timer(0, (ignored) =>
2185 _connectionOpened(socketConn, connection, !proxy.isDirect));
2186
2187 // Get rid of eviction timer if there are no more active connections.
2188 if (socketConnections.isEmpty()) _openSockets.remove(key);
2189 if (_openSockets.isEmpty()) _cancelEvictionTimer();
2190 }
2191 2207
2192 return connection; 2208 return connection;
2193 } 2209 }
2194 2210
2195 void _returnSocketConnection(_SocketConnection socketConn) { 2211 void _returnSocketConnection(_SocketConnection socketConn) {
2196 // Mark socket as returned to unregister from the old connection. 2212 // Mark socket as returned to unregister from the old connection.
2197 socketConn._markReturned(); 2213 socketConn._markReturned();
2198 2214
2199 // If the HTTP client is beeing shutdown don't return the connection. 2215 // If the HTTP client is beeing shutdown don't return the connection.
2200 if (_shutdown) { 2216 if (_shutdown) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
2273 2289
2274 2290
2275 class _RedirectInfo implements RedirectInfo { 2291 class _RedirectInfo implements RedirectInfo {
2276 const _RedirectInfo(int this.statusCode, 2292 const _RedirectInfo(int this.statusCode,
2277 String this.method, 2293 String this.method,
2278 Uri this.location); 2294 Uri this.location);
2279 final int statusCode; 2295 final int statusCode;
2280 final String method; 2296 final String method;
2281 final Uri location; 2297 final Uri location;
2282 } 2298 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/io/http_proxy_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698