You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/09/26 14:14:53 UTC
[httpcomponents-client] branch master updated: Corrected resolution
of the target host in DefaultUserTokenHandler
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
The following commit(s) were added to refs/heads/master by this push:
new 52202c4 Corrected resolution of the target host in DefaultUserTokenHandler
52202c4 is described below
commit 52202c428ab2b05920732b547f4fffac9e3a2d03
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Sep 26 15:51:23 2021 +0200
Corrected resolution of the target host in DefaultUserTokenHandler
---
.../client5/http/impl/DefaultUserTokenHandler.java | 34 +++++++++++++---------
1 file changed, 21 insertions(+), 13 deletions(-)
diff --git a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java
index 6570cc4..62c3db6 100644
--- a/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java
+++ b/httpclient5/src/main/java/org/apache/hc/client5/http/impl/DefaultUserTokenHandler.java
@@ -37,6 +37,8 @@ import org.apache.hc.client5.http.auth.AuthScheme;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.annotation.Contract;
import org.apache.hc.core5.annotation.ThreadingBehavior;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.protocol.HttpContext;
/**
@@ -64,25 +66,31 @@ public class DefaultUserTokenHandler implements UserTokenHandler {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
- Principal userPrincipal = null;
+ final HttpRequest request = clientContext.getRequest();
+ final HttpHost target = request != null ? new HttpHost(request.getScheme(), request.getAuthority()) : route.getTargetHost();
- final AuthExchange targetAuthExchange = clientContext.getAuthExchange(route.getTargetHost());
+ final AuthExchange targetAuthExchange = clientContext.getAuthExchange(target);
if (targetAuthExchange != null) {
- userPrincipal = getAuthPrincipal(targetAuthExchange);
- if (userPrincipal == null && route.getProxyHost() != null) {
- final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(route.getProxyHost());
- userPrincipal = getAuthPrincipal(proxyAuthExchange);
+ final Principal authPrincipal = getAuthPrincipal(targetAuthExchange);
+ if (authPrincipal != null) {
+ return authPrincipal;
}
}
-
- if (userPrincipal == null) {
- final SSLSession sslSession = clientContext.getSSLSession();
- if (sslSession != null) {
- userPrincipal = sslSession.getLocalPrincipal();
+ final HttpHost proxy = route.getProxyHost();
+ if (proxy != null) {
+ final AuthExchange proxyAuthExchange = clientContext.getAuthExchange(proxy);
+ if (proxyAuthExchange != null) {
+ final Principal authPrincipal = getAuthPrincipal(proxyAuthExchange);
+ if (authPrincipal != null) {
+ return authPrincipal;
+ }
}
}
-
- return userPrincipal;
+ final SSLSession sslSession = clientContext.getSSLSession();
+ if (sslSession != null) {
+ return sslSession.getLocalPrincipal();
+ }
+ return null;
}
private static Principal getAuthPrincipal(final AuthExchange authExchange) {