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 package org.apache.hc.client5.http.impl.cache;
28
29 import java.net.URI;
30 import java.net.URISyntaxException;
31
32 import org.apache.hc.client5.http.utils.URIUtils;
33 import org.apache.hc.core5.http.HttpHost;
34 import org.apache.hc.core5.http.HttpRequest;
35 import org.apache.hc.core5.http.URIScheme;
36 import org.apache.hc.core5.net.URIAuthority;
37 import org.apache.hc.core5.net.URIBuilder;
38 import org.apache.hc.core5.util.Args;
39
40
41
42
43
44
45 public final class HttpCacheSupport {
46
47 private static final URI BASE_URI = URI.create("http://example.com/");
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static String getRequestUri(final HttpRequest request, final HttpHost target) {
63 Args.notNull(request, "HTTP request");
64 Args.notNull(target, "Target");
65 final StringBuilder buf = new StringBuilder();
66 final URIAuthority authority = request.getAuthority();
67 if (authority != null) {
68 final String scheme = request.getScheme();
69 buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
70 buf.append(authority.getHostName());
71 if (authority.getPort() >= 0) {
72 buf.append(":").append(authority.getPort());
73 }
74 } else {
75 buf.append(target.getSchemeName()).append("://");
76 buf.append(target.getHostName());
77 if (target.getPort() >= 0) {
78 buf.append(":").append(target.getPort());
79 }
80 }
81 final String path = request.getPath();
82 if (path == null) {
83 buf.append("/");
84 } else {
85 if (buf.length() > 0 && !path.startsWith("/")) {
86 buf.append("/");
87 }
88 buf.append(path);
89 }
90 return buf.toString();
91 }
92
93
94
95
96
97
98
99
100
101
102 public static URI normalize(final URI requestUri) throws URISyntaxException {
103 Args.notNull(requestUri, "URI");
104 final URIBuilder builder = new URIBuilder(requestUri.isAbsolute() ? URIUtils.resolve(BASE_URI, requestUri) : requestUri) ;
105 if (builder.getHost() != null) {
106 if (builder.getScheme() == null) {
107 builder.setScheme(URIScheme.HTTP.id);
108 }
109 if (builder.getPort() <= -1) {
110 if (URIScheme.HTTP.same(builder.getScheme())) {
111 builder.setPort(80);
112 } else if (URIScheme.HTTPS.same(builder.getScheme())) {
113 builder.setPort(443);
114 }
115 }
116 }
117 builder.setFragment(null);
118 return builder.normalizeSyntax().build();
119 }
120
121
122
123
124
125 @Deprecated
126 public static URI normalizeQuetly(final String requestUri) {
127 if (requestUri == null) {
128 return null;
129 }
130 try {
131 return normalize(new URI(requestUri));
132 } catch (final URISyntaxException ex) {
133 return null;
134 }
135 }
136
137
138
139
140
141 public static URI normalizeQuietly(final String requestUri) {
142 if (requestUri == null) {
143 return null;
144 }
145 try {
146 return normalize(new URI(requestUri));
147 } catch (final URISyntaxException ex) {
148 return null;
149 }
150 }
151
152 }