changeset 726:14f136a4641f

use enctype for multipart/form-data
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 09 Jun 2016 18:43:32 -0600
parents a741a3a33423
children d6a191618c60
files core/src/luan/modules/url/LuanUrl.java core/src/luan/modules/url/MultipartClient.java
diffstat 2 files changed, 50 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
diff -r a741a3a33423 -r 14f136a4641f core/src/luan/modules/url/LuanUrl.java
--- a/core/src/luan/modules/url/LuanUrl.java	Wed Jun 08 23:13:10 2016 -0600
+++ b/core/src/luan/modules/url/LuanUrl.java	Thu Jun 09 18:43:32 2016 -0600
@@ -85,50 +85,58 @@
 				headers.put("Authorization",val);
 			}
 			Map params = getMap(luan,map,"parameters");
-			if( params != null ) {
-				if( this.method==Method.POST && "multipart/form-data".equals(headers.get("Content-Type")) ) {
-					multipart = new MultipartClient(params);
-				} else {
-					StringBuilder sb = new StringBuilder();
-					for( Object hack : params.entrySet() ) {
-						Map.Entry entry = (Map.Entry)hack;
-						String key = (String)entry.getKey();
-						Object val = entry.getValue();
-						String keyEnc = encode(key);
-						if( val instanceof String ) {
+			String enctype = getString(map,"enctype");
+			if( enctype != null ) {
+				if( !enctype.equals("multipart/form-data") )
+					throw new LuanException( "unrecognized enctype: "+enctype );
+				if( this.method!=Method.POST )
+					throw new LuanException( "multipart/form-data can only be used with POST" );
+				if( params==null )
+					throw new LuanException( "multipart/form-data requires parameters" );
+				if( params.isEmpty() )
+					throw new LuanException( "multipart/form-data parameters can't be empty" );
+				multipart = new MultipartClient(params);
+			}
+			else if( params != null ) {
+				StringBuilder sb = new StringBuilder();
+				for( Object hack : params.entrySet() ) {
+					Map.Entry entry = (Map.Entry)hack;
+					String key = (String)entry.getKey();
+					Object val = entry.getValue();
+					String keyEnc = encode(key);
+					if( val instanceof String ) {
+						and(sb);
+						sb.append( keyEnc ).append( '=' ).append( encode((String)val) );
+					} else {
+						if( !(val instanceof LuanTable) )
+							throw new LuanException( "parameter '"+key+"' must be string or table" );
+						LuanTable t = (LuanTable)val;
+						if( !t.isList() )
+							throw new LuanException( "parameter '"+key+"' table must be list" );
+						for( Object obj : t.asList() ) {
+							if( !(obj instanceof String) )
+								throw new LuanException( "parameter '"+key+"' values must be strings" );
 							and(sb);
-							sb.append( keyEnc ).append( '=' ).append( encode((String)val) );
-						} else {
-							if( !(val instanceof LuanTable) )
-								throw new LuanException( "parameter '"+key+"' must be string or table" );
-							LuanTable t = (LuanTable)val;
-							if( !t.isList() )
-								throw new LuanException( "parameter '"+key+"' table must be list" );
-							for( Object obj : t.asList() ) {
-								if( !(obj instanceof String) )
-									throw new LuanException( "parameter '"+key+"' values must be strings" );
-								and(sb);
-								sb.append( keyEnc ).append( '=' ).append( encode((String)obj) );
-							}
+							sb.append( keyEnc ).append( '=' ).append( encode((String)obj) );
 						}
 					}
-					if( this.method==Method.DELETE )
-						throw new LuanException( "the DELETE method cannot take parameters" );
-					if( this.method==Method.POST ) {
-						content = sb.toString();
-					} else { // GET
-						String urlS = this.url.toString();
-						if( urlS.indexOf('?') == -1 ) {
-							urlS += '?';
-						} else {
-							urlS += '&';
-						}
-						urlS += sb;
-						try {
-							this.url = new URL(urlS);
-						} catch(IOException e) {
-							throw new RuntimeException(e);
-						}
+				}
+				if( this.method==Method.DELETE )
+					throw new LuanException( "the DELETE method cannot take parameters" );
+				if( this.method==Method.POST ) {
+					content = sb.toString();
+				} else { // GET
+					String urlS = this.url.toString();
+					if( urlS.indexOf('?') == -1 ) {
+						urlS += '?';
+					} else {
+						urlS += '&';
+					}
+					urlS += sb;
+					try {
+						this.url = new URL(urlS);
+					} catch(IOException e) {
+						throw new RuntimeException(e);
 					}
 				}
 			}
@@ -229,8 +237,8 @@
 //			httpCon.setRequestProperty("Content-Length",Integer.toString(post.length));
 			out = httpCon.getOutputStream();
 			out.write(post);
-			out.flush();
 		}
+		out.flush();
 		try {
 			try {
 				return httpCon.getInputStream();
diff -r a741a3a33423 -r 14f136a4641f core/src/luan/modules/url/MultipartClient.java
--- a/core/src/luan/modules/url/MultipartClient.java	Wed Jun 08 23:13:10 2016 -0600
+++ b/core/src/luan/modules/url/MultipartClient.java	Thu Jun 09 18:43:32 2016 -0600
@@ -57,9 +57,6 @@
 		        out.write(__DASHDASH);
 		        out.write(boundaryBytes);
 		        out.write(__CRLF);
-//		        if (contentType != null)
-//		            out.write(("Content-Type: "+contentType).getBytes(__ISO_8859_1));
-//		        out.write(__CRLF);
 	            out.write(("Content-Disposition: form-data; name=\""+name+"\"").getBytes(__ISO_8859_1));
 	            out.write(__CRLF);
 		        out.write(__CRLF);