Index: modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java =================================================================== --- modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java (revision 410068) +++ modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java (working copy) @@ -92,6 +92,8 @@ private boolean hasTriedCache = false; + private boolean usingProxy = false; + private HttpOutputStream os; private boolean sentRequest = false; @@ -523,22 +525,64 @@ if (getFromCache()) { return; } - Socket socket; - int connectTimeout = getConnectTimeout(); - InetAddress host = getHostAddress(); - int port = getHostPort(); - SocketAddress sa = new InetSocketAddress(host, port); - if (null == currentProxy || Proxy.Type.HTTP == currentProxy.type()) { - socket = new Socket(); + // socket to be used for connection + Socket socket = null; + // try to determine: to use the proxy or not + if (proxy != null) { + // try to make the connection to the proxy + // specified in constructor. + // IOException will be thrown in the case of failure + if (proxy.type() == Proxy.Type.HTTP) { + socket = getHTTPConnection(proxy.address()); + } else { + // using DIRECT or SOCKS proxy + socket = new Socket(proxy); + socket.connect( + new InetSocketAddress(url.getHost(), url.getPort())); + } } else { - socket = new Socket(currentProxy); + // Use system-wide ProxySelect to select proxy list, + // then try to connect via elements in the proxy list. + ProxySelector selector = ProxySelector.getDefault(); + List proxyList = selector.select(uri); + if (proxyList != null) { + for (Proxy selectedProxy: proxyList) { + try { + socket = getHTTPConnection(selectedProxy.address()); + proxy = selectedProxy; + break; // connected + } catch (IOException e) { + socket = null; + // failed to connect, tell it to the selector + selector.connectFailed( + uri, selectedProxy.address(), e); + } + } + } } - socket.connect(sa, connectTimeout); + if (socket != null) { + // connection was made through the proxy + usingProxy = true; + } else { + // make direct connection + socket = getHTTPConnection( + new InetSocketAddress(url.getHost(), url.getPort())); + } socket.setSoTimeout(getReadTimeout()); connected = true; socketOut = socket.getOutputStream(); is = new BufferedInputStream(socket.getInputStream()); } + + /** + * Returns the socket to be used for this HTTP connection. + * TODO: implement persistent connections. + */ + protected Socket getHTTPConnection(SocketAddress address) throws IOException { + Socket socket = new Socket(); + socket.connect(address, getConnectTimeout()); + return socket; + } // Tries to get head and body from cache, return true if has got this time or // already got before @@ -1194,20 +1238,9 @@ * if a proxy port has been set. */ private int getHostPort() { - if (usingProxy()) { - if (null == currentProxy) { - // get from system property - String portString = getSystemPropertyOrAlternative( - "http.proxyPort", "proxyPort"); - if (portString != null) { - hostPort = Integer.parseInt(portString); - } - } else { - // get from proxy - InetSocketAddress addr = (InetSocketAddress) currentProxy - .address(); - hostPort = addr.getPort(); - } + int hostPort; + if (proxy != null) { + hostPort = ((InetSocketAddress) proxy.address()).getPort(); } else { hostPort = url.getPort(); } @@ -1222,7 +1255,11 @@ * given in the URL or the address of the proxy server. */ private InetAddress getHostAddress() throws IOException { - return InetAddress.getByName(getHostName()); + if (proxy != null) { + return ((InetSocketAddress) proxy.address()).getAddress(); + } else { + return InetAddress.getByName(url.getHost()); + } } /** @@ -1230,21 +1267,11 @@ * in the URL or the name of the proxy server. */ private String getHostName() { - if (proxyName != null) { - return proxyName; + if (proxy != null) { + return ((InetSocketAddress) proxy.address()).getHostName(); + } else { + return url.getHost(); } - if (usingProxy()) { - if (null == currentProxy) { - proxyName = getSystemPropertyOrAlternative("http.proxyHost", - "proxyHost"); - } else { - InetSocketAddress addr = (InetSocketAddress) currentProxy - .address(); - proxyName = addr.getHostName(); - } - return proxyName; - } - return url.getHost(); } private String getSystemPropertyOrAlternative(final String key, @@ -1268,27 +1295,7 @@ * 1.0 and JDK 1.1 */ public boolean usingProxy() { - if (Proxy.NO_PROXY == currentProxy) { - return false; - } - // using proxy if http proxy is set by caller. - if (null != currentProxy && Proxy.Type.HTTP == currentProxy.type()) { - return true; - } - // First check whether the user explicitly set whether to use a proxy. - String proxySet = getSystemProperty("http.proxySet"); - if (proxySet != null) - return proxySet.toLowerCase().equals("true"); - - proxySet = getSystemProperty("proxySet"); - if (proxySet != null) - return proxySet.toLowerCase().equals("true"); - - // The user didn't explicitly set whether to use a proxy. Answer true if - // the user specified a proxyHost. - if (getSystemProperty("http.proxyHost") != null) - return true; - return getSystemProperty("proxyHost") != null; + return usingProxy; } /** @@ -1306,41 +1313,7 @@ } return; } - // Use system-wide ProxySelect to select proxy list, - // then try to connect via elements in the proxy list. - if (null != proxy) { - proxyList = new ArrayList(1); - proxyList.add(proxy); - } else { - proxyList = NetUtil.getProxyList(uri); - } - if (null == proxyList) { - currentProxy = null; - doRequestInternal(); - } else { - // try the proxy list until one of them establish - // the connection successfully. - ProxySelector selector = ProxySelector.getDefault(); - Iterator iter = proxyList.iterator(); - boolean doRequestOK = false; - while (iter.hasNext() && !doRequestOK) { - currentProxy = (Proxy) iter.next(); - try { - doRequestInternal(); - doRequestOK = true; - } catch (IOException ioe) { - // if connect failed, callback method "connectFailed" - // should be invoked. - if (null != selector && Proxy.NO_PROXY != currentProxy) { - selector - .connectFailed(uri, currentProxy.address(), ioe); - } - } - } - if (!doRequestOK) { - throw new IOException(Msg.getString("K0097")); - } - } + doRequestInternal(); } void doRequestInternal() throws IOException {