diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 5b6bff59a..ad9b5ee8f 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -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 + * [HTTPCLIENT-1416] Fixed NPE in CachingHttpClientBuilder#build(). Contributed by Oleg Kalnichevski diff --git a/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java b/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java index dd06dbb36..ce5e2830d 100644 --- a/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java +++ b/httpclient/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java @@ -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
formatCookies(final List 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), diff --git a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java index d46e74ff1..3cb0c92af 100644 --- a/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java +++ b/httpclient/src/test/java/org/apache/http/impl/cookie/TestBrowserCompatSpec.java @@ -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 cookies = new ArrayList(1); + cookies.add(cookie); + final List
headers = cookiespec.formatCookies(cookies); + Assert.assertNotNull(headers); + Assert.assertEquals(1, headers.size()); + Assert.assertEquals("name", headers.get(0).getValue()); + } + /** * Tests generic cookie formatting. */