Skip to content

Commit 6fcbe42

Browse files
author
Stephane Landelle
committed
Move UriComponents.getNonEmptyPath to AsyncHttpProviderUtils
1 parent 6fa9742 commit 6fcbe42

File tree

6 files changed

+41
-27
lines changed

6 files changed

+41
-27
lines changed

api/src/main/java/org/asynchttpclient/uri/UriComponents.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,6 @@ public String getQuery() {
6666
return query;
6767
}
6868

69-
/**
70-
* Convenient for HTTP layer when targeting server root
71-
*
72-
* @return the raw path or "/" if it's null
73-
*/
74-
public String getNonEmptyPath() {
75-
return isNonEmpty(path) ? path : "/";
76-
}
77-
7869
public String getPath() {
7970
return path;
8071
}

api/src/main/java/org/asynchttpclient/util/AsyncHttpProviderUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.asynchttpclient.util;
1414

15+
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
16+
1517
import org.asynchttpclient.AsyncHttpClientConfig;
1618
import org.asynchttpclient.AsyncHttpProvider;
1719
import org.asynchttpclient.HttpResponseBodyPart;
@@ -102,6 +104,15 @@ public final static int getDefaultPort(UriComponents uri) {
102104
return port;
103105
}
104106

107+
/**
108+
* Convenient for HTTP layer when targeting server root
109+
*
110+
* @return the raw path or "/" if it's null
111+
*/
112+
public final static String getNonEmptyPath(UriComponents uri) {
113+
return isNonEmpty(uri.getPath()) ? uri.getPath() : "/";
114+
}
115+
105116
public static String constructUserAgent(Class<? extends AsyncHttpProvider> httpProvider, AsyncHttpClientConfig config) {
106117
return new StringBuilder("AHC (").append(httpProvider.getSimpleName()).append(" - ").append(System.getProperty("os.name"))
107118
.append(" - ").append(System.getProperty("os.version")).append(" - ").append(System.getProperty("java.version"))

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/filters/AsyncHttpClientFilter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static org.asynchttpclient.providers.grizzly.filters.SwitchingSSLFilter.getHandshakeError;
1717
import static org.asynchttpclient.providers.grizzly.GrizzlyAsyncHttpProvider.NTLM_ENGINE;
1818
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getAuthority;
19+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
1920
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
2021
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
2122
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
@@ -238,7 +239,7 @@ private boolean sendAsGrizzlyRequest(
238239
final int port = uri.getPort();
239240
requestPacket.setRequestURI(uri.getHost() + ':' + (port == -1 ? 443 : port));
240241
} else {
241-
requestPacket.setRequestURI(uri.getNonEmptyPath());
242+
requestPacket.setRequestURI(getNonEmptyPath(uri));
242243
}
243244

244245
final BodyHandler bodyHandler = isPayloadAllowed(method) ?

providers/grizzly/src/main/java/org/asynchttpclient/providers/grizzly/statushandler/AuthorizationHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
package org.asynchttpclient.providers.grizzly.statushandler;
1515

16+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
1617
import static org.asynchttpclient.providers.grizzly.statushandler.StatusHandler.InvocationStatus.STOP;
1718

1819
import org.asynchttpclient.Realm;
@@ -67,7 +68,7 @@ public boolean handleStatus(final HttpResponsePacket responsePacket, final HttpT
6768
responsePacket.setSkipRemainder(true); // ignore the remainder of the response
6869

6970
final Request req = httpTransactionContext.getRequest();
70-
realm = new Realm.RealmBuilder().clone(realm).setScheme(realm.getAuthScheme()).setUri(req.getURI().getNonEmptyPath())
71+
realm = new Realm.RealmBuilder().clone(realm).setScheme(realm.getAuthScheme()).setUri(getNonEmptyPath(req.getURI()))
7172
.setMethodName(req.getMethod()).setUsePreemptiveAuth(true).parseWWWAuthenticateHeader(auth).build();
7273
String lowerCaseAuth = auth.toLowerCase(Locale.ENGLISH);
7374
if (lowerCaseAuth.startsWith("basic")) {

providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler/HttpProtocol.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static io.netty.handler.codec.http.HttpResponseStatus.PROXY_AUTHENTICATION_REQUIRED;
2121
import static io.netty.handler.codec.http.HttpResponseStatus.UNAUTHORIZED;
2222
import static org.asynchttpclient.providers.netty.util.HttpUtil.isNTLM;
23+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
2324
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2425

2526
import org.asynchttpclient.AsyncHandler;
@@ -87,7 +88,7 @@ private Realm kerberosChallenge(List<String> proxyAuth, Request request, ProxySe
8788
headers.add(HttpHeaders.Names.AUTHORIZATION, "Negotiate " + challengeHeader);
8889

8990
return newRealmBuilder(realm)//
90-
.setUri(uri.getNonEmptyPath())//
91+
.setUri(getNonEmptyPath(uri))//
9192
.setMethodName(request.getMethod())//
9293
.setScheme(Realm.AuthScheme.KERBEROS)//
9394
.build();
@@ -127,7 +128,7 @@ private Realm ntlmChallenge(List<String> wwwAuth, Request request, ProxyServer p
127128
future.getAndSetAuth(false);
128129
return newRealmBuilder(realm)//
129130
.setScheme(realm.getAuthScheme())//
130-
.setUri(uri.getNonEmptyPath())//
131+
.setUri(getNonEmptyPath(uri))//
131132
.setMethodName(request.getMethod())//
132133
.setNtlmMessageType2Received(true)//
133134
.build();
@@ -137,7 +138,7 @@ private Realm ntlmChallenge(List<String> wwwAuth, Request request, ProxyServer p
137138
Realm.AuthScheme authScheme = realm != null ? realm.getAuthScheme() : Realm.AuthScheme.NTLM;
138139
return newRealmBuilder(realm)//
139140
.setScheme(authScheme)//
140-
.setUri(request.getURI().getNonEmptyPath())//
141+
.setUri(getNonEmptyPath(request.getURI()))//
141142
.setMethodName(request.getMethod())//
142143
.build();
143144
}
@@ -153,7 +154,7 @@ private Realm ntlmProxyChallenge(List<String> wwwAuth, Request request, ProxySer
153154

154155
return newRealmBuilder(realm)//
155156
// .setScheme(realm.getAuthScheme())
156-
.setUri(request.getURI().getNonEmptyPath())//
157+
.setUri(getNonEmptyPath(request.getURI()))//
157158
.setMethodName(request.getMethod()).build();
158159
}
159160

@@ -208,15 +209,16 @@ private void markAsDone(NettyResponseFuture<?> future, final Channel channel) {
208209
private final String computeRealmURI(Realm realm, UriComponents requestURI) {
209210
if (realm.isUseAbsoluteURI()) {
210211
if (realm.isOmitQuery() && isNonEmpty(requestURI.getQuery())) {
211-
return requestURI.withNewQuery(null).toString();
212+
return requestURI.withNewQuery(null).toUrl();
212213
} else {
213-
return requestURI.toString();
214+
return requestURI.toUrl();
214215
}
215216
} else {
217+
String path = getNonEmptyPath(requestURI);
216218
if (realm.isOmitQuery() || !isNonEmpty(requestURI.getQuery())) {
217-
return requestURI.getNonEmptyPath();
219+
return path;
218220
} else {
219-
return requestURI.getNonEmptyPath() + "?" + requestURI.getQuery();
221+
return path + "?" + requestURI.getQuery();
220222
}
221223
}
222224
}
@@ -241,9 +243,14 @@ private boolean handleUnauthorizedAndExit(int statusCode, Realm realm, final Req
241243
return true;
242244
}
243245
} else {
244-
newRealm = new Realm.RealmBuilder().clone(realm).setScheme(realm.getAuthScheme()).setUri(request.getURI().getNonEmptyPath())
245-
.setMethodName(request.getMethod()).setUsePreemptiveAuth(true)
246-
.parseWWWAuthenticateHeader(authenticateHeaders.get(0)).build();
246+
newRealm = new Realm.RealmBuilder()//
247+
.clone(realm)//
248+
.setScheme(realm.getAuthScheme())//
249+
.setUri(getNonEmptyPath(request.getURI()))//
250+
.setMethodName(request.getMethod())//
251+
.setUsePreemptiveAuth(true)//
252+
.parseWWWAuthenticateHeader(authenticateHeaders.get(0))//
253+
.build();
247254
}
248255

249256
String realmURI = computeRealmURI(newRealm, request.getURI());

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.asynchttpclient.providers.netty.util.HttpUtil.isSecure;
2020
import static org.asynchttpclient.providers.netty.util.HttpUtil.isWebSocket;
2121
import static org.asynchttpclient.util.AsyncHttpProviderUtils.DEFAULT_CHARSET;
22+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
2223
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
2324
import io.netty.buffer.Unpooled;
2425
import io.netty.handler.codec.http.DefaultFullHttpRequest;
@@ -78,11 +79,13 @@ private String requestUri(UriComponents uri, ProxyServer proxyServer, HttpMethod
7879
else if (proxyServer != null && !(isSecure(uri) && config.isUseRelativeURIsWithSSLProxies()))
7980
return uri.toString();
8081

81-
else if (uri.getQuery() != null)
82-
return uri.getNonEmptyPath() + "?" + uri.getQuery();
83-
84-
else
85-
return uri.getNonEmptyPath();
82+
else {
83+
String path = getNonEmptyPath(uri);
84+
if (isNonEmpty(uri.getQuery()))
85+
return path + "?" + uri.getQuery();
86+
else
87+
return path;
88+
}
8689
}
8790

8891
private String hostHeader(Request request, UriComponents uri, Realm realm) {

0 commit comments

Comments
 (0)