Added support for decoding application/x-www-form-urlencoded POST parameters.
authorFredrik Tolf <fredrik@dolda2000.com>
Wed, 14 Oct 2009 00:41:29 +0000 (02:41 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Wed, 14 Oct 2009 00:41:29 +0000 (02:41 +0200)
src/dolda/jsvc/j2ee/J2eeRequest.java
src/dolda/jsvc/util/Misc.java
src/dolda/jsvc/util/Params.java

index 9beddf5..a014507 100644 (file)
@@ -14,6 +14,7 @@ public class J2eeRequest extends ResponseBuffer {
     private HttpServletResponse resp;
     private String method, path;
     private URL url;
+    private MultiMap<String, String> params = null;
     private Map<Object, Object> props = new HashMap<Object, Object>();
     
     public J2eeRequest(ServletConfig cfg, HttpServletRequest req, HttpServletResponse resp) {
@@ -139,7 +140,15 @@ public class J2eeRequest extends ResponseBuffer {
     }
     
     public MultiMap<String, String> params() {
-       return(null);
+       if(params == null) {
+           params = Params.urlparams(this);
+           if(method == "POST") {
+               MultiMap<String, String> pp = Params.postparams(this);
+               if(pp != null)
+                   params.putAll(pp);
+           }
+       }
+       return(params);
     }
     
     protected void backflush() {
index a86735a..122a654 100644 (file)
@@ -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) {
index e2cff46..99320a5 100644 (file)
@@ -81,7 +81,10 @@ public class Params {
     }
 
     public static MultiMap<String, String> urlparams(URL url) {
-       return(urlparams(url.getQuery()));
+       String q = url.getQuery();
+       if(q == null)
+           q = "";
+       return(urlparams(q));
     }
 
     public static MultiMap<String, String> urlparams(Request req) {
@@ -101,4 +104,35 @@ public class Params {
        }
        return(buf.toString());
     }
+    
+    public static MultiMap<String, String> 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);
+    }
 }