1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 28 package ch.boye.httpclientandroidlib.conn.routing; 29 30 import ch.boye.httpclientandroidlib.annotation.Immutable; 31 import ch.boye.httpclientandroidlib.util.Args; 32 33 /** 34 * Basic {@link HttpRouteDirector} implementation. 35 * 36 * @since 4.0 37 */ 38 @Immutable 39 public class BasicRouteDirector implements HttpRouteDirector { 40 41 /** 42 * Provides the next step. 43 * 44 * @param plan the planned route 45 * @param fact the currently established route, or 46 * <code>null</code> if nothing is established 47 * 48 * @return one of the constants defined in this class, indicating 49 * either the next step to perform, or success, or failure. 50 * 0 is for success, a negative value for failure. 51 */ nextStep(final RouteInfo plan, final RouteInfo fact)52 public int nextStep(final RouteInfo plan, final RouteInfo fact) { 53 Args.notNull(plan, "Planned route"); 54 55 int step = UNREACHABLE; 56 57 if ((fact == null) || (fact.getHopCount() < 1)) { 58 step = firstStep(plan); 59 } else if (plan.getHopCount() > 1) { 60 step = proxiedStep(plan, fact); 61 } else { 62 step = directStep(plan, fact); 63 } 64 65 return step; 66 67 } // nextStep 68 69 70 /** 71 * Determines the first step to establish a route. 72 * 73 * @param plan the planned route 74 * 75 * @return the first step 76 */ firstStep(final RouteInfo plan)77 protected int firstStep(final RouteInfo plan) { 78 79 return (plan.getHopCount() > 1) ? 80 CONNECT_PROXY : CONNECT_TARGET; 81 } 82 83 84 /** 85 * Determines the next step to establish a direct connection. 86 * 87 * @param plan the planned route 88 * @param fact the currently established route 89 * 90 * @return one of the constants defined in this class, indicating 91 * either the next step to perform, or success, or failure 92 */ directStep(final RouteInfo plan, final RouteInfo fact)93 protected int directStep(final RouteInfo plan, final RouteInfo fact) { 94 95 if (fact.getHopCount() > 1) { 96 return UNREACHABLE; 97 } 98 if (!plan.getTargetHost().equals(fact.getTargetHost())) 99 { 100 return UNREACHABLE; 101 // If the security is too low, we could now suggest to layer 102 // a secure protocol on the direct connection. Layering on direct 103 // connections has not been supported in HttpClient 3.x, we don't 104 // consider it here until there is a real-life use case for it. 105 } 106 107 // Should we tolerate if security is better than planned? 108 // (plan.isSecure() && !fact.isSecure()) 109 if (plan.isSecure() != fact.isSecure()) { 110 return UNREACHABLE; 111 } 112 113 // Local address has to match only if the plan specifies one. 114 if ((plan.getLocalAddress() != null) && 115 !plan.getLocalAddress().equals(fact.getLocalAddress()) 116 ) { 117 return UNREACHABLE; 118 } 119 120 return COMPLETE; 121 } 122 123 124 /** 125 * Determines the next step to establish a connection via proxy. 126 * 127 * @param plan the planned route 128 * @param fact the currently established route 129 * 130 * @return one of the constants defined in this class, indicating 131 * either the next step to perform, or success, or failure 132 */ proxiedStep(final RouteInfo plan, final RouteInfo fact)133 protected int proxiedStep(final RouteInfo plan, final RouteInfo fact) { 134 135 if (fact.getHopCount() <= 1) { 136 return UNREACHABLE; 137 } 138 if (!plan.getTargetHost().equals(fact.getTargetHost())) { 139 return UNREACHABLE; 140 } 141 final int phc = plan.getHopCount(); 142 final int fhc = fact.getHopCount(); 143 if (phc < fhc) { 144 return UNREACHABLE; 145 } 146 147 for (int i=0; i<fhc-1; i++) { 148 if (!plan.getHopTarget(i).equals(fact.getHopTarget(i))) { 149 return UNREACHABLE; 150 } 151 } 152 // now we know that the target matches and proxies so far are the same 153 if (phc > fhc) 154 { 155 return TUNNEL_PROXY; // need to extend the proxy chain 156 } 157 158 // proxy chain and target are the same, check tunnelling and layering 159 if ((fact.isTunnelled() && !plan.isTunnelled()) || 160 (fact.isLayered() && !plan.isLayered())) { 161 return UNREACHABLE; 162 } 163 164 if (plan.isTunnelled() && !fact.isTunnelled()) { 165 return TUNNEL_TARGET; 166 } 167 if (plan.isLayered() && !fact.isLayered()) { 168 return LAYER_PROTOCOL; 169 } 170 171 // tunnel and layering are the same, remains to check the security 172 // Should we tolerate if security is better than planned? 173 // (plan.isSecure() && !fact.isSecure()) 174 if (plan.isSecure() != fact.isSecure()) { 175 return UNREACHABLE; 176 } 177 178 return COMPLETE; 179 } 180 181 } 182