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