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
This commit is contained in:
Oleg Kalnichevski 2008-10-27 16:08:27 +00:00
parent 0cdd9b7fc2
commit 988bfe0b82
3 changed files with 107 additions and 29 deletions

View File

@ -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
*
@ -75,23 +113,6 @@ public class RFC2965Spec extends RFC2109Spec {
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<Cookie> 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<Cookie> parse(
final HeaderElement[] elems,
CookieOrigin origin) throws MalformedCookieException {
origin = adjustEffectiveHost(origin);
return createCookies(elems, origin, COOKIE2_FACTORY);
}
private List<Cookie> createCookies(
final HeaderElement[] elems,
final CookieOrigin origin,
final ClientCookieFactory cookieFactory) throws MalformedCookieException {
List<Cookie> cookies = new ArrayList<Cookie>(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();

View File

@ -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<Cookie> 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<Cookie> 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);

View File

@ -122,7 +122,7 @@ public class TestCookieRFC2965Spec extends TestCase {
/**
* Test parsing cookie <tt>"Domain"</tt> 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 <tt>"Discard"</tt> 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<Cookie> 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 <tt>Domain</tt> validation. Effective host name