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 import android.util.Log; 24 25 import java.net.InetAddress; 26 import java.net.InetSocketAddress; 27 import java.net.UnknownHostException; 28 29 /** 30 * A container class for the http proxy info 31 * @hide 32 */ 33 public class ProxyProperties implements Parcelable { 34 35 private String mHost; 36 private int mPort; 37 private String mExclusionList; 38 private String[] mParsedExclusionList; 39 ProxyProperties(String host, int port, String exclList)40 public ProxyProperties(String host, int port, String exclList) { 41 mHost = host; 42 mPort = port; 43 setExclusionList(exclList); 44 } 45 ProxyProperties(String host, int port, String exclList, String[] parsedExclList)46 private ProxyProperties(String host, int port, String exclList, String[] parsedExclList) { 47 mHost = host; 48 mPort = port; 49 mExclusionList = exclList; 50 mParsedExclusionList = parsedExclList; 51 } 52 53 // copy constructor instead of clone ProxyProperties(ProxyProperties source)54 public ProxyProperties(ProxyProperties source) { 55 if (source != null) { 56 mHost = source.getHost(); 57 mPort = source.getPort(); 58 mExclusionList = source.getExclusionList(); 59 mParsedExclusionList = source.mParsedExclusionList; 60 } 61 } 62 getSocketAddress()63 public InetSocketAddress getSocketAddress() { 64 InetSocketAddress inetSocketAddress = null; 65 try { 66 inetSocketAddress = new InetSocketAddress(mHost, mPort); 67 } catch (IllegalArgumentException e) { } 68 return inetSocketAddress; 69 } 70 getHost()71 public String getHost() { 72 return mHost; 73 } 74 getPort()75 public int getPort() { 76 return mPort; 77 } 78 79 // comma separated getExclusionList()80 public String getExclusionList() { 81 return mExclusionList; 82 } 83 84 // comma separated setExclusionList(String exclusionList)85 private void setExclusionList(String exclusionList) { 86 mExclusionList = exclusionList; 87 if (mExclusionList == null) { 88 mParsedExclusionList = new String[0]; 89 } else { 90 String splitExclusionList[] = exclusionList.toLowerCase().split(","); 91 mParsedExclusionList = new String[splitExclusionList.length * 2]; 92 for (int i = 0; i < splitExclusionList.length; i++) { 93 String s = splitExclusionList[i].trim(); 94 if (s.startsWith(".")) s = s.substring(1); 95 mParsedExclusionList[i*2] = s; 96 mParsedExclusionList[(i*2)+1] = "." + s; 97 } 98 } 99 } 100 isExcluded(String url)101 public boolean isExcluded(String url) { 102 if (TextUtils.isEmpty(url) || mParsedExclusionList == null || 103 mParsedExclusionList.length == 0) return false; 104 105 Uri u = Uri.parse(url); 106 String urlDomain = u.getHost(); 107 if (urlDomain == null) return false; 108 for (int i = 0; i< mParsedExclusionList.length; i+=2) { 109 if (urlDomain.equals(mParsedExclusionList[i]) || 110 urlDomain.endsWith(mParsedExclusionList[i+1])) { 111 return true; 112 } 113 } 114 return false; 115 } 116 makeProxy()117 public java.net.Proxy makeProxy() { 118 java.net.Proxy proxy = java.net.Proxy.NO_PROXY; 119 if (mHost != null) { 120 try { 121 InetSocketAddress inetSocketAddress = new InetSocketAddress(mHost, mPort); 122 proxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, inetSocketAddress); 123 } catch (IllegalArgumentException e) { 124 } 125 } 126 return proxy; 127 } 128 129 @Override toString()130 public String toString() { 131 StringBuilder sb = new StringBuilder(); 132 if (mHost != null) { 133 sb.append("["); 134 sb.append(mHost); 135 sb.append("] "); 136 sb.append(Integer.toString(mPort)); 137 if (mExclusionList != null) { 138 sb.append(" xl=").append(mExclusionList); 139 } 140 } else { 141 sb.append("[ProxyProperties.mHost == null]"); 142 } 143 return sb.toString(); 144 } 145 146 @Override equals(Object o)147 public boolean equals(Object o) { 148 if (!(o instanceof ProxyProperties)) return false; 149 ProxyProperties p = (ProxyProperties)o; 150 if (mExclusionList != null && !mExclusionList.equals(p.getExclusionList())) return false; 151 if (mHost != null && p.getHost() != null && mHost.equals(p.getHost()) == false) { 152 return false; 153 } 154 if (mHost != null && p.mHost == null) return false; 155 if (mHost == null && p.mHost != null) return false; 156 if (mPort != p.mPort) return false; 157 return true; 158 } 159 160 /** 161 * Implement the Parcelable interface 162 * @hide 163 */ describeContents()164 public int describeContents() { 165 return 0; 166 } 167 168 @Override 169 /* 170 * generate hashcode based on significant fields 171 */ hashCode()172 public int hashCode() { 173 return ((null == mHost) ? 0 : mHost.hashCode()) 174 + ((null == mExclusionList) ? 0 : mExclusionList.hashCode()) 175 + mPort; 176 } 177 178 /** 179 * Implement the Parcelable interface. 180 * @hide 181 */ writeToParcel(Parcel dest, int flags)182 public void writeToParcel(Parcel dest, int flags) { 183 if (mHost != null) { 184 dest.writeByte((byte)1); 185 dest.writeString(mHost); 186 dest.writeInt(mPort); 187 } else { 188 dest.writeByte((byte)0); 189 } 190 dest.writeString(mExclusionList); 191 dest.writeStringArray(mParsedExclusionList); 192 } 193 194 /** 195 * Implement the Parcelable interface. 196 * @hide 197 */ 198 public static final Creator<ProxyProperties> CREATOR = 199 new Creator<ProxyProperties>() { 200 public ProxyProperties createFromParcel(Parcel in) { 201 String host = null; 202 int port = 0; 203 if (in.readByte() == 1) { 204 host = in.readString(); 205 port = in.readInt(); 206 } 207 String exclList = in.readString(); 208 String[] parsedExclList = in.readStringArray(); 209 ProxyProperties proxyProperties = 210 new ProxyProperties(host, port, exclList, parsedExclList); 211 return proxyProperties; 212 } 213 214 public ProxyProperties[] newArray(int size) { 215 return new ProxyProperties[size]; 216 } 217 }; 218 } 219