1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/util/ProxySelector.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +/* Licensed to the Apache Software Foundation (ASF) under one or more 1.5 + * contributor license agreements. See the NOTICE file distributed with 1.6 + * this work for additional information regarding copyright ownership. 1.7 + * The ASF licenses this file to You under the Apache License, Version 2.0 1.8 + * (the "License"); you may not use this file except in compliance with 1.9 + * the License. You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +// This code is based on AOSP /libcore/luni/src/main/java/java/net/ProxySelectorImpl.java 1.21 + 1.22 +package org.mozilla.gecko.util; 1.23 + 1.24 +import android.text.TextUtils; 1.25 + 1.26 +import java.io.IOException; 1.27 +import java.net.InetSocketAddress; 1.28 +import java.net.Proxy; 1.29 + 1.30 +public class ProxySelector { 1.31 + public ProxySelector() { 1.32 + } 1.33 + 1.34 + public Proxy select(String scheme, String host) { 1.35 + int port = -1; 1.36 + Proxy proxy = null; 1.37 + String nonProxyHostsKey = null; 1.38 + boolean httpProxyOkay = true; 1.39 + if ("http".equalsIgnoreCase(scheme)) { 1.40 + port = 80; 1.41 + nonProxyHostsKey = "http.nonProxyHosts"; 1.42 + proxy = lookupProxy("http.proxyHost", "http.proxyPort", Proxy.Type.HTTP, port); 1.43 + } else if ("https".equalsIgnoreCase(scheme)) { 1.44 + port = 443; 1.45 + nonProxyHostsKey = "https.nonProxyHosts"; // RI doesn't support this 1.46 + proxy = lookupProxy("https.proxyHost", "https.proxyPort", Proxy.Type.HTTP, port); 1.47 + } else if ("ftp".equalsIgnoreCase(scheme)) { 1.48 + port = 80; // not 21 as you might guess 1.49 + nonProxyHostsKey = "ftp.nonProxyHosts"; 1.50 + proxy = lookupProxy("ftp.proxyHost", "ftp.proxyPort", Proxy.Type.HTTP, port); 1.51 + } else if ("socket".equalsIgnoreCase(scheme)) { 1.52 + httpProxyOkay = false; 1.53 + } else { 1.54 + return Proxy.NO_PROXY; 1.55 + } 1.56 + 1.57 + if (nonProxyHostsKey != null 1.58 + && isNonProxyHost(host, System.getProperty(nonProxyHostsKey))) { 1.59 + return Proxy.NO_PROXY; 1.60 + } 1.61 + 1.62 + if (proxy != null) { 1.63 + return proxy; 1.64 + } 1.65 + 1.66 + if (httpProxyOkay) { 1.67 + proxy = lookupProxy("proxyHost", "proxyPort", Proxy.Type.HTTP, port); 1.68 + if (proxy != null) { 1.69 + return proxy; 1.70 + } 1.71 + } 1.72 + 1.73 + proxy = lookupProxy("socksProxyHost", "socksProxyPort", Proxy.Type.SOCKS, 1080); 1.74 + if (proxy != null) { 1.75 + return proxy; 1.76 + } 1.77 + 1.78 + return Proxy.NO_PROXY; 1.79 + } 1.80 + 1.81 + /** 1.82 + * Returns the proxy identified by the {@code hostKey} system property, or 1.83 + * null. 1.84 + */ 1.85 + private Proxy lookupProxy(String hostKey, String portKey, Proxy.Type type, int defaultPort) { 1.86 + String host = System.getProperty(hostKey); 1.87 + if (TextUtils.isEmpty(host)) { 1.88 + return null; 1.89 + } 1.90 + 1.91 + int port = getSystemPropertyInt(portKey, defaultPort); 1.92 + return new Proxy(type, InetSocketAddress.createUnresolved(host, port)); 1.93 + } 1.94 + 1.95 + private int getSystemPropertyInt(String key, int defaultValue) { 1.96 + String string = System.getProperty(key); 1.97 + if (string != null) { 1.98 + try { 1.99 + return Integer.parseInt(string); 1.100 + } catch (NumberFormatException ignored) { 1.101 + } 1.102 + } 1.103 + return defaultValue; 1.104 + } 1.105 + 1.106 + /** 1.107 + * Returns true if the {@code nonProxyHosts} system property pattern exists 1.108 + * and matches {@code host}. 1.109 + */ 1.110 + private boolean isNonProxyHost(String host, String nonProxyHosts) { 1.111 + if (host == null || nonProxyHosts == null) { 1.112 + return false; 1.113 + } 1.114 + 1.115 + // construct pattern 1.116 + StringBuilder patternBuilder = new StringBuilder(); 1.117 + for (int i = 0; i < nonProxyHosts.length(); i++) { 1.118 + char c = nonProxyHosts.charAt(i); 1.119 + switch (c) { 1.120 + case '.': 1.121 + patternBuilder.append("\\."); 1.122 + break; 1.123 + case '*': 1.124 + patternBuilder.append(".*"); 1.125 + break; 1.126 + default: 1.127 + patternBuilder.append(c); 1.128 + } 1.129 + } 1.130 + // check whether the host is the nonProxyHosts. 1.131 + String pattern = patternBuilder.toString(); 1.132 + return host.matches(pattern); 1.133 + } 1.134 +} 1.135 +