CookieNetscapeDraft to ignore Version and Max-Age attributes; BrowserCompatSpec to reset cookie version to 0 for Neetscape style cookies

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1617278 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2014-08-11 13:32:46 +00:00
parent f955504b33
commit 3df564f0c6
4 changed files with 53 additions and 5 deletions

View File

@ -28,15 +28,19 @@
package org.apache.http.impl.cookie; package org.apache.http.impl.cookie;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import org.apache.http.FormattedHeader; import org.apache.http.FormattedHeader;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HeaderElement; import org.apache.http.HeaderElement;
import org.apache.http.NameValuePair;
import org.apache.http.annotation.NotThreadSafe; import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.utils.DateUtils; import org.apache.http.client.utils.DateUtils;
import org.apache.http.cookie.ClientCookie; import org.apache.http.cookie.ClientCookie;
import org.apache.http.cookie.Cookie; import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieAttributeHandler;
import org.apache.http.cookie.CookieOrigin; import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException; import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SM; import org.apache.http.cookie.SM;
@ -133,7 +137,7 @@ public class BrowserCompatSpec extends CookieSpecBase {
throw new MalformedCookieException("Unrecognized cookie header '" throw new MalformedCookieException("Unrecognized cookie header '"
+ header.toString() + "'"); + header.toString() + "'");
} }
HeaderElement[] helems = header.getElements(); final HeaderElement[] helems = header.getElements();
boolean versioned = false; boolean versioned = false;
boolean netscape = false; boolean netscape = false;
for (final HeaderElement helem: helems) { for (final HeaderElement helem: helems) {
@ -164,9 +168,35 @@ public class BrowserCompatSpec extends CookieSpecBase {
buffer.append(s); buffer.append(s);
cursor = new ParserCursor(0, buffer.length()); 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) { private static boolean isQuoteEnclosed(final String s) {

View File

@ -69,7 +69,6 @@ public class NetscapeDraftSpec extends CookieSpecBase {
} }
registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler()); registerAttribHandler(ClientCookie.PATH_ATTR, new BasicPathHandler());
registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler()); registerAttribHandler(ClientCookie.DOMAIN_ATTR, new NetscapeDomainHandler());
registerAttribHandler(ClientCookie.MAX_AGE_ATTR, new BasicMaxAgeHandler());
registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler()); registerAttribHandler(ClientCookie.SECURE_ATTR, new BasicSecureHandler());
registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler()); registerAttribHandler(ClientCookie.COMMENT_ATTR, new BasicCommentHandler());
registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler( registerAttribHandler(ClientCookie.EXPIRES_ATTR, new BasicExpiresHandler(

View File

@ -460,7 +460,7 @@ public class TestBrowserCompatSpec {
Assert.assertTrue("Secure",cookies.get(0).isSecure()); Assert.assertTrue("Secure",cookies.get(0).isSecure());
Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate()); Assert.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate());
Assert.assertEquals("Comment","This is a comment.",cookies.get(0).getComment()); 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 @Test

View File

@ -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. * Tests Netscape specific cookie formatting.
*/ */