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

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

Issue 11035030: Refactor HttpClient internals to use Uri class for passing connection information (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 | no next file » | 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 1940 matching lines...) Expand 10 before | Expand all | Expand 10 after
1951 1951
1952 class _HttpClient implements HttpClient { 1952 class _HttpClient implements HttpClient {
1953 static const int DEFAULT_EVICTION_TIMEOUT = 60000; 1953 static const int DEFAULT_EVICTION_TIMEOUT = 60000;
1954 1954
1955 _HttpClient() : _openSockets = new Map(), 1955 _HttpClient() : _openSockets = new Map(),
1956 _activeSockets = new Set(), 1956 _activeSockets = new Set(),
1957 _shutdown = false; 1957 _shutdown = false;
1958 1958
1959 HttpClientConnection open( 1959 HttpClientConnection open(
1960 String method, String host, int port, String path) { 1960 String method, String host, int port, String path) {
1961 return _open(method, host, port, path); 1961 // TODO(sgjesse): The path set here can contain both query and
1962 // fragment. They should be cracked and set correctly.
1963 return _open(method, new Uri.fromComponents(
1964 scheme: "http", domain: host, port: port, path: path));
1962 } 1965 }
1963 1966
1964 HttpClientConnection _open(String method, 1967 HttpClientConnection _open(String method,
1965 String host, 1968 Uri uri,
1966 int port,
1967 String path,
1968 [_HttpClientConnection connection]) { 1969 [_HttpClientConnection connection]) {
1969 if (_shutdown) throw new HttpException("HttpClient shutdown"); 1970 if (_shutdown) throw new HttpException("HttpClient shutdown");
1970 if (method == null || host == null || port == null || path == null) { 1971 if (method == null || uri.domain.isEmpty() == null) {
1971 throw new ArgumentError(null); 1972 throw new ArgumentError(null);
1972 } 1973 }
1973 return _prepareHttpClientConnection(host, port, method, path, connection); 1974 return _prepareHttpClientConnection(method, uri, connection);
1974 } 1975 }
1975 1976
1976 HttpClientConnection openUrl(String method, Uri url) { 1977 HttpClientConnection openUrl(String method, Uri url) {
1977 _openUrl(method, url); 1978 _openUrl(method, url);
1978 } 1979 }
1979 1980
1980 HttpClientConnection _openUrl(String method, 1981 HttpClientConnection _openUrl(String method,
1981 Uri url, 1982 Uri url,
1982 [_HttpClientConnection connection]) { 1983 [_HttpClientConnection connection]) {
1983 if (url.scheme != "http") { 1984 if (url.scheme != "http") {
1984 throw new HttpException("Unsupported URL scheme ${url.scheme}"); 1985 throw new HttpException("Unsupported URL scheme ${url.scheme}");
1985 } 1986 }
1986 if (url.userInfo != "") { 1987 if (url.userInfo != "") {
1987 throw new HttpException("Unsupported user info ${url.userInfo}"); 1988 throw new HttpException("Unsupported user info ${url.userInfo}");
1988 } 1989 }
1989 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port; 1990 return _open(method, url, connection);
1990 String path;
1991 if (url.query != "") {
1992 if (url.fragment != "") {
1993 path = "${url.path}?${url.query}#${url.fragment}";
1994 } else {
1995 path = "${url.path}?${url.query}";
1996 }
1997 } else {
1998 path = url.path;
1999 }
2000 return _open(method, url.domain, port, path, connection);
2001 } 1991 }
2002 1992
2003 HttpClientConnection get(String host, int port, String path) { 1993 HttpClientConnection get(String host, int port, String path) {
2004 return _open("GET", host, port, path); 1994 return open("GET", host, port, path);
2005 } 1995 }
2006 1996
2007 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url); 1997 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url);
2008 1998
2009 HttpClientConnection post(String host, int port, String path) { 1999 HttpClientConnection post(String host, int port, String path) {
2010 return _open("POST", host, port, path); 2000 return open("POST", host, port, path);
2011 } 2001 }
2012 2002
2013 HttpClientConnection postUrl(Uri url) => _openUrl("POST", url); 2003 HttpClientConnection postUrl(Uri url) => _openUrl("POST", url);
2014 2004
2015 void shutdown() { 2005 void shutdown() {
2016 _openSockets.forEach((String key, Queue<_SocketConnection> connections) { 2006 _openSockets.forEach((String key, Queue<_SocketConnection> connections) {
2017 while (!connections.isEmpty()) { 2007 while (!connections.isEmpty()) {
2018 _SocketConnection socketConn = connections.removeFirst(); 2008 _SocketConnection socketConn = connections.removeFirst();
2019 socketConn._socket.close(); 2009 socketConn._socket.close();
2020 } 2010 }
2021 }); 2011 });
2022 _activeSockets.forEach((_SocketConnection socketConn) { 2012 _activeSockets.forEach((_SocketConnection socketConn) {
2023 socketConn._socket.close(); 2013 socketConn._socket.close();
2024 }); 2014 });
2025 if (_evictionTimer != null) _cancelEvictionTimer(); 2015 if (_evictionTimer != null) _cancelEvictionTimer();
2026 _shutdown = true; 2016 _shutdown = true;
2027 } 2017 }
2028 2018
2029 void _cancelEvictionTimer() { 2019 void _cancelEvictionTimer() {
2030 _evictionTimer.cancel(); 2020 _evictionTimer.cancel();
2031 _evictionTimer = null; 2021 _evictionTimer = null;
2032 } 2022 }
2033 2023
2034 String _connectionKey(String host, int port) { 2024 String _connectionKey(String host, int port) {
2035 return "$host:$port"; 2025 return "$host:$port";
2036 } 2026 }
2037 2027
2038 HttpClientConnection _prepareHttpClientConnection( 2028 HttpClientConnection _prepareHttpClientConnection(
2039 String host,
2040 int port,
2041 String method, 2029 String method,
2042 String path, 2030 Uri url,
2043 [_HttpClientConnection connection]) { 2031 [_HttpClientConnection connection]) {
2044 2032
2045 void _connectionOpened(_SocketConnection socketConn, 2033 void _connectionOpened(_SocketConnection socketConn,
2046 _HttpClientConnection connection) { 2034 _HttpClientConnection connection) {
2047 connection._connectionEstablished(socketConn); 2035 connection._connectionEstablished(socketConn);
2036 String path;
2037 if (url.query != "") {
2038 if (url.fragment != "") {
2039 path = "${url.path}?${url.query}#${url.fragment}";
2040 } else {
2041 path = "${url.path}?${url.query}";
2042 }
2043 } else {
2044 path = url.path;
2045 }
2048 HttpClientRequest request = connection.open(method, path); 2046 HttpClientRequest request = connection.open(method, path);
2049 request.headers.host = host; 2047 request.headers.host = socketConn._host;
Anders Johnsen 2012/10/04 13:15:29 Why not url.domain/url.port?
Søren Gjesse 2012/10/04 14:11:48 That was wrong fixed.
2050 request.headers.port = port; 2048 request.headers.port = socketConn._port;
2051 if (connection._onRequest != null) { 2049 if (connection._onRequest != null) {
2052 connection._onRequest(request); 2050 connection._onRequest(request);
2053 } else { 2051 } else {
2054 request.outputStream.close(); 2052 request.outputStream.close();
2055 } 2053 }
2056 } 2054 }
2057 2055
2058 // Create a new connection if we are not re-using an existing one. 2056 // Create a new connection if we are not re-using an existing one.
2059 if (connection == null) { 2057 if (connection == null) {
2060 connection = new _HttpClientConnection(this); 2058 connection = new _HttpClientConnection(this);
2061 } 2059 }
2062 connection.onDetach = () => _activeSockets.remove(connection._socketConn); 2060 connection.onDetach = () => _activeSockets.remove(connection._socketConn);
2063 2061
2064 // If there are active connections for this key get the first one 2062 // If there are active connections for this key get the first one
2065 // otherwise create a new one. 2063 // otherwise create a new one.
2064 String host = url.domain;
2065 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port;
2066 String key = _connectionKey(host, port); 2066 String key = _connectionKey(host, port);
2067 Queue socketConnections = _openSockets[key]; 2067 Queue socketConnections = _openSockets[key];
2068 if (socketConnections == null || socketConnections.isEmpty()) { 2068 if (socketConnections == null || socketConnections.isEmpty()) {
2069 Socket socket = new Socket(host, port); 2069 Socket socket = new Socket(host, port);
2070 // Until the connection is established handle connection errors 2070 // Until the connection is established handle connection errors
2071 // here as the HttpClientConnection object is not yet associated 2071 // here as the HttpClientConnection object is not yet associated
2072 // with the socket. 2072 // with the socket.
2073 socket.onError = (e) { 2073 socket.onError = (e) {
2074 // Report the error through the HttpClientConnection object to 2074 // Report the error through the HttpClientConnection object to
2075 // the client. 2075 // the client.
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
2151 2151
2152 // Return connection. 2152 // Return connection.
2153 _activeSockets.remove(socketConn); 2153 _activeSockets.remove(socketConn);
2154 sockets.addFirst(socketConn); 2154 sockets.addFirst(socketConn);
2155 } 2155 }
2156 2156
2157 Function _onOpen; 2157 Function _onOpen;
2158 Map<String, Queue<_SocketConnection>> _openSockets; 2158 Map<String, Queue<_SocketConnection>> _openSockets;
2159 Set<_SocketConnection> _activeSockets; 2159 Set<_SocketConnection> _activeSockets;
2160 Timer _evictionTimer; 2160 Timer _evictionTimer;
2161 Function _findProxy;
Anders Johnsen 2012/10/04 13:15:29 I guess this is an (too) early start on proxy? :)
2161 bool _shutdown; // Has this HTTP client been shutdown? 2162 bool _shutdown; // Has this HTTP client been shutdown?
2162 } 2163 }
2163 2164
2164 2165
2165 class _HttpConnectionInfo implements HttpConnectionInfo { 2166 class _HttpConnectionInfo implements HttpConnectionInfo {
2166 String remoteHost; 2167 String remoteHost;
2167 int remotePort; 2168 int remotePort;
2168 int localPort; 2169 int localPort;
2169 } 2170 }
2170 2171
2171 2172
2172 class _DetachedSocket implements DetachedSocket { 2173 class _DetachedSocket implements DetachedSocket {
2173 _DetachedSocket(this._socket, this._unparsedData); 2174 _DetachedSocket(this._socket, this._unparsedData);
2174 Socket get socket => _socket; 2175 Socket get socket => _socket;
2175 List<int> get unparsedData => _unparsedData; 2176 List<int> get unparsedData => _unparsedData;
2176 Socket _socket; 2177 Socket _socket;
2177 List<int> _unparsedData; 2178 List<int> _unparsedData;
2178 } 2179 }
2179 2180
2180 2181
2181 class _RedirectInfo implements RedirectInfo { 2182 class _RedirectInfo implements RedirectInfo {
2182 const _RedirectInfo(int this.statusCode, 2183 const _RedirectInfo(int this.statusCode,
2183 String this.method, 2184 String this.method,
2184 Uri this.location); 2185 Uri this.location);
2185 final int statusCode; 2186 final int statusCode;
2186 final String method; 2187 final String method;
2187 final Uri location; 2188 final Uri location;
2188 } 2189 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698