Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.conn.routing; |
michael@0 | 29 | |
michael@0 | 30 | import java.net.InetAddress; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.annotation.NotThreadSafe; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.util.LangUtils; |
michael@0 | 34 | |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Helps tracking the steps in establishing a route. |
michael@0 | 39 | * |
michael@0 | 40 | * @since 4.0 |
michael@0 | 41 | */ |
michael@0 | 42 | @NotThreadSafe |
michael@0 | 43 | public final class RouteTracker implements RouteInfo, Cloneable { |
michael@0 | 44 | |
michael@0 | 45 | /** The target host to connect to. */ |
michael@0 | 46 | private final HttpHost targetHost; |
michael@0 | 47 | |
michael@0 | 48 | /** |
michael@0 | 49 | * The local address to connect from. |
michael@0 | 50 | * <code>null</code> indicates that the default should be used. |
michael@0 | 51 | */ |
michael@0 | 52 | private final InetAddress localAddress; |
michael@0 | 53 | |
michael@0 | 54 | // the attributes above are fixed at construction time |
michael@0 | 55 | // now follow attributes that indicate the established route |
michael@0 | 56 | |
michael@0 | 57 | /** Whether the first hop of the route is established. */ |
michael@0 | 58 | private boolean connected; |
michael@0 | 59 | |
michael@0 | 60 | /** The proxy chain, if any. */ |
michael@0 | 61 | private HttpHost[] proxyChain; |
michael@0 | 62 | |
michael@0 | 63 | /** Whether the the route is tunnelled end-to-end through proxies. */ |
michael@0 | 64 | private TunnelType tunnelled; |
michael@0 | 65 | |
michael@0 | 66 | /** Whether the route is layered over a tunnel. */ |
michael@0 | 67 | private LayerType layered; |
michael@0 | 68 | |
michael@0 | 69 | /** Whether the route is secure. */ |
michael@0 | 70 | private boolean secure; |
michael@0 | 71 | |
michael@0 | 72 | /** |
michael@0 | 73 | * Creates a new route tracker. |
michael@0 | 74 | * The target and origin need to be specified at creation time. |
michael@0 | 75 | * |
michael@0 | 76 | * @param target the host to which to route |
michael@0 | 77 | * @param local the local address to route from, or |
michael@0 | 78 | * <code>null</code> for the default |
michael@0 | 79 | */ |
michael@0 | 80 | public RouteTracker(HttpHost target, InetAddress local) { |
michael@0 | 81 | if (target == null) { |
michael@0 | 82 | throw new IllegalArgumentException("Target host may not be null."); |
michael@0 | 83 | } |
michael@0 | 84 | this.targetHost = target; |
michael@0 | 85 | this.localAddress = local; |
michael@0 | 86 | this.tunnelled = TunnelType.PLAIN; |
michael@0 | 87 | this.layered = LayerType.PLAIN; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * Creates a new tracker for the given route. |
michael@0 | 93 | * Only target and origin are taken from the route, |
michael@0 | 94 | * everything else remains to be tracked. |
michael@0 | 95 | * |
michael@0 | 96 | * @param route the route to track |
michael@0 | 97 | */ |
michael@0 | 98 | public RouteTracker(HttpRoute route) { |
michael@0 | 99 | this(route.getTargetHost(), route.getLocalAddress()); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Tracks connecting to the target. |
michael@0 | 104 | * |
michael@0 | 105 | * @param secure <code>true</code> if the route is secure, |
michael@0 | 106 | * <code>false</code> otherwise |
michael@0 | 107 | */ |
michael@0 | 108 | public final void connectTarget(boolean secure) { |
michael@0 | 109 | if (this.connected) { |
michael@0 | 110 | throw new IllegalStateException("Already connected."); |
michael@0 | 111 | } |
michael@0 | 112 | this.connected = true; |
michael@0 | 113 | this.secure = secure; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | /** |
michael@0 | 117 | * Tracks connecting to the first proxy. |
michael@0 | 118 | * |
michael@0 | 119 | * @param proxy the proxy connected to |
michael@0 | 120 | * @param secure <code>true</code> if the route is secure, |
michael@0 | 121 | * <code>false</code> otherwise |
michael@0 | 122 | */ |
michael@0 | 123 | public final void connectProxy(HttpHost proxy, boolean secure) { |
michael@0 | 124 | if (proxy == null) { |
michael@0 | 125 | throw new IllegalArgumentException("Proxy host may not be null."); |
michael@0 | 126 | } |
michael@0 | 127 | if (this.connected) { |
michael@0 | 128 | throw new IllegalStateException("Already connected."); |
michael@0 | 129 | } |
michael@0 | 130 | this.connected = true; |
michael@0 | 131 | this.proxyChain = new HttpHost[]{ proxy }; |
michael@0 | 132 | this.secure = secure; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | /** |
michael@0 | 136 | * Tracks tunnelling to the target. |
michael@0 | 137 | * |
michael@0 | 138 | * @param secure <code>true</code> if the route is secure, |
michael@0 | 139 | * <code>false</code> otherwise |
michael@0 | 140 | */ |
michael@0 | 141 | public final void tunnelTarget(boolean secure) { |
michael@0 | 142 | if (!this.connected) { |
michael@0 | 143 | throw new IllegalStateException("No tunnel unless connected."); |
michael@0 | 144 | } |
michael@0 | 145 | if (this.proxyChain == null) { |
michael@0 | 146 | throw new IllegalStateException("No tunnel without proxy."); |
michael@0 | 147 | } |
michael@0 | 148 | this.tunnelled = TunnelType.TUNNELLED; |
michael@0 | 149 | this.secure = secure; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | /** |
michael@0 | 153 | * Tracks tunnelling to a proxy in a proxy chain. |
michael@0 | 154 | * This will extend the tracked proxy chain, but it does not mark |
michael@0 | 155 | * the route as tunnelled. Only end-to-end tunnels are considered there. |
michael@0 | 156 | * |
michael@0 | 157 | * @param proxy the proxy tunnelled to |
michael@0 | 158 | * @param secure <code>true</code> if the route is secure, |
michael@0 | 159 | * <code>false</code> otherwise |
michael@0 | 160 | */ |
michael@0 | 161 | public final void tunnelProxy(HttpHost proxy, boolean secure) { |
michael@0 | 162 | if (proxy == null) { |
michael@0 | 163 | throw new IllegalArgumentException("Proxy host may not be null."); |
michael@0 | 164 | } |
michael@0 | 165 | if (!this.connected) { |
michael@0 | 166 | throw new IllegalStateException("No tunnel unless connected."); |
michael@0 | 167 | } |
michael@0 | 168 | if (this.proxyChain == null) { |
michael@0 | 169 | throw new IllegalStateException("No proxy tunnel without proxy."); |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | // prepare an extended proxy chain |
michael@0 | 173 | HttpHost[] proxies = new HttpHost[this.proxyChain.length+1]; |
michael@0 | 174 | System.arraycopy(this.proxyChain, 0, |
michael@0 | 175 | proxies, 0, this.proxyChain.length); |
michael@0 | 176 | proxies[proxies.length-1] = proxy; |
michael@0 | 177 | |
michael@0 | 178 | this.proxyChain = proxies; |
michael@0 | 179 | this.secure = secure; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | /** |
michael@0 | 183 | * Tracks layering a protocol. |
michael@0 | 184 | * |
michael@0 | 185 | * @param secure <code>true</code> if the route is secure, |
michael@0 | 186 | * <code>false</code> otherwise |
michael@0 | 187 | */ |
michael@0 | 188 | public final void layerProtocol(boolean secure) { |
michael@0 | 189 | // it is possible to layer a protocol over a direct connection, |
michael@0 | 190 | // although this case is probably not considered elsewhere |
michael@0 | 191 | if (!this.connected) { |
michael@0 | 192 | throw new IllegalStateException |
michael@0 | 193 | ("No layered protocol unless connected."); |
michael@0 | 194 | } |
michael@0 | 195 | this.layered = LayerType.LAYERED; |
michael@0 | 196 | this.secure = secure; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | public final HttpHost getTargetHost() { |
michael@0 | 200 | return this.targetHost; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | public final InetAddress getLocalAddress() { |
michael@0 | 204 | return this.localAddress; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | public final int getHopCount() { |
michael@0 | 208 | int hops = 0; |
michael@0 | 209 | if (this.connected) { |
michael@0 | 210 | if (proxyChain == null) |
michael@0 | 211 | hops = 1; |
michael@0 | 212 | else |
michael@0 | 213 | hops = proxyChain.length + 1; |
michael@0 | 214 | } |
michael@0 | 215 | return hops; |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | public final HttpHost getHopTarget(int hop) { |
michael@0 | 219 | if (hop < 0) |
michael@0 | 220 | throw new IllegalArgumentException |
michael@0 | 221 | ("Hop index must not be negative: " + hop); |
michael@0 | 222 | final int hopcount = getHopCount(); |
michael@0 | 223 | if (hop >= hopcount) { |
michael@0 | 224 | throw new IllegalArgumentException |
michael@0 | 225 | ("Hop index " + hop + |
michael@0 | 226 | " exceeds tracked route length " + hopcount +"."); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | HttpHost result = null; |
michael@0 | 230 | if (hop < hopcount-1) |
michael@0 | 231 | result = this.proxyChain[hop]; |
michael@0 | 232 | else |
michael@0 | 233 | result = this.targetHost; |
michael@0 | 234 | |
michael@0 | 235 | return result; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | public final HttpHost getProxyHost() { |
michael@0 | 239 | return (this.proxyChain == null) ? null : this.proxyChain[0]; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | public final boolean isConnected() { |
michael@0 | 243 | return this.connected; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | public final TunnelType getTunnelType() { |
michael@0 | 247 | return this.tunnelled; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | public final boolean isTunnelled() { |
michael@0 | 251 | return (this.tunnelled == TunnelType.TUNNELLED); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | public final LayerType getLayerType() { |
michael@0 | 255 | return this.layered; |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | public final boolean isLayered() { |
michael@0 | 259 | return (this.layered == LayerType.LAYERED); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | public final boolean isSecure() { |
michael@0 | 263 | return this.secure; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | /** |
michael@0 | 267 | * Obtains the tracked route. |
michael@0 | 268 | * If a route has been tracked, it is {@link #isConnected connected}. |
michael@0 | 269 | * If not connected, nothing has been tracked so far. |
michael@0 | 270 | * |
michael@0 | 271 | * @return the tracked route, or |
michael@0 | 272 | * <code>null</code> if nothing has been tracked so far |
michael@0 | 273 | */ |
michael@0 | 274 | public final HttpRoute toRoute() { |
michael@0 | 275 | return !this.connected ? |
michael@0 | 276 | null : new HttpRoute(this.targetHost, this.localAddress, |
michael@0 | 277 | this.proxyChain, this.secure, |
michael@0 | 278 | this.tunnelled, this.layered); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | /** |
michael@0 | 282 | * Compares this tracked route to another. |
michael@0 | 283 | * |
michael@0 | 284 | * @param o the object to compare with |
michael@0 | 285 | * |
michael@0 | 286 | * @return <code>true</code> if the argument is the same tracked route, |
michael@0 | 287 | * <code>false</code> |
michael@0 | 288 | */ |
michael@0 | 289 | @Override |
michael@0 | 290 | public final boolean equals(Object o) { |
michael@0 | 291 | if (o == this) |
michael@0 | 292 | return true; |
michael@0 | 293 | if (!(o instanceof RouteTracker)) |
michael@0 | 294 | return false; |
michael@0 | 295 | |
michael@0 | 296 | RouteTracker that = (RouteTracker) o; |
michael@0 | 297 | return |
michael@0 | 298 | // Do the cheapest checks first |
michael@0 | 299 | (this.connected == that.connected) && |
michael@0 | 300 | (this.secure == that.secure) && |
michael@0 | 301 | (this.tunnelled == that.tunnelled) && |
michael@0 | 302 | (this.layered == that.layered) && |
michael@0 | 303 | LangUtils.equals(this.targetHost, that.targetHost) && |
michael@0 | 304 | LangUtils.equals(this.localAddress, that.localAddress) && |
michael@0 | 305 | LangUtils.equals(this.proxyChain, that.proxyChain); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | /** |
michael@0 | 309 | * Generates a hash code for this tracked route. |
michael@0 | 310 | * Route trackers are modifiable and should therefore not be used |
michael@0 | 311 | * as lookup keys. Use {@link #toRoute toRoute} to obtain an |
michael@0 | 312 | * unmodifiable representation of the tracked route. |
michael@0 | 313 | * |
michael@0 | 314 | * @return the hash code |
michael@0 | 315 | */ |
michael@0 | 316 | @Override |
michael@0 | 317 | public final int hashCode() { |
michael@0 | 318 | int hash = LangUtils.HASH_SEED; |
michael@0 | 319 | hash = LangUtils.hashCode(hash, this.targetHost); |
michael@0 | 320 | hash = LangUtils.hashCode(hash, this.localAddress); |
michael@0 | 321 | if (this.proxyChain != null) { |
michael@0 | 322 | for (int i = 0; i < this.proxyChain.length; i++) { |
michael@0 | 323 | hash = LangUtils.hashCode(hash, this.proxyChain[i]); |
michael@0 | 324 | } |
michael@0 | 325 | } |
michael@0 | 326 | hash = LangUtils.hashCode(hash, this.connected); |
michael@0 | 327 | hash = LangUtils.hashCode(hash, this.secure); |
michael@0 | 328 | hash = LangUtils.hashCode(hash, this.tunnelled); |
michael@0 | 329 | hash = LangUtils.hashCode(hash, this.layered); |
michael@0 | 330 | return hash; |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | /** |
michael@0 | 334 | * Obtains a description of the tracked route. |
michael@0 | 335 | * |
michael@0 | 336 | * @return a human-readable representation of the tracked route |
michael@0 | 337 | */ |
michael@0 | 338 | @Override |
michael@0 | 339 | public final String toString() { |
michael@0 | 340 | StringBuilder cab = new StringBuilder(50 + getHopCount()*30); |
michael@0 | 341 | |
michael@0 | 342 | cab.append("RouteTracker["); |
michael@0 | 343 | if (this.localAddress != null) { |
michael@0 | 344 | cab.append(this.localAddress); |
michael@0 | 345 | cab.append("->"); |
michael@0 | 346 | } |
michael@0 | 347 | cab.append('{'); |
michael@0 | 348 | if (this.connected) |
michael@0 | 349 | cab.append('c'); |
michael@0 | 350 | if (this.tunnelled == TunnelType.TUNNELLED) |
michael@0 | 351 | cab.append('t'); |
michael@0 | 352 | if (this.layered == LayerType.LAYERED) |
michael@0 | 353 | cab.append('l'); |
michael@0 | 354 | if (this.secure) |
michael@0 | 355 | cab.append('s'); |
michael@0 | 356 | cab.append("}->"); |
michael@0 | 357 | if (this.proxyChain != null) { |
michael@0 | 358 | for (int i=0; i<this.proxyChain.length; i++) { |
michael@0 | 359 | cab.append(this.proxyChain[i]); |
michael@0 | 360 | cab.append("->"); |
michael@0 | 361 | } |
michael@0 | 362 | } |
michael@0 | 363 | cab.append(this.targetHost); |
michael@0 | 364 | cab.append(']'); |
michael@0 | 365 | |
michael@0 | 366 | return cab.toString(); |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | |
michael@0 | 370 | // default implementation of clone() is sufficient |
michael@0 | 371 | @Override |
michael@0 | 372 | public Object clone() throws CloneNotSupportedException { |
michael@0 | 373 | return super.clone(); |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | } |