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

View File

@ -68,7 +68,7 @@ public class CookieEncoder {
* this encoder.
*/
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() {
if (cookies.size() > 1) {
throw new IllegalStateException(
"encode() can encode only one cookie on server mode: " + cookies.size() + " cookies added");
}
StringBuilder sb = new StringBuilder();
for (Cookie cookie: cookies) {

View File

@ -144,10 +144,17 @@ public class HttpSnoopServerHandler extends ChannelInboundMessageHandlerAdapter<
CookieEncoder cookieEncoder = new CookieEncoder(true);
for (Cookie cookie : cookies) {
cookieEncoder.addCookie(cookie);
}
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.
ChannelFuture future = ctx.write(response);