commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r399679 - in /jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src: java/org/apache/commons/httpclient/cookie/ test/org/apache/commons/httpclient/cookie/
Date Thu, 04 May 2006 11:45:19 GMT
Author: olegk
Date: Thu May  4 04:45:16 2006
New Revision: 399679

URL: http://svn.apache.org/viewcvs?rev=399679&view=rev
Log:
Improved compliance with the section 3.3.4 of the RFC 2965

Modified:
    jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java
    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/Cookie2.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java?rev=399679&r1=399678&r2=399679&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java
(original)
+++ jakarta/commons/proper/httpclient/branches/COOKIE_2_BRANCH/src/java/org/apache/commons/httpclient/cookie/Cookie2.java
Thu May  4 04:45:16 2006
@@ -187,54 +187,8 @@
     }
 
     /**
-     * Indicates whether the cookie had a domain attribute specified in the
-     * <tt>Set-Cookie2</tt> response header. This property
-     * has two uses:
-     * <ul>
-     * <li>This value is important for generating the <tt>Cookie</tt> request
header
-     * because the specification requires that the <tt>Cookie</tt> header
-     * should only include a domain attribute if the domain was specified in the
-     * <tt>Set-Cookie2</tt> header.
-     * </li>
-     * <li>This also ensures that if domain attribute appears more than once in
-     * the <tt>Set-Cookie2</tt> header, only the first appearance of domain
-     * attribute is used.
-     * </li>
-     * </ul>
-     * @param value <tt>true</tt> if the cookie's domain is specified
-     * in response header.
-     *
-     * @see #isDomainAttributeSpecified
-     */
-    public void setDomainAttributeSpecified(boolean value) {
-        hasDomainAttribute = value;
-    }
-
-    /**
-     * @return <tt>true</tt> if cookie domain attribute was specified in the
-     * <tt>Set-Cookie2</tt> header.
-     *
-     * @see #setDomainAttributeSpecified
-     */
-    public boolean isDomainAttributeSpecified() {
-        return hasDomainAttribute;
-    }
-
-    /**
      * Indicates whether the cookie had a port attribute specified in the
-     * <tt>Set-Cookie2</tt> response header. This property
-     * has two uses:
-     * <ul>
-     * <li>This value is important for generating the <tt>Cookie</tt> request
header
-     * because the specification requires that the <tt>Cookie</tt> header
-     * should only include a port attribute if the port was specified in the
-     * <tt>Set-Cookie2</tt> header.
-     * </li>
-     * <li>This also ensures that if port attribute appears more than once in
-     * the <tt>Set-Cookie2</tt> header, only the first appearance of the port
-     * attribute is used.
-     * </li>
-     * </ul>
+     * <tt>Set-Cookie2</tt> response header. 
      *
      * @param value <tt>true</tt> if port attribute is specified in response
      * header.
@@ -285,13 +239,7 @@
 
     /**
      * Indicates whether the cookie had a version attribute specified in the
-     * <tt>Set-Cookie2</tt> response header. This property has two uses:
-     * <ul>
-     * <li>Only the first occurence of the version attribute in <tt>Set-Cookie2</tt>
-     * header is used, others ignored.</li>
-     * <li>Since version attribute is required for valid cookies, this property
-     * helps in checking this condition.</li>
-     * </ul>
+     * <tt>Set-Cookie2</tt> response header. 
      *
      * @param value <tt>true</tt> if version attribute is specified in response
      * header.
@@ -336,12 +284,6 @@
      * Discard attribute.
      */
     private boolean discard = false;
-
-    /**
-     * Indicates if the set-cookie header included a Domain attribute for this
-     * cookie
-     */
-    private boolean hasDomainAttribute = false;
 
     /**
      * Indicates if the set-cookie2 header included a Port attribute for this

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=399679&r1=399678&r2=399679&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
Thu May  4 04:45:16 2006
@@ -434,8 +434,9 @@
         if (cookie instanceof Cookie2) {
             /* format cookie2 cookie */
             Cookie2 cookie2 = (Cookie2) cookie;
+            int version = cookie2.getVersion();
             final StringBuffer buffer = new StringBuffer();
-            this.formatter.format(buffer, new NameValuePair("$Version", "1"));
+            this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));
             buffer.append("; ");
             doFormatCookie2(cookie2, buffer);
             return buffer.toString();
@@ -452,7 +453,7 @@
      * @param cookies an array of {@link org.apache.commons.httpclient.Cookie}s to be formatted
      * @return a string suitable for sending in a Cookie header.
      */
-    public String formatCookies(Cookie[] cookies) {
+    public String formatCookies(final Cookie[] cookies) {
         LOG.trace("enter RFC2965Spec.formatCookieHeader(Cookie[])");
 
         if (cookies == null) {
@@ -460,20 +461,28 @@
         }
         // check if cookies array contains a set-cookie (old style) cookie
         boolean hasOldStyleCookie = false;
+        int version = -1;
         for (int i = 0; i < cookies.length; i++) {
-            if (!(cookies[i] instanceof Cookie2)) {
+            Cookie cookie = cookies[i];
+            if (!(cookie instanceof Cookie2)) {
                 hasOldStyleCookie = true;
                 break;
             }
+            if (cookie.getVersion() > version) {
+                version = cookie.getVersion();
+            }
+        }
+        if (version < 0) {
+            version = 0;
         }
-        if (hasOldStyleCookie) {
+        if (hasOldStyleCookie || version < 1) {
             // delegate old-style cookie formatting to rfc2109Spec
             return this.rfc2109.formatCookies(cookies);
         }
         /* format cookie2 cookies */
         final StringBuffer buffer = new StringBuffer();
         // format cookie version
-        this.formatter.format(buffer, new NameValuePair("$Version", "1"));
+        this.formatter.format(buffer, new NameValuePair("$Version", Integer.toString(version)));
         for (int i = 0; i < cookies.length; i++) {
             buffer.append("; ");
             Cookie2 cookie = (Cookie2) cookies[i];
@@ -1063,7 +1072,7 @@
                 if (version < 0) {
                     throw new MalformedCookieException("Invalid cookie version.");
                 }
-                cookie2.setVersion(Integer.parseInt(value));
+                cookie2.setVersion(version);
                 cookie2.setVersionAttributeSpecified(true);
             }
         }

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=399679&r1=399678&r2=399679&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
Thu May  4 04:45:16 2006
@@ -646,13 +646,6 @@
             cookieParse(cookiespec, "www.domain.com", 8000, "/", false, header);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException e) {}
-
-        // version attribute must be 1 with Set-Cookie2 header
-        header = new Header("Set-Cookie2", "name=value; version=0");
-        try {
-            cookieParse(cookiespec, "www.domain.com", 8000, "/", false, header);
-            fail("MalformedCookieException should have been thrown");
-        } catch (MalformedCookieException e) {}
     }
 
     // ------------------------------------------------------- Test Cookie Matching
@@ -763,30 +756,42 @@
      */
     public void testRFC2965CookieFormatting() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Cookie2 cookie = new Cookie2(".domain.com", "name",
+        Cookie2 cookie1 = new Cookie2(".domain.com", "name1",
                                      "value", "/", null, false, new int[] {80,8080});
-        cookie.setVersion(1);
+        cookie1.setVersion(1);
         // domain, path, port specified
-        cookie.setDomainAttributeSpecified(true);
-        cookie.setPathAttributeSpecified(true);
-        cookie.setPortAttributeSpecified(true);
-        assertEquals("$Version=\"1\"; name=\"value\"; $Domain=\".domain.com\"; $Path=\"/\";
$Port=\"80,8080\"",
-                     cookiespec.formatCookie(cookie));
-
+        cookie1.setDomainAttributeSpecified(true);
+        cookie1.setPathAttributeSpecified(true);
+        cookie1.setPortAttributeSpecified(true);
+        assertEquals("$Version=\"1\"; name1=\"value\"; $Domain=\".domain.com\"; $Path=\"/\";
$Port=\"80,8080\"",
+                     cookiespec.formatCookie(cookie1));
+
+        Cookie2 cookie2 = new Cookie2(".domain.com", "name2",
+                "value", "/a/", null, false, new int[] {80,8080});
+        cookie2.setVersion(2);
         // domain, path specified  but port unspecified
-        cookie.setDomainAttributeSpecified(true);
-        cookie.setPathAttributeSpecified(true);
-        cookie.setPortAttributeSpecified(false);
-        assertEquals("$Version=\"1\"; name=\"value\"; $Domain=\".domain.com\"; $Path=\"/\"",
-                     cookiespec.formatCookie(cookie));
-
+        cookie2.setDomainAttributeSpecified(true);
+        cookie2.setPathAttributeSpecified(true);
+        cookie2.setPortAttributeSpecified(false);
+        assertEquals("$Version=\"2\"; name2=\"value\"; $Domain=\".domain.com\"; $Path=\"/a/\"",
+                     cookiespec.formatCookie(cookie2));
+
+        Cookie2 cookie3 = new Cookie2(".domain.com", "name3",
+                "value", "/a/b/", null, false, new int[] {80,8080});
+        cookie3.setVersion(1);
         // path specified, port specified but blank, domain unspecified
-        cookie.setDomainAttributeSpecified(false);
-        cookie.setPathAttributeSpecified(true);
-        cookie.setPortAttributeSpecified(true);
-        cookie.setPortAttributeBlank(true);
-        assertEquals("$Version=\"1\"; name=\"value\"; $Path=\"/\"; $Port=\"\"",
-                     cookiespec.formatCookie(cookie));
+        cookie3.setDomainAttributeSpecified(false);
+        cookie3.setPathAttributeSpecified(true);
+        cookie3.setPortAttributeSpecified(true);
+        cookie3.setPortAttributeBlank(true);
+        assertEquals("$Version=\"1\"; name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"",
+                     cookiespec.formatCookie(cookie3));
+
+        assertEquals("$Version=\"2\"; " +
+                "name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"; " +
+                "name2=\"value\"; $Domain=\".domain.com\"; $Path=\"/a/\"; " +
+                "name1=\"value\"; $Domain=\".domain.com\"; $Path=\"/\"; $Port=\"80,8080\"",
+                cookiespec.formatCookies(new Cookie[] {cookie3, cookie2, cookie1}));
     }
 
     /**



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


Mime
View raw message