1 /* 2 * Copyright (C) 2010 The Android Open Source Project 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; 22 import android.text.TextUtils; 23 24 import java.net.InetSocketAddress; 25 import java.net.UnknownHostException; 26 import java.util.Locale; 27 28 /** 29 * A container class for the http proxy info 30 * @hide 31 */ 32 public class ProxyProperties implements Parcelable { 33 34 private String mHost; 35 private int mPort; 36 private String mExclusionList; 37 private String[] mParsedExclusionList; 38 39 private String mPacFileUrl; 40 public static final String LOCAL_EXCL_LIST = ""; 41 public static final int LOCAL_PORT = -1; 42 public static final String LOCAL_HOST = "localhost"; 43 44 public ProxyProperties(String host, int port, String exclList) { 45 mHost = host; 46 mPort = port; 47 setExclusionList(exclList); 48 } 49 50 public ProxyProperties(String pacFileUrl) { 51 mHost = LOCAL_HOST; 52 mPort = LOCAL_PORT; 53 setExclusionList(LOCAL_EXCL_LIST); 54 mPacFileUrl = pacFileUrl; 55 } 56 57 // 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 65 private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) { 66 mHost = host; 67 mPort = port; 68 mExclusionList = exclList; 69 mParsedExclusionList = parsedExclList; 70 mPacFileUrl = null; 71 } 72 73 // copy constructor instead of clone 74 public ProxyProperties(ProxyProperties source) { 75 if (source != null) { 76 mHost = source.getHost(); 77 mPort = source.getPort(); 78 mPacFileUrl = source.getPacFileUrl(); 79 mExclusionList = source.getExclusionList(); 80 mParsedExclusionList = source.mParsedExclusionList; 81 } 82 } 83 84 public InetSocketAddress getSocketAddress() { 85 InetSocketAddress inetSocketAddress = null; 86 try { 87 inetSocketAddress = new InetSocketAddress(mHost, mPort); 88 } catch (IllegalArgumentException e) { } 89 return inetSocketAddress; 90 } 91 92 public String getPacFileUrl() { 93 return mPacFileUrl; 94 } 95 96 public String getHost() { 97 return mHost; 98 } 99 100 public int getPort() { 101 return mPort; 102 } 103 104 // comma separated 105 public String getExclusionList() { 106 return mExclusionList; 107 } 108 109 // comma separated 110 private void setExclusionList(String exclusionList) { 111 mExclusionList = exclusionList; 112 if (mExclusionList == null) { 113 mParsedExclusionList = new String[0]; 114 } else { 115 String splitExclusionList[] = exclusionList.toLowerCase(Locale.ROOT).split(","); 116 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 142 public java.net.Proxy makeProxy() { 143 java.net.Proxy proxy = java.net.Proxy.NO_PROXY; 144 if (mHost != null) { 145 try { 146 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort); 147 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress); 148 } catch (IllegalArgumentException e) { 149 } 150 } 151 return proxy; 152 } 153 154 @Override 155 public String toString() { 156 StringBuilder sb = new StringBuilder(); 157 if (mPacFileUrl != null) { 158 sb.append("PAC Script: "); 159 sb.append(mPacFileUrl); 160 } else if (mHost != null) { 161 sb.append("["); 162 sb.append(mHost); 163 sb.append("] "); 164 sb.append(Integer.toString(mPort)); 165 if (mExclusionList != null) { 166 sb.append(" xl=").append(mExclusionList); 167 } 168 } else { 169 sb.append("[ProxyProperties.mHost == null]"); 170 } 171 return sb.toString(); 172 } 173 174 @Override 175 public boolean equals(Object o) { 176 if (!(o instanceof ProxyProperties)) return false; 177 ProxyProperties p = (ProxyProperties)o; 178 // If PAC URL is present in either then they must be equal. 179 // Other parameters will only be for fall back. 180 if (!TextUtils.isEmpty(mPacFileUrl)) { 181 return mPacFileUrl.equals(p.getPacFileUrl()) && mPort == p.mPort; 182 } 183 if (!TextUtils.isEmpty(p.getPacFileUrl())) { 184 return false; 185 } 186 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionList())) return false; 187 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) { 188 return false; 189 } 190 if (mHost != null && p.mHost == null) return false; 191 if (mHost == null && p.mHost != null) return false; 192 if (mPort != p.mPort) return false; 193 return true; 194 } 195 196 /** 197 * Implement the Parcelable interface 198 * @hide 199 */ 200 public int describeContents() { 201 return 0; 202 } 203 204 @Override 205 /* 206 * generate hashcode based on significant fields 207 */ 208 public int hashCode() { 209 return ((null == mHost) ? 0 : mHost.hashCode()) 210 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode()) 211 + mPort; 212 } 213 214 /** 215 * Implement the Parcelable interface. 216 * @hide 217 */ 218 public void writeToParcel(Parcel dest, int flags) { 219 if (mPacFileUrl != null) { 220 dest.writeByte((byte)1); 221 dest.writeString(mPacFileUrl); 222 dest.writeInt(mPort); 223 return; 224 } else { 225 dest.writeByte((byte)0); 226 } 227 if (mHost != null) { 228 dest.writeByte((byte)1); 229 dest.writeString(mHost); 230 dest.writeInt(mPort); 231 } else { 232 dest.writeByte((byte)0); 233 } 234 dest.writeString(mExclusionList); 235 dest.writeStringArray(mParsedExclusionList); 236 } 237 238 /** 239 * Implement the Parcelable interface. 240 * @hide 241 */ 242 public static final Creator<ProxyProperties> CREATOR = 243 new Creator<ProxyProperties>() { 244 public ProxyProperties createFromParcel(Parcel in) { 245 String host = null; 246 int port = 0; 247 if (in.readByte() != 0) { 248 String url = in.readString(); 249 int localPort = in.readInt(); 250 return new ProxyProperties(url, localPort); 251 } 252 if (in.readByte() != 0) { 253 host = in.readString(); 254 port = in.readInt(); 255 } 256 String exclList = in.readString(); 257 String[] parsedExclList = in.readStringArray(); 258 ProxyProperties proxyProperties = 259 new ProxyProperties(host, port, exclList, parsedExclList); 260 return proxyProperties; 261 } 262 263 public ProxyProperties[] newArray(int size) { 264 return new ProxyProperties[size]; 265 } 266 }; 267 } 268