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 2012/10/10 21:14:10 UTC

svn commit: r1396748 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/client/protocol/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/java/org/apache/http/impl/client/

Author: olegk
Date: Wed Oct 10 19:14:10 2012
New Revision: 1396748

URL: http://svn.apache.org/viewvc?rev=1396748&view=rev
Log:
HTTPCLIENT-1215: BasicAuthCache does not take default ports into consideration when looking up cached authentication details by HttpHost key

Added:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java   (with props)
Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AuthenticationStrategyImpl.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=1396748&r1=1396747&r2=1396748&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Wed Oct 10 19:14:10 2012
@@ -1,6 +1,10 @@
 Changes since 4.2.1 
 -------------------
 
+* [HTTPCLIENT-1215] BasicAuthCache does not take default ports into consideration when
+  looking up cached authentication details by HttpHost key.
+  Contributed by Oleg Kalnichevski <olegk at apache.org>
+
 * [HTTPCLIENT-1241] (regression) Preemptive BASIC authentication failure should be considered 
   final and no further attempts to re-authenticate using the same credentials should be made. 
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java?rev=1396748&r1=1396747&r2=1396748&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java Wed Oct 10 19:14:10 2012
@@ -84,15 +84,15 @@ public class ResponseAuthCache implement
                 this.log.debug("Target auth state: " + targetState.getState());
             }
             if (isCachable(targetState)) {
+                SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
+                        ClientContext.SCHEME_REGISTRY);
                 if (target.getPort() < 0) {
-                    SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
-                            ClientContext.SCHEME_REGISTRY);
                     Scheme scheme = schemeRegistry.getScheme(target);
                     target = new HttpHost(target.getHostName(),
                             scheme.resolvePort(target.getPort()), target.getSchemeName());
                 }
                 if (authCache == null) {
-                    authCache = new BasicAuthCache();
+                    authCache = new BasicAuthCache(schemeRegistry);
                     context.setAttribute(ClientContext.AUTH_CACHE, authCache);
                 }
                 switch (targetState.getState()) {

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AuthenticationStrategyImpl.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AuthenticationStrategyImpl.java?rev=1396748&r1=1396747&r2=1396748&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AuthenticationStrategyImpl.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AuthenticationStrategyImpl.java Wed Oct 10 19:14:10 2012
@@ -54,6 +54,7 @@ import org.apache.http.client.Authentica
 import org.apache.http.client.CredentialsProvider;
 import org.apache.http.client.params.AuthPolicy;
 import org.apache.http.client.protocol.ClientContext;
+import org.apache.http.conn.scheme.SchemeRegistry;
 import org.apache.http.protocol.HTTP;
 import org.apache.http.protocol.HttpContext;
 import org.apache.http.util.CharArrayBuffer;
@@ -220,7 +221,9 @@ class AuthenticationStrategyImpl impleme
         if (isCachable(authScheme)) {
             AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);
             if (authCache == null) {
-                authCache = new BasicAuthCache();
+                SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(
+                        ClientContext.SCHEME_REGISTRY);
+                authCache = new BasicAuthCache(schemeRegistry);
                 context.setAttribute(ClientContext.AUTH_CACHE, authCache);
             }
             if (this.log.isDebugEnabled()) {

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java?rev=1396748&r1=1396747&r2=1396748&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java Wed Oct 10 19:14:10 2012
@@ -32,6 +32,8 @@ import org.apache.http.HttpHost;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.auth.AuthScheme;
 import org.apache.http.client.AuthCache;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
 
 /**
  * Default implementation of {@link AuthCache}.
@@ -41,35 +43,60 @@ import org.apache.http.client.AuthCache;
 @NotThreadSafe
 public class BasicAuthCache implements AuthCache {
 
+    private final SchemeRegistry schemeRegistry;
     private final HashMap<HttpHost, AuthScheme> map;
 
     /**
      * Default constructor.
+     *
+     * @param schemeRegistry the scheme registry to resolve default port by scheme name.
+     *
+     * @since 4.3
      */
-    public BasicAuthCache() {
+    public BasicAuthCache(final SchemeRegistry schemeRegistry) {
         super();
+        this.schemeRegistry = schemeRegistry;
         this.map = new HashMap<HttpHost, AuthScheme>();
     }
 
+    public BasicAuthCache() {
+        this(null);
+    }
+
+    protected HttpHost getKey(final HttpHost host) {
+        if (host.getPort() <= 0) {
+            int port;
+            if (this.schemeRegistry != null) {
+                Scheme scheme = this.schemeRegistry.getScheme(host);
+                port = scheme.resolvePort(host.getPort());
+            } else {
+                port = host.getSchemeName().equalsIgnoreCase("https") ? 443 : 80;
+            }
+            return new HttpHost(host.getHostName(), port, host.getSchemeName());
+        } else {
+            return host;
+        }
+    }
+
     public void put(final HttpHost host, final AuthScheme authScheme) {
         if (host == null) {
             throw new IllegalArgumentException("HTTP host may not be null");
         }
-        this.map.put(host, authScheme);
+        this.map.put(getKey(host), authScheme);
     }
 
     public AuthScheme get(final HttpHost host) {
         if (host == null) {
             throw new IllegalArgumentException("HTTP host may not be null");
         }
-        return this.map.get(host);
+        return this.map.get(getKey(host));
     }
 
     public void remove(final HttpHost host) {
         if (host == null) {
             throw new IllegalArgumentException("HTTP host may not be null");
         }
-        this.map.remove(host);
+        this.map.remove(getKey(host));
     }
 
     public void clear() {

Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java?rev=1396748&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java Wed Oct 10 19:14:10 2012
@@ -0,0 +1,77 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.impl.client;
+
+import org.apache.http.HttpHost;
+import org.apache.http.auth.AuthScheme;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.scheme.SchemeSocketFactory;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Unit tests for {@link BasicAuthCache}.
+ */
+public class TestBasicAuthCache {
+
+    @Test
+    public void testBasics() throws Exception {
+        BasicAuthCache cache = new BasicAuthCache();
+        AuthScheme authScheme = Mockito.mock(AuthScheme.class);
+        cache.put(new HttpHost("localhost", 80), authScheme);
+        Assert.assertSame(authScheme, cache.get(new HttpHost("localhost", 80)));
+        cache.remove(new HttpHost("localhost", 80));
+        Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
+        cache.put(new HttpHost("localhost", 80), authScheme);
+        cache.clear();
+        Assert.assertNull(cache.get(new HttpHost("localhost", 80)));
+    }
+
+    @Test
+    public void testGetKey() throws Exception {
+        BasicAuthCache cache = new BasicAuthCache();
+        HttpHost target = new HttpHost("localhost", 443, "https");
+        Assert.assertSame(target, cache.getKey(target));
+        Assert.assertEquals(target, cache.getKey(new HttpHost("localhost", -1, "https")));
+    }
+
+    @Test
+    public void testGetKeyWithSchemeRegistry() throws Exception {
+        SchemeSocketFactory socketFactory = Mockito.mock(SchemeSocketFactory.class);
+        Scheme scheme = new Scheme("https", 443, socketFactory);
+        SchemeRegistry registory = new SchemeRegistry();
+        registory.register(scheme);
+        BasicAuthCache cache = new BasicAuthCache(registory);
+        HttpHost target = new HttpHost("localhost", 443, "https");
+        Assert.assertSame(target, cache.getKey(target));
+        Assert.assertEquals(target, cache.getKey(new HttpHost("localhost", -1, "https")));
+    }
+
+}

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain