diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java b/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java index 5ed27759a..018d5b982 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java @@ -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; @@ -133,7 +137,7 @@ public class BrowserCompatSpec extends CookieSpecBase { 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) { @@ -164,9 +168,35 @@ public class BrowserCompatSpec extends CookieSpecBase { 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.singletonList(cookie); + } else { + return parse(helems, origin); } - return parse(helems, origin); } private static boolean isQuoteEnclosed(final String s) { diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java b/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java index 337fa02d7..4f993b081 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/NetscapeDraftSpec.java @@ -69,7 +69,6 @@ public class NetscapeDraftSpec extends CookieSpecBase { } 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( diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java index 544a0050a..00ba174b0 100644 --- a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java @@ -460,7 +460,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 diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java index cd7c0f3cd..ec03d4718 100644 --- a/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestCookieNetscapeDraft.java @@ -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 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. */