HTTPCLIENT-1417: Fixed NPE in BrowserCompatSpec#formatCookies caused by version 1 cookies with null cookie value

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1530218 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-10-08 10:36:30 +00:00
parent df36e1087c
commit 4f03bedbf8
3 changed files with 28 additions and 3 deletions

View File

@ -1,6 +1,10 @@
Changes sicne 4.3.1
Changes since 4.3.1
-------------------
* [HTTPCLIENT-1417] Fixed NPE in BrowserCompatSpec#formatCookies caused by version 1
cookies with null cookie value.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* [HTTPCLIENT-1416] Fixed NPE in CachingHttpClientBuilder#build().
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -168,6 +168,10 @@ public class BrowserCompatSpec extends CookieSpecBase {
return parse(helems, origin);
}
private static boolean isQuoteEnclosed(final String s) {
return s != null && s.startsWith("\"") && s.endsWith("\"");
}
public List<Header> formatCookies(final List<Cookie> cookies) {
Args.notEmpty(cookies, "List of cookies");
final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size());
@ -180,8 +184,7 @@ public class BrowserCompatSpec extends CookieSpecBase {
}
final String cookieName = cookie.getName();
final String cookieValue = cookie.getValue();
if (cookie.getVersion() > 0 &&
!(cookieValue.startsWith("\"") && cookieValue.endsWith("\""))) {
if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) {
BasicHeaderValueFormatter.INSTANCE.formatHeaderElement(
buffer,
new BasicHeaderElement(cookieName, cookieValue),

View File

@ -922,6 +922,24 @@ public class TestBrowserCompatSpec {
Assert.assertEquals("name=", headers.get(0).getValue());
}
@Test
public void testNullCookieValueFormattingCookieVersion1() {
final BasicClientCookie cookie = new BasicClientCookie("name", null);
cookie.setVersion(1);
cookie.setDomain(".whatever.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
cookie.setPath("/");
cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());
final CookieSpec cookiespec = new BrowserCompatSpec();
final List<Cookie> cookies = new ArrayList<Cookie>(1);
cookies.add(cookie);
final List<Header> headers = cookiespec.formatCookies(cookies);
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
Assert.assertEquals("name", headers.get(0).getValue());
}
/**
* Tests generic cookie formatting.
*/