Fix #405: CookieEncoder should refuse to encode more than one cookie

.. if on server mode
This commit is contained in:
Trustin Lee 2012-06-24 12:59:44 +09:00
parent 4c072694be
commit a6e3d7d0ca
3 changed files with 19 additions and 8 deletions

View File

@ -41,12 +41,11 @@ import java.util.regex.Pattern;
*/ */
public class CookieDecoder { public class CookieDecoder {
private static final Pattern PATTERN = private static final Pattern PATTERN = Pattern.compile(
Pattern.compile( // See: https://github.com/netty/netty/pull/96
// See: https://github.com/netty/netty/pull/96 //"(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;,]*)))?(\\s*(?:[;,]+\\s*|$))"
//"(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;,]*)))?(\\s*(?:[;,]+\\s*|$))" "(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;]*)))?(\\s*(?:[;,]+\\s*|$))"
"(?:\\s|[;,])*\\$*([^;=]+)(?:=(?:[\"']((?:\\\\.|[^\"])*)[\"']|([^;]*)))?(\\s*(?:[;,]+\\s*|$))" );
);
private static final String COMMA = ","; private static final String COMMA = ",";

View File

@ -68,7 +68,7 @@ public class CookieEncoder {
* this encoder. * this encoder.
*/ */
public void addCookie(String name, String value) { public void addCookie(String name, String value) {
cookies.add(new DefaultCookie(name, value)); addCookie(new DefaultCookie(name, value));
} }
/** /**
@ -98,6 +98,11 @@ public class CookieEncoder {
} }
private String encodeServerSide() { private String encodeServerSide() {
if (cookies.size() > 1) {
throw new IllegalStateException(
"encode() can encode only one cookie on server mode: " + cookies.size() + " cookies added");
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Cookie cookie: cookies) { for (Cookie cookie: cookies) {

View File

@ -144,9 +144,16 @@ public class HttpSnoopServerHandler extends ChannelInboundMessageHandlerAdapter<
CookieEncoder cookieEncoder = new CookieEncoder(true); CookieEncoder cookieEncoder = new CookieEncoder(true);
for (Cookie cookie : cookies) { for (Cookie cookie : cookies) {
cookieEncoder.addCookie(cookie); cookieEncoder.addCookie(cookie);
response.addHeader(SET_COOKIE, cookieEncoder.encode());
} }
response.addHeader(SET_COOKIE, cookieEncoder.encode());
} }
} else {
// Browser sent no cookie. Add some.
CookieEncoder cookieEncoder = new CookieEncoder(true);
cookieEncoder.addCookie("key1", "value1");
response.addHeader(SET_COOKIE, cookieEncoder.encode());
cookieEncoder.addCookie("key2", "value2");
response.addHeader(SET_COOKIE, cookieEncoder.encode());
} }
// Write the response. // Write the response.