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 2011/05/07 17:30:18 UTC

svn commit: r1100557 - /httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java

Author: olegk
Date: Sat May  7 15:30:17 2011
New Revision: 1100557

URL: http://svn.apache.org/viewvc?rev=1100557&view=rev
Log:
Implemented proxy authentication for tunneled SSL connections

Modified:
    httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java

Modified: httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java?rev=1100557&r1=1100556&r2=1100557&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java (original)
+++ httpcomponents/httpasyncclient/trunk/httpasyncclient/src/main/java/org/apache/http/impl/nio/client/DefaultAsyncRequestDirector.java Sat May  7 15:30:17 2011
@@ -287,7 +287,10 @@ class DefaultAsyncRequestDirector<T> imp
             if (method.equalsIgnoreCase("CONNECT") && status == HttpStatus.SC_OK) {
                 this.managedConn.tunnelTarget(this.params);
             } else {
-                this.finalResponse = response;
+                this.followup = handleConnectResponse();
+                if (this.followup == null) {
+                    this.finalResponse = response;
+                }
             }
         } else {
             this.followup = handleResponse();
@@ -569,11 +572,52 @@ class DefaultAsyncRequestDirector<T> imp
     }
 
     private RoutedRequest handleResponse() throws HttpException {
-        HttpRoute route = this.mainRequest.getRoute();
-        RequestWrapper request = this.mainRequest.getRequest();
-        if (HttpClientParams.isRedirecting(this.params) && this.redirectStrategy.isRedirected(
+        RoutedRequest followup = null;
+        if (HttpClientParams.isRedirecting(this.params)) {
+            followup = handleRedirect();
+            if (followup != null) {
+                return followup;
+            }
+        }
+        if (HttpClientParams.isAuthenticating(this.params)) {
+            CredentialsProvider credsProvider = (CredentialsProvider) this.localContext.getAttribute(
+                    ClientContext.CREDS_PROVIDER);
+            if (credsProvider != null) {
+                followup = handleTargetChallenge(credsProvider);
+                if (followup != null) {
+                    return followup;
+                }
+                followup = handleProxyChallenge(credsProvider);
+                if (followup != null) {
+                    return followup;
+                }
+            }
+        }
+        return null;
+    }
+
+    private RoutedRequest handleConnectResponse() throws HttpException {
+        RoutedRequest followup = null;
+        if (HttpClientParams.isAuthenticating(this.params)) {
+            CredentialsProvider credsProvider = (CredentialsProvider) this.localContext.getAttribute(
+                    ClientContext.CREDS_PROVIDER);
+            if (credsProvider != null) {
+                followup = handleProxyChallenge(credsProvider);
+                if (followup != null) {
+                    return followup;
+                }
+            }
+        }
+        return null;
+    }
+
+    private RoutedRequest handleRedirect() throws HttpException {
+        if (this.redirectStrategy.isRedirected(
                 this.currentRequest, this.currentResponse, this.localContext)) {
 
+            HttpRoute route = this.mainRequest.getRoute();
+            RequestWrapper request = this.mainRequest.getRequest();
+
             int maxRedirects = this.params.getIntParameter(ClientPNames.MAX_REDIRECTS, 100);
             if (this.redirectCount >= maxRedirects) {
                 throw new RedirectException("Maximum redirects ("
@@ -615,75 +659,79 @@ class DefaultAsyncRequestDirector<T> imp
             }
             return new RoutedRequest(newRequest, newRoute);
         }
+        return null;
+    }
 
-        CredentialsProvider credsProvider = (CredentialsProvider) this.localContext.getAttribute(
-                ClientContext.CREDS_PROVIDER);
-
-        if (credsProvider != null && HttpClientParams.isAuthenticating(params)) {
-
-            if (this.targetAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) {
+    private RoutedRequest handleTargetChallenge(
+            final CredentialsProvider credsProvider) throws HttpException {
+        if (this.targetAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) {
+            HttpRoute route = this.mainRequest.getRoute();
 
-                HttpHost target = (HttpHost) this.localContext.getAttribute(
-                        ExecutionContext.HTTP_TARGET_HOST);
-                if (target == null) {
-                    target = route.getTargetHost();
-                }
+            HttpHost target = (HttpHost) this.localContext.getAttribute(
+                    ExecutionContext.HTTP_TARGET_HOST);
+            if (target == null) {
+                target = route.getTargetHost();
+            }
 
-                this.log.debug("Target requested authentication");
-                Map<String, Header> challenges = this.targetAuthHandler.getChallenges(
+            this.log.debug("Target requested authentication");
+            Map<String, Header> challenges = this.targetAuthHandler.getChallenges(
+                    this.currentResponse, this.localContext);
+            try {
+                processChallenges(challenges,
+                        this.targetAuthState, this.targetAuthHandler,
                         this.currentResponse, this.localContext);
-                try {
-                    processChallenges(challenges,
-                            this.targetAuthState, this.targetAuthHandler,
-                            this.currentResponse, this.localContext);
-                } catch (AuthenticationException ex) {
-                    if (this.log.isWarnEnabled()) {
-                        this.log.warn("Authentication error: " +  ex.getMessage());
-                        return null;
-                    }
-                }
-                updateAuthState(this.targetAuthState, target, credsProvider);
-
-                if (this.targetAuthState.getCredentials() != null) {
-                    // Re-try the same request via the same route
-                    return this.mainRequest;
-                } else {
+            } catch (AuthenticationException ex) {
+                if (this.log.isWarnEnabled()) {
+                    this.log.warn("Authentication error: " +  ex.getMessage());
                     return null;
                 }
+            }
+            updateAuthState(this.targetAuthState, target, credsProvider);
+
+            if (this.targetAuthState.getCredentials() != null) {
+                // Re-try the same request via the same route
+                return this.mainRequest;
             } else {
-                // Reset target auth scope
-                this.targetAuthState.setAuthScope(null);
+                return null;
             }
+        } else {
+            // Reset target auth scope
+            this.targetAuthState.setAuthScope(null);
+        }
+        return null;
+    }
 
-            if (this.proxyAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) {
+    private RoutedRequest handleProxyChallenge(
+            final CredentialsProvider credsProvider) throws HttpException {
+        if (this.proxyAuthHandler.isAuthenticationRequested(this.currentResponse, this.localContext)) {
+            HttpRoute route = this.mainRequest.getRoute();
 
-                HttpHost proxy = route.getProxyHost();
+            HttpHost proxy = route.getProxyHost();
 
-                this.log.debug("Proxy requested authentication");
-                Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(
+            this.log.debug("Proxy requested authentication");
+            Map<String, Header> challenges = this.proxyAuthHandler.getChallenges(
+                    this.currentResponse, this.localContext);
+            try {
+                processChallenges(challenges,
+                        this.proxyAuthState, this.proxyAuthHandler,
                         this.currentResponse, this.localContext);
-                try {
-                    processChallenges(challenges,
-                            this.proxyAuthState, this.proxyAuthHandler,
-                            this.currentResponse, this.localContext);
-                } catch (AuthenticationException ex) {
-                    if (this.log.isWarnEnabled()) {
-                        this.log.warn("Authentication error: " +  ex.getMessage());
-                        return null;
-                    }
-                }
-                updateAuthState(this.proxyAuthState, proxy, credsProvider);
-
-                if (this.proxyAuthState.getCredentials() != null) {
-                    // Re-try the same request via the same route
-                    return this.mainRequest;
-                } else {
+            } catch (AuthenticationException ex) {
+                if (this.log.isWarnEnabled()) {
+                    this.log.warn("Authentication error: " +  ex.getMessage());
                     return null;
                 }
+            }
+            updateAuthState(this.proxyAuthState, proxy, credsProvider);
+
+            if (this.proxyAuthState.getCredentials() != null) {
+                // Re-try the same request via the same route
+                return this.mainRequest;
             } else {
-                // Reset proxy auth scope
-                this.proxyAuthState.setAuthScope(null);
+                return null;
             }
+        } else {
+            // Reset proxy auth scope
+            this.proxyAuthState.setAuthScope(null);
         }
         return null;
     }