X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FParams.java;h=7a4f27a1646045676f13d2fbddd99037554e6619;hb=141e5e3c8719f1fc12e30a1f06d902580f9a34a2;hp=99320a50117fafc2bf8b9639f43edc67e802a944;hpb=ca045757135b00baa1f9161d2fb1e2e9e4f2bb5e;p=jsvc.git diff --git a/src/dolda/jsvc/util/Params.java b/src/dolda/jsvc/util/Params.java index 99320a5..7a4f27a 100644 --- a/src/dolda/jsvc/util/Params.java +++ b/src/dolda/jsvc/util/Params.java @@ -7,35 +7,29 @@ import java.net.*; import java.nio.charset.CharacterCodingException; public class Params { - public static class EncodingException extends RequestRestart { + public static class EncodingException extends ClientError { public EncodingException(String msg) { - super(msg); - } - - public void respond(Request req) { - throw(Restarts.stdresponse(400, "Invalid parameter encoding", getMessage())); + super("Invalid parameter encoding", msg); } } - public static MultiMap urlparams(String q) { + public static MultiMap urlparams(Reader in) throws IOException { try { MultiMap ret = new WrappedMultiMap(new TreeMap>()); String st = "key"; String key = null; /* Java is stupid. */ MixedBuffer buf = new MixedBuffer(); - int i = 0; while(true) { - int c = (i >= q.length())?-1:(q.charAt(i++)); + int c = in.read(); if(st == "key") { if(c == '%') { - if(q.length() - i < 2) - throw(new EncodingException("Invalid character escape")); try { - buf.append((byte)((Misc.hex2int(q.charAt(i)) << 4) | Misc.hex2int(q.charAt(i + 1)))); + int d1 = in.read(); + int d2 = in.read(); + buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2))); } catch(NumberFormatException e) { throw(new EncodingException("Invalid character escape")); } - i += 2; } else if(c == '=') { key = buf.convert(); buf = new MixedBuffer(); @@ -55,14 +49,13 @@ public class Params { } } else if(st == "val") { if(c == '%') { - if(q.length() - i < 2) - throw(new EncodingException("Invalid character escape")); try { - buf.append((byte)((Misc.hex2int(q.charAt(i)) << 4) | Misc.hex2int(q.charAt(i + 1)))); + int d1 = in.read(); + int d2 = in.read(); + buf.append((byte)((Misc.hex2int((char)d1) << 4) | Misc.hex2int((char)d2))); } catch(NumberFormatException e) { throw(new EncodingException("Invalid character escape")); } - i += 2; } else if((c == '&') || (c == -1)) { ret.add(key, buf.convert()); buf = new MixedBuffer(); @@ -80,6 +73,16 @@ public class Params { } } + public static MultiMap urlparams(String q) { + try { + return(urlparams(new StringReader(q))); + } catch(IOException e) { + /* This will, of course, never ever once happen, but do + * you think Javac cares? */ + throw(new Error(e)); + } + } + public static MultiMap urlparams(URL url) { String q = url.getQuery(); if(q == null) @@ -108,31 +111,28 @@ public class Params { public static MultiMap postparams(Request req) { if(req.method() != "POST") return(null); - String clens = req.inheaders().get("Content-Length"); - if(clens == null) - return(null); - int clen; - try { - clen = Integer.parseInt(clens); - } catch(NumberFormatException e) { - return(null); - } String ctype = req.inheaders().get("Content-Type"); if(ctype == null) return(null); ctype = ctype.toLowerCase(); if(ctype.equals("application/x-www-form-urlencoded")) { - if(clen > (1 << 20)) /* Just to be safe */ - return(null); byte[] data; try { - data = Misc.readall(req.input()); + return(urlparams(new InputStreamReader(req.input(), "UTF-8"))); } catch(IOException e) { return(null); } - String dec = new String(data, Misc.utf8); - return(urlparams(dec)); } return(null); } + + public static MultiMap stdparams(Request req) { + MultiMap params = Params.urlparams(req); + if(req.method() == "POST") { + MultiMap pp = Params.postparams(req); + if(pp != null) + params.putAll(pp); + } + return(params); + } }