hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r708225 - in /httpcomponents/httpclient/trunk/module-client/src: main/java/org/apache/http/impl/cookie/ test/java/org/apache/http/impl/cookie/
Date Mon, 27 Oct 2008 16:08:27 GMT
Author: olegk
Date: Mon Oct 27 09:08:27 2008
New Revision: 708225

URL: http://svn.apache.org/viewvc?rev=708225&view=rev
Log:
Fixed broken #parse(HeaderElement[], CookieOrigin) method in the RFC2965Spec cookie spec


Modified:
    httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java
    httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java
    httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java

Modified: httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java?rev=708225&r1=708224&r2=708225&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java
Mon Oct 27 09:08:27 2008
@@ -58,6 +58,44 @@
  */
 public class RFC2965Spec extends RFC2109Spec {
 
+    private static final ClientCookieFactory COOKIE_FACTORY = new BasicClientCookieFactory();
+    private static final ClientCookieFactory COOKIE2_FACTORY = new BasicClientCookie2Factory();
+    
+    private static interface ClientCookieFactory {
+        
+        BasicClientCookie createCookie(String name, String value, CookieOrigin origin);
+        
+    }
+    
+    private static class BasicClientCookieFactory implements ClientCookieFactory {
+
+        public BasicClientCookie createCookie(
+                final String name, 
+                final String value, 
+                final CookieOrigin origin) {
+            BasicClientCookie cookie = new BasicClientCookie(name, value);
+            cookie.setPath(getDefaultPath(origin));
+            cookie.setDomain(getDefaultDomain(origin));
+            return cookie;
+        }
+        
+    }
+    
+    private static class BasicClientCookie2Factory implements ClientCookieFactory {
+
+        public BasicClientCookie createCookie(
+                final String name, 
+                final String value, 
+                final CookieOrigin origin) {
+            BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
+            cookie.setPath(getDefaultPath(origin));
+            cookie.setDomain(getDefaultDomain(origin));
+            cookie.setPorts(new int [] { origin.getPort() });
+            return cookie;
+        }
+        
+    }
+
     /** 
      * Default constructor 
      * 
@@ -74,24 +112,7 @@
         registerAttribHandler(ClientCookie.DISCARD_ATTR, new RFC2965DiscardAttributeHandler());
         registerAttribHandler(ClientCookie.VERSION_ATTR, new RFC2965VersionAttributeHandler());
     }
-    
-    private BasicClientCookie createCookie(
-            final String name, final String value, final CookieOrigin origin) {
-        BasicClientCookie cookie = new BasicClientCookie(name, value);
-        cookie.setPath(getDefaultPath(origin));
-        cookie.setDomain(getDefaultDomain(origin));
-        return cookie;
-    }
-    
-    private BasicClientCookie createCookie2(
-            final String name, final String value, final CookieOrigin origin) {
-        BasicClientCookie2 cookie = new BasicClientCookie2(name, value);
-        cookie.setPath(getDefaultPath(origin));
-        cookie.setDomain(getDefaultDomain(origin));
-        cookie.setPorts(new int [] { origin.getPort() });
-        return cookie;
-    }
-    
+
     @Override
     public List<Cookie> parse(
             final Header header, 
@@ -104,9 +125,28 @@
         }
         
         origin = adjustEffectiveHost(origin);
-        
         HeaderElement[] elems = header.getElements();
+        ClientCookieFactory cookieFactory;
+        if (header.getName().equals(SM.SET_COOKIE2)) {
+            cookieFactory = COOKIE2_FACTORY;
+        } else {
+            cookieFactory = COOKIE_FACTORY;
+        }
+        return createCookies(elems, origin, cookieFactory);
+    }
+    
+    @Override
+    protected List<Cookie> parse(
+            final HeaderElement[] elems, 
+            CookieOrigin origin) throws MalformedCookieException {
+        origin = adjustEffectiveHost(origin);
+        return createCookies(elems, origin, COOKIE2_FACTORY);
+    }
 
+    private List<Cookie> createCookies(
+            final HeaderElement[] elems, 
+            final CookieOrigin origin,
+            final ClientCookieFactory cookieFactory) throws MalformedCookieException {
         List<Cookie> cookies = new ArrayList<Cookie>(elems.length);
         for (HeaderElement headerelement : elems) {
             String name = headerelement.getName();
@@ -115,13 +155,7 @@
                 throw new MalformedCookieException("Cookie name may not be empty");
             }
 
-            BasicClientCookie cookie;
-            if (header.getName().equals(SM.SET_COOKIE2)) {
-                cookie = createCookie2(name, value, origin);
-            } else {
-                cookie = createCookie(name, value, origin);
-            }
-
+            BasicClientCookie cookie = cookieFactory.createCookie(name, value, origin);
             // cycle through the parameters
             NameValuePair[] attribs = headerelement.getParameters();
 

Modified: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java?rev=708225&r1=708224&r2=708225&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java
Mon Oct 27 09:08:27 2008
@@ -43,6 +43,7 @@
 import org.apache.http.cookie.CookieOrigin;
 import org.apache.http.cookie.CookieSpec;
 import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.cookie.SetCookie2;
 import org.apache.http.message.BasicHeader;
 
 /**
@@ -123,6 +124,36 @@
         }
     }
 
+    public void testCookieStandardCompliantParsingLocalHost() throws Exception {
+        CookieSpec cookiespec = new BestMatchSpec();
+        CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
+
+        Header header = new BasicHeader("Set-Cookie", "special=\"abcdigh\"; Version=1");
+
+        List<Cookie> cookies = cookiespec.parse(header, origin);
+        for (int i = 0; i < cookies.size(); i++) {
+            Cookie cookie = cookies.get(i);
+            cookiespec.validate(cookie, origin);
+            assertEquals("localhost.local", cookie.getDomain());
+            assertTrue(cookie instanceof SetCookie2);
+        }
+    }
+
+    public void testCookieStandardCompliantParsingLocalHost2() throws Exception {
+        CookieSpec cookiespec = new BestMatchSpec();
+        CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
+
+        Header header = new BasicHeader("Set-Cookie2", "special=\"abcdigh\"; Version=1");
+
+        List<Cookie> cookies = cookiespec.parse(header, origin);
+        for (int i = 0; i < cookies.size(); i++) {
+            Cookie cookie = cookies.get(i);
+            cookiespec.validate(cookie, origin);
+            assertEquals("localhost.local", cookie.getDomain());
+            assertTrue(cookie instanceof SetCookie2);
+        }
+    }
+
     public void testCookieBrowserCompatMatch() throws Exception {
         CookieSpec cookiespec = new BestMatchSpec();
         CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);

Modified: httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java?rev=708225&r1=708224&r2=708225&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java
(original)
+++ httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java
Mon Oct 27 09:08:27 2008
@@ -122,7 +122,7 @@
     /**
      * Test parsing cookie <tt>"Domain"</tt> attribute.
      */
-    public void testcookiesomain() throws Exception {
+    public void testParseDomain() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
         CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         Header header = new BasicHeader("Set-Cookie2", "name=value;Domain=.domain.com;Version=1;Domain=");
@@ -143,7 +143,7 @@
         assertEquals(".domain.com", cookie.getDomain());
     }
 
-    public void testcookiesomainDefault() throws Exception {
+    public void testParseDomainDefaultValue() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
         CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // Domain is OPTIONAL, defaults to the request host
@@ -392,7 +392,7 @@
     /**
      * test parsing <tt>"Discard"</tt> attribute.
      */
-    public void testcookiesiscard() throws Exception {
+    public void testParseDiscard() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
         CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         Header header = new BasicHeader("Set-Cookie2", "name=value;Discard;Max-age=36000;Version=1");
@@ -549,6 +549,19 @@
         } catch (MalformedCookieException expected) {}
     }
 
+    public void testValidateDomainLocalhost() throws Exception {
+        CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value; version=1");
+        List<Cookie> cookies = cookiespec.parse(header, origin);
+        for (int i = 0; i < cookies.size(); i++) {
+            cookiespec.validate(cookies.get(i), origin);
+        }
+        assertNotNull(cookies);
+        assertEquals(1, cookies.size());
+        ClientCookie cookie = (ClientCookie) cookies.get(0);
+        assertEquals("localhost.local", cookie.getDomain());
+    }
 
     /**
      * Test <tt>Domain</tt> validation. Effective host name



Mime
View raw message