changeset 361:0581238084ad

fix HTTP parameters for multipart
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 15 Apr 2015 16:58:51 -0600
parents cbb94a7c7a9e
children 9dbf3433f70f
files web/src/luan/modules/web/HttpServicer.java
diffstat 1 files changed, 36 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/web/src/luan/modules/web/HttpServicer.java	Wed Apr 15 11:32:30 2015 -0600
+++ b/web/src/luan/modules/web/HttpServicer.java	Wed Apr 15 16:58:51 2015 -0600
@@ -111,7 +111,6 @@
 	private final HttpServletResponse response;
 //	private PrintWriter writer = null;
 //	private ServletOutputStream sos = null;
-	private LuanTable parts = null;
 
 	private HttpServicer(HttpServletRequest request,HttpServletResponse response) {
 		this.request = request;
@@ -210,61 +209,46 @@
 		};
 		tbl.put( "cookies", cookies );
 
-		tbl.put( "parts", new LuanProperty() { public Object get() {
-			if( parts == null ) {
-				String contentType = request.getContentType();
-				if( contentType!=null && contentType.startsWith("multipart/form-data") ) {
-					try {
-						InputStream in = new BufferedInputStream(request.getInputStream());
-						final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null);
-						mpis.setDeleteOnExit(true);
-						parts = new NameTable() {
-				
-							@Override Object get(String name) {
-								try {
-									MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)mpis.getPart(name);
-									if( part==null )
-										return null;
-									LuanTable tbl = Luan.newTable();
-									tbl.put("filename",part.getContentDispositionFilename());
-									tbl.put("content_type",part.getContentType());
-									InputStream in = part.getInputStream();
-									byte[] content = Utils.readAll(in);
-									in.close();
-									tbl.put("content",content);
-									return tbl;
-								} catch(IOException e) {
-									throw new RuntimeException(e);
-								} catch(ServletException e) {
-									throw new RuntimeException(e);
-								}
+		String contentType = request.getContentType();
+		if( contentType!=null && contentType.startsWith("multipart/form-data") ) {
+			try {
+				InputStream in = new BufferedInputStream(request.getInputStream());
+				final MultiPartInputStream mpis = new MultiPartInputStream(in,contentType,null,null);
+				mpis.setDeleteOnExit(true);
+				parameters = Luan.newTable();
+				LuanTable parts = Luan.newTable();
+				for( Part p : mpis.getParts() ) {
+					final MultiPartInputStream.MultiPart part = (MultiPartInputStream.MultiPart)p;
+					String name = part.getName();
+					String filename = part.getContentDispositionFilename();
+					if( filename == null ) {
+						String value = new String(part.getBytes());
+						parameters.put(name,value);
+					} else {
+						LuanTable partTbl = Luan.newPropertyTable();
+						partTbl.put("filename",filename);
+						partTbl.put("content_type",part.getContentType());
+						partTbl.put( "content", new LuanProperty() { public Object get() {
+							try {
+								InputStream in = part.getInputStream();
+								byte[] content = Utils.readAll(in);
+								in.close();
+								return content;
+							} catch(IOException e) {
+								throw new RuntimeException(e);
 							}
-				
-							@Override Iterator<String> names() {
-								try {
-									List<String> names = new ArrayList<String>();
-									for( Part part : mpis.getParts() ) {
-										names.add(part.getName());
-									}
-									return names.iterator();
-								} catch(IOException e) {
-									throw new RuntimeException(e);
-								} catch(ServletException e) {
-									throw new RuntimeException(e);
-								}
-							}
-				
-							@Override protected String type() {
-								return "request.parts-table";
-							}
-						};
-					} catch(IOException e) {
-						throw new RuntimeException(e);
+						} } );
+						parts.put(name,partTbl);
 					}
 				}
+				tbl.put( "parameters", parameters );
+				tbl.put( "parts", parts );
+			} catch(IOException e) {
+				throw new RuntimeException(e);
+			} catch(ServletException e) {
+				throw new RuntimeException(e);
 			}
-			return parts;
-		} } );
+		}
 
 		return tbl;
 	}