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:13:13 UTC

svn commit: r1396747 - in /httpcomponents/httpclient/branches/4.2.x: ./ 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:13:12 2012
New Revision: 1396747

URL: http://svn.apache.org/viewvc?rev=1396747&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/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java   (with props)
Modified:
    httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
    httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java

Modified: httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt?rev=1396747&r1=1396746&r2=1396747&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/branches/4.2.x/RELEASE_NOTES.txt Wed Oct 10 19:13:12 2012
@@ -1,5 +1,9 @@
 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/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java?rev=1396747&r1=1396746&r2=1396747&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java (original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/client/protocol/ResponseAuthCache.java Wed Oct 10 19:13:12 2012
@@ -84,9 +84,9 @@ 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());

Modified: httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java?rev=1396747&r1=1396746&r2=1396747&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java (original)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/main/java/org/apache/http/impl/client/BasicAuthCache.java Wed Oct 10 19:13:12 2012
@@ -51,25 +51,34 @@ public class BasicAuthCache implements A
         this.map = new HashMap<HttpHost, AuthScheme>();
     }
 
+    protected HttpHost getKey(final HttpHost host) {
+        if (host.getPort() <= 0) {
+            int 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/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java?rev=1396747&view=auto
==============================================================================
--- httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java (added)
+++ httpcomponents/httpclient/branches/4.2.x/httpclient/src/test/java/org/apache/http/impl/client/TestBasicAuthCache.java Wed Oct 10 19:13:12 2012
@@ -0,0 +1,62 @@
+/*
+ * ====================================================================
+ * 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.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")));
+    }
+
+}

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

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

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