BestMatchSpec to use Netscape policy instead of browser compatibility for netscape style cookies; browser compatibility policy is to be deprecated

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1619780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-08-22 12:35:14 +00:00
parent aa4204b412
commit 5c006f8d30
3 changed files with 34 additions and 13 deletions

View File

@ -58,7 +58,7 @@ public class BestMatchSpec implements CookieSpec {
// Cached values of CookieSpec instances
private RFC2965Spec strict; // @NotThreadSafe
private RFC2109Spec obsoleteStrict; // @NotThreadSafe
private BrowserCompatSpec compat; // @NotThreadSafe
private NetscapeDraftSpec netscape; // @NotThreadSafe
public BestMatchSpec(final String[] datepatterns, final boolean oneHeader) {
super();
@ -84,11 +84,11 @@ public class BestMatchSpec implements CookieSpec {
return obsoleteStrict;
}
private BrowserCompatSpec getCompat() {
if (this.compat == null) {
this.compat = new BrowserCompatSpec(this.datepatterns);
private NetscapeDraftSpec getNetscapeCompat() {
if (this.netscape == null) {
this.netscape = new NetscapeDraftSpec(false, this.datepatterns);
}
return compat;
return netscape;
}
@Override
@ -129,7 +129,7 @@ public class BestMatchSpec implements CookieSpec {
cursor = new ParserCursor(0, buffer.length());
}
helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) };
return getCompat().parse(helems, origin);
return getNetscapeCompat().parse(helems, origin);
} else {
if (SM.SET_COOKIE2.equals(header.getName())) {
return getStrict().parse(helems, origin);
@ -152,7 +152,7 @@ public class BestMatchSpec implements CookieSpec {
getObsoleteStrict().validate(cookie, origin);
}
} else {
getCompat().validate(cookie, origin);
getNetscapeCompat().validate(cookie, origin);
}
}
@ -167,7 +167,7 @@ public class BestMatchSpec implements CookieSpec {
return getObsoleteStrict().match(cookie, origin);
}
} else {
return getCompat().match(cookie, origin);
return getNetscapeCompat().match(cookie, origin);
}
}
@ -191,7 +191,7 @@ public class BestMatchSpec implements CookieSpec {
return getObsoleteStrict().formatCookies(cookies);
}
} else {
return getCompat().formatCookies(cookies);
return getNetscapeCompat().formatCookies(cookies);
}
}

View File

@ -59,8 +59,7 @@ public class NetscapeDraftSpec extends CookieSpecBase {
private final String[] datepatterns;
/** Default constructor */
public NetscapeDraftSpec(final String[] datepatterns) {
NetscapeDraftSpec(final boolean strictDomainValidation, final String[] datepatterns) {
super();
if (datepatterns != null) {
this.datepatterns = datepatterns.clone();
@ -68,14 +67,18 @@ public class NetscapeDraftSpec extends CookieSpecBase {
this.datepatterns = new String[] { EXPIRES_PATTERN };
}
registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
registerAttribHandler(ClientCookie.DOMAIN_ATTR,
strictDomainValidation ? new NetscapeDomainHandler() : new BasicDomainHandler());
registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(
this.datepatterns));
}
/** Default constructor */
public NetscapeDraftSpec(final String[] datepatterns) {
this(true, datepatterns);
}
public NetscapeDraftSpec() {
this(null);
}

View File

@ -277,5 +277,23 @@ public class TestCookieBestMatchSpec {
}
}
@Test
public void testVersion1CookieWithInvalidExpires() throws Exception {
final CookieSpec cookiespec = new BestMatchSpec();
final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
final Header origHeader = new BasicHeader("Set-Cookie",
"test=\"test\"; Version=1; Expires=Mon, 11-Feb-2013 10:39:19 GMT; Path=/");
final List<Cookie> cookies = cookiespec.parse(origHeader, origin);
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
final List<Header> headers = cookiespec.formatCookies(cookies);
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
final Header header1 = headers.get(0);
Assert.assertEquals("test=\"test\"", header1.getValue());
}
}