From 988bfe0b82378baea5642148e559517e348832bc Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Mon, 27 Oct 2008 16:08:27 +0000 Subject: [PATCH] Fixed broken #parse(HeaderElement[], CookieOrigin) method in the RFC2965Spec cookie spec git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@708225 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/http/impl/cookie/RFC2965Spec.java | 86 +++++++++++++------ .../impl/cookie/TestCookieBestMatchSpec.java | 31 +++++++ .../impl/cookie/TestCookieRFC2965Spec.java | 19 +++- 3 files changed, 107 insertions(+), 29 deletions(-) diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java b/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java index 19f587c52..daa7526ef 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/RFC2965Spec.java @@ -58,6 +58,44 @@ import org.apache.http.util.CharArrayBuffer; */ 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 @@ public class RFC2965Spec extends RFC2109Spec { 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 parse( final Header header, @@ -104,9 +125,28 @@ public class RFC2965Spec extends RFC2109Spec { } 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 parse( + final HeaderElement[] elems, + CookieOrigin origin) throws MalformedCookieException { + origin = adjustEffectiveHost(origin); + return createCookies(elems, origin, COOKIE2_FACTORY); + } + private List createCookies( + final HeaderElement[] elems, + final CookieOrigin origin, + final ClientCookieFactory cookieFactory) throws MalformedCookieException { List cookies = new ArrayList(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); @@ -115,13 +155,7 @@ public class RFC2965Spec extends RFC2109Spec { 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(); diff --git a/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java b/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java index 93722f9a0..41d92e952 100644 --- a/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java +++ b/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieBestMatchSpec.java @@ -43,6 +43,7 @@ import org.apache.http.cookie.Cookie; 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 class TestCookieBestMatchSpec extends TestCase { } } + 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 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 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); diff --git a/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java b/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java index 33e2ff640..acad0313d 100644 --- a/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java +++ b/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java @@ -122,7 +122,7 @@ public class TestCookieRFC2965Spec extends TestCase { /** * Test parsing cookie "Domain" 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 @@ public class TestCookieRFC2965Spec extends TestCase { 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 @@ public class TestCookieRFC2965Spec extends TestCase { /** * test parsing "Discard" 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 @@ public class TestCookieRFC2965Spec extends TestCase { } 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 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 Domain validation. Effective host name