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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 ch.boye.httpclientandroidlib.annotation.Immutable;
michael@0 31
michael@0 32 /**
michael@0 33 * Basic implementation of an {@link HttpRouteDirector HttpRouteDirector}.
michael@0 34 * This implementation is stateless and therefore thread-safe.
michael@0 35 *
michael@0 36 * @since 4.0
michael@0 37 */
michael@0 38 @Immutable
michael@0 39 public class BasicRouteDirector implements HttpRouteDirector {
michael@0 40
michael@0 41 /**
michael@0 42 * Provides the next step.
michael@0 43 *
michael@0 44 * @param plan the planned route
michael@0 45 * @param fact the currently established route, or
michael@0 46 * <code>null</code> if nothing is established
michael@0 47 *
michael@0 48 * @return one of the constants defined in this class, indicating
michael@0 49 * either the next step to perform, or success, or failure.
michael@0 50 * 0 is for success, a negative value for failure.
michael@0 51 */
michael@0 52 public int nextStep(RouteInfo plan, RouteInfo fact) {
michael@0 53 if (plan == null) {
michael@0 54 throw new IllegalArgumentException
michael@0 55 ("Planned route may not be null.");
michael@0 56 }
michael@0 57
michael@0 58 int step = UNREACHABLE;
michael@0 59
michael@0 60 if ((fact == null) || (fact.getHopCount() < 1))
michael@0 61 step = firstStep(plan);
michael@0 62 else if (plan.getHopCount() > 1)
michael@0 63 step = proxiedStep(plan, fact);
michael@0 64 else
michael@0 65 step = directStep(plan, fact);
michael@0 66
michael@0 67 return step;
michael@0 68
michael@0 69 } // nextStep
michael@0 70
michael@0 71
michael@0 72 /**
michael@0 73 * Determines the first step to establish a route.
michael@0 74 *
michael@0 75 * @param plan the planned route
michael@0 76 *
michael@0 77 * @return the first step
michael@0 78 */
michael@0 79 protected int firstStep(RouteInfo plan) {
michael@0 80
michael@0 81 return (plan.getHopCount() > 1) ?
michael@0 82 CONNECT_PROXY : CONNECT_TARGET;
michael@0 83 }
michael@0 84
michael@0 85
michael@0 86 /**
michael@0 87 * Determines the next step to establish a direct connection.
michael@0 88 *
michael@0 89 * @param plan the planned route
michael@0 90 * @param fact the currently established route
michael@0 91 *
michael@0 92 * @return one of the constants defined in this class, indicating
michael@0 93 * either the next step to perform, or success, or failure
michael@0 94 */
michael@0 95 protected int directStep(RouteInfo plan, RouteInfo fact) {
michael@0 96
michael@0 97 if (fact.getHopCount() > 1)
michael@0 98 return UNREACHABLE;
michael@0 99 if (!plan.getTargetHost().equals(fact.getTargetHost()))
michael@0 100 return UNREACHABLE;
michael@0 101 // If the security is too low, we could now suggest to layer
michael@0 102 // a secure protocol on the direct connection. Layering on direct
michael@0 103 // connections has not been supported in HttpClient 3.x, we don't
michael@0 104 // consider it here until there is a real-life use case for it.
michael@0 105
michael@0 106 // Should we tolerate if security is better than planned?
michael@0 107 // (plan.isSecure() && !fact.isSecure())
michael@0 108 if (plan.isSecure() != fact.isSecure())
michael@0 109 return UNREACHABLE;
michael@0 110
michael@0 111 // Local address has to match only if the plan specifies one.
michael@0 112 if ((plan.getLocalAddress() != null) &&
michael@0 113 !plan.getLocalAddress().equals(fact.getLocalAddress())
michael@0 114 )
michael@0 115 return UNREACHABLE;
michael@0 116
michael@0 117 return COMPLETE;
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 /**
michael@0 122 * Determines the next step to establish a connection via proxy.
michael@0 123 *
michael@0 124 * @param plan the planned route
michael@0 125 * @param fact the currently established route
michael@0 126 *
michael@0 127 * @return one of the constants defined in this class, indicating
michael@0 128 * either the next step to perform, or success, or failure
michael@0 129 */
michael@0 130 protected int proxiedStep(RouteInfo plan, RouteInfo fact) {
michael@0 131
michael@0 132 if (fact.getHopCount() <= 1)
michael@0 133 return UNREACHABLE;
michael@0 134 if (!plan.getTargetHost().equals(fact.getTargetHost()))
michael@0 135 return UNREACHABLE;
michael@0 136 final int phc = plan.getHopCount();
michael@0 137 final int fhc = fact.getHopCount();
michael@0 138 if (phc < fhc)
michael@0 139 return UNREACHABLE;
michael@0 140
michael@0 141 for (int i=0; i<fhc-1; i++) {
michael@0 142 if (!plan.getHopTarget(i).equals(fact.getHopTarget(i)))
michael@0 143 return UNREACHABLE;
michael@0 144 }
michael@0 145 // now we know that the target matches and proxies so far are the same
michael@0 146 if (phc > fhc)
michael@0 147 return TUNNEL_PROXY; // need to extend the proxy chain
michael@0 148
michael@0 149 // proxy chain and target are the same, check tunnelling and layering
michael@0 150 if ((fact.isTunnelled() && !plan.isTunnelled()) ||
michael@0 151 (fact.isLayered() && !plan.isLayered()))
michael@0 152 return UNREACHABLE;
michael@0 153
michael@0 154 if (plan.isTunnelled() && !fact.isTunnelled())
michael@0 155 return TUNNEL_TARGET;
michael@0 156 if (plan.isLayered() && !fact.isLayered())
michael@0 157 return LAYER_PROTOCOL;
michael@0 158
michael@0 159 // tunnel and layering are the same, remains to check the security
michael@0 160 // Should we tolerate if security is better than planned?
michael@0 161 // (plan.isSecure() && !fact.isSecure())
michael@0 162 if (plan.isSecure() != fact.isSecure())
michael@0 163 return UNREACHABLE;
michael@0 164
michael@0 165 return COMPLETE;
michael@0 166 }
michael@0 167
michael@0 168 }

mercurial