diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/cookie/ServerCookieDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/cookie/ServerCookieDecoder.java index ccf8eceaa1..29dc1b9bcd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/cookie/ServerCookieDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/cookie/ServerCookieDecoder.java @@ -17,7 +17,10 @@ package io.netty.handler.codec.http.cookie; import static java.util.Objects.requireNonNull; +import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -56,20 +59,41 @@ public final class ServerCookieDecoder extends CookieDecoder { super(strict); } + /** + * Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}. Unlike {@link #decode(String)}, this + * includes all cookie values present, even if they have the same name. + * + * @return the decoded {@link Cookie} + */ + public List decodeAll(String header) { + List cookies = new ArrayList<>(); + decode(cookies, header); + return Collections.unmodifiableList(cookies); + } + /** * Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}. * * @return the decoded {@link Cookie} */ public Set decode(String header) { + Set cookies = new TreeSet<>(); + decode(cookies, header); + return cookies; + } + + /** + * Decodes the specified Set-Cookie HTTP header value into a {@link Cookie}. + * + * @return the decoded {@link Cookie} + */ + private void decode(Collection cookies, String header) { final int headerLen = requireNonNull(header, "header").length(); if (headerLen == 0) { - return Collections.emptySet(); + return; } - Set cookies = new TreeSet<>(); - int i = 0; boolean rfc2965Style = false; @@ -149,7 +173,5 @@ public final class ServerCookieDecoder extends CookieDecoder { cookies.add(cookie); } } - - return cookies; } } diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/cookie/ServerCookieDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/cookie/ServerCookieDecoderTest.java index b157bc3c73..b6b3365575 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/cookie/ServerCookieDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/cookie/ServerCookieDecoderTest.java @@ -15,6 +15,7 @@ */ package io.netty.handler.codec.http.cookie; +import java.util.List; import org.junit.Test; import java.util.Iterator; @@ -53,6 +54,26 @@ public class ServerCookieDecoderTest { assertEquals("myValue3", cookie.value()); } + @Test + public void testDecodingAllMultipleCookies() { + String c1 = "myCookie=myValue;"; + String c2 = "myCookie=myValue2;"; + String c3 = "myCookie=myValue3;"; + + List cookies = ServerCookieDecoder.STRICT.decodeAll(c1 + c2 + c3); + assertEquals(3, cookies.size()); + Iterator it = cookies.iterator(); + Cookie cookie = it.next(); + assertNotNull(cookie); + assertEquals("myValue", cookie.value()); + cookie = it.next(); + assertNotNull(cookie); + assertEquals("myValue2", cookie.value()); + cookie = it.next(); + assertNotNull(cookie); + assertEquals("myValue3", cookie.value()); + } + @Test public void testDecodingGoogleAnalyticsCookie() { String source =