blob: 5f5e6235eb8bfdea81f8b93981f581e8a0558a03 [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
20import android.os.Parcel;
21import android.os.Parcelable;
Robert Greenwalt434203a2010-10-11 16:00:27 -070022import android.text.TextUtils;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070023
Robert Greenwalt70af5742010-09-03 13:02:05 -070024import java.net.InetSocketAddress;
Jason Monk207900c2014-04-25 15:00:09 -040025import java.net.URLConnection;
26import java.util.List;
Elliott Hughescb64d432013-08-02 10:00:44 -070027import java.util.Locale;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070028
29/**
Jason Monk207900c2014-04-25 15:00:09 -040030 * Describes a proxy configuration.
31 *
Narayan Kamath3bdd3272014-11-27 18:17:35 +000032 * Proxy configurations are already integrated within the {@code java.net} and
33 * Apache HTTP stack. So {@link URLConnection} and Apache's {@code HttpClient} will use
34 * them automatically.
Jason Monk207900c2014-04-25 15:00:09 -040035 *
36 * Other HTTP stacks will need to obtain the proxy info from
37 * {@link Proxy#PROXY_CHANGE_ACTION} broadcast as the extra {@link Proxy#EXTRA_PROXY_INFO}.
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070038 */
Jason Monk207900c2014-04-25 15:00:09 -040039public class ProxyInfo implements Parcelable {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070040
Robert Greenwalt434203a2010-10-11 16:00:27 -070041 private String mHost;
42 private int mPort;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070043 private String mExclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -070044 private String[] mParsedExclusionList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070045
Jason Monk83520b92014-05-09 15:16:06 -040046 private Uri mPacFileUrl;
Jason Monk207900c2014-04-25 15:00:09 -040047 /**
48 *@hide
49 */
Jason Monk602b2322013-07-03 17:04:33 -040050 public static final String LOCAL_EXCL_LIST = "";
Jason Monk207900c2014-04-25 15:00:09 -040051 /**
52 *@hide
53 */
Jason Monk6f8a68f2013-08-23 19:21:25 -040054 public static final int LOCAL_PORT = -1;
Jason Monk207900c2014-04-25 15:00:09 -040055 /**
56 *@hide
57 */
Jason Monk602b2322013-07-03 17:04:33 -040058 public static final String LOCAL_HOST = "localhost";
59
Jason Monk207900c2014-04-25 15:00:09 -040060 /**
61 * Constructs a {@link ProxyInfo} object that points at a Direct proxy
62 * on the specified host and port.
63 */
64 public static ProxyInfo buildDirectProxy(String host, int port) {
65 return new ProxyInfo(host, port, null);
66 }
67
68 /**
69 * Constructs a {@link ProxyInfo} object that points at a Direct proxy
70 * on the specified host and port.
71 *
72 * The proxy will not be used to access any host in exclusion list, exclList.
73 *
74 * @param exclList Hosts to exclude using the proxy on connections for. These
75 * hosts can use wildcards such as *.example.com.
76 */
77 public static ProxyInfo buildDirectProxy(String host, int port, List<String> exclList) {
78 String[] array = exclList.toArray(new String[exclList.size()]);
79 return new ProxyInfo(host, port, TextUtils.join(",", array), array);
80 }
81
82 /**
83 * Construct a {@link ProxyInfo} that will download and run the PAC script
84 * at the specified URL.
85 */
86 public static ProxyInfo buildPacProxy(Uri pacUri) {
Jason Monk83520b92014-05-09 15:16:06 -040087 return new ProxyInfo(pacUri);
Jason Monk207900c2014-04-25 15:00:09 -040088 }
89
90 /**
91 * Create a ProxyProperties that points at a HTTP Proxy.
92 * @hide
93 */
94 public ProxyInfo(String host, int port, String exclList) {
Robert Greenwalt434203a2010-10-11 16:00:27 -070095 mHost = host;
96 mPort = port;
97 setExclusionList(exclList);
Jason Monk83520b92014-05-09 15:16:06 -040098 mPacFileUrl = Uri.EMPTY;
99 }
100
101 /**
102 * Create a ProxyProperties that points at a PAC URL.
103 * @hide
104 */
105 public ProxyInfo(Uri pacFileUrl) {
106 mHost = LOCAL_HOST;
107 mPort = LOCAL_PORT;
108 setExclusionList(LOCAL_EXCL_LIST);
109 if (pacFileUrl == null) {
110 throw new NullPointerException();
111 }
112 mPacFileUrl = pacFileUrl;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700113 }
114
Jason Monk207900c2014-04-25 15:00:09 -0400115 /**
116 * Create a ProxyProperties that points at a PAC URL.
117 * @hide
118 */
119 public ProxyInfo(String pacFileUrl) {
Jason Monk602b2322013-07-03 17:04:33 -0400120 mHost = LOCAL_HOST;
121 mPort = LOCAL_PORT;
122 setExclusionList(LOCAL_EXCL_LIST);
Jason Monk83520b92014-05-09 15:16:06 -0400123 mPacFileUrl = Uri.parse(pacFileUrl);
Jason Monk602b2322013-07-03 17:04:33 -0400124 }
125
Jason Monk207900c2014-04-25 15:00:09 -0400126 /**
127 * Only used in PacManager after Local Proxy is bound.
128 * @hide
129 */
Jason Monk83520b92014-05-09 15:16:06 -0400130 public ProxyInfo(Uri pacFileUrl, int localProxyPort) {
Jason Monk6f8a68f2013-08-23 19:21:25 -0400131 mHost = LOCAL_HOST;
132 mPort = localProxyPort;
133 setExclusionList(LOCAL_EXCL_LIST);
Jason Monk83520b92014-05-09 15:16:06 -0400134 if (pacFileUrl == null) {
135 throw new NullPointerException();
136 }
Jason Monk6f8a68f2013-08-23 19:21:25 -0400137 mPacFileUrl = pacFileUrl;
138 }
139
Jason Monk207900c2014-04-25 15:00:09 -0400140 private ProxyInfo(String host, int port, String exclList, String[] parsedExclList) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700141 mHost = host;
142 mPort = port;
143 mExclusionList = exclList;
144 mParsedExclusionList = parsedExclList;
Jason Monk83520b92014-05-09 15:16:06 -0400145 mPacFileUrl = Uri.EMPTY;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700146 }
147
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700148 // copy constructor instead of clone
Jason Monk207900c2014-04-25 15:00:09 -0400149 /**
150 * @hide
151 */
152 public ProxyInfo(ProxyInfo source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700153 if (source != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700154 mHost = source.getHost();
155 mPort = source.getPort();
Jason Monk207900c2014-04-25 15:00:09 -0400156 mPacFileUrl = source.mPacFileUrl;
157 mExclusionList = source.getExclusionListAsString();
Robert Greenwalt434203a2010-10-11 16:00:27 -0700158 mParsedExclusionList = source.mParsedExclusionList;
Sreeram Ramachandran4696ee42014-05-14 14:45:00 -0700159 } else {
160 mPacFileUrl = Uri.EMPTY;
Irfan Sheriffef6c1432010-08-30 20:37:17 -0700161 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700162 }
163
Jason Monk207900c2014-04-25 15:00:09 -0400164 /**
165 * @hide
166 */
Robert Greenwalt70af5742010-09-03 13:02:05 -0700167 public InetSocketAddress getSocketAddress() {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700168 InetSocketAddress inetSocketAddress = null;
169 try {
170 inetSocketAddress = new InetSocketAddress(mHost, mPort);
171 } catch (IllegalArgumentException e) { }
172 return inetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700173 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700174
Jason Monk207900c2014-04-25 15:00:09 -0400175 /**
176 * Returns the URL of the current PAC script or null if there is
177 * no PAC script.
178 */
179 public Uri getPacFileUrl() {
Jason Monk83520b92014-05-09 15:16:06 -0400180 return mPacFileUrl;
Jason Monk602b2322013-07-03 17:04:33 -0400181 }
182
Jason Monk207900c2014-04-25 15:00:09 -0400183 /**
184 * When configured to use a Direct Proxy this returns the host
185 * of the proxy.
186 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700187 public String getHost() {
188 return mHost;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700189 }
190
Jason Monk207900c2014-04-25 15:00:09 -0400191 /**
192 * When configured to use a Direct Proxy this returns the port
193 * of the proxy
194 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700195 public int getPort() {
196 return mPort;
197 }
198
Jason Monk207900c2014-04-25 15:00:09 -0400199 /**
200 * When configured to use a Direct Proxy this returns the list
201 * of hosts for which the proxy is ignored.
202 */
203 public String[] getExclusionList() {
204 return mParsedExclusionList;
205 }
206
207 /**
208 * comma separated
209 * @hide
210 */
211 public String getExclusionListAsString() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700212 return mExclusionList;
213 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700214
Robert Greenwalt434203a2010-10-11 16:00:27 -0700215 // comma separated
216 private void setExclusionList(String exclusionList) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700217 mExclusionList = exclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700218 if (mExclusionList == null) {
219 mParsedExclusionList = new String[0];
220 } else {
Jason Monk207900c2014-04-25 15:00:09 -0400221 mParsedExclusionList = exclusionList.toLowerCase(Locale.ROOT).split(",");
Robert Greenwalt434203a2010-10-11 16:00:27 -0700222 }
223 }
224
Jason Monk207900c2014-04-25 15:00:09 -0400225 /**
226 * @hide
227 */
Raj Mamadgi92d024912013-11-11 13:52:58 -0800228 public boolean isValid() {
Jason Monk83520b92014-05-09 15:16:06 -0400229 if (!Uri.EMPTY.equals(mPacFileUrl)) return true;
Yuhao Zheng90704842014-02-28 17:22:45 -0800230 return Proxy.PROXY_VALID == Proxy.validate(mHost == null ? "" : mHost,
231 mPort == 0 ? "" : Integer.toString(mPort),
232 mExclusionList == null ? "" : mExclusionList);
Raj Mamadgi92d024912013-11-11 13:52:58 -0800233 }
234
Jason Monk207900c2014-04-25 15:00:09 -0400235 /**
236 * @hide
237 */
Robert Greenwalt434203a2010-10-11 16:00:27 -0700238 public java.net.Proxy makeProxy() {
239 java.net.Proxy proxy = java.net.Proxy.NO_PROXY;
240 if (mHost != null) {
241 try {
242 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort);
243 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress);
244 } catch (IllegalArgumentException e) {
245 }
246 }
247 return proxy;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700248 }
249
Wink Saville1f6408a2010-08-27 11:15:18 -0700250 @Override
251 public String toString() {
252 StringBuilder sb = new StringBuilder();
Jason Monk83520b92014-05-09 15:16:06 -0400253 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400254 sb.append("PAC Script: ");
255 sb.append(mPacFileUrl);
Paul Jensene0bef712014-12-10 15:12:18 -0500256 }
257 if (mHost != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700258 sb.append("[");
259 sb.append(mHost);
260 sb.append("] ");
261 sb.append(Integer.toString(mPort));
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700262 if (mExclusionList != null) {
263 sb.append(" xl=").append(mExclusionList);
264 }
Wink Savillec9acde92011-09-21 11:05:43 -0700265 } else {
266 sb.append("[ProxyProperties.mHost == null]");
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700267 }
Wink Saville1f6408a2010-08-27 11:15:18 -0700268 return sb.toString();
269 }
270
Robert Greenwalt434203a2010-10-11 16:00:27 -0700271 @Override
272 public boolean equals(Object o) {
Jason Monk207900c2014-04-25 15:00:09 -0400273 if (!(o instanceof ProxyInfo)) return false;
274 ProxyInfo p = (ProxyInfo)o;
Jason Monk602b2322013-07-03 17:04:33 -0400275 // If PAC URL is present in either then they must be equal.
276 // Other parameters will only be for fall back.
Jason Monk83520b92014-05-09 15:16:06 -0400277 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monkdecd2952013-10-10 14:02:51 -0400278 return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
Jason Monk602b2322013-07-03 17:04:33 -0400279 }
Jason Monk83520b92014-05-09 15:16:06 -0400280 if (!Uri.EMPTY.equals(p.mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400281 return false;
282 }
Jason Monk83520b92014-05-09 15:16:06 -0400283 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionListAsString())) {
284 return false;
285 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700286 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
287 return false;
288 }
289 if (mHost != null && p.mHost == null) return false;
290 if (mHost == null && p.mHost != null) return false;
291 if (mPort != p.mPort) return false;
292 return true;
293 }
294
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700295 /**
296 * Implement the Parcelable interface
297 * @hide
298 */
299 public int describeContents() {
300 return 0;
301 }
302
John Wang4e900092011-04-04 12:35:42 -0700303 @Override
304 /*
305 * generate hashcode based on significant fields
306 */
307 public int hashCode() {
308 return ((null == mHost) ? 0 : mHost.hashCode())
309 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
310 + mPort;
311 }
312
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700313 /**
314 * Implement the Parcelable interface.
315 * @hide
316 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700317 public void writeToParcel(Parcel dest, int flags) {
Jason Monk83520b92014-05-09 15:16:06 -0400318 if (!Uri.EMPTY.equals(mPacFileUrl)) {
Jason Monk602b2322013-07-03 17:04:33 -0400319 dest.writeByte((byte)1);
Jason Monk83520b92014-05-09 15:16:06 -0400320 mPacFileUrl.writeToParcel(dest, 0);
Jason Monkdecd2952013-10-10 14:02:51 -0400321 dest.writeInt(mPort);
Jason Monk602b2322013-07-03 17:04:33 -0400322 return;
323 } else {
324 dest.writeByte((byte)0);
325 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700326 if (mHost != null) {
Irfan Sheriff798ffa12010-09-26 14:54:54 -0700327 dest.writeByte((byte)1);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700328 dest.writeString(mHost);
329 dest.writeInt(mPort);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700330 } else {
331 dest.writeByte((byte)0);
332 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700333 dest.writeString(mExclusionList);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700334 dest.writeStringArray(mParsedExclusionList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700335 }
336
Jason Monk207900c2014-04-25 15:00:09 -0400337 public static final Creator<ProxyInfo> CREATOR =
338 new Creator<ProxyInfo>() {
339 public ProxyInfo createFromParcel(Parcel in) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700340 String host = null;
341 int port = 0;
Jason Monk602b2322013-07-03 17:04:33 -0400342 if (in.readByte() != 0) {
Jason Monk83520b92014-05-09 15:16:06 -0400343 Uri url = Uri.CREATOR.createFromParcel(in);
Jason Monkdecd2952013-10-10 14:02:51 -0400344 int localPort = in.readInt();
Jason Monk207900c2014-04-25 15:00:09 -0400345 return new ProxyInfo(url, localPort);
Jason Monk602b2322013-07-03 17:04:33 -0400346 }
347 if (in.readByte() != 0) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700348 host = in.readString();
349 port = in.readInt();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700350 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700351 String exclList = in.readString();
352 String[] parsedExclList = in.readStringArray();
Jason Monk207900c2014-04-25 15:00:09 -0400353 ProxyInfo proxyProperties =
354 new ProxyInfo(host, port, exclList, parsedExclList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700355 return proxyProperties;
356 }
357
Jason Monk207900c2014-04-25 15:00:09 -0400358 public ProxyInfo[] newArray(int size) {
359 return new ProxyInfo[size];
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700360 }
361 };
Andrew Stadlerac19f242010-08-31 14:28:58 -0700362}