Skip to content

Commit bf1738b

Browse files
committed
Move some methods from static HttpUtils to Uri
1 parent d0d4967 commit bf1738b

File tree

10 files changed

+91
-107
lines changed

10 files changed

+91
-107
lines changed

client/src/main/java/org/asynchttpclient/RequestBuilderBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
4444
import static java.nio.charset.StandardCharsets.UTF_8;
4545
import static org.asynchttpclient.util.HttpUtils.extractContentTypeCharsetAttribute;
46-
import static org.asynchttpclient.util.HttpUtils.validateSupportedScheme;
4746
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
4847
import static org.asynchttpclient.util.MiscUtils.withDefault;
4948

@@ -603,7 +602,7 @@ private Uri computeUri() {
603602
LOGGER.debug("setUrl hasn't been invoked. Using {}", DEFAULT_REQUEST_URL);
604603
tempUri = DEFAULT_REQUEST_URL;
605604
} else {
606-
validateSupportedScheme(tempUri);
605+
Uri.validateSupportedScheme(tempUri);
607606
}
608607

609608
return uriEncoder.encode(tempUri, queryParams);

client/src/main/java/org/asynchttpclient/channel/ChannelPoolPartitioning.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.asynchttpclient.proxy.ProxyServer;
1616
import org.asynchttpclient.proxy.ProxyType;
1717
import org.asynchttpclient.uri.Uri;
18-
import org.asynchttpclient.util.HttpUtils;
1918

2019
public interface ChannelPoolPartitioning {
2120

@@ -26,7 +25,7 @@ enum PerHostChannelPoolPartitioning implements ChannelPoolPartitioning {
2625
INSTANCE;
2726

2827
public Object getPartitionKey(Uri uri, String virtualHost, ProxyServer proxyServer) {
29-
String targetHostBaseUrl = HttpUtils.getBaseUrl(uri);
28+
String targetHostBaseUrl = uri.getBaseUrl();
3029
if (proxyServer == null) {
3130
if (virtualHost == null) {
3231
return targetHostBaseUrl;

client/src/main/java/org/asynchttpclient/netty/channel/NettyConnectListener.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import java.net.ConnectException;
3232
import java.net.InetSocketAddress;
3333

34-
import static org.asynchttpclient.util.HttpUtils.getBaseUrl;
35-
3634
/**
3735
* Non Blocking connect.
3836
*/
@@ -178,7 +176,7 @@ public void onFailure(Channel channel, Throwable cause) {
178176
LOGGER.debug("Failed to recover from connect exception: {} with channel {}", cause, channel);
179177

180178
boolean printCause = cause.getMessage() != null;
181-
String printedCause = printCause ? cause.getMessage() : getBaseUrl(future.getUri());
179+
String printedCause = printCause ? cause.getMessage() : future.getUri().getBaseUrl();
182180
ConnectException e = new ConnectException(printedCause);
183181
e.initCause(cause);
184182
future.abort(e);

client/src/main/java/org/asynchttpclient/netty/handler/intercept/Redirect30xInterceptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import static org.asynchttpclient.util.HttpConstants.Methods.GET;
4141
import static org.asynchttpclient.util.HttpConstants.ResponseStatusCodes.*;
4242
import static org.asynchttpclient.util.HttpUtils.followRedirect;
43-
import static org.asynchttpclient.util.HttpUtils.isSameBase;
4443
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
4544
import static org.asynchttpclient.util.ThrowableUtil.unknownStackTrace;
4645

@@ -136,7 +135,7 @@ else if (request.getBodyGenerator() != null)
136135
requestBuilder.addOrReplaceCookie(cookie);
137136
}
138137

139-
boolean sameBase = isSameBase(request.getUri(), newUri);
138+
boolean sameBase = request.getUri().isSameBase(newUri);
140139

141140
if (sameBase) {
142141
// we can only assume the virtual host is still valid if the baseUrl is the same

client/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public NettyRequest newNettyRequest(Request request, boolean performConnectReque
221221
private String requestUri(Uri uri, ProxyServer proxyServer, boolean connect) {
222222
if (connect) {
223223
// proxy tunnelling, connect need host and explicit port
224-
return getAuthority(uri);
224+
return uri.getAuthority();
225225

226226
} else if (proxyServer != null && !uri.isSecured() && proxyServer.getProxyType().isHttp()) {
227227
// proxy over HTTP, need full url

client/src/main/java/org/asynchttpclient/uri/Uri.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ public String toRelativeUrl() {
168168
return sb.toString();
169169
}
170170

171+
public String getBaseUrl() {
172+
return scheme + "://" + host + ":" + getExplicitPort();
173+
}
174+
175+
public String getAuthority() {
176+
return host + ":" + getExplicitPort();
177+
}
178+
179+
public boolean isSameBase(Uri other) {
180+
return scheme.equals(other.getScheme())
181+
&& host.equals(other.getHost())
182+
&& getExplicitPort() == other.getExplicitPort();
183+
}
184+
185+
public String getNonEmptyPath() {
186+
return isNonEmpty(path) ? path : "/";
187+
}
188+
171189
@Override
172190
public String toString() {
173191
// for now, but might change
@@ -243,4 +261,13 @@ public boolean equals(Object obj) {
243261
return false;
244262
return true;
245263
}
264+
265+
public static void validateSupportedScheme(Uri uri) {
266+
final String scheme = uri.getScheme();
267+
if (scheme == null || !scheme.equalsIgnoreCase(HTTP) && !scheme.equalsIgnoreCase(HTTPS)
268+
&& !scheme.equalsIgnoreCase(WS) && !scheme.equalsIgnoreCase(WSS)) {
269+
throw new IllegalArgumentException("The URI scheme, of the URI " + uri
270+
+ ", must be equal (ignoring case) to 'http', 'https', 'ws', or 'wss'");
271+
}
272+
}
246273
}

client/src/main/java/org/asynchttpclient/util/AuthenticatorUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import static io.netty.handler.codec.http.HttpHeaderNames.PROXY_AUTHORIZATION;
2828
import static java.nio.charset.StandardCharsets.ISO_8859_1;
2929
import static org.asynchttpclient.Dsl.realm;
30-
import static org.asynchttpclient.util.HttpUtils.getNonEmptyPath;
3130
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
3231

3332
public final class AuthenticatorUtils {
@@ -58,7 +57,7 @@ public static String computeRealmURI(Uri uri, boolean useAbsoluteURI, boolean om
5857
if (useAbsoluteURI) {
5958
return omitQuery && MiscUtils.isNonEmpty(uri.getQuery()) ? uri.withNewQuery(null).toUrl() : uri.toUrl();
6059
} else {
61-
String path = getNonEmptyPath(uri);
60+
String path = uri.getNonEmptyPath();
6261
return omitQuery || !MiscUtils.isNonEmpty(uri.getQuery()) ? path : path + "?" + uri.getQuery();
6362
}
6463
}

client/src/main/java/org/asynchttpclient/util/HttpUtils.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.concurrent.ThreadLocalRandom;
2828

2929
import static java.nio.charset.StandardCharsets.*;
30-
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
3130

3231
/**
3332
* {@link org.asynchttpclient.AsyncHttpClient} common utilities.
@@ -133,37 +132,6 @@ public static String patchContentTypeWithBoundaryAttribute(CharSequence base, by
133132
return sb.append(' ').append(CONTENT_TYPE_BOUNDARY_ATTRIBUTE).append(new String(boundary, US_ASCII)).toString();
134133
}
135134

136-
public static void validateSupportedScheme(Uri uri) {
137-
final String scheme = uri.getScheme();
138-
if (scheme == null || !scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https")
139-
&& !scheme.equalsIgnoreCase("ws") && !scheme.equalsIgnoreCase("wss")) {
140-
throw new IllegalArgumentException("The URI scheme, of the URI " + uri
141-
+ ", must be equal (ignoring case) to 'http', 'https', 'ws', or 'wss'");
142-
}
143-
}
144-
145-
public static String getBaseUrl(Uri uri) {
146-
// getAuthority duplicate but we don't want to re-concatenate
147-
return uri.getScheme() + "://" + uri.getHost() + ":" + uri.getExplicitPort();
148-
}
149-
150-
public static String getAuthority(Uri uri) {
151-
return uri.getHost() + ":" + uri.getExplicitPort();
152-
}
153-
154-
public static boolean isSameBase(Uri uri1, Uri uri2) {
155-
return uri1.getScheme().equals(uri2.getScheme()) && uri1.getHost().equals(uri2.getHost())
156-
&& uri1.getExplicitPort() == uri2.getExplicitPort();
157-
}
158-
159-
/**
160-
* @param uri the uri
161-
* @return the raw path or "/" if it's null
162-
*/
163-
public static String getNonEmptyPath(Uri uri) {
164-
return isNonEmpty(uri.getPath()) ? uri.getPath() : "/";
165-
}
166-
167135
public static boolean followRedirect(AsyncHttpClientConfig config, Request request) {
168136
return request.getFollowRedirect() != null ? request.getFollowRedirect() : config.isFollowRedirect();
169137
}

client/src/test/java/org/asynchttpclient/uri/UriTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,62 @@ public void creatingUriWithMissingSchemeThrowsIllegalArgumentException() {
269269
public void creatingUriWithMissingHostThrowsIllegalArgumentException() {
270270
Uri.create("http://");
271271
}
272+
273+
@Test
274+
public void testGetAuthority() {
275+
Uri uri = Uri.create("http://stackoverflow.com/questions/17814461/jacoco-maven-testng-0-test-coverage");
276+
assertEquals(uri.getAuthority(), "stackoverflow.com:80", "Incorrect authority returned from getAuthority");
277+
}
278+
279+
@Test
280+
public void testGetAuthorityWithPortInUrl() {
281+
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
282+
assertEquals(uri.getAuthority(), "stackoverflow.com:8443", "Incorrect authority returned from getAuthority");
283+
}
284+
285+
@Test
286+
public void testGetBaseUrl() {
287+
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
288+
assertEquals(uri.getBaseUrl(), "http://stackoverflow.com:8443", "Incorrect base URL returned from getBaseURL");
289+
}
290+
291+
@Test
292+
public void testIsSameBaseUrlReturnsFalseWhenPortDifferent() {
293+
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
294+
Uri uri2 = Uri.create("http://stackoverflow.com:8442/questions/1057564/pretty-git-branch-graphs");
295+
assertFalse(uri1.isSameBase(uri2), "Base URLs should be different, but true was returned from isSameBase");
296+
}
297+
298+
@Test
299+
public void testIsSameBaseUrlReturnsFalseWhenSchemeDifferent() {
300+
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
301+
Uri uri2 = Uri.create("ws://stackoverflow.com:8443/questions/1057564/pretty-git-branch-graphs");
302+
assertFalse(uri1.isSameBase(uri2), "Base URLs should be different, but true was returned from isSameBase");
303+
}
304+
305+
@Test
306+
public void testIsSameBaseUrlReturnsFalseWhenHostDifferent() {
307+
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
308+
Uri uri2 = Uri.create("http://example.com:8443/questions/1057564/pretty-git-branch-graphs");
309+
assertFalse(uri1.isSameBase(uri2), "Base URLs should be different, but true was returned from isSameBase");
310+
}
311+
312+
@Test
313+
public void testIsSameBaseUrlReturnsTrueWhenOneUriHasDefaultPort() {
314+
Uri uri1 = Uri.create("http://stackoverflow.com:80/questions/17814461/jacoco-maven-testng-0-test-coverage");
315+
Uri uri2 = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs");
316+
assertTrue(uri1.isSameBase(uri2), "Base URLs should be same, but false was returned from isSameBase");
317+
}
318+
319+
@Test
320+
public void testGetPathWhenPathIsNonEmpty() {
321+
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
322+
assertEquals(uri.getNonEmptyPath(), "/questions/17814461/jacoco-maven-testng-0-test-coverage", "Incorrect path returned from getNonEmptyPath");
323+
}
324+
325+
@Test
326+
public void testGetPathWhenPathIsEmpty() {
327+
Uri uri = Uri.create("http://stackoverflow.com");
328+
assertEquals(uri.getNonEmptyPath(), "/", "Incorrect path returned from getNonEmptyPath");
329+
}
272330
}

client/src/test/java/org/asynchttpclient/util/HttpUtilsTest.java

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -44,69 +44,6 @@ private static String toUsAsciiString(ByteBuffer buf) {
4444
}
4545
}
4646

47-
@Test
48-
public void testGetAuthority() {
49-
Uri uri = Uri.create("http://stackoverflow.com/questions/17814461/jacoco-maven-testng-0-test-coverage");
50-
String authority = HttpUtils.getAuthority(uri);
51-
assertEquals(authority, "stackoverflow.com:80", "Incorrect authority returned from getAuthority");
52-
}
53-
54-
@Test
55-
public void testGetAuthorityWithPortInUrl() {
56-
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
57-
String authority = HttpUtils.getAuthority(uri);
58-
assertEquals(authority, "stackoverflow.com:8443", "Incorrect authority returned from getAuthority");
59-
}
60-
61-
@Test
62-
public void testGetBaseUrl() {
63-
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
64-
String baseUrl = HttpUtils.getBaseUrl(uri);
65-
assertEquals(baseUrl, "http://stackoverflow.com:8443", "Incorrect base URL returned from getBaseURL");
66-
}
67-
68-
@Test
69-
public void testIsSameBaseUrlReturnsFalseWhenPortDifferent() {
70-
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
71-
Uri uri2 = Uri.create("http://stackoverflow.com:8442/questions/1057564/pretty-git-branch-graphs");
72-
assertFalse(HttpUtils.isSameBase(uri1, uri2), "Base URLs should be different, but true was returned from isSameBase");
73-
}
74-
75-
@Test
76-
public void testIsSameBaseUrlReturnsFalseWhenSchemeDifferent() {
77-
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
78-
Uri uri2 = Uri.create("ws://stackoverflow.com:8443/questions/1057564/pretty-git-branch-graphs");
79-
assertFalse(HttpUtils.isSameBase(uri1, uri2), "Base URLs should be different, but true was returned from isSameBase");
80-
}
81-
82-
@Test
83-
public void testIsSameBaseUrlReturnsFalseWhenHostDifferent() {
84-
Uri uri1 = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
85-
Uri uri2 = Uri.create("http://example.com:8443/questions/1057564/pretty-git-branch-graphs");
86-
assertFalse(HttpUtils.isSameBase(uri1, uri2), "Base URLs should be different, but true was returned from isSameBase");
87-
}
88-
89-
@Test
90-
public void testGetPathWhenPathIsNonEmpty() {
91-
Uri uri = Uri.create("http://stackoverflow.com:8443/questions/17814461/jacoco-maven-testng-0-test-coverage");
92-
String path = HttpUtils.getNonEmptyPath(uri);
93-
assertEquals(path, "/questions/17814461/jacoco-maven-testng-0-test-coverage", "Incorrect path returned from getNonEmptyPath");
94-
}
95-
96-
@Test
97-
public void testGetPathWhenPathIsEmpty() {
98-
Uri uri = Uri.create("http://stackoverflow.com");
99-
String path = HttpUtils.getNonEmptyPath(uri);
100-
assertEquals(path, "/", "Incorrect path returned from getNonEmptyPath");
101-
}
102-
103-
@Test
104-
public void testIsSameBaseUrlReturnsTrueWhenOneUriHasDefaultPort() {
105-
Uri uri1 = Uri.create("http://stackoverflow.com:80/questions/17814461/jacoco-maven-testng-0-test-coverage");
106-
Uri uri2 = Uri.create("http://stackoverflow.com/questions/1057564/pretty-git-branch-graphs");
107-
assertTrue(HttpUtils.isSameBase(uri1, uri2), "Base URLs should be same, but false was returned from isSameBase");
108-
}
109-
11047
@Test
11148
public void testExtractCharsetWithoutQuotes() {
11249
Charset charset = HttpUtils.extractContentTypeCharsetAttribute("text/html; charset=iso-8859-1");

0 commit comments

Comments
 (0)