You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2006/04/30 13:25:09 UTC

svn commit: r398315 - in /jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src: java/org/apache/commons/httpclient/cookie/RFC2965Spec.java test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java

Author: olegk
Date: Sun Apr 30 04:25:07 2006
New Revision: 398315

URL: http://svn.apache.org/viewcvs?rev=398315&view=rev
Log:
Refactored handling of the 'Secure' cookie attribute

Modified:
    jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
    jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java

Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java?rev=398315&r1=398314&r2=398315&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/RFC2965Spec.java Sun Apr 30 04:25:07 2006
@@ -93,8 +93,9 @@
         registerAttribHandler(Cookie2.PATH, new Cookie2PathAttributeHandler());
         registerAttribHandler(Cookie2.DOMAIN, new Cookie2DomainAttributeHandler());
         registerAttribHandler(Cookie2.PORT, new Cookie2PortAttributeHandler());
-        registerAttribHandler(Cookie2.VERSION, new Cookie2VersionAttributeHandler());
         registerAttribHandler(Cookie2.MAXAGE, new Cookie2MaxageAttributeHandler());
+        registerAttribHandler(Cookie2.SECURE, new CookieSecureAttributeHandler());
+        registerAttribHandler(Cookie2.VERSION, new Cookie2VersionAttributeHandler());
     }
 
     protected void registerAttribHandler(
@@ -309,8 +310,6 @@
         if (paramName.equals(Cookie2.COMMENT)) {
             if (cookie.getComment() == null)
                 cookie.setComment(paramValue);
-        } else if (paramName.equals(Cookie2.SECURE)) {
-            cookie.setSecure(true);
         } else if (paramName.equals(Cookie2.COMMENTURL)) {
             if (cookie.getCommentURL() == null)
                 cookie.setCommentURL(paramValue);
@@ -336,7 +335,7 @@
      * @throws MalformedCookieException if an exception occurs during
      * validation
      */
-    public void validate(String host, int port, String path,
+    public void validate(final String host, int port, final String path,
                          boolean secure, final Cookie cookie)
             throws MalformedCookieException {
 
@@ -380,6 +379,10 @@
             throw new IllegalArgumentException("Cookie may not be null");
         }
         if (cookie instanceof Cookie2) {
+            // check if cookie has expired
+            if (cookie.isPersistent() && cookie.isExpired()) {
+                return false;
+            }
             CookieSource cookiesource = new CookieSource(host, port, path, secure); 
             for (Iterator i = getAttribHandlerIterator(); i.hasNext(); ) {
                 CookieAttributeHandler handler = (CookieAttributeHandler) i.next();
@@ -387,16 +390,6 @@
                     return false;
                 }
             }
-            // check if cookie has expired
-            if (cookie.isPersistent() && cookie.isExpired()) {
-                return false;
-            }
-            // finally make sure that if cookie Secure attribute is set, then this
-            // request is made using a secure connection
-            if (cookie.getSecure()) {
-                return secure;
-            }
-            // if we get to this stage, we have a match
             return true;
         } else {
             // old-style cookies are matched according to the old rules
@@ -965,6 +958,36 @@
           return true;
       }
 
+  }
+
+  /**
+   * <tt>"Secure"</tt> cookie attribute handler for RFC 2965 cookie spec.
+   */
+  private class CookieSecureAttributeHandler
+          implements CookieAttributeHandler {
+
+      public void parse(final Cookie cookie, final String secure)
+              throws MalformedCookieException {
+          cookie.setSecure(true);
+      }
+
+      public void validate(final Cookie cookie, final CookieSource source)
+              throws MalformedCookieException {
+      }
+
+      /**
+       * @see CookieAttributeHandler#match(org.apache.commons.httpclient.Cookie, String)
+       */
+      public boolean match(final Cookie cookie, final CookieSource source) {
+          if (cookie == null) {
+              throw new IllegalArgumentException("Cookie may not be null");
+          }
+          if (source == null) {
+              throw new IllegalArgumentException("Cookie source may not be null");
+          }
+          return cookie.getSecure() == source.isSecure();
+      }
+      
   }
 
     /**

Modified: jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java?rev=398315&r1=398314&r2=398315&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java (original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java Sun Apr 30 04:25:07 2006
@@ -374,6 +374,19 @@
     }
 
     /**
+     * test parsing <tt>"Secure"</tt> attribute.
+     */
+    public void testParseSecure() throws Exception {
+        CookieSpec cookiespec = new RFC2965Spec();
+        Header header = new Header("Set-Cookie2", "name=value;Secure;Version=1");
+        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        assertNotNull(parsed);
+        assertEquals(1, parsed.length);
+        Cookie2 cookie = (Cookie2) parsed[0];
+        assertTrue(cookie.getSecure());
+    }
+
+    /**
      * test parsing <tt>"Discard"</tt> attribute.
      */
     public void testParseDiscard() throws Exception {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org