mobile/android/thirdparty/ch/boye/httpclientandroidlib/conn/routing/HttpRoute.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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.Immutable;
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 * The route for a request.
michael@0 39 * Instances of this class are unmodifiable and therefore suitable
michael@0 40 * for use as lookup keys.
michael@0 41 *
michael@0 42 * @since 4.0
michael@0 43 */
michael@0 44 @Immutable
michael@0 45 public final class HttpRoute implements RouteInfo, Cloneable {
michael@0 46
michael@0 47 private static final HttpHost[] EMPTY_HTTP_HOST_ARRAY = new HttpHost[]{};
michael@0 48
michael@0 49 /** The target host to connect to. */
michael@0 50 private final HttpHost targetHost;
michael@0 51
michael@0 52 /**
michael@0 53 * The local address to connect from.
michael@0 54 * <code>null</code> indicates that the default should be used.
michael@0 55 */
michael@0 56 private final InetAddress localAddress;
michael@0 57
michael@0 58 /** The proxy servers, if any. Never null. */
michael@0 59 private final HttpHost[] proxyChain;
michael@0 60
michael@0 61 /** Whether the the route is tunnelled through the proxy. */
michael@0 62 private final TunnelType tunnelled;
michael@0 63
michael@0 64 /** Whether the route is layered. */
michael@0 65 private final LayerType layered;
michael@0 66
michael@0 67 /** Whether the route is (supposed to be) secure. */
michael@0 68 private final boolean secure;
michael@0 69
michael@0 70
michael@0 71 /**
michael@0 72 * Internal, fully-specified constructor.
michael@0 73 * This constructor does <i>not</i> clone the proxy chain array,
michael@0 74 * nor test it for <code>null</code> elements. This conversion and
michael@0 75 * check is the responsibility of the public constructors.
michael@0 76 * The order of arguments here is different from the similar public
michael@0 77 * constructor, as required by Java.
michael@0 78 *
michael@0 79 * @param local the local address to route from, or
michael@0 80 * <code>null</code> for the default
michael@0 81 * @param target the host to which to route
michael@0 82 * @param proxies the proxy chain to use, or
michael@0 83 * <code>null</code> for a direct route
michael@0 84 * @param secure <code>true</code> if the route is (to be) secure,
michael@0 85 * <code>false</code> otherwise
michael@0 86 * @param tunnelled the tunnel type of this route, or
michael@0 87 * <code>null</code> for PLAIN
michael@0 88 * @param layered the layering type of this route, or
michael@0 89 * <code>null</code> for PLAIN
michael@0 90 */
michael@0 91 private HttpRoute(InetAddress local,
michael@0 92 HttpHost target, HttpHost[] proxies,
michael@0 93 boolean secure,
michael@0 94 TunnelType tunnelled, LayerType layered) {
michael@0 95 if (target == null) {
michael@0 96 throw new IllegalArgumentException
michael@0 97 ("Target host may not be null.");
michael@0 98 }
michael@0 99 if (proxies == null) {
michael@0 100 throw new IllegalArgumentException
michael@0 101 ("Proxies may not be null.");
michael@0 102 }
michael@0 103 if ((tunnelled == TunnelType.TUNNELLED) && (proxies.length == 0)) {
michael@0 104 throw new IllegalArgumentException
michael@0 105 ("Proxy required if tunnelled.");
michael@0 106 }
michael@0 107
michael@0 108 // tunnelled is already checked above, that is in line with the default
michael@0 109 if (tunnelled == null)
michael@0 110 tunnelled = TunnelType.PLAIN;
michael@0 111 if (layered == null)
michael@0 112 layered = LayerType.PLAIN;
michael@0 113
michael@0 114 this.targetHost = target;
michael@0 115 this.localAddress = local;
michael@0 116 this.proxyChain = proxies;
michael@0 117 this.secure = secure;
michael@0 118 this.tunnelled = tunnelled;
michael@0 119 this.layered = layered;
michael@0 120 }
michael@0 121
michael@0 122
michael@0 123 /**
michael@0 124 * Creates a new route with all attributes specified explicitly.
michael@0 125 *
michael@0 126 * @param target the host to which to route
michael@0 127 * @param local the local address to route from, or
michael@0 128 * <code>null</code> for the default
michael@0 129 * @param proxies the proxy chain to use, or
michael@0 130 * <code>null</code> for a direct route
michael@0 131 * @param secure <code>true</code> if the route is (to be) secure,
michael@0 132 * <code>false</code> otherwise
michael@0 133 * @param tunnelled the tunnel type of this route
michael@0 134 * @param layered the layering type of this route
michael@0 135 */
michael@0 136 public HttpRoute(HttpHost target, InetAddress local, HttpHost[] proxies,
michael@0 137 boolean secure, TunnelType tunnelled, LayerType layered) {
michael@0 138 this(local, target, toChain(proxies), secure, tunnelled, layered);
michael@0 139 }
michael@0 140
michael@0 141
michael@0 142 /**
michael@0 143 * Creates a new route with at most one proxy.
michael@0 144 *
michael@0 145 * @param target the host to which to route
michael@0 146 * @param local the local address to route from, or
michael@0 147 * <code>null</code> for the default
michael@0 148 * @param proxy the proxy to use, or
michael@0 149 * <code>null</code> for a direct route
michael@0 150 * @param secure <code>true</code> if the route is (to be) secure,
michael@0 151 * <code>false</code> otherwise
michael@0 152 * @param tunnelled <code>true</code> if the route is (to be) tunnelled
michael@0 153 * via the proxy,
michael@0 154 * <code>false</code> otherwise
michael@0 155 * @param layered <code>true</code> if the route includes a
michael@0 156 * layered protocol,
michael@0 157 * <code>false</code> otherwise
michael@0 158 */
michael@0 159 public HttpRoute(HttpHost target, InetAddress local, HttpHost proxy,
michael@0 160 boolean secure, TunnelType tunnelled, LayerType layered) {
michael@0 161 this(local, target, toChain(proxy), secure, tunnelled, layered);
michael@0 162 }
michael@0 163
michael@0 164
michael@0 165 /**
michael@0 166 * Creates a new direct route.
michael@0 167 * That is a route without a proxy.
michael@0 168 *
michael@0 169 * @param target the host to which to route
michael@0 170 * @param local the local address to route from, or
michael@0 171 * <code>null</code> for the default
michael@0 172 * @param secure <code>true</code> if the route is (to be) secure,
michael@0 173 * <code>false</code> otherwise
michael@0 174 */
michael@0 175 public HttpRoute(HttpHost target, InetAddress local, boolean secure) {
michael@0 176 this(local, target, EMPTY_HTTP_HOST_ARRAY, secure, TunnelType.PLAIN, LayerType.PLAIN);
michael@0 177 }
michael@0 178
michael@0 179
michael@0 180 /**
michael@0 181 * Creates a new direct insecure route.
michael@0 182 *
michael@0 183 * @param target the host to which to route
michael@0 184 */
michael@0 185 public HttpRoute(HttpHost target) {
michael@0 186 this(null, target, EMPTY_HTTP_HOST_ARRAY, false, TunnelType.PLAIN, LayerType.PLAIN);
michael@0 187 }
michael@0 188
michael@0 189
michael@0 190 /**
michael@0 191 * Creates a new route through a proxy.
michael@0 192 * When using this constructor, the <code>proxy</code> MUST be given.
michael@0 193 * For convenience, it is assumed that a secure connection will be
michael@0 194 * layered over a tunnel through the proxy.
michael@0 195 *
michael@0 196 * @param target the host to which to route
michael@0 197 * @param local the local address to route from, or
michael@0 198 * <code>null</code> for the default
michael@0 199 * @param proxy the proxy to use
michael@0 200 * @param secure <code>true</code> if the route is (to be) secure,
michael@0 201 * <code>false</code> otherwise
michael@0 202 */
michael@0 203 public HttpRoute(HttpHost target, InetAddress local, HttpHost proxy,
michael@0 204 boolean secure) {
michael@0 205 this(local, target, toChain(proxy), secure,
michael@0 206 secure ? TunnelType.TUNNELLED : TunnelType.PLAIN,
michael@0 207 secure ? LayerType.LAYERED : LayerType.PLAIN);
michael@0 208 if (proxy == null) {
michael@0 209 throw new IllegalArgumentException
michael@0 210 ("Proxy host may not be null.");
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214
michael@0 215 /**
michael@0 216 * Helper to convert a proxy to a proxy chain.
michael@0 217 *
michael@0 218 * @param proxy the only proxy in the chain, or <code>null</code>
michael@0 219 *
michael@0 220 * @return a proxy chain array, may be empty (never null)
michael@0 221 */
michael@0 222 private static HttpHost[] toChain(HttpHost proxy) {
michael@0 223 if (proxy == null)
michael@0 224 return EMPTY_HTTP_HOST_ARRAY;
michael@0 225
michael@0 226 return new HttpHost[]{ proxy };
michael@0 227 }
michael@0 228
michael@0 229
michael@0 230 /**
michael@0 231 * Helper to duplicate and check a proxy chain.
michael@0 232 * <code>null</code> is converted to an empty proxy chain.
michael@0 233 *
michael@0 234 * @param proxies the proxy chain to duplicate, or <code>null</code>
michael@0 235 *
michael@0 236 * @return a new proxy chain array, may be empty (never null)
michael@0 237 */
michael@0 238 private static HttpHost[] toChain(HttpHost[] proxies) {
michael@0 239 if ((proxies == null) || (proxies.length < 1))
michael@0 240 return EMPTY_HTTP_HOST_ARRAY;
michael@0 241
michael@0 242 for (HttpHost proxy : proxies) {
michael@0 243 if (proxy == null)
michael@0 244 throw new IllegalArgumentException
michael@0 245 ("Proxy chain may not contain null elements.");
michael@0 246 }
michael@0 247
michael@0 248 // copy the proxy chain, the traditional way
michael@0 249 HttpHost[] result = new HttpHost[proxies.length];
michael@0 250 System.arraycopy(proxies, 0, result, 0, proxies.length);
michael@0 251
michael@0 252 return result;
michael@0 253 }
michael@0 254
michael@0 255
michael@0 256
michael@0 257 // non-JavaDoc, see interface RouteInfo
michael@0 258 public final HttpHost getTargetHost() {
michael@0 259 return this.targetHost;
michael@0 260 }
michael@0 261
michael@0 262
michael@0 263 // non-JavaDoc, see interface RouteInfo
michael@0 264 public final InetAddress getLocalAddress() {
michael@0 265 return this.localAddress;
michael@0 266 }
michael@0 267
michael@0 268
michael@0 269 public final int getHopCount() {
michael@0 270 return proxyChain.length+1;
michael@0 271 }
michael@0 272
michael@0 273
michael@0 274 public final HttpHost getHopTarget(int hop) {
michael@0 275 if (hop < 0)
michael@0 276 throw new IllegalArgumentException
michael@0 277 ("Hop index must not be negative: " + hop);
michael@0 278 final int hopcount = getHopCount();
michael@0 279 if (hop >= hopcount)
michael@0 280 throw new IllegalArgumentException
michael@0 281 ("Hop index " + hop +
michael@0 282 " exceeds route length " + hopcount);
michael@0 283
michael@0 284 HttpHost result = null;
michael@0 285 if (hop < hopcount-1)
michael@0 286 result = this.proxyChain[hop];
michael@0 287 else
michael@0 288 result = this.targetHost;
michael@0 289
michael@0 290 return result;
michael@0 291 }
michael@0 292
michael@0 293
michael@0 294 public final HttpHost getProxyHost() {
michael@0 295 return (this.proxyChain.length == 0) ? null : this.proxyChain[0];
michael@0 296 }
michael@0 297
michael@0 298
michael@0 299 public final TunnelType getTunnelType() {
michael@0 300 return this.tunnelled;
michael@0 301 }
michael@0 302
michael@0 303
michael@0 304 public final boolean isTunnelled() {
michael@0 305 return (this.tunnelled == TunnelType.TUNNELLED);
michael@0 306 }
michael@0 307
michael@0 308
michael@0 309 public final LayerType getLayerType() {
michael@0 310 return this.layered;
michael@0 311 }
michael@0 312
michael@0 313
michael@0 314 public final boolean isLayered() {
michael@0 315 return (this.layered == LayerType.LAYERED);
michael@0 316 }
michael@0 317
michael@0 318
michael@0 319 public final boolean isSecure() {
michael@0 320 return this.secure;
michael@0 321 }
michael@0 322
michael@0 323
michael@0 324 /**
michael@0 325 * Compares this route to another.
michael@0 326 *
michael@0 327 * @param obj the object to compare with
michael@0 328 *
michael@0 329 * @return <code>true</code> if the argument is the same route,
michael@0 330 * <code>false</code>
michael@0 331 */
michael@0 332 @Override
michael@0 333 public final boolean equals(Object obj) {
michael@0 334 if (this == obj) return true;
michael@0 335 if (obj instanceof HttpRoute) {
michael@0 336 HttpRoute that = (HttpRoute) obj;
michael@0 337 return
michael@0 338 // Do the cheapest tests first
michael@0 339 (this.secure == that.secure) &&
michael@0 340 (this.tunnelled == that.tunnelled) &&
michael@0 341 (this.layered == that.layered) &&
michael@0 342 LangUtils.equals(this.targetHost, that.targetHost) &&
michael@0 343 LangUtils.equals(this.localAddress, that.localAddress) &&
michael@0 344 LangUtils.equals(this.proxyChain, that.proxyChain);
michael@0 345 } else {
michael@0 346 return false;
michael@0 347 }
michael@0 348 }
michael@0 349
michael@0 350
michael@0 351 /**
michael@0 352 * Generates a hash code for this route.
michael@0 353 *
michael@0 354 * @return the hash code
michael@0 355 */
michael@0 356 @Override
michael@0 357 public final int hashCode() {
michael@0 358 int hash = LangUtils.HASH_SEED;
michael@0 359 hash = LangUtils.hashCode(hash, this.targetHost);
michael@0 360 hash = LangUtils.hashCode(hash, this.localAddress);
michael@0 361 for (int i = 0; i < this.proxyChain.length; i++) {
michael@0 362 hash = LangUtils.hashCode(hash, this.proxyChain[i]);
michael@0 363 }
michael@0 364 hash = LangUtils.hashCode(hash, this.secure);
michael@0 365 hash = LangUtils.hashCode(hash, this.tunnelled);
michael@0 366 hash = LangUtils.hashCode(hash, this.layered);
michael@0 367 return hash;
michael@0 368 }
michael@0 369
michael@0 370
michael@0 371 /**
michael@0 372 * Obtains a description of this route.
michael@0 373 *
michael@0 374 * @return a human-readable representation of this route
michael@0 375 */
michael@0 376 @Override
michael@0 377 public final String toString() {
michael@0 378 StringBuilder cab = new StringBuilder(50 + getHopCount()*30);
michael@0 379
michael@0 380 cab.append("HttpRoute[");
michael@0 381 if (this.localAddress != null) {
michael@0 382 cab.append(this.localAddress);
michael@0 383 cab.append("->");
michael@0 384 }
michael@0 385 cab.append('{');
michael@0 386 if (this.tunnelled == TunnelType.TUNNELLED)
michael@0 387 cab.append('t');
michael@0 388 if (this.layered == LayerType.LAYERED)
michael@0 389 cab.append('l');
michael@0 390 if (this.secure)
michael@0 391 cab.append('s');
michael@0 392 cab.append("}->");
michael@0 393 for (HttpHost aProxyChain : this.proxyChain) {
michael@0 394 cab.append(aProxyChain);
michael@0 395 cab.append("->");
michael@0 396 }
michael@0 397 cab.append(this.targetHost);
michael@0 398 cab.append(']');
michael@0 399
michael@0 400 return cab.toString();
michael@0 401 }
michael@0 402
michael@0 403
michael@0 404 // default implementation of clone() is sufficient
michael@0 405 @Override
michael@0 406 public Object clone() throws CloneNotSupportedException {
michael@0 407 return super.clone();
michael@0 408 }
michael@0 409
michael@0 410 }

mercurial