Skip to content

Commit 04e256c

Browse files
author
Stephane Landelle
committed
Introduce Realm useAbsoluteURI and omitQuery, close AsyncHttpClient#553
1 parent 1e9ae84 commit 04e256c

File tree

2 files changed

+67
-10
lines changed
  • api/src/main/java/org/asynchttpclient
  • providers/netty/src/main/java/org/asynchttpclient/providers/netty/handler

2 files changed

+67
-10
lines changed

api/src/main/java/org/asynchttpclient/Realm.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ public class Realm {
5050
private final boolean messageType2Received;
5151
private final String domain;
5252
private final Charset charset;
53+
private final boolean useAbsoluteURI;
54+
private final boolean omitQuery;
5355

5456
public enum AuthScheme {
5557
DIGEST, BASIC, NTLM, SPNEGO, KERBEROS, NONE
5658
}
5759

5860
private Realm(AuthScheme scheme, String principal, String password, String realmName, String nonce, String algorithm, String response,
5961
String qop, String nc, String cnonce, String uri, String method, boolean usePreemptiveAuth, String domain, String enc,
60-
String host, boolean messageType2Received, String opaque) {
62+
String host, boolean messageType2Received, String opaque, boolean useAbsoluteURI, boolean omitQuery) {
6163

6264
this.principal = principal;
6365
this.password = password;
@@ -78,6 +80,8 @@ private Realm(AuthScheme scheme, String principal, String password, String realm
7880
this.host = host;
7981
this.messageType2Received = messageType2Received;
8082
this.charset = enc != null ? Charset.forName(enc) : null;
83+
this.useAbsoluteURI = useAbsoluteURI;
84+
this.omitQuery = omitQuery;
8185
}
8286

8387
public String getPrincipal() {
@@ -176,6 +180,14 @@ public boolean isNtlmMessageType2Received() {
176180
return messageType2Received;
177181
}
178182

183+
public boolean isUseAbsoluteURI() {
184+
return useAbsoluteURI;
185+
}
186+
187+
public boolean isOmitQuery() {
188+
return omitQuery;
189+
}
190+
179191
@Override
180192
public boolean equals(Object o) {
181193
if (this == o)
@@ -207,15 +219,17 @@ public boolean equals(Object o) {
207219
return false;
208220
if (uri != null ? !uri.equals(realm.uri) : realm.uri != null)
209221
return false;
210-
222+
if (useAbsoluteURI != !realm.useAbsoluteURI) return false;
223+
if (omitQuery != !realm.omitQuery) return false;
211224
return true;
212225
}
213226

214227
@Override
215228
public String toString() {
216229
return "Realm{" + "principal='" + principal + '\'' + ", scheme=" + scheme + ", realmName='" + realmName + '\'' + ", nonce='"
217230
+ nonce + '\'' + ", algorithm='" + algorithm + '\'' + ", response='" + response + '\'' + ", qop='" + qop + '\'' + ", nc='"
218-
+ nc + '\'' + ", cnonce='" + cnonce + '\'' + ", uri='" + uri + '\'' + ", methodName='" + methodName + '\'' + '}';
231+
+ nc + '\'' + ", cnonce='" + cnonce + '\'' + ", uri='" + uri + '\'' + ", methodName='" + methodName + '\'' + ", useAbsoluteURI='" + useAbsoluteURI + '\''
232+
+ ", omitQuery='" + omitQuery + '\'' +'}';
219233
}
220234

221235
@Override
@@ -261,6 +275,8 @@ public static class RealmBuilder {
261275
private String enc = StandardCharsets.UTF_8.name();
262276
private String host = "localhost";
263277
private boolean messageType2Received = false;
278+
private boolean useAbsoluteURI = true;
279+
private boolean omitQuery = false;
264280

265281
public String getNtlmDomain() {
266282
return domain;
@@ -397,6 +413,29 @@ public RealmBuilder setUsePreemptiveAuth(boolean usePreemptiveAuth) {
397413
return this;
398414
}
399415

416+
public RealmBuilder setNtlmMessageType2Received(boolean messageType2Received) {
417+
this.messageType2Received = messageType2Received;
418+
return this;
419+
}
420+
421+
public boolean isUseAbsoluteURI() {
422+
return useAbsoluteURI;
423+
}
424+
425+
public RealmBuilder setUseAbsoluteURI(boolean useAbsoluteURI) {
426+
this.useAbsoluteURI = useAbsoluteURI;
427+
return this;
428+
}
429+
430+
public boolean isOmitQuery() {
431+
return omitQuery;
432+
}
433+
434+
public RealmBuilder setOmitQuery(boolean omitQuery) {
435+
this.omitQuery = omitQuery;
436+
return this;
437+
}
438+
400439
public RealmBuilder parseWWWAuthenticateHeader(String headerLine) {
401440
setRealmName(match(headerLine, "realm"));
402441
setNonce(match(headerLine, "nonce"));
@@ -427,11 +466,6 @@ public RealmBuilder parseProxyAuthenticateHeader(String headerLine) {
427466
return this;
428467
}
429468

430-
public RealmBuilder setNtlmMessageType2Received(boolean messageType2Received) {
431-
this.messageType2Received = messageType2Received;
432-
return this;
433-
}
434-
435469
public RealmBuilder clone(Realm clone) {
436470
setRealmName(clone.getRealmName());
437471
setAlgorithm(clone.getAlgorithm());
@@ -574,7 +608,7 @@ public Realm build() {
574608
}
575609

576610
return new Realm(scheme, principal, password, realmName, nonce, algorithm, response, qop, nc, cnonce, uri, methodName,
577-
usePreemptive, domain, enc, host, messageType2Received, opaque);
611+
usePreemptive, domain, enc, host, messageType2Received, opaque, useAbsoluteURI, omitQuery);
578612
}
579613
}
580614
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.io.IOException;
6262
import java.net.MalformedURLException;
6363
import java.net.URI;
64+
import java.net.URISyntaxException;
6465
import java.util.List;
6566

6667
final class HttpProtocol extends Protocol {
@@ -202,6 +203,27 @@ private void markAsDone(NettyResponseFuture<?> future, final Channel channel) th
202203
}
203204
}
204205

206+
private final String computeRealmURI(Realm realm, URI requestURI) throws URISyntaxException {
207+
if (realm.isUseAbsoluteURI()) {
208+
if (realm.isOmitQuery() && isNonEmpty(requestURI.getQuery())) {
209+
return new URI(
210+
requestURI.getScheme(),
211+
requestURI.getAuthority(),
212+
requestURI.getPath(),
213+
null,
214+
null).toString();
215+
} else {
216+
return requestURI.toString();
217+
}
218+
} else {
219+
if (realm.isOmitQuery() && isNonEmpty(requestURI.getQuery())) {
220+
return requestURI.getPath();
221+
} else {
222+
return requestURI.getPath() + "?" + requestURI.getQuery();
223+
}
224+
}
225+
}
226+
205227
private boolean handleUnauthorizedAndExit(int statusCode, Realm realm, final Request request, HttpResponse response,
206228
final NettyResponseFuture<?> future, ProxyServer proxyServer, final Channel channel) throws Exception {
207229
if (statusCode == UNAUTHORIZED.code() && realm != null) {
@@ -227,7 +249,8 @@ private boolean handleUnauthorizedAndExit(int statusCode, Realm realm, final Req
227249
.parseWWWAuthenticateHeader(authenticateHeaders.get(0)).build();
228250
}
229251

230-
Realm nr = new Realm.RealmBuilder().clone(newRealm).setUri(request.getURI().getPath()).build();
252+
String realmURI = computeRealmURI(newRealm, request.getURI());
253+
Realm nr = new Realm.RealmBuilder().clone(newRealm).setUri(realmURI).build();
231254
final Request nextRequest = new RequestBuilder(future.getRequest()).setHeaders(request.getHeaders()).setRealm(nr).build();
232255

233256
LOGGER.debug("Sending authentication to {}", request.getUrl());

0 commit comments

Comments
 (0)