1 package dolda.jsvc.util;
7 import java.nio.charset.CharacterCodingException;
10 public static class EncodingException extends ClientError {
11 public EncodingException(String msg) {
12 super("Invalid parameter encoding", msg);
16 public static MultiMap<String, String> urlparams(Reader in) throws IOException {
18 MultiMap<String, String> ret = new WrappedMultiMap<String, String>(new TreeMap<String, Collection<String>>());
20 String key = null; /* Java is stupid. */
21 MixedBuffer buf = new MixedBuffer();
29 buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2)));
30 } catch(NumberFormatException e) {
31 throw(new EncodingException("Invalid character escape"));
35 buf = new MixedBuffer();
38 ret.add(buf.convert(), "");
39 buf = new MixedBuffer();
44 ret.add(buf.convert(), "");
45 buf = new MixedBuffer();
50 } else if(st == "val") {
55 buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2)));
56 } catch(NumberFormatException e) {
57 throw(new EncodingException("Invalid character escape"));
59 } else if((c == '&') || (c == -1)) {
60 ret.add(key, buf.convert());
61 buf = new MixedBuffer();
71 } catch(CharacterCodingException e) {
72 throw(new EncodingException("Escaped parameter text is not proper UTF-8"));
76 public static MultiMap<String, String> urlparams(String q) {
78 return(urlparams(new StringReader(q)));
79 } catch(IOException e) {
80 /* This will, of course, never ever once happen, but do
81 * you think Javac cares? */
86 public static MultiMap<String, String> urlparams(URL url) {
87 String q = url.getQuery();
93 public static MultiMap<String, String> urlparams(Request req) {
94 return(urlparams(req.url()));
97 public static String encquery(Map<String, String> pars) {
98 StringBuilder buf = new StringBuilder();
100 for(Map.Entry<String, String> par : pars.entrySet()) {
103 buf.append(Misc.urlq(par.getKey()));
105 buf.append(Misc.urlq(par.getValue()));
108 return(buf.toString());
111 public static MultiMap<String, String> postparams(Request req) {
112 if(req.method() != "POST")
114 String ctype = req.inheaders().get("Content-Type");
117 ctype = ctype.toLowerCase();
118 if(ctype.equals("application/x-www-form-urlencoded")) {
121 return(urlparams(new InputStreamReader(req.input(), "UTF-8")));
122 } catch(IOException e) {
129 public static MultiMap<String, String> stdparams(Request req) {
130 MultiMap<String, String> params = Params.urlparams(req);
131 if(req.method() == "POST") {
132 MultiMap<String, String> pp = Params.postparams(req);