Chromium Code Reviews| 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 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 Loading... | |
| 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) { |
|
floitsch
2012/10/22 17:09:26
should the "== null" go away?
Søren Gjesse
2012/10/23 09:37:23
Absolutely - good catch.
| |
| 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 |
| 2033 String host = url.domain; | |
| 2034 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port; | |
| 2035 | |
| 2045 void _connectionOpened(_SocketConnection socketConn, | 2036 void _connectionOpened(_SocketConnection socketConn, |
| 2046 _HttpClientConnection connection) { | 2037 _HttpClientConnection connection) { |
| 2047 connection._connectionEstablished(socketConn); | 2038 connection._connectionEstablished(socketConn); |
| 2039 String path; | |
| 2040 if (url.query != "") { | |
| 2041 if (url.fragment != "") { | |
| 2042 path = "${url.path}?${url.query}#${url.fragment}"; | |
| 2043 } else { | |
| 2044 path = "${url.path}?${url.query}"; | |
| 2045 } | |
| 2046 } else { | |
| 2047 path = url.path; | |
| 2048 } | |
| 2048 HttpClientRequest request = connection.open(method, path); | 2049 HttpClientRequest request = connection.open(method, path); |
| 2049 request.headers.host = host; | 2050 request.headers.host = host; |
| 2050 request.headers.port = port; | 2051 request.headers.port = port; |
| 2051 if (connection._onRequest != null) { | 2052 if (connection._onRequest != null) { |
| 2052 connection._onRequest(request); | 2053 connection._onRequest(request); |
| 2053 } else { | 2054 } else { |
| 2054 request.outputStream.close(); | 2055 request.outputStream.close(); |
| 2055 } | 2056 } |
| 2056 } | 2057 } |
| 2057 | 2058 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2151 | 2152 |
| 2152 // Return connection. | 2153 // Return connection. |
| 2153 _activeSockets.remove(socketConn); | 2154 _activeSockets.remove(socketConn); |
| 2154 sockets.addFirst(socketConn); | 2155 sockets.addFirst(socketConn); |
| 2155 } | 2156 } |
| 2156 | 2157 |
| 2157 Function _onOpen; | 2158 Function _onOpen; |
| 2158 Map<String, Queue<_SocketConnection>> _openSockets; | 2159 Map<String, Queue<_SocketConnection>> _openSockets; |
| 2159 Set<_SocketConnection> _activeSockets; | 2160 Set<_SocketConnection> _activeSockets; |
| 2160 Timer _evictionTimer; | 2161 Timer _evictionTimer; |
| 2162 Function _findProxy; | |
| 2161 bool _shutdown; // Has this HTTP client been shutdown? | 2163 bool _shutdown; // Has this HTTP client been shutdown? |
| 2162 } | 2164 } |
| 2163 | 2165 |
| 2164 | 2166 |
| 2165 class _HttpConnectionInfo implements HttpConnectionInfo { | 2167 class _HttpConnectionInfo implements HttpConnectionInfo { |
| 2166 String remoteHost; | 2168 String remoteHost; |
| 2167 int remotePort; | 2169 int remotePort; |
| 2168 int localPort; | 2170 int localPort; |
| 2169 } | 2171 } |
| 2170 | 2172 |
| 2171 | 2173 |
| 2172 class _DetachedSocket implements DetachedSocket { | 2174 class _DetachedSocket implements DetachedSocket { |
| 2173 _DetachedSocket(this._socket, this._unparsedData); | 2175 _DetachedSocket(this._socket, this._unparsedData); |
| 2174 Socket get socket => _socket; | 2176 Socket get socket => _socket; |
| 2175 List<int> get unparsedData => _unparsedData; | 2177 List<int> get unparsedData => _unparsedData; |
| 2176 Socket _socket; | 2178 Socket _socket; |
| 2177 List<int> _unparsedData; | 2179 List<int> _unparsedData; |
| 2178 } | 2180 } |
| 2179 | 2181 |
| 2180 | 2182 |
| 2181 class _RedirectInfo implements RedirectInfo { | 2183 class _RedirectInfo implements RedirectInfo { |
| 2182 const _RedirectInfo(int this.statusCode, | 2184 const _RedirectInfo(int this.statusCode, |
| 2183 String this.method, | 2185 String this.method, |
| 2184 Uri this.location); | 2186 Uri this.location); |
| 2185 final int statusCode; | 2187 final int statusCode; |
| 2186 final String method; | 2188 final String method; |
| 2187 final Uri location; | 2189 final Uri location; |
| 2188 } | 2190 } |
| OLD | NEW |