The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * 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 | package android.net; |
| 18 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 23 | import android.text.TextUtils; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 24 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 26 | import java.net.InetSocketAddress; |
| 27 | import java.net.ProxySelector; |
| 28 | import java.net.URI; |
Narayan Kamath | 2b4378f | 2014-11-03 15:54:34 +0000 | [diff] [blame] | 29 | import java.util.List; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 30 | import java.util.regex.Matcher; |
| 31 | import java.util.regex.Pattern; |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | /** |
| 34 | * A convenience class for accessing the user and default proxy |
| 35 | * settings. |
| 36 | */ |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 37 | public final class Proxy { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 39 | private static final String TAG = "Proxy"; |
Dave Bort | fb83907 | 2009-04-09 15:12:58 -0700 | [diff] [blame] | 40 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 41 | private static final ProxySelector sDefaultProxySelector; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 42 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 43 | /** |
Paul Jensen | ad82342 | 2015-03-11 11:51:46 -0400 | [diff] [blame] | 44 | * Used to notify an app that's caching the proxy that either the default |
| 45 | * connection has changed or any connection's proxy has changed. The new |
| 46 | * proxy should be queried using {@link ConnectivityManager#getDefaultProxy()}. |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 47 | * |
| 48 | * <p class="note">This is a protected intent that can only be sent by the system |
| 49 | */ |
| 50 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 51 | public static final String PROXY_CHANGE_ACTION = "android.intent.action.PROXY_CHANGE"; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 52 | /** |
| 53 | * Intent extra included with {@link #PROXY_CHANGE_ACTION} intents. |
| 54 | * It describes the new proxy being used (as a {@link ProxyInfo} object). |
Paul Jensen | ad82342 | 2015-03-11 11:51:46 -0400 | [diff] [blame] | 55 | * @deprecated Because {@code PROXY_CHANGE_ACTION} is sent whenever the proxy |
| 56 | * for any network on the system changes, applications should always use |
| 57 | * {@link ConnectivityManager#getDefaultProxy()} or |
| 58 | * {@link ConnectivityManager#getLinkProperties(Network)}.{@link LinkProperties#getHttpProxy()} |
| 59 | * to get the proxy for the Network(s) they are using. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 60 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 61 | @Deprecated |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 62 | public static final String EXTRA_PROXY_INFO = "android.intent.extra.PROXY_INFO"; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 63 | |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 64 | /** @hide */ |
| 65 | public static final int PROXY_VALID = 0; |
| 66 | /** @hide */ |
| 67 | public static final int PROXY_HOSTNAME_EMPTY = 1; |
| 68 | /** @hide */ |
| 69 | public static final int PROXY_HOSTNAME_INVALID = 2; |
| 70 | /** @hide */ |
| 71 | public static final int PROXY_PORT_EMPTY = 3; |
| 72 | /** @hide */ |
| 73 | public static final int PROXY_PORT_INVALID = 4; |
| 74 | /** @hide */ |
| 75 | public static final int PROXY_EXCLLIST_INVALID = 5; |
| 76 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 77 | private static ConnectivityManager sConnectivityManager = null; |
| 78 | |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 79 | // Hostname / IP REGEX validation |
| 80 | // Matches blank input, ips, and domain names |
| 81 | private static final String NAME_IP_REGEX = |
| 82 | "[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*"; |
| 83 | |
| 84 | private static final String HOSTNAME_REGEXP = "^$|^" + NAME_IP_REGEX + "$"; |
| 85 | |
| 86 | private static final Pattern HOSTNAME_PATTERN; |
| 87 | |
Yuhao Zheng | 3dce5af | 2014-03-03 15:47:06 -0800 | [diff] [blame] | 88 | private static final String EXCL_REGEX = |
| 89 | "[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*(\\.[a-zA-Z0-9*]+(\\-[a-zA-Z0-9*]+)*)*"; |
| 90 | |
| 91 | private static final String EXCLLIST_REGEXP = "^$|^" + EXCL_REGEX + "(," + EXCL_REGEX + ")*$"; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 92 | |
| 93 | private static final Pattern EXCLLIST_PATTERN; |
| 94 | |
| 95 | static { |
| 96 | HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP); |
| 97 | EXCLLIST_PATTERN = Pattern.compile(EXCLLIST_REGEXP); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 98 | sDefaultProxySelector = ProxySelector.getDefault(); |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 101 | /** |
| 102 | * Return the proxy object to be used for the URL given as parameter. |
| 103 | * @param ctx A Context used to get the settings for the proxy host. |
| 104 | * @param url A URL to be accessed. Used to evaluate exclusion list. |
| 105 | * @return Proxy (java.net) object containing the host name. If the |
| 106 | * user did not set a hostname it returns the default host. |
| 107 | * A null value means that no host is to be used. |
| 108 | * {@hide} |
| 109 | */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 110 | @UnsupportedAppUsage |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 111 | public static final java.net.Proxy getProxy(Context ctx, String url) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 112 | String host = ""; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 113 | if ((url != null) && !isLocalHost(host)) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 114 | URI uri = URI.create(url); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 115 | ProxySelector proxySelector = ProxySelector.getDefault(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 116 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 117 | List<java.net.Proxy> proxyList = proxySelector.select(uri); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 118 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 119 | if (proxyList.size() > 0) { |
| 120 | return proxyList.get(0); |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 121 | } |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 122 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 123 | return java.net.Proxy.NO_PROXY; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Return the proxy host set by the user. |
| 129 | * @param ctx A Context used to get the settings for the proxy host. |
| 130 | * @return String containing the host name. If the user did not set a host |
| 131 | * name it returns the default host. A null value means that no |
| 132 | * host is to be used. |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 133 | * @deprecated Use standard java vm proxy values to find the host, port |
| 134 | * and exclusion list. This call ignores the exclusion list. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 136 | @Deprecated |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 137 | public static final String getHost(Context ctx) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 138 | java.net.Proxy proxy = getProxy(ctx, null); |
| 139 | if (proxy == java.net.Proxy.NO_PROXY) return null; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 140 | try { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 141 | return ((InetSocketAddress)(proxy.address())).getHostName(); |
| 142 | } catch (Exception e) { |
| 143 | return null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return the proxy port set by the user. |
| 149 | * @param ctx A Context used to get the settings for the proxy port. |
| 150 | * @return The port number to use or -1 if no proxy is to be used. |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 151 | * @deprecated Use standard java vm proxy values to find the host, port |
| 152 | * and exclusion list. This call ignores the exclusion list. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 154 | @Deprecated |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 155 | public static final int getPort(Context ctx) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 156 | java.net.Proxy proxy = getProxy(ctx, null); |
| 157 | if (proxy == java.net.Proxy.NO_PROXY) return -1; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 158 | try { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 159 | return ((InetSocketAddress)(proxy.address())).getPort(); |
| 160 | } catch (Exception e) { |
| 161 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Return the default proxy host specified by the carrier. |
| 167 | * @return String containing the host name or null if there is no proxy for |
| 168 | * this carrier. |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 169 | * @deprecated Use standard java vm proxy values to find the host, port and |
| 170 | * exclusion list. This call ignores the exclusion list and no |
| 171 | * longer reports only mobile-data apn-based proxy values. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 172 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 173 | @Deprecated |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 174 | public static final String getDefaultHost() { |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 175 | String host = System.getProperty("http.proxyHost"); |
| 176 | if (TextUtils.isEmpty(host)) return null; |
| 177 | return host; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Return the default proxy port specified by the carrier. |
| 182 | * @return The port number to be used with the proxy host or -1 if there is |
| 183 | * no proxy for this carrier. |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 184 | * @deprecated Use standard java vm proxy values to find the host, port and |
| 185 | * exclusion list. This call ignores the exclusion list and no |
| 186 | * longer reports only mobile-data apn-based proxy values. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 188 | @Deprecated |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 189 | public static final int getDefaultPort() { |
Robert Greenwalt | 778c0ba | 2010-12-14 11:55:36 -0800 | [diff] [blame] | 190 | if (getDefaultHost() == null) return -1; |
| 191 | try { |
| 192 | return Integer.parseInt(System.getProperty("http.proxyPort")); |
| 193 | } catch (NumberFormatException e) { |
| 194 | return -1; |
| 195 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 198 | private static final boolean isLocalHost(String host) { |
| 199 | if (host == null) { |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 200 | return false; |
| 201 | } |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 202 | try { |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 203 | if (host != null) { |
Elliott Hughes | 6b7af60 | 2010-11-04 16:46:00 -0700 | [diff] [blame] | 204 | if (host.equalsIgnoreCase("localhost")) { |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 205 | return true; |
| 206 | } |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 207 | if (NetworkUtils.numericToInetAddress(host).isLoopbackAddress()) { |
| 208 | return true; |
Elliott Hughes | 6b7af60 | 2010-11-04 16:46:00 -0700 | [diff] [blame] | 209 | } |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 210 | } |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 211 | } catch (IllegalArgumentException iex) { |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 212 | } |
Andreas Sandblad | 2986f85 | 2010-06-16 13:10:49 +0200 | [diff] [blame] | 213 | return false; |
| 214 | } |
| 215 | |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Validate syntax of hostname, port and exclusion list entries |
| 218 | * {@hide} |
| 219 | */ |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 220 | public static int validate(String hostname, String port, String exclList) { |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 221 | Matcher match = HOSTNAME_PATTERN.matcher(hostname); |
| 222 | Matcher listMatch = EXCLLIST_PATTERN.matcher(exclList); |
| 223 | |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 224 | if (!match.matches()) return PROXY_HOSTNAME_INVALID; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 225 | |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 226 | if (!listMatch.matches()) return PROXY_EXCLLIST_INVALID; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 227 | |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 228 | if (hostname.length() > 0 && port.length() == 0) return PROXY_PORT_EMPTY; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 229 | |
| 230 | if (port.length() > 0) { |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 231 | if (hostname.length() == 0) return PROXY_HOSTNAME_EMPTY; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 232 | int portVal = -1; |
| 233 | try { |
| 234 | portVal = Integer.parseInt(port); |
| 235 | } catch (NumberFormatException ex) { |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 236 | return PROXY_PORT_INVALID; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 237 | } |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 238 | if (portVal <= 0 || portVal > 0xFFFF) return PROXY_PORT_INVALID; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 239 | } |
Yuhao Zheng | 9070484 | 2014-02-28 17:22:45 -0800 | [diff] [blame] | 240 | return PROXY_VALID; |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 241 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 242 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 243 | /** @hide */ |
Mathew Inwood | 53f089f | 2018-08-08 14:44:44 +0100 | [diff] [blame] | 244 | @UnsupportedAppUsage |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 245 | public static final void setHttpProxySystemProperty(ProxyInfo p) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 246 | String host = null; |
| 247 | String port = null; |
| 248 | String exclList = null; |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 249 | Uri pacFileUrl = Uri.EMPTY; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 250 | if (p != null) { |
| 251 | host = p.getHost(); |
| 252 | port = Integer.toString(p.getPort()); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 253 | exclList = p.getExclusionListAsString(); |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 254 | pacFileUrl = p.getPacFileUrl(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 255 | } |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 256 | setHttpProxySystemProperty(host, port, exclList, pacFileUrl); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** @hide */ |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 260 | public static final void setHttpProxySystemProperty(String host, String port, String exclList, |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 261 | Uri pacFileUrl) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 262 | if (exclList != null) exclList = exclList.replace(",", "|"); |
| 263 | if (false) Log.d(TAG, "setHttpProxySystemProperty :"+host+":"+port+" - "+exclList); |
| 264 | if (host != null) { |
| 265 | System.setProperty("http.proxyHost", host); |
| 266 | System.setProperty("https.proxyHost", host); |
| 267 | } else { |
| 268 | System.clearProperty("http.proxyHost"); |
| 269 | System.clearProperty("https.proxyHost"); |
| 270 | } |
| 271 | if (port != null) { |
| 272 | System.setProperty("http.proxyPort", port); |
| 273 | System.setProperty("https.proxyPort", port); |
| 274 | } else { |
| 275 | System.clearProperty("http.proxyPort"); |
| 276 | System.clearProperty("https.proxyPort"); |
| 277 | } |
| 278 | if (exclList != null) { |
| 279 | System.setProperty("http.nonProxyHosts", exclList); |
| 280 | System.setProperty("https.nonProxyHosts", exclList); |
| 281 | } else { |
| 282 | System.clearProperty("http.nonProxyHosts"); |
| 283 | System.clearProperty("https.nonProxyHosts"); |
| 284 | } |
Jason Monk | 83520b9 | 2014-05-09 15:16:06 -0400 | [diff] [blame] | 285 | if (!Uri.EMPTY.equals(pacFileUrl)) { |
Jason Monk | 9ced3cd | 2013-08-12 16:42:38 -0400 | [diff] [blame] | 286 | ProxySelector.setDefault(new PacProxySelector()); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 287 | } else { |
| 288 | ProxySelector.setDefault(sDefaultProxySelector); |
| 289 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 290 | } |
Oscar Montemayor | 16fb791 | 2010-08-02 09:38:48 -0700 | [diff] [blame] | 291 | } |