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 2014/08/11 15:30:53 UTC

svn commit: r1617277 - in /httpcomponents/httpclient/branches/4.3.x/httpclient/src: main/java/org/apache/http/impl/cookie/ test/java/org/apache/http/impl/cookie/

Author: olegk
Date: Mon Aug 11 13:30:53 2014
New Revision: 1617277

URL: http://svn.apache.org/r1617277
Log:
CookieNetscapeDraft to ignore Version and Max-Age attributes; BrowserCompatSpec to reset cookie version to 0 for Neetscape style cookies

Modified:
    httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
    httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java
    httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java
    httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java

Modified: httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java?rev=1617277&r1=1617276&r2=1617277&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java (original)
+++ httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java Mon Aug 11 13:30:53 2014
@@ -28,15 +28,19 @@
 package org.apache.http.impl.cookie;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.Locale;
 
 import org.apache.http.FormattedHeader;
 import org.apache.http.Header;
 import org.apache.http.HeaderElement;
+import org.apache.http.NameValuePair;
 import org.apache.http.annotation.NotThreadSafe;
 import org.apache.http.client.utils.DateUtils;
 import org.apache.http.cookie.ClientCookie;
 import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieAttributeHandler;
 import org.apache.http.cookie.CookieOrigin;
 import org.apache.http.cookie.MalformedCookieException;
 import org.apache.http.cookie.SM;
@@ -132,7 +136,7 @@ public class BrowserCompatSpec extends C
             throw new MalformedCookieException("Unrecognized cookie header '"
                     + header.toString() + "'");
         }
-        HeaderElement[] helems = header.getElements();
+        final HeaderElement[] helems = header.getElements();
         boolean versioned = false;
         boolean netscape = false;
         for (final HeaderElement helem: helems) {
@@ -163,9 +167,35 @@ public class BrowserCompatSpec extends C
                 buffer.append(s);
                 cursor = new ParserCursor(0, buffer.length());
             }
-            helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
+            final HeaderElement elem = parser.parseHeader(buffer, cursor);
+            final String name = elem.getName();
+            final String value = elem.getValue();
+            if (name == null || name.isEmpty()) {
+                throw new MalformedCookieException("Cookie name may not be empty");
+            }
+            final BasicClientCookie cookie = new BasicClientCookie(name, value);
+            cookie.setPath(getDefaultPath(origin));
+            cookie.setDomain(getDefaultDomain(origin));
+
+            // cycle through the parameters
+            final NameValuePair[] attribs = elem.getParameters();
+            for (int j = attribs.length - 1; j >= 0; j--) {
+                final NameValuePair attrib = attribs[j];
+                final String s = attrib.getName().toLowerCase(Locale.ROOT);
+                cookie.setAttribute(s, attrib.getValue());
+                final CookieAttributeHandler handler = findAttribHandler(s);
+                if (handler != null) {
+                    handler.parse(cookie, attrib.getValue());
+                }
+            }
+            // Override version for Netscape style cookies
+            if (netscape) {
+                cookie.setVersion(0);
+            }
+            return Collections.<Cookie>singletonList(cookie);
+        } else {
+            return parse(helems, origin);
         }
-        return parse(helems, origin);
     }
 
     private static boolean isQuoteEnclosed(final String s) {

Modified: httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java?rev=1617277&r1=1617276&r2=1617277&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java (original)
+++ httpcomponents/httpclient/branches/4.3.x/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java Mon Aug 11 13:30:53 2014
@@ -69,7 +69,6 @@ public class NetscapeDraftSpec extends C
         }
         registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
         registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
-        registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
         registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
         registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
         registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(

Modified: httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java?rev=1617277&r1=1617276&r2=1617277&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java (original)
+++ httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java Mon Aug 11 13:30:53 2014
@@ -459,7 +459,7 @@ public class TestBrowserCompatSpec {
         Assert.assertTrue("Secure",cookies.get(0).isSecure());
         Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate());
         Assert.assertEquals("Comment","This is a comment.",cookies.get(0).getComment());
-        Assert.assertEquals("Version",1,cookies.get(0).getVersion());
+        Assert.assertEquals("Version",0,cookies.get(0).getVersion());
     }
 
     @Test

Modified: httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java?rev=1617277&r1=1617276&r2=1617277&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java (original)
+++ httpcomponents/httpclient/branches/4.3.x/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java Mon Aug 11 13:30:53 2014
@@ -132,6 +132,25 @@ public class TestCookieNetscapeDraft {
         }
     }
 
+    @Test
+    public void testParseVersionIgnored() throws Exception {
+        final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/path/;Version=1;");
+
+        final CookieSpec cookiespec = new NetscapeDraftSpec();
+        final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
+        final List<Cookie> cookies = cookiespec.parse(header, origin);
+        for (int i = 0; i < cookies.size(); i++) {
+            cookiespec.validate(cookies.get(i), origin);
+        }
+        Assert.assertEquals("Found 1 cookies.",1,cookies.size());
+        final Cookie cookie = cookies.get(0);
+        Assert.assertEquals("Name","name1", cookie.getName());
+        Assert.assertEquals("Value", "value1", cookie.getValue());
+        Assert.assertEquals("Domain", "host", cookie.getDomain());
+        Assert.assertEquals("Path","/path/", cookie.getPath());
+        Assert.assertEquals(0, cookie.getVersion());
+    }
+
     /**
      * Tests Netscape specific cookie formatting.
      */