blob: ef2269a145d07acc0c132ebc6e4a357aed2f0fb5 [file] [log] [blame]
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07001/*
Wink Saville6e809972010-09-21 09:15:35 -07002 * Copyright (C) 2010 The Android Open Source Project
Robert Greenwalt47f69fe2010-06-15 15:43:39 -07003 *
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
17package android.net;
18
19
Mathew Inwood53f089f2018-08-08 14:44:44 +010020import android.annotation.UnsupportedAppUsage;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070021import android.os.Parcel;
22import android.os.Parcelable;
Robert Greenwalt434203a2010-10-11 16:00:27 -070023import android.text.TextUtils;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070024
Robert Greenwalt70af5742010-09-03 13:02:05 -070025import java.net.InetSocketAddress;
Jason Monk207900c2014-04-25 15:00:09 -040026import java.net.URLConnection;
27import java.util.List;
Elliott Hughescb64d432013-08-02 10:00:44 -070028import java.util.Locale;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070029
30/**
Jason Monk207900c2014-04-25 15:00:09 -040031 * Describes a proxy configuration.
32 *
Narayan Kamath3bdd3272014-11-27 18:17:35 +000033 * Proxy configurations are already integrated within the {@code java.net} and
34 * Apache HTTP stack. So {@link URLConnection} and Apache's {@code HttpClient} will use
35 * them automatically.
Jason Monk207900c2014-04-25 15:00:09 -040036 *
37 * Other HTTP stacks will need to obtain the proxy info from
38 * {@link Proxy#PROXY_CHANGE_ACTION} broadcast as the extra {@link Proxy#EXTRA_PROXY_INFO}.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070039 */
Jason Monk207900c2014-04-25 15:00:09 -040040public class ProxyInfo implements Parcelable {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070041
Irina Dumitrescu18622d32018-12-05 16:19:47 +000042 private final String mHost;
43 private final int mPort;
44 private final String mExclusionList;
45 private final String[] mParsedExclusionList;
46 private final Uri mPacFileUrl;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070047
Jason Monk207900c2014-04-25 15:00:09 -040048 /**
49 *@hide
50 */
Jason Monk602b2322013-07-03 17:04:33 -040051 public static final String LOCAL_EXCL_LIST = "";
Jason Monk207900c2014-04-25 15:00:09 -040052 /**
53 *@hide
54 */
Jason Monk6f8a68f2013-08-23 19:21:25 -040055 public static final int LOCAL_PORT = -1;
Jason Monk207900c2014-04-25 15:00:09 -040056 /**
57 *@hide
58 */
Jason Monk602b2322013-07-03 17:04:33 -040059 public static final String LOCAL_HOST = "localhost";
60
Jason Monk207900c2014-04-25 15:00:09 -040061 /**
62 * Constructs a {@link ProxyInfo} object that points at a Direct proxy
63 * on the specified host and port.
64 */
65 public static ProxyInfo buildDirectProxy(String host, int port) {
66 return new ProxyInfo(host, port, null);
67 }
68
69 /**
70 * Constructs a {@link ProxyInfo} object that points at a Direct proxy
71 * on the specified host and port.
72 *
73 * The proxy will not be used to access any host in exclusion list, exclList.
74 *
75 * @param exclList Hosts to exclude using the proxy on connections for. These
76 * hosts can use wildcards such as *.example.com.
77 */
78 public static ProxyInfo buildDirectProxy(String host, int port, List<String> exclList) {
79 String[] array = exclList.toArray(new String[exclList.size()]);
80 return new ProxyInfo(host, port, TextUtils.join(",", array), array);
81 }
82
83 /**
84 * Construct a {@link ProxyInfo} that will download and run the PAC script
85 * at the specified URL.
86 */
87 public static ProxyInfo buildPacProxy(Uri pacUri) {
Jason Monk83520b92014-05-09 15:16:06 -040088 return new ProxyInfo(pacUri);
Jason Monk207900c2014-04-25 15:00:09 -040089 }
90
91 /**
92 * Create a ProxyProperties that points at a HTTP Proxy.
93 * @hide
94 */
Mathew Inwood53f089f2018-08-08 14:44:44 +010095 @UnsupportedAppUsage
Jason Monk207900c2014-04-25 15:00:09 -040096 public ProxyInfo(String host, int port, String exclList) {
Robert Greenwalt434203a2010-10-11 16:00:27 -070097 mHost = host;
98 mPort = port;
Irina Dumitrescu18622d32018-12-05 16:19:47 +000099 mExclusionList = exclList;
100 mParsedExclusionList = parseExclusionList(mExclusionList);
Jason Monk83520b92014-05-09 15:16:06 -0400101 mPacFileUrl = Uri.EMPTY;
102 }
103
104 /**
105 * Create a ProxyProperties that points at a PAC URL.
106 * @hide
107 */
108 public ProxyInfo(Uri pacFileUrl) {
109 mHost = LOCAL_HOST;
110 mPort = LOCAL_PORT;
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000111 mExclusionList = LOCAL_EXCL_LIST;
112 mParsedExclusionList = parseExclusionList(mExclusionList);
Jason Monk83520b92014-05-09 15:16:06 -0400113 if (pacFileUrl == null) {
114 throw new NullPointerException();
115 }
116 mPacFileUrl = pacFileUrl;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700117 }
118
Jason Monk207900c2014-04-25 15:00:09 -0400119 /**
120 * Create a ProxyProperties that points at a PAC URL.
121 * @hide
122 */
123 public ProxyInfo(String pacFileUrl) {
Jason Monk602b2322013-07-03 17:04:33 -0400124 mHost = LOCAL_HOST;
125 mPort = LOCAL_PORT;
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000126 mExclusionList = LOCAL_EXCL_LIST;
127 mParsedExclusionList = parseExclusionList(mExclusionList);
Jason Monk83520b92014-05-09 15:16:06 -0400128 mPacFileUrl = Uri.parse(pacFileUrl);
Jason Monk602b2322013-07-03 17:04:33 -0400129 }
130
Jason Monk207900c2014-04-25 15:00:09 -0400131 /**
132 * Only used in PacManager after Local Proxy is bound.
133 * @hide
134 */
Jason Monk83520b92014-05-09 15:16:06 -0400135 public ProxyInfo(Uri pacFileUrl, int localProxyPort) {
Jason Monk6f8a68f2013-08-23 19:21:25 -0400136 mHost = LOCAL_HOST;
137 mPort = localProxyPort;
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000138 mExclusionList = LOCAL_EXCL_LIST;
139 mParsedExclusionList = parseExclusionList(mExclusionList);
Jason Monk83520b92014-05-09 15:16:06 -0400140 if (pacFileUrl == null) {
141 throw new NullPointerException();
142 }
Jason Monk6f8a68f2013-08-23 19:21:25 -0400143 mPacFileUrl = pacFileUrl;
144 }
145
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000146 private static String[] parseExclusionList(String exclusionList) {
147 if (exclusionList == null) {
148 return new String[0];
149 } else {
150 return exclusionList.toLowerCase(Locale.ROOT).split(",");
151 }
152 }
153
Jason Monk207900c2014-04-25 15:00:09 -0400154 private ProxyInfo(String host, int port, String exclList, String[] parsedExclList) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700155 mHost = host;
156 mPort = port;
157 mExclusionList = exclList;
158 mParsedExclusionList = parsedExclList;
Jason Monk83520b92014-05-09 15:16:06 -0400159 mPacFileUrl = Uri.EMPTY;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700160 }
161
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700162 // copy constructor instead of clone
Jason Monk207900c2014-04-25 15:00:09 -0400163 /**
164 * @hide
165 */
166 public ProxyInfo(ProxyInfo source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700167 if (source != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700168 mHost = source.getHost();
169 mPort = source.getPort();
Jason Monk207900c2014-04-25 15:00:09 -0400170 mPacFileUrl = source.mPacFileUrl;
171 mExclusionList = source.getExclusionListAsString();
Robert Greenwalt434203a2010-10-11 16:00:27 -0700172 mParsedExclusionList = source.mParsedExclusionList;
Sreeram Ramachandran4696ee42014-05-14 14:45:00 -0700173 } else {
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000174 mHost = null;
175 mPort = 0;
176 mExclusionList = null;
177 mParsedExclusionList = null;
Sreeram Ramachandran4696ee42014-05-14 14:45:00 -0700178 mPacFileUrl = Uri.EMPTY;
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700179 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700180 }
181
Jason Monk207900c2014-04-25 15:00:09 -0400182 /**
183 * @hide
184 */
Robert Greenwalt70af5742010-09-03 13:02:05 -0700185 public InetSocketAddress getSocketAddress() {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700186 InetSocketAddress inetSocketAddress = null;
187 try {
188 inetSocketAddress = new InetSocketAddress(mHost, mPort);
189 } catch (IllegalArgumentException e) { }
190 return inetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700191 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700192
Jason Monk207900c2014-04-25 15:00:09 -0400193 /**
194 * Returns the URL of the current PAC script or null if there is
195 * no PAC script.
196 */
197 public Uri getPacFileUrl() {
Jason Monk83520b92014-05-09 15:16:06 -0400198 return mPacFileUrl;
Jason Monk602b2322013-07-03 17:04:33 -0400199 }
200
Jason Monk207900c2014-04-25 15:00:09 -0400201 /**
202 * When configured to use a Direct Proxy this returns the host
203 * of the proxy.
204 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700205 public String getHost() {
206 return mHost;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700207 }
208
Jason Monk207900c2014-04-25 15:00:09 -0400209 /**
210 * When configured to use a Direct Proxy this returns the port
211 * of the proxy
212 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700213 public int getPort() {
214 return mPort;
215 }
216
Jason Monk207900c2014-04-25 15:00:09 -0400217 /**
218 * When configured to use a Direct Proxy this returns the list
219 * of hosts for which the proxy is ignored.
220 */
221 public String[] getExclusionList() {
222 return mParsedExclusionList;
223 }
224
225 /**
226 * comma separated
227 * @hide
228 */
229 public String getExclusionListAsString() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700230 return mExclusionList;
231 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700232
Jason Monk207900c2014-04-25 15:00:09 -0400233 /**
234 * @hide
235 */
Raj Mamadgi92d024912013-11-11 13:52:58 -0800236 public boolean isValid() {
Jason Monk83520b92014-05-09 15:16:06 -0400237 if (!Uri.EMPTY.equals(mPacFileUrl)) return true;
Yuhao Zheng90704842014-02-28 17:22:45 -0800238 return Proxy.PROXY_VALID == Proxy.validate(mHost == null ? "" : mHost,
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000239 mPort == 0 ? "" : Integer.toString(mPort),
240 mExclusionList == null ? "" : mExclusionList);
Raj Mamadgi92d024912013-11-11 13:52:58 -0800241 }
242
Jason Monk207900c2014-04-25 15:00:09 -0400243 /**
244 * @hide
245 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700246 public java.net.Proxy makeProxy() {
247 java.net.Proxy proxy = java.net.Proxy.NO_PROXY;
248 if (mHost != null) {
249 try {
250 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort);
251 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress);
252 } catch (IllegalArgumentException e) {
253 }
254 }
255 return proxy;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700256 }
257
Wink Saville1f6408a2010-08-27 11:15:18 -0700258 @Override
259 public String toString() {
260 StringBuilder sb = new StringBuilder();
Jason Monk83520b92014-05-09 15:16:06 -0400261 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400262 sb.append("PAC Script: ");
263 sb.append(mPacFileUrl);
Paul Jensene0bef712014-12-10 15:12:18 -0500264 }
265 if (mHost != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700266 sb.append("[");
267 sb.append(mHost);
268 sb.append("] ");
269 sb.append(Integer.toString(mPort));
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700270 if (mExclusionList != null) {
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000271 sb.append(" xl=").append(mExclusionList);
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700272 }
Wink Savillec9acde92011-09-21 11:05:43 -0700273 } else {
274 sb.append("[ProxyProperties.mHost == null]");
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700275 }
Wink Saville1f6408a2010-08-27 11:15:18 -0700276 return sb.toString();
277 }
278
Robert Greenwalt434203a2010-10-11 16:00:27 -0700279 @Override
280 public boolean equals(Object o) {
Jason Monk207900c2014-04-25 15:00:09 -0400281 if (!(o instanceof ProxyInfo)) return false;
282 ProxyInfo p = (ProxyInfo)o;
Jason Monk602b2322013-07-03 17:04:33 -0400283 // If PAC URL is present in either then they must be equal.
284 // Other parameters will only be for fall back.
Jason Monk83520b92014-05-09 15:16:06 -0400285 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monkdecd2952013-10-10 14:02:51 -0400286 return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
Jason Monk602b2322013-07-03 17:04:33 -0400287 }
Jason Monk83520b92014-05-09 15:16:06 -0400288 if (!Uri.EMPTY.equals(p.mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400289 return false;
290 }
Jason Monk83520b92014-05-09 15:16:06 -0400291 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionListAsString())) {
292 return false;
293 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700294 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
295 return false;
296 }
297 if (mHost != null && p.mHost == null) return false;
298 if (mHost == null && p.mHost != null) return false;
299 if (mPort != p.mPort) return false;
300 return true;
301 }
302
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700303 /**
304 * Implement the Parcelable interface
305 * @hide
306 */
307 public int describeContents() {
308 return 0;
309 }
310
John Wang4e900092011-04-04 12:35:42 -0700311 @Override
312 /*
313 * generate hashcode based on significant fields
314 */
315 public int hashCode() {
316 return ((null == mHost) ? 0 : mHost.hashCode())
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000317 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
318 + mPort;
John Wang4e900092011-04-04 12:35:42 -0700319 }
320
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700321 /**
322 * Implement the Parcelable interface.
323 * @hide
324 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700325 public void writeToParcel(Parcel dest, int flags) {
Jason Monk83520b92014-05-09 15:16:06 -0400326 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400327 dest.writeByte((byte)1);
Jason Monk83520b92014-05-09 15:16:06 -0400328 mPacFileUrl.writeToParcel(dest, 0);
Jason Monkdecd2952013-10-10 14:02:51 -0400329 dest.writeInt(mPort);
Jason Monk602b2322013-07-03 17:04:33 -0400330 return;
331 } else {
332 dest.writeByte((byte)0);
333 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700334 if (mHost != null) {
Irfan Sheriff798ffa12010-09-26 14:54:54 -0700335 dest.writeByte((byte)1);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700336 dest.writeString(mHost);
337 dest.writeInt(mPort);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700338 } else {
339 dest.writeByte((byte)0);
340 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700341 dest.writeString(mExclusionList);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700342 dest.writeStringArray(mParsedExclusionList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700343 }
344
Jason Monk207900c2014-04-25 15:00:09 -0400345 public static final Creator<ProxyInfo> CREATOR =
346 new Creator<ProxyInfo>() {
347 public ProxyInfo createFromParcel(Parcel in) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700348 String host = null;
349 int port = 0;
Jason Monk602b2322013-07-03 17:04:33 -0400350 if (in.readByte() != 0) {
Jason Monk83520b92014-05-09 15:16:06 -0400351 Uri url = Uri.CREATOR.createFromParcel(in);
Jason Monkdecd2952013-10-10 14:02:51 -0400352 int localPort = in.readInt();
Jason Monk207900c2014-04-25 15:00:09 -0400353 return new ProxyInfo(url, localPort);
Jason Monk602b2322013-07-03 17:04:33 -0400354 }
355 if (in.readByte() != 0) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700356 host = in.readString();
357 port = in.readInt();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700358 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700359 String exclList = in.readString();
360 String[] parsedExclList = in.readStringArray();
Irina Dumitrescu18622d32018-12-05 16:19:47 +0000361 ProxyInfo proxyProperties = new ProxyInfo(host, port, exclList, parsedExclList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700362 return proxyProperties;
363 }
364
Jason Monk207900c2014-04-25 15:00:09 -0400365 public ProxyInfo[] newArray(int size) {
366 return new ProxyInfo[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700367 }
368 };
Andrew Stadlerac19f242010-08-31 14:28:58 -0700369}