|
1 /* Licensed to the Apache Software Foundation (ASF) under one or more |
|
2 * contributor license agreements. See the NOTICE file distributed with |
|
3 * this work for additional information regarding copyright ownership. |
|
4 * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5 * (the "License"); you may not use this file except in compliance with |
|
6 * the License. You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 |
|
17 // This code is based on AOSP /libcore/luni/src/main/java/java/net/ProxySelectorImpl.java |
|
18 |
|
19 package org.mozilla.gecko.util; |
|
20 |
|
21 import android.text.TextUtils; |
|
22 |
|
23 import java.io.IOException; |
|
24 import java.net.InetSocketAddress; |
|
25 import java.net.Proxy; |
|
26 |
|
27 public class ProxySelector { |
|
28 public ProxySelector() { |
|
29 } |
|
30 |
|
31 public Proxy select(String scheme, String host) { |
|
32 int port = -1; |
|
33 Proxy proxy = null; |
|
34 String nonProxyHostsKey = null; |
|
35 boolean httpProxyOkay = true; |
|
36 if ("http".equalsIgnoreCase(scheme)) { |
|
37 port = 80; |
|
38 nonProxyHostsKey = "http.nonProxyHosts"; |
|
39 proxy = lookupProxy("http.proxyHost", "http.proxyPort", Proxy.Type.HTTP, port); |
|
40 } else if ("https".equalsIgnoreCase(scheme)) { |
|
41 port = 443; |
|
42 nonProxyHostsKey = "https.nonProxyHosts"; // RI doesn't support this |
|
43 proxy = lookupProxy("https.proxyHost", "https.proxyPort", Proxy.Type.HTTP, port); |
|
44 } else if ("ftp".equalsIgnoreCase(scheme)) { |
|
45 port = 80; // not 21 as you might guess |
|
46 nonProxyHostsKey = "ftp.nonProxyHosts"; |
|
47 proxy = lookupProxy("ftp.proxyHost", "ftp.proxyPort", Proxy.Type.HTTP, port); |
|
48 } else if ("socket".equalsIgnoreCase(scheme)) { |
|
49 httpProxyOkay = false; |
|
50 } else { |
|
51 return Proxy.NO_PROXY; |
|
52 } |
|
53 |
|
54 if (nonProxyHostsKey != null |
|
55 && isNonProxyHost(host, System.getProperty(nonProxyHostsKey))) { |
|
56 return Proxy.NO_PROXY; |
|
57 } |
|
58 |
|
59 if (proxy != null) { |
|
60 return proxy; |
|
61 } |
|
62 |
|
63 if (httpProxyOkay) { |
|
64 proxy = lookupProxy("proxyHost", "proxyPort", Proxy.Type.HTTP, port); |
|
65 if (proxy != null) { |
|
66 return proxy; |
|
67 } |
|
68 } |
|
69 |
|
70 proxy = lookupProxy("socksProxyHost", "socksProxyPort", Proxy.Type.SOCKS, 1080); |
|
71 if (proxy != null) { |
|
72 return proxy; |
|
73 } |
|
74 |
|
75 return Proxy.NO_PROXY; |
|
76 } |
|
77 |
|
78 /** |
|
79 * Returns the proxy identified by the {@code hostKey} system property, or |
|
80 * null. |
|
81 */ |
|
82 private Proxy lookupProxy(String hostKey, String portKey, Proxy.Type type, int defaultPort) { |
|
83 String host = System.getProperty(hostKey); |
|
84 if (TextUtils.isEmpty(host)) { |
|
85 return null; |
|
86 } |
|
87 |
|
88 int port = getSystemPropertyInt(portKey, defaultPort); |
|
89 return new Proxy(type, InetSocketAddress.createUnresolved(host, port)); |
|
90 } |
|
91 |
|
92 private int getSystemPropertyInt(String key, int defaultValue) { |
|
93 String string = System.getProperty(key); |
|
94 if (string != null) { |
|
95 try { |
|
96 return Integer.parseInt(string); |
|
97 } catch (NumberFormatException ignored) { |
|
98 } |
|
99 } |
|
100 return defaultValue; |
|
101 } |
|
102 |
|
103 /** |
|
104 * Returns true if the {@code nonProxyHosts} system property pattern exists |
|
105 * and matches {@code host}. |
|
106 */ |
|
107 private boolean isNonProxyHost(String host, String nonProxyHosts) { |
|
108 if (host == null || nonProxyHosts == null) { |
|
109 return false; |
|
110 } |
|
111 |
|
112 // construct pattern |
|
113 StringBuilder patternBuilder = new StringBuilder(); |
|
114 for (int i = 0; i < nonProxyHosts.length(); i++) { |
|
115 char c = nonProxyHosts.charAt(i); |
|
116 switch (c) { |
|
117 case '.': |
|
118 patternBuilder.append("\\."); |
|
119 break; |
|
120 case '*': |
|
121 patternBuilder.append(".*"); |
|
122 break; |
|
123 default: |
|
124 patternBuilder.append(c); |
|
125 } |
|
126 } |
|
127 // check whether the host is the nonProxyHosts. |
|
128 String pattern = patternBuilder.toString(); |
|
129 return host.matches(pattern); |
|
130 } |
|
131 } |
|
132 |