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:24:08 UTC

[httpcomponents-client] branch 5.1.x 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 5.1.x
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git


The following commit(s) were added to refs/heads/5.1.x by this push:
     new f87bb75  Corrected resolution of the target host in DefaultUserTokenHandler
f87bb75 is described below

commit f87bb75a40cd244dbe3462be286b7e069d0d3777
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) {