diff --git a/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java b/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java index cfda4bd604..8fa8ec9b94 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/CookieDecoder.java @@ -36,6 +36,19 @@ public class CookieDecoder { private final static String semicolon = ";"; private final static String equals = "="; + private final String charset; + + public CookieDecoder() { + this(QueryStringDecoder.DEFAULT_CHARSET); + } + + public CookieDecoder(String charset) { + if (charset == null) { + throw new NullPointerException("charset"); + } + this.charset = charset; + } + public Map decode(String header) { // FIXME: Support both version 0 and 1 cookies // FIXME: Decode all cookie fields, including domain, path, maxAge, secure, and comment. @@ -47,7 +60,7 @@ public class CookieDecoder { String[] cookie = s.split(equals); if(cookie != null && cookie.length == 2) { String name = cookie[0].trim(); - String value = cookie[1].trim(); + String value = QueryStringDecoder.decodeComponent(cookie[1], charset); cookies.put(name, new DefaultCookie(name, value)); } } diff --git a/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java b/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java index b8d6e242d1..acfa2c3bdf 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/CookieEncoder.java @@ -36,6 +36,19 @@ public class CookieEncoder { private final Map cookies = new TreeMap(CaseIgnoringComparator.INSTANCE); + private final String charset; + + public CookieEncoder() { + this(QueryStringDecoder.DEFAULT_CHARSET); + } + + public CookieEncoder(String charset) { + if (charset == null) { + throw new NullPointerException("charset"); + } + this.charset = charset; + } + public void addCookie(String name, String val) { cookies.put(name, new DefaultCookie(name, val)); } @@ -53,10 +66,11 @@ public class CookieEncoder { if(cookieNames.isEmpty()) { return null; } - for (String cookieName : cookieNames) { + for (String cookieName: cookieNames) { sb.append(cookieName); sb.append((char) HttpCodecUtil.EQUALS); - sb.append(cookies.get(cookieName).getValue()); + sb.append(QueryStringEncoder.encodeComponent( + cookies.get(cookieName).getValue(), charset)); sb.append((char) HttpCodecUtil.SEMICOLON); } return sb.toString(); diff --git a/src/main/java/org/jboss/netty/handler/codec/http/QueryStringDecoder.java b/src/main/java/org/jboss/netty/handler/codec/http/QueryStringDecoder.java index 9953bd29ca..2bd57114d1 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/QueryStringDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/QueryStringDecoder.java @@ -114,21 +114,21 @@ public class QueryStringDecoder { String[] params = s.split("&"); for (String param : params) { String[] split = param.split("="); - String key = decodeComponent(split[0]); + String key = decodeComponent(split[0], charset); List values = this.params.get(key); if(values == null) { values = new ArrayList(); this.params.put(key,values); } if (split.length > 1) { - values.add(decodeComponent(split[1])); + values.add(decodeComponent(split[1], charset)); } else { values.add(""); } } } - private String decodeComponent(String s) { + static String decodeComponent(String s, String charset) { try { return URLDecoder.decode(s, charset); } catch (UnsupportedEncodingException e) { diff --git a/src/main/java/org/jboss/netty/handler/codec/http/QueryStringEncoder.java b/src/main/java/org/jboss/netty/handler/codec/http/QueryStringEncoder.java index 394a83d61a..e715e462c0 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/QueryStringEncoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/QueryStringEncoder.java @@ -81,9 +81,9 @@ public class QueryStringEncoder { StringBuilder sb = new StringBuilder(uri).append("?"); for (int i = 0; i < params.size(); i++) { Param param = params.get(i); - sb.append(encodeComponent(param.name)); + sb.append(encodeComponent(param.name, charset)); sb.append("="); - sb.append(encodeComponent(param.value)); + sb.append(encodeComponent(param.value, charset)); if(i != params.size() - 1) { sb.append("&"); } @@ -92,7 +92,7 @@ public class QueryStringEncoder { } } - private String encodeComponent(String s) { + static String encodeComponent(String s, String charset) { try { return URLEncoder.encode(s, charset).replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) {