* Optimized QueryStringDecoder

* Moved DEFAULT_CHARSET to HttpCodecUtil
This commit is contained in:
Trustin Lee 2009-03-14 13:48:01 +00:00
parent 701d8a8aaa
commit a074c0a95d
3 changed files with 29 additions and 18 deletions

View File

@ -70,6 +70,8 @@ class HttpCodecUtil {
static final byte DOUBLE_QUOTE = '"'; static final byte DOUBLE_QUOTE = '"';
static final String DEFAULT_CHARSET = "UTF-8";
private HttpCodecUtil() { private HttpCodecUtil() {
super(); super();
} }

View File

@ -29,6 +29,8 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
@ -40,7 +42,7 @@ import java.util.Map;
*/ */
public class QueryStringDecoder { public class QueryStringDecoder {
static final String DEFAULT_CHARSET = "UTF-8"; private static final Pattern PARAM_PATTERN = Pattern.compile("([^=]*)=([^&]*)&*");
private final String charset; private final String charset;
private final String uri; private final String uri;
@ -48,7 +50,7 @@ public class QueryStringDecoder {
private final Map<String, List<String>> params = new HashMap<String, List<String>>(); private final Map<String, List<String>> params = new HashMap<String, List<String>>();
public QueryStringDecoder(String uri) { public QueryStringDecoder(String uri) {
this(uri, DEFAULT_CHARSET); this(uri, HttpCodecUtil.DEFAULT_CHARSET);
} }
public QueryStringDecoder(String uri, String charset) { public QueryStringDecoder(String uri, String charset) {
@ -64,7 +66,7 @@ public class QueryStringDecoder {
} }
public QueryStringDecoder(URI uri) { public QueryStringDecoder(URI uri) {
this(uri, DEFAULT_CHARSET); this(uri, HttpCodecUtil.DEFAULT_CHARSET);
} }
public QueryStringDecoder(URI uri, String charset){ public QueryStringDecoder(URI uri, String charset){
@ -105,30 +107,37 @@ public class QueryStringDecoder {
} }
private void decode() { private void decode() {
String[] split = uri.split("\\?", 2); int pathEndPos = uri.indexOf('?');
path = split[0]; if (pathEndPos < 0) {
decodeParams(split[1]); path = uri;
} else {
path = uri.substring(0, pathEndPos);
decodeParams(uri.substring(pathEndPos + 1));
}
} }
private void decodeParams(String s) { private void decodeParams(String s) {
String[] params = s.split("&"); Matcher m = PARAM_PATTERN.matcher(s);
for (String param : params) { int pos = 0;
String[] split = param.split("="); while (m.find(pos)) {
String key = decodeComponent(split[0], charset); pos = m.end();
List<String> values = this.params.get(key); String key = decodeComponent(m.group(1), charset);
String value = decodeComponent(m.group(2), charset);
List<String> values = params.get(key);
if(values == null) { if(values == null) {
values = new ArrayList<String>(); values = new ArrayList<String>();
this.params.put(key,values); params.put(key,values);
}
if (split.length > 1) {
values.add(decodeComponent(split[1], charset));
} else {
values.add("");
} }
values.add(value);
} }
} }
private static String decodeComponent(String s, String charset) { private static String decodeComponent(String s, String charset) {
if (s == null) {
return "";
}
try { try {
return URLDecoder.decode(s, charset); return URLDecoder.decode(s, charset);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {

View File

@ -44,7 +44,7 @@ public class QueryStringEncoder {
private final List<Param> params = new ArrayList<Param>(); private final List<Param> params = new ArrayList<Param>();
public QueryStringEncoder(String uri) { public QueryStringEncoder(String uri) {
this(uri, QueryStringDecoder.DEFAULT_CHARSET); this(uri, HttpCodecUtil.DEFAULT_CHARSET);
} }
public QueryStringEncoder(String uri, String charset) { public QueryStringEncoder(String uri, String charset) {