Skip to content

Commit

Permalink
fixing unbounded field issue
Browse files Browse the repository at this point in the history
  • Loading branch information
udarakr committed Oct 14, 2015
1 parent a331d88 commit f3a4fa2
Showing 1 changed file with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
Expand All @@ -42,7 +43,9 @@ public class RequestHostObject extends ScriptableObject {

private boolean isMultipart = false;

private Map<String, FileItem> parameterMap = new HashMap<String, FileItem>();
private Map<String, ArrayList<FileItem>> parameterMap = new HashMap<String, ArrayList<FileItem>>();

//Map<String, List<String>> m = new HashMap<String, List<String>>();

private Scriptable parameters = null;

Expand Down Expand Up @@ -270,9 +273,9 @@ public static Scriptable jsFunction_getAllParameters(Context cx, Scriptable this
}
encoding = (String) args[0];
}
parseMultipart(rho);
parseMultipart(rho, thisObj);
// if no encoding is specified, UTF-8 is assumed.
parseMultipartParams(rho, encoding == null ? "UTF-8" : encoding);
parseMultipartParams(rho, encoding == null ? "UTF-8" : encoding, cx, thisObj);
return rho.parameters;
}

Expand All @@ -288,7 +291,7 @@ public static Scriptable jsFunction_getAllFiles(Context cx, Scriptable thisObj,
if (!rho.isMultipart) {
return null;
}
parseMultipart(rho);
parseMultipart(rho, thisObj);
return rho.files;
}

Expand Down Expand Up @@ -435,8 +438,8 @@ public static Object jsFunction_getParameter(Context cx, Scriptable thisObj, Obj
if (!rho.isMultipart) {
return getParameter(parameter, rho.request, rho);
}
parseMultipart(rho);
FileItem item = rho.parameterMap.get(parameter);
parseMultipart(rho, thisObj);
FileItem item = rho.parameterMap.get(parameter).get(0);
if (item == null) {
return null;
}
Expand Down Expand Up @@ -465,7 +468,7 @@ public static Scriptable jsFunction_getFile(Context cx, Scriptable thisObj, Obje
if (!rho.isMultipart) {
return null;
}
parseMultipart(rho);
parseMultipart(rho, thisObj);
Object obj = rho.files.get((String) args[0], thisObj);
if (obj instanceof Scriptable) {
return (Scriptable) obj;
Expand All @@ -477,7 +480,7 @@ public HttpServletRequest getHttpServletRequest() {
return this.request;
}

private static void parseMultipart(RequestHostObject rho) throws ScriptException {
private static void parseMultipart(RequestHostObject rho, Scriptable scope) throws ScriptException {
if (rho.files != null) {
return;
}
Expand All @@ -497,21 +500,43 @@ private static void parseMultipart(RequestHostObject rho) throws ScriptException
FileItem item = (FileItem) obj;
name = item.getFieldName();
if (item.isFormField()) {
rho.parameterMap.put(name, item);
//ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
if(x == null){
ArrayList<FileItem> array = new ArrayList<FileItem>(1);
array.add(item);
//x.add((FileItem) array);
rho.parameterMap.put(name, array);
}else{
x.add(item);
}
// rho.parameterMap.put(name, item);
} else {
rho.files.put(item.getFieldName(), rho.files, rho.context.newObject(rho, "File", new Object[]{item}));
}
}
}

private static void parseMultipartParams(RequestHostObject rho, String encoding) throws ScriptException {
private static void parseMultipartParams(RequestHostObject rho, String encoding,Context cx, Scriptable scope) throws ScriptException {
if (rho.parameters != null) {
return;
}
rho.parameters = rho.context.newObject(rho);
for (String name : rho.parameterMap.keySet()) {
try {
rho.parameters.put(name, rho.parameters, rho.parameterMap.get(name).getString(encoding));
ArrayList<FileItem> x = (ArrayList<FileItem>) rho.parameterMap.get(name);
if(x.size()>1){
Scriptable y = cx.newArray(scope, x.size());

for (int i = 0; i < x.size(); i++) {
y.put(i, y, x.get(i).getString(encoding));
}
rho.parameters.put(name, rho.parameters,y);

}else{
rho.parameters.put(name, rho.parameters,x.get(0).getString(encoding));
}

} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
Expand Down

0 comments on commit f3a4fa2

Please sign in to comment.