blob: 010e5277074c09406aac3e23dc438adc51dd8f06 [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;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070025import java.net.UnknownHostException;
Elliott Hughescb64d432013-08-02 10:00:44 -070026import java.util.Locale;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070027
28/**
29 * A container class for the http proxy info
30 * @hide
31 */
32public class ProxyProperties implements Parcelable {
33
Robert Greenwalt434203a2010-10-11 16:00:27 -070034 private String mHost;
35 private int mPort;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070036 private String mExclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -070037 private String[] mParsedExclusionList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070038
Jason Monk602b2322013-07-03 17:04:33 -040039 private String mPacFileUrl;
40 public static final String LOCAL_EXCL_LIST = "";
Jason Monk6f8a68f2013-08-23 19:21:25 -040041 public static final int LOCAL_PORT = -1;
Jason Monk602b2322013-07-03 17:04:33 -040042 public static final String LOCAL_HOST = "localhost";
43
Robert Greenwalt434203a2010-10-11 16:00:27 -070044 public ProxyProperties(String host, int port, String exclList) {
45 mHost = host;
46 mPort = port;
47 setExclusionList(exclList);
48 }
49
Jason Monk602b2322013-07-03 17:04:33 -040050 public ProxyProperties(String pacFileUrl) {
51 mHost = LOCAL_HOST;
52 mPort = LOCAL_PORT;
53 setExclusionList(LOCAL_EXCL_LIST);
54 mPacFileUrl = pacFileUrl;
55 }
56
Jason Monk6f8a68f2013-08-23 19:21:25 -040057 // Only used in PacManager after Local Proxy is bound.
58 public ProxyProperties(String pacFileUrl, int localProxyPort) {
59 mHost = LOCAL_HOST;
60 mPort = localProxyPort;
61 setExclusionList(LOCAL_EXCL_LIST);
62 mPacFileUrl = pacFileUrl;
63 }
64
Robert Greenwalt434203a2010-10-11 16:00:27 -070065 private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) {
66 mHost = host;
67 mPort = port;
68 mExclusionList = exclList;
69 mParsedExclusionList = parsedExclList;
Jason Monk602b2322013-07-03 17:04:33 -040070 mPacFileUrl = null;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070071 }
72
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070073 // copy constructor instead of clone
74 public ProxyProperties(ProxyProperties source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -070075 if (source != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -070076 mHost = source.getHost();
77 mPort = source.getPort();
Jason Monk602b2322013-07-03 17:04:33 -040078 mPacFileUrl = source.getPacFileUrl();
Robert Greenwalt434203a2010-10-11 16:00:27 -070079 mExclusionList = source.getExclusionList();
80 mParsedExclusionList = source.mParsedExclusionList;
Irfan Sheriffef6c1432010-08-30 20:37:17 -070081 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070082 }
83
Robert Greenwalt70af5742010-09-03 13:02:05 -070084 public InetSocketAddress getSocketAddress() {
Robert Greenwalt434203a2010-10-11 16:00:27 -070085 InetSocketAddress inetSocketAddress = null;
86 try {
87 inetSocketAddress = new InetSocketAddress(mHost, mPort);
88 } catch (IllegalArgumentException e) { }
89 return inetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070090 }
Andrew Stadlerac19f242010-08-31 14:28:58 -070091
Jason Monk602b2322013-07-03 17:04:33 -040092 public String getPacFileUrl() {
93 return mPacFileUrl;
94 }
95
Robert Greenwalt434203a2010-10-11 16:00:27 -070096 public String getHost() {
97 return mHost;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070098 }
99
Robert Greenwalt434203a2010-10-11 16:00:27 -0700100 public int getPort() {
101 return mPort;
102 }
103
104 // comma separated
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700105 public String getExclusionList() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700106 return mExclusionList;
107 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700108
Robert Greenwalt434203a2010-10-11 16:00:27 -0700109 // comma separated
110 private void setExclusionList(String exclusionList) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700111 mExclusionList = exclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700112 if (mExclusionList == null) {
113 mParsedExclusionList = new String[0];
114 } else {
Elliott Hughescb64d432013-08-02 10:00:44 -0700115 String splitExclusionList[] = exclusionList.toLowerCase(Locale.ROOT).split(",");
Robert Greenwalt434203a2010-10-11 16:00:27 -0700116 mParsedExclusionList = new String[splitExclusionList.length * 2];
117 for (int i = 0; i < splitExclusionList.length; i++) {
118 String s = splitExclusionList[i].trim();
119 if (s.startsWith(".")) s = s.substring(1);
120 mParsedExclusionList[i*2] = s;
121 mParsedExclusionList[(i*2)+1] = "." + s;
122 }
123 }
124 }
125
126 public boolean isExcluded(String url) {
127 if (TextUtils.isEmpty(url) || mParsedExclusionList == null ||
128 mParsedExclusionList.length == 0) return false;
129
130 Uri u = Uri.parse(url);
131 String urlDomain = u.getHost();
132 if (urlDomain == null) return false;
133 for (int i = 0; i< mParsedExclusionList.length; i+=2) {
134 if (urlDomain.equals(mParsedExclusionList[i]) ||
135 urlDomain.endsWith(mParsedExclusionList[i+1])) {
136 return true;
137 }
138 }
139 return false;
140 }
141
Raj Mamadgi92d024912013-11-11 13:52:58 -0800142 public boolean isValid() {
143 if (!TextUtils.isEmpty(mPacFileUrl)) return true;
144 try {
145 Proxy.validate(mHost == null ? "" : mHost, mPort == 0 ? "" : Integer.toString(mPort),
146 mExclusionList == null ? "" : mExclusionList);
147 } catch (IllegalArgumentException e) {
148 return false;
149 }
150 return true;
151 }
152
Robert Greenwalt434203a2010-10-11 16:00:27 -0700153 public java.net.Proxy makeProxy() {
154 java.net.Proxy proxy = java.net.Proxy.NO_PROXY;
155 if (mHost != null) {
156 try {
157 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort);
158 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress);
159 } catch (IllegalArgumentException e) {
160 }
161 }
162 return proxy;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700163 }
164
Wink Saville1f6408a2010-08-27 11:15:18 -0700165 @Override
166 public String toString() {
167 StringBuilder sb = new StringBuilder();
Jason Monk602b2322013-07-03 17:04:33 -0400168 if (mPacFileUrl != null) {
169 sb.append("PAC Script: ");
170 sb.append(mPacFileUrl);
171 } else if (mHost != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700172 sb.append("[");
173 sb.append(mHost);
174 sb.append("] ");
175 sb.append(Integer.toString(mPort));
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700176 if (mExclusionList != null) {
177 sb.append(" xl=").append(mExclusionList);
178 }
Wink Savillec9acde92011-09-21 11:05:43 -0700179 } else {
180 sb.append("[ProxyProperties.mHost == null]");
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700181 }
Wink Saville1f6408a2010-08-27 11:15:18 -0700182 return sb.toString();
183 }
184
Robert Greenwalt434203a2010-10-11 16:00:27 -0700185 @Override
186 public boolean equals(Object o) {
187 if (!(o instanceof ProxyProperties)) return false;
188 ProxyProperties p = (ProxyProperties)o;
Jason Monk602b2322013-07-03 17:04:33 -0400189 // If PAC URL is present in either then they must be equal.
190 // Other parameters will only be for fall back.
191 if (!TextUtils.isEmpty(mPacFileUrl)) {
Jason Monkdecd2952013-10-10 14:02:51 -0400192 return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
Jason Monk602b2322013-07-03 17:04:33 -0400193 }
194 if (!TextUtils.isEmpty(p.getPacFileUrl())) {
195 return false;
196 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700197 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionList())) return false;
198 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
199 return false;
200 }
201 if (mHost != null && p.mHost == null) return false;
202 if (mHost == null && p.mHost != null) return false;
203 if (mPort != p.mPort) return false;
204 return true;
205 }
206
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700207 /**
208 * Implement the Parcelable interface
209 * @hide
210 */
211 public int describeContents() {
212 return 0;
213 }
214
John Wang4e900092011-04-04 12:35:42 -0700215 @Override
216 /*
217 * generate hashcode based on significant fields
218 */
219 public int hashCode() {
220 return ((null == mHost) ? 0 : mHost.hashCode())
221 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
222 + mPort;
223 }
224
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700225 /**
226 * Implement the Parcelable interface.
227 * @hide
228 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700229 public void writeToParcel(Parcel dest, int flags) {
Jason Monk602b2322013-07-03 17:04:33 -0400230 if (mPacFileUrl != null) {
231 dest.writeByte((byte)1);
232 dest.writeString(mPacFileUrl);
Jason Monkdecd2952013-10-10 14:02:51 -0400233 dest.writeInt(mPort);
Jason Monk602b2322013-07-03 17:04:33 -0400234 return;
235 } else {
236 dest.writeByte((byte)0);
237 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700238 if (mHost != null) {
Irfan Sheriff798ffa12010-09-26 14:54:54 -0700239 dest.writeByte((byte)1);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700240 dest.writeString(mHost);
241 dest.writeInt(mPort);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700242 } else {
243 dest.writeByte((byte)0);
244 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700245 dest.writeString(mExclusionList);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700246 dest.writeStringArray(mParsedExclusionList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700247 }
248
249 /**
250 * Implement the Parcelable interface.
251 * @hide
252 */
253 public static final Creator<ProxyProperties> CREATOR =
254 new Creator<ProxyProperties>() {
255 public ProxyProperties createFromParcel(Parcel in) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700256 String host = null;
257 int port = 0;
Jason Monk602b2322013-07-03 17:04:33 -0400258 if (in.readByte() != 0) {
Jason Monkdecd2952013-10-10 14:02:51 -0400259 String url = in.readString();
260 int localPort = in.readInt();
261 return new ProxyProperties(url, localPort);
Jason Monk602b2322013-07-03 17:04:33 -0400262 }
263 if (in.readByte() != 0) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700264 host = in.readString();
265 port = in.readInt();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700266 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700267 String exclList = in.readString();
268 String[] parsedExclList = in.readStringArray();
269 ProxyProperties proxyProperties =
270 new ProxyProperties(host, port, exclList, parsedExclList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700271 return proxyProperties;
272 }
273
274 public ProxyProperties[] newArray(int size) {
275 return new ProxyProperties[size];
276 }
277 };
Andrew Stadlerac19f242010-08-31 14:28:58 -0700278}