1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.routing;
29
30 import java.net.InetAddress;
31 import java.util.Objects;
32
33 import org.apache.hc.client5.http.HttpRoute;
34 import org.apache.hc.client5.http.SchemePortResolver;
35 import org.apache.hc.client5.http.config.RequestConfig;
36 import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
37 import org.apache.hc.client5.http.protocol.HttpClientContext;
38 import org.apache.hc.client5.http.routing.HttpRoutePlanner;
39 import org.apache.hc.client5.http.routing.RoutingSupport;
40 import org.apache.hc.core5.annotation.Contract;
41 import org.apache.hc.core5.annotation.ThreadingBehavior;
42 import org.apache.hc.core5.http.HttpException;
43 import org.apache.hc.core5.http.HttpHost;
44 import org.apache.hc.core5.http.HttpRequest;
45 import org.apache.hc.core5.http.ProtocolException;
46 import org.apache.hc.core5.http.URIScheme;
47 import org.apache.hc.core5.http.protocol.HttpContext;
48 import org.apache.hc.core5.net.NamedEndpoint;
49 import org.apache.hc.core5.net.URIAuthority;
50
51
52
53
54
55
56
57 @Contract(threading = ThreadingBehavior.STATELESS)
58 public class DefaultRoutePlanner implements HttpRoutePlanner {
59
60 private final SchemePortResolver schemePortResolver;
61
62 public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
63 super();
64 this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
65 }
66
67 private static boolean sameNamedEndpoint(final NamedEndpoint n1, final NamedEndpoint n2) {
68 if (n1 == null || n2 == null) {
69 return false;
70 }
71 return Objects.equals(n1.getHostName(), n2.getHostName()) && n1.getPort() == n2.getPort();
72 }
73
74 @Override
75 public final HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
76 if (host == null) {
77 throw new ProtocolException("Target host is not specified");
78 }
79 final HttpClientContext clientContext = HttpClientContext.cast(context);
80 final RequestConfig config = clientContext.getRequestConfigOrDefault();
81 @SuppressWarnings("deprecation")
82 HttpHost proxy = config.getProxy();
83 if (proxy == null) {
84 proxy = determineProxy(host, context);
85 }
86 final HttpHost target = RoutingSupport.normalize(host, schemePortResolver);
87 if (target.getPort() < 0) {
88 throw new ProtocolException("Unroutable protocol scheme: " + target);
89 }
90 final boolean secure = URIScheme.HTTPS.same(target.getSchemeName());
91 final URIAuthority authority;
92 if (secure && request != null && !sameNamedEndpoint(request.getAuthority(), host)) {
93 authority = request.getAuthority();
94 } else {
95 authority = null;
96 }
97 final InetAddress inetAddress = determineLocalAddress(target, context);
98
99 if (proxy == null) {
100 return new HttpRoute(target, authority, inetAddress, secure);
101 }
102 return new HttpRoute(target, authority, inetAddress, proxy, secure);
103 }
104
105 @Override
106 public final HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException {
107 return determineRoute(host, null, context);
108 }
109
110
111
112
113
114
115 protected HttpHost determineProxy(
116 final HttpHost target,
117 final HttpContext context) throws HttpException {
118 return null;
119 }
120
121
122
123
124
125
126 protected InetAddress determineLocalAddress(
127 final HttpHost firstHop,
128 final HttpContext context) throws HttpException {
129 return null;
130 }
131
132 }