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) {
}
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() {
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) {
}
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) {
}
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);
+ }
}