1 package dolda.jsvc.util;
7 import java.nio.charset.CharacterCodingException;
10 public static class EncodingException extends RequestRestart {
11 public EncodingException(String msg) {
15 public void respond(Request req) {
16 throw(Restarts.stdresponse(400, "Invalid parameter encoding", getMessage()));
20 public static MultiMap<String, String> urlparams(Reader in) throws IOException {
22 MultiMap<String, String> ret = new WrappedMultiMap<String, String>(new TreeMap<String, Collection<String>>());
24 String key = null; /* Java is stupid. */
25 MixedBuffer buf = new MixedBuffer();
33 buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2)));
34 } catch(NumberFormatException e) {
35 throw(new EncodingException("Invalid character escape"));
39 buf = new MixedBuffer();
42 ret.add(buf.convert(), "");
43 buf = new MixedBuffer();
48 ret.add(buf.convert(), "");
49 buf = new MixedBuffer();
54 } else if(st == "val") {
59 buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2)));
60 } catch(NumberFormatException e) {
61 throw(new EncodingException("Invalid character escape"));
63 } else if((c == '&') || (c == -1)) {
64 ret.add(key, buf.convert());
65 buf = new MixedBuffer();
75 } catch(CharacterCodingException e) {
76 throw(new EncodingException("Escaped parameter text is not proper UTF-8"));
80 public static MultiMap<String, String> urlparams(String q) {
82 return(urlparams(new StringReader(q)));
83 } catch(IOException e) {
84 /* This will, of course, never ever once happen, but do
85 * you think Javac cares? */
90 public static MultiMap<String, String> urlparams(URL url) {
91 String q = url.getQuery();
97 public static MultiMap<String, String> urlparams(Request req) {
98 return(urlparams(req.url()));
101 public static String encquery(Map<String, String> pars) {
102 StringBuilder buf = new StringBuilder();
104 for(Map.Entry<String, String> par : pars.entrySet()) {
107 buf.append(Misc.urlq(par.getKey()));
109 buf.append(Misc.urlq(par.getValue()));
112 return(buf.toString());
115 public static MultiMap<String, String> postparams(Request req) {
116 if(req.method() != "POST")
118 String ctype = req.inheaders().get("Content-Type");
121 ctype = ctype.toLowerCase();
122 if(ctype.equals("application/x-www-form-urlencoded")) {
125 return(urlparams(new InputStreamReader(req.input(), "UTF-8")));
126 } catch(IOException e) {