Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1 | /* |
Wink Saville | 6e80997 | 2010-09-21 09:15:35 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 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 | |
| 19 | |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 23 | |
Robert Greenwalt | 70af574 | 2010-09-03 13:02:05 -0700 | [diff] [blame] | 24 | import java.net.InetSocketAddress; |
Elliott Hughes | cb64d43 | 2013-08-02 10:00:44 -0700 | [diff] [blame] | 25 | import java.util.Locale; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * A container class for the http proxy info |
| 29 | * @hide |
| 30 | */ |
| 31 | public class ProxyProperties implements Parcelable { |
| 32 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 33 | private String mHost; |
| 34 | private int mPort; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 35 | private String mExclusionList; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 36 | private String[] mParsedExclusionList; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 37 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 38 | private String mPacFileUrl; |
| 39 | public static final String LOCAL_EXCL_LIST = ""; |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 40 | public static final int LOCAL_PORT = -1; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 41 | public static final String LOCAL_HOST = "localhost"; |
| 42 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 43 | public ProxyProperties(String host, int port, String exclList) { |
| 44 | mHost = host; |
| 45 | mPort = port; |
| 46 | setExclusionList(exclList); |
| 47 | } |
| 48 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 49 | public ProxyProperties(String pacFileUrl) { |
| 50 | mHost = LOCAL_HOST; |
| 51 | mPort = LOCAL_PORT; |
| 52 | setExclusionList(LOCAL_EXCL_LIST); |
| 53 | mPacFileUrl = pacFileUrl; |
| 54 | } |
| 55 | |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 56 | // 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 Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 64 | private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) { |
| 65 | mHost = host; |
| 66 | mPort = port; |
| 67 | mExclusionList = exclList; |
| 68 | mParsedExclusionList = parsedExclList; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 69 | mPacFileUrl = null; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 72 | // copy constructor instead of clone |
| 73 | public ProxyProperties(ProxyProperties source) { |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 74 | if (source != null) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 75 | mHost = source.getHost(); |
| 76 | mPort = source.getPort(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 77 | mPacFileUrl = source.getPacFileUrl(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 78 | mExclusionList = source.getExclusionList(); |
| 79 | mParsedExclusionList = source.mParsedExclusionList; |
Irfan Sheriff | ef6c143 | 2010-08-30 20:37:17 -0700 | [diff] [blame] | 80 | } |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Robert Greenwalt | 70af574 | 2010-09-03 13:02:05 -0700 | [diff] [blame] | 83 | public InetSocketAddress getSocketAddress() { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 84 | InetSocketAddress inetSocketAddress = null; |
| 85 | try { |
| 86 | inetSocketAddress = new InetSocketAddress(mHost, mPort); |
| 87 | } catch (IllegalArgumentException e) { } |
| 88 | return inetSocketAddress; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 89 | } |
Andrew Stadler | ac19f24 | 2010-08-31 14:28:58 -0700 | [diff] [blame] | 90 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 91 | public String getPacFileUrl() { |
| 92 | return mPacFileUrl; |
| 93 | } |
| 94 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 95 | public String getHost() { |
| 96 | return mHost; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 99 | public int getPort() { |
| 100 | return mPort; |
| 101 | } |
| 102 | |
| 103 | // comma separated |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 104 | public String getExclusionList() { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 105 | return mExclusionList; |
| 106 | } |
Andrew Stadler | ac19f24 | 2010-08-31 14:28:58 -0700 | [diff] [blame] | 107 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 108 | // comma separated |
| 109 | private void setExclusionList(String exclusionList) { |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 110 | mExclusionList = exclusionList; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 111 | if (mExclusionList == null) { |
| 112 | mParsedExclusionList = new String[0]; |
| 113 | } else { |
Elliott Hughes | cb64d43 | 2013-08-02 10:00:44 -0700 | [diff] [blame] | 114 | String splitExclusionList[] = exclusionList.toLowerCase(Locale.ROOT).split(","); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 115 | 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 Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 141 | 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 Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 152 | 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 Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 164 | @Override |
| 165 | public String toString() { |
| 166 | StringBuilder sb = new StringBuilder(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 167 | if (mPacFileUrl != null) { |
| 168 | sb.append("PAC Script: "); |
| 169 | sb.append(mPacFileUrl); |
| 170 | } else if (mHost != null) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 171 | sb.append("["); |
| 172 | sb.append(mHost); |
| 173 | sb.append("] "); |
| 174 | sb.append(Integer.toString(mPort)); |
Robert Greenwalt | f6682b0 | 2010-09-01 09:12:38 -0700 | [diff] [blame] | 175 | if (mExclusionList != null) { |
| 176 | sb.append(" xl=").append(mExclusionList); |
| 177 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 178 | } else { |
| 179 | sb.append("[ProxyProperties.mHost == null]"); |
Robert Greenwalt | f6682b0 | 2010-09-01 09:12:38 -0700 | [diff] [blame] | 180 | } |
Wink Saville | 1f6408a | 2010-08-27 11:15:18 -0700 | [diff] [blame] | 181 | return sb.toString(); |
| 182 | } |
| 183 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 184 | @Override |
| 185 | public boolean equals(Object o) { |
| 186 | if (!(o instanceof ProxyProperties)) return false; |
| 187 | ProxyProperties p = (ProxyProperties)o; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 188 | // 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 Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 191 | return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 192 | } |
| 193 | if (!TextUtils.isEmpty(p.getPacFileUrl())) { |
| 194 | return false; |
| 195 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 196 | 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 Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 206 | /** |
| 207 | * Implement the Parcelable interface |
| 208 | * @hide |
| 209 | */ |
| 210 | public int describeContents() { |
| 211 | return 0; |
| 212 | } |
| 213 | |
John Wang | 4e90009 | 2011-04-04 12:35:42 -0700 | [diff] [blame] | 214 | @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 Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Implement the Parcelable interface. |
| 226 | * @hide |
| 227 | */ |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 228 | public void writeToParcel(Parcel dest, int flags) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 229 | if (mPacFileUrl != null) { |
| 230 | dest.writeByte((byte)1); |
| 231 | dest.writeString(mPacFileUrl); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 232 | dest.writeInt(mPort); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 233 | return; |
| 234 | } else { |
| 235 | dest.writeByte((byte)0); |
| 236 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 237 | if (mHost != null) { |
Irfan Sheriff | 798ffa1 | 2010-09-26 14:54:54 -0700 | [diff] [blame] | 238 | dest.writeByte((byte)1); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 239 | dest.writeString(mHost); |
| 240 | dest.writeInt(mPort); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 241 | } else { |
| 242 | dest.writeByte((byte)0); |
| 243 | } |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 244 | dest.writeString(mExclusionList); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 245 | dest.writeStringArray(mParsedExclusionList); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 246 | } |
| 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 Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 255 | String host = null; |
| 256 | int port = 0; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 257 | if (in.readByte() != 0) { |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 258 | String url = in.readString(); |
| 259 | int localPort = in.readInt(); |
| 260 | return new ProxyProperties(url, localPort); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 261 | } |
| 262 | if (in.readByte() != 0) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 263 | host = in.readString(); |
| 264 | port = in.readInt(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 265 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 266 | String exclList = in.readString(); |
| 267 | String[] parsedExclList = in.readStringArray(); |
| 268 | ProxyProperties proxyProperties = |
| 269 | new ProxyProperties(host, port, exclList, parsedExclList); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 270 | return proxyProperties; |
| 271 | } |
| 272 | |
| 273 | public ProxyProperties[] newArray(int size) { |
| 274 | return new ProxyProperties[size]; |
| 275 | } |
| 276 | }; |
Andrew Stadler | ac19f24 | 2010-08-31 14:28:58 -0700 | [diff] [blame] | 277 | } |