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