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 // The close queue handles graceful closing of HTTP connections. When | 5 // The close queue handles graceful closing of HTTP connections. When |
| 6 // a connection is added to the queue it will enter a wait state | 6 // a connection is added to the queue it will enter a wait state |
| 7 // waiting for all data written and possibly socket shutdown from | 7 // waiting for all data written and possibly socket shutdown from |
| 8 // peer. | 8 // peer. |
| 9 class _CloseQueue { | 9 class _CloseQueue { |
| 10 _CloseQueue() : _q = new Set<_HttpConnectionBase>(); | 10 _CloseQueue() : _q = new Set<_HttpConnectionBase>(); |
| (...skipping 1686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1697 return _prepareHttpClientConnection(method, uri, connection); | 1697 return _prepareHttpClientConnection(method, uri, connection); |
| 1698 } | 1698 } |
| 1699 | 1699 |
| 1700 HttpClientConnection openUrl(String method, Uri url) { | 1700 HttpClientConnection openUrl(String method, Uri url) { |
| 1701 return _openUrl(method, url); | 1701 return _openUrl(method, url); |
| 1702 } | 1702 } |
| 1703 | 1703 |
| 1704 HttpClientConnection _openUrl(String method, | 1704 HttpClientConnection _openUrl(String method, |
| 1705 Uri url, | 1705 Uri url, |
| 1706 [_HttpClientConnection connection]) { | 1706 [_HttpClientConnection connection]) { |
| 1707 if (url.scheme != "http") { | 1707 if (url.scheme != "http" && url.scheme != "https") { |
| 1708 throw new HttpException("Unsupported URL scheme ${url.scheme}"); | 1708 throw new HttpException("Unsupported URL scheme ${url.scheme}"); |
| 1709 } | 1709 } |
| 1710 return _open(method, url, connection); | 1710 return _open(method, url, connection); |
| 1711 } | 1711 } |
| 1712 | 1712 |
| 1713 HttpClientConnection get(String host, int port, String path) { | 1713 HttpClientConnection get(String host, int port, String path) { |
| 1714 return open("GET", host, port, path); | 1714 return open("GET", host, port, path); |
| 1715 } | 1715 } |
| 1716 | 1716 |
| 1717 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url); | 1717 HttpClientConnection getUrl(Uri url) => _openUrl("GET", url); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1762 | 1762 |
| 1763 HttpClientConnection _prepareHttpClientConnection( | 1763 HttpClientConnection _prepareHttpClientConnection( |
| 1764 String method, | 1764 String method, |
| 1765 Uri url, | 1765 Uri url, |
| 1766 [_HttpClientConnection connection]) { | 1766 [_HttpClientConnection connection]) { |
| 1767 | 1767 |
| 1768 void _establishConnection(String host, | 1768 void _establishConnection(String host, |
| 1769 int port, | 1769 int port, |
| 1770 _ProxyConfiguration proxyConfiguration, | 1770 _ProxyConfiguration proxyConfiguration, |
| 1771 int proxyIndex, | 1771 int proxyIndex, |
| 1772 bool reusedConnection) { | 1772 bool reusedConnection, |
| 1773 bool secure) { | |
| 1773 | 1774 |
| 1774 void _connectionOpened(_SocketConnection socketConn, | 1775 void _connectionOpened(_SocketConnection socketConn, |
| 1775 _HttpClientConnection connection, | 1776 _HttpClientConnection connection, |
| 1776 bool usingProxy) { | 1777 bool usingProxy) { |
| 1777 socketConn._httpClientConnection = connection; | 1778 socketConn._httpClientConnection = connection; |
| 1778 connection._usingProxy = usingProxy; | 1779 connection._usingProxy = usingProxy; |
| 1779 connection._connectionEstablished(socketConn); | 1780 connection._connectionEstablished(socketConn); |
| 1780 HttpClientRequest request = connection.open(method, url); | 1781 HttpClientRequest request = connection.open(method, url); |
| 1781 request.headers.host = host; | 1782 request.headers.host = host; |
| 1782 request.headers.port = port; | 1783 request.headers.port = port; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1814 connectPort = port; | 1815 connectPort = port; |
| 1815 } else { | 1816 } else { |
| 1816 connectHost = proxy.host; | 1817 connectHost = proxy.host; |
| 1817 connectPort = proxy.port; | 1818 connectPort = proxy.port; |
| 1818 } | 1819 } |
| 1819 | 1820 |
| 1820 // If there are active connections for this key get the first one | 1821 // If there are active connections for this key get the first one |
| 1821 // otherwise create a new one. | 1822 // otherwise create a new one. |
| 1822 String key = _connectionKey(connectHost, connectPort); | 1823 String key = _connectionKey(connectHost, connectPort); |
| 1823 Queue socketConnections = _openSockets[key]; | 1824 Queue socketConnections = _openSockets[key]; |
| 1825 // Remove active connections that are of the wrong type (HTTP or HTTPS). | |
| 1826 while (socketConnections != null && | |
| 1827 !socketConnections.isEmpty && | |
| 1828 secure != (socketConnections.first._socket is SecureSocket)) { | |
| 1829 socketConnection.removeFirst()._close(); | |
| 1830 } | |
| 1824 if (socketConnections == null || socketConnections.isEmpty) { | 1831 if (socketConnections == null || socketConnections.isEmpty) { |
| 1825 Socket socket = new Socket(connectHost, connectPort); | 1832 Socket socket = secure ? new SecureSocket(connectHost, connectPort) : |
| 1833 new Socket(connectHost, connectPort); | |
| 1826 // Until the connection is established handle connection errors | 1834 // Until the connection is established handle connection errors |
| 1827 // here as the HttpClientConnection object is not yet associated | 1835 // here as the HttpClientConnection object is not yet associated |
| 1828 // with the socket. | 1836 // with the socket. |
| 1829 socket.onError = (e) { | 1837 socket.onError = (e) { |
| 1830 proxyIndex++; | 1838 proxyIndex++; |
| 1831 if (proxyIndex < proxyConfiguration.proxies.length) { | 1839 if (proxyIndex < proxyConfiguration.proxies.length) { |
| 1832 // Try the next proxy in the list. | 1840 // Try the next proxy in the list. |
| 1833 _establishConnection( | 1841 _establishConnection( |
| 1834 host, port, proxyConfiguration, proxyIndex, false); | 1842 host, port, proxyConfiguration, proxyIndex, false); |
| 1835 } else { | 1843 } else { |
| 1836 // Report the error through the HttpClientConnection object to | 1844 // Report the error through the HttpClientConnection object to |
| 1837 // the client. | 1845 // the client. |
| 1838 connection._onError(e); | 1846 connection._onError(e); |
| 1839 } | 1847 } |
| 1840 }; | 1848 }; |
| 1841 socket.onConnect = () { | 1849 socket.onConnect = () { |
| 1842 // When the connection is established, clear the error | 1850 // When the connection is established, clear the error |
| 1843 // callback as it will now be handled by the | 1851 // callback as it will now be handled by the |
| 1844 // HttpClientConnection object which will be associated with | 1852 // HttpClientConnection object which will be associated with |
| 1845 // the connected socket. | 1853 // the connected socket. |
| 1846 socket.onError = null; | 1854 socket.onError = null; |
| 1847 _SocketConnection socketConn = | 1855 _SocketConnection socketConn = |
| 1848 new _SocketConnection(connectHost, connectPort, socket); | 1856 new _SocketConnection(connectHost, connectPort, socket); |
| 1849 _activeSockets.add(socketConn); | 1857 _activeSockets.add(socketConn); |
| 1850 _connectionOpened(socketConn, connection, !proxy.isDirect); | 1858 _connectionOpened(socketConn, connection, !proxy.isDirect); |
| 1851 }; | 1859 }; |
| 1852 } else { | 1860 } else { |
| 1853 _SocketConnection socketConn = socketConnections.removeFirst(); | 1861 _SocketConnection socketConn = socketConnections.removeFirst(); |
| 1854 _activeSockets.add(socketConn); | 1862 _activeSockets.add(socketConn); |
| 1855 new Timer(0, (ignored) => | 1863 new Timer(0, (ignored) => |
| 1856 _connectionOpened(socketConn, connection, !proxy.isDirect)); | 1864 _connectionOpened(socketConn, connection, !proxy.isDirect)); |
| 1857 | 1865 |
| 1858 // Get rid of eviction timer if there are no more active connections. | 1866 // Get rid of eviction timer if there are no more active connections. |
| 1859 if (socketConnections.isEmpty) _openSockets.remove(key); | 1867 if (socketConnections.isEmpty) _openSockets.remove(key); |
| 1860 if (_openSockets.isEmpty) _cancelEvictionTimer(); | 1868 if (_openSockets.isEmpty) _cancelEvictionTimer(); |
| 1861 } | 1869 } |
| 1862 } | 1870 } |
| 1863 | 1871 |
| 1872 // Find out if we want a secure socket. | |
| 1873 bool secure = (url.scheme == "https"); | |
|
Mads Ager (google)
2012/11/29 08:29:12
I would remove the extra parenthesis here.
| |
| 1874 | |
| 1864 // Find the TCP host and port. | 1875 // Find the TCP host and port. |
| 1865 String host = url.domain; | 1876 String host = url.domain; |
| 1866 int port = url.port == 0 ? HttpClient.DEFAULT_HTTP_PORT : url.port; | 1877 int port = url.port; |
| 1867 | 1878 if (port == 0) { |
| 1879 port = secure ? | |
| 1880 HttpClient.DEFAULT_HTTPS_PORT : | |
| 1881 HttpClient.DEFAULT_HTTP_PORT; | |
| 1882 } | |
| 1868 // Create a new connection object if we are not re-using an existing one. | 1883 // Create a new connection object if we are not re-using an existing one. |
| 1869 var reusedConnection = false; | 1884 var reusedConnection = false; |
| 1870 if (connection == null) { | 1885 if (connection == null) { |
| 1871 connection = new _HttpClientConnection(this); | 1886 connection = new _HttpClientConnection(this); |
| 1872 } else { | 1887 } else { |
| 1873 reusedConnection = true; | 1888 reusedConnection = true; |
| 1874 } | 1889 } |
| 1875 connection.onDetach = () => _activeSockets.remove(connection._socketConn); | 1890 connection.onDetach = () => _activeSockets.remove(connection._socketConn); |
| 1876 | 1891 |
| 1877 // Check to see if a proxy server should be used for this connection. | 1892 // Check to see if a proxy server should be used for this connection. |
| 1878 _ProxyConfiguration proxyConfiguration = const _ProxyConfiguration.direct(); | 1893 _ProxyConfiguration proxyConfiguration = const _ProxyConfiguration.direct(); |
| 1879 if (_findProxy != null) { | 1894 if (_findProxy != null) { |
| 1880 // TODO(sgjesse): Keep a map of these as normally only a few | 1895 // TODO(sgjesse): Keep a map of these as normally only a few |
| 1881 // configuration strings will be used. | 1896 // configuration strings will be used. |
| 1882 proxyConfiguration = new _ProxyConfiguration(_findProxy(url)); | 1897 proxyConfiguration = new _ProxyConfiguration(_findProxy(url)); |
| 1883 } | 1898 } |
| 1884 | 1899 |
| 1885 // Establish the connection starting with the first proxy configured. | 1900 // Establish the connection starting with the first proxy configured. |
| 1886 _establishConnection(host, port, proxyConfiguration, 0, reusedConnection); | 1901 _establishConnection(host, |
| 1902 port, | |
| 1903 proxyConfiguration, | |
| 1904 0, | |
| 1905 reusedConnection, | |
| 1906 secure); | |
| 1887 | 1907 |
| 1888 return connection; | 1908 return connection; |
| 1889 } | 1909 } |
| 1890 | 1910 |
| 1891 void _returnSocketConnection(_SocketConnection socketConn) { | 1911 void _returnSocketConnection(_SocketConnection socketConn) { |
| 1892 // If the HTTP client is being shutdown don't return the connection. | 1912 // If the HTTP client is being shutdown don't return the connection. |
| 1893 if (_shutdown) { | 1913 if (_shutdown) { |
| 1894 socketConn._close(); | 1914 socketConn._close(); |
| 1895 return; | 1915 return; |
| 1896 }; | 1916 }; |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2105 | 2125 |
| 2106 | 2126 |
| 2107 class _RedirectInfo implements RedirectInfo { | 2127 class _RedirectInfo implements RedirectInfo { |
| 2108 const _RedirectInfo(int this.statusCode, | 2128 const _RedirectInfo(int this.statusCode, |
| 2109 String this.method, | 2129 String this.method, |
| 2110 Uri this.location); | 2130 Uri this.location); |
| 2111 final int statusCode; | 2131 final int statusCode; |
| 2112 final String method; | 2132 final String method; |
| 2113 final Uri location; | 2133 final Uri location; |
| 2114 } | 2134 } |
| OLD | NEW |