X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Futil%2FParams.java;h=aac3639dbdaa7b102828e0a1edb328caad219226;hb=80e2f8488b75b0eddf8df3d22a05f5a0481c2c0e;hp=e2cff465138def3f5b6124f8efbe67d541bfaa48;hpb=efa9722bc37910a6224346bb20210205c96ecc47;p=jsvc.git diff --git a/src/dolda/jsvc/util/Params.java b/src/dolda/jsvc/util/Params.java index e2cff46..aac3639 100644 --- a/src/dolda/jsvc/util/Params.java +++ b/src/dolda/jsvc/util/Params.java @@ -17,25 +17,23 @@ public class Params { } } - 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 +53,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,8 +77,21 @@ 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) { - return(urlparams(url.getQuery())); + String q = url.getQuery(); + if(q == null) + q = ""; + return(urlparams(q)); } public static MultiMap urlparams(Request req) { @@ -101,4 +111,32 @@ public class Params { } return(buf.toString()); } + + public static MultiMap postparams(Request req) { + if(req.method() != "POST") + 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")) { + byte[] data; + try { + return(urlparams(new InputStreamReader(req.input(), "UTF-8"))); + } catch(IOException e) { + return(null); + } + } + 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); + } }