blob: 54fc01d73636ea7004859629c6c90d3cf60d994c [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;
Elliott Hughescb64d432013-08-02 10:00:44 -070025import java.util.Locale;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070026
27/**
28 * A container class for the http proxy info
29 * @hide
30 */
31public class ProxyProperties implements Parcelable {
32
Robert Greenwalt434203a2010-10-11 16:00:27 -070033 private String mHost;
34 private int mPort;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070035 private String mExclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -070036 private String[] mParsedExclusionList;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070037
Jason Monk602b2322013-07-03 17:04:33 -040038 private String mPacFileUrl;
39 public static final String LOCAL_EXCL_LIST = "";
Jason Monk6f8a68f2013-08-23 19:21:25 -040040 public static final int LOCAL_PORT = -1;
Jason Monk602b2322013-07-03 17:04:33 -040041 public static final String LOCAL_HOST = "localhost";
42
Robert Greenwalt434203a2010-10-11 16:00:27 -070043 public ProxyProperties(String host, int port, String exclList) {
44 mHost = host;
45 mPort = port;
46 setExclusionList(exclList);
47 }
48
Jason Monk602b2322013-07-03 17:04:33 -040049 public ProxyProperties(String pacFileUrl) {
50 mHost = LOCAL_HOST;
51 mPort = LOCAL_PORT;
52 setExclusionList(LOCAL_EXCL_LIST);
53 mPacFileUrl = pacFileUrl;
54 }
55
Jason Monk6f8a68f2013-08-23 19:21:25 -040056 // Only used in PacManager after Local Proxy is bound.
57 public ProxyProperties(String pacFileUrl, int localProxyPort) {
58 mHost = LOCAL_HOST;
59 mPort = localProxyPort;
60 setExclusionList(LOCAL_EXCL_LIST);
61 mPacFileUrl = pacFileUrl;
62 }
63
Robert Greenwalt434203a2010-10-11 16:00:27 -070064 private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) {
65 mHost = host;
66 mPort = port;
67 mExclusionList = exclList;
68 mParsedExclusionList = parsedExclList;
Jason Monk602b2322013-07-03 17:04:33 -040069 mPacFileUrl = null;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070070 }
71
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070072 // copy constructor instead of clone
73 public ProxyProperties(ProxyProperties source) {
Irfan Sheriffef6c1432010-08-30 20:37:17 -070074 if (source != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -070075 mHost = source.getHost();
76 mPort = source.getPort();
Jason Monk602b2322013-07-03 17:04:33 -040077 mPacFileUrl = source.getPacFileUrl();
Robert Greenwalt434203a2010-10-11 16:00:27 -070078 mExclusionList = source.getExclusionList();
79 mParsedExclusionList = source.mParsedExclusionList;
Irfan Sheriffef6c1432010-08-30 20:37:17 -070080 }
Robert Greenwalt37e65eb2010-08-30 10:56:47 -070081 }
82
Robert Greenwalt70af5742010-09-03 13:02:05 -070083 public InetSocketAddress getSocketAddress() {
Robert Greenwalt434203a2010-10-11 16:00:27 -070084 InetSocketAddress inetSocketAddress = null;
85 try {
86 inetSocketAddress = new InetSocketAddress(mHost, mPort);
87 } catch (IllegalArgumentException e) { }
88 return inetSocketAddress;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070089 }
Andrew Stadlerac19f242010-08-31 14:28:58 -070090
Jason Monk602b2322013-07-03 17:04:33 -040091 public String getPacFileUrl() {
92 return mPacFileUrl;
93 }
94
Robert Greenwalt434203a2010-10-11 16:00:27 -070095 public String getHost() {
96 return mHost;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -070097 }
98
Robert Greenwalt434203a2010-10-11 16:00:27 -070099 public int getPort() {
100 return mPort;
101 }
102
103 // comma separated
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700104 public String getExclusionList() {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700105 return mExclusionList;
106 }
Andrew Stadlerac19f242010-08-31 14:28:58 -0700107
Robert Greenwalt434203a2010-10-11 16:00:27 -0700108 // comma separated
109 private void setExclusionList(String exclusionList) {
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700110 mExclusionList = exclusionList;
Robert Greenwalt434203a2010-10-11 16:00:27 -0700111 if (mExclusionList == null) {
112 mParsedExclusionList = new String[0];
113 } else {
Elliott Hughescb64d432013-08-02 10:00:44 -0700114 String splitExclusionList[] = exclusionList.toLowerCase(Locale.ROOT).split(",");
Robert Greenwalt434203a2010-10-11 16:00:27 -0700115 mParsedExclusionList = new String[splitExclusionList.length * 2];
116 for (int i = 0; i < splitExclusionList.length; i++) {
117 String s = splitExclusionList[i].trim();
118 if (s.startsWith(".")) s = s.substring(1);
119 mParsedExclusionList[i*2] = s;
120 mParsedExclusionList[(i*2)+1] = "." + s;
121 }
122 }
123 }
124
125 public boolean isExcluded(String url) {
126 if (TextUtils.isEmpty(url) || mParsedExclusionList == null ||
127 mParsedExclusionList.length == 0) return false;
128
129 Uri u = Uri.parse(url);
130 String urlDomain = u.getHost();
131 if (urlDomain == null) return false;
132 for (int i = 0; i< mParsedExclusionList.length; i+=2) {
133 if (urlDomain.equals(mParsedExclusionList[i]) ||
134 urlDomain.endsWith(mParsedExclusionList[i+1])) {
135 return true;
136 }
137 }
138 return false;
139 }
140
Raj Mamadgi92d024912013-11-11 13:52:58 -0800141 public boolean isValid() {
142 if (!TextUtils.isEmpty(mPacFileUrl)) return true;
143 try {
144 Proxy.validate(mHost == null ? "" : mHost, mPort == 0 ? "" : Integer.toString(mPort),
145 mExclusionList == null ? "" : mExclusionList);
146 } catch (IllegalArgumentException e) {
147 return false;
148 }
149 return true;
150 }
151
Robert Greenwalt434203a2010-10-11 16:00:27 -0700152 public java.net.Proxy makeProxy() {
153 java.net.Proxy proxy = java.net.Proxy.NO_PROXY;
154 if (mHost != null) {
155 try {
156 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort);
157 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress);
158 } catch (IllegalArgumentException e) {
159 }
160 }
161 return proxy;
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700162 }
163
Wink Saville1f6408a2010-08-27 11:15:18 -0700164 @Override
165 public String toString() {
166 StringBuilder sb = new StringBuilder();
Jason Monk602b2322013-07-03 17:04:33 -0400167 if (mPacFileUrl != null) {
168 sb.append("PAC Script: ");
169 sb.append(mPacFileUrl);
170 } else if (mHost != null) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700171 sb.append("[");
172 sb.append(mHost);
173 sb.append("] ");
174 sb.append(Integer.toString(mPort));
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700175 if (mExclusionList != null) {
176 sb.append(" xl=").append(mExclusionList);
177 }
Wink Savillec9acde92011-09-21 11:05:43 -0700178 } else {
179 sb.append("[ProxyProperties.mHost == null]");
Robert Greenwaltf6682b02010-09-01 09:12:38 -0700180 }
Wink Saville1f6408a2010-08-27 11:15:18 -0700181 return sb.toString();
182 }
183
Robert Greenwalt434203a2010-10-11 16:00:27 -0700184 @Override
185 public boolean equals(Object o) {
186 if (!(o instanceof ProxyProperties)) return false;
187 ProxyProperties p = (ProxyProperties)o;
Jason Monk602b2322013-07-03 17:04:33 -0400188 // If PAC URL is present in either then they must be equal.
189 // Other parameters will only be for fall back.
190 if (!TextUtils.isEmpty(mPacFileUrl)) {
Jason Monkdecd2952013-10-10 14:02:51 -0400191 return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort;
Jason Monk602b2322013-07-03 17:04:33 -0400192 }
193 if (!TextUtils.isEmpty(p.getPacFileUrl())) {
194 return false;
195 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700196 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionList())) return false;
197 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) {
198 return false;
199 }
200 if (mHost != null && p.mHost == null) return false;
201 if (mHost == null && p.mHost != null) return false;
202 if (mPort != p.mPort) return false;
203 return true;
204 }
205
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700206 /**
207 * Implement the Parcelable interface
208 * @hide
209 */
210 public int describeContents() {
211 return 0;
212 }
213
John Wang4e900092011-04-04 12:35:42 -0700214 @Override
215 /*
216 * generate hashcode based on significant fields
217 */
218 public int hashCode() {
219 return ((null == mHost) ? 0 : mHost.hashCode())
220 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode())
221 + mPort;
222 }
223
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700224 /**
225 * Implement the Parcelable interface.
226 * @hide
227 */
Robert Greenwalt37e65eb2010-08-30 10:56:47 -0700228 public void writeToParcel(Parcel dest, int flags) {
Jason Monk602b2322013-07-03 17:04:33 -0400229 if (mPacFileUrl != null) {
230 dest.writeByte((byte)1);
231 dest.writeString(mPacFileUrl);
Jason Monkdecd2952013-10-10 14:02:51 -0400232 dest.writeInt(mPort);
Jason Monk602b2322013-07-03 17:04:33 -0400233 return;
234 } else {
235 dest.writeByte((byte)0);
236 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700237 if (mHost != null) {
Irfan Sheriff798ffa12010-09-26 14:54:54 -0700238 dest.writeByte((byte)1);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700239 dest.writeString(mHost);
240 dest.writeInt(mPort);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700241 } else {
242 dest.writeByte((byte)0);
243 }
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700244 dest.writeString(mExclusionList);
Robert Greenwalt434203a2010-10-11 16:00:27 -0700245 dest.writeStringArray(mParsedExclusionList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700246 }
247
248 /**
249 * Implement the Parcelable interface.
250 * @hide
251 */
252 public static final Creator<ProxyProperties> CREATOR =
253 new Creator<ProxyProperties>() {
254 public ProxyProperties createFromParcel(Parcel in) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700255 String host = null;
256 int port = 0;
Jason Monk602b2322013-07-03 17:04:33 -0400257 if (in.readByte() != 0) {
Jason Monkdecd2952013-10-10 14:02:51 -0400258 String url = in.readString();
259 int localPort = in.readInt();
260 return new ProxyProperties(url, localPort);
Jason Monk602b2322013-07-03 17:04:33 -0400261 }
262 if (in.readByte() != 0) {
Robert Greenwalt434203a2010-10-11 16:00:27 -0700263 host = in.readString();
264 port = in.readInt();
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700265 }
Robert Greenwalt434203a2010-10-11 16:00:27 -0700266 String exclList = in.readString();
267 String[] parsedExclList = in.readStringArray();
268 ProxyProperties proxyProperties =
269 new ProxyProperties(host, port, exclList, parsedExclList);
Robert Greenwalt47f69fe2010-06-15 15:43:39 -0700270 return proxyProperties;
271 }
272
273 public ProxyProperties[] newArray(int size) {
274 return new ProxyProperties[size];
275 }
276 };
Andrew Stadlerac19f242010-08-31 14:28:58 -0700277}