From ca045757135b00baa1f9161d2fb1e2e9e4f2bb5e Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 14 Oct 2009 02:41:29 +0200 Subject: [PATCH] Added support for decoding application/x-www-form-urlencoded POST parameters. --- src/dolda/jsvc/j2ee/J2eeRequest.java | 11 ++++++++++- src/dolda/jsvc/util/Misc.java | 19 +++++++++++++++++++ src/dolda/jsvc/util/Params.java | 36 +++++++++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/dolda/jsvc/j2ee/J2eeRequest.java b/src/dolda/jsvc/j2ee/J2eeRequest.java index 9beddf5..a014507 100644 --- a/src/dolda/jsvc/j2ee/J2eeRequest.java +++ b/src/dolda/jsvc/j2ee/J2eeRequest.java @@ -14,6 +14,7 @@ public class J2eeRequest extends ResponseBuffer { private HttpServletResponse resp; private String method, path; private URL url; + private MultiMap params = null; private Map props = new HashMap(); public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) { @@ -139,7 +140,15 @@ public class J2eeRequest extends ResponseBuffer { } public MultiMap params() { - return(null); + if(params == null) { + params = Params.urlparams(this); + if(method == "POST") { + MultiMap pp = Params.postparams(this); + if(pp != null) + params.putAll(pp); + } + } + return(params); } protected void backflush() { diff --git a/src/dolda/jsvc/util/Misc.java b/src/dolda/jsvc/util/Misc.java index a86735a..122a654 100644 --- a/src/dolda/jsvc/util/Misc.java +++ b/src/dolda/jsvc/util/Misc.java @@ -37,6 +37,25 @@ public class Misc { return(p); } + static byte[] readall(InputStream in) throws IOException { + byte[] buf = new byte[4096]; + int off = 0; + while(true) { + if(off == buf.length) { + byte[] n = new byte[buf.length * 2]; + System.arraycopy(buf, 0, n, 0, buf.length); + buf = n; + } + int ret = in.read(buf, off, buf.length - off); + if(ret < 0) { + byte[] n = new byte[off]; + System.arraycopy(buf, 0, n, 0, off); + return(n); + } + off += ret; + } + } + public static void cpstream(InputStream in, OutputStream out) throws IOException { byte[] buf = new byte[4096]; while(true) { diff --git a/src/dolda/jsvc/util/Params.java b/src/dolda/jsvc/util/Params.java index e2cff46..99320a5 100644 --- a/src/dolda/jsvc/util/Params.java +++ b/src/dolda/jsvc/util/Params.java @@ -81,7 +81,10 @@ public class Params { } 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 +104,35 @@ public class Params { } return(buf.toString()); } + + 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()); + } catch(IOException e) { + return(null); + } + String dec = new String(data, Misc.utf8); + return(urlparams(dec)); + } + return(null); + } } -- 2.11.0