* Fixed a problem where CookieEncoder trims the value (it should not)

* Made sure the cookie values follow the URI encoding
This commit is contained in:
Trustin Lee 2009-02-26 08:44:58 +00:00
parent 9550c0759d
commit 5e64bb66e8
4 changed files with 36 additions and 9 deletions

View File

@ -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<String, Cookie> 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));
}
}

View File

@ -36,6 +36,19 @@ public class CookieEncoder {
private final Map<String, Cookie> cookies = new TreeMap<String, Cookie>(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();

View File

@ -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<String> values = this.params.get(key);
if(values == null) {
values = new ArrayList<String>();
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) {

View File

@ -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) {