Mercurial Hosting > luan
comparison core/src/luan/modules/url/LuanUrl.java @ 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 | ffbbe25dab09 |
comparison
equal
deleted
inserted
replaced
725:a741a3a33423 | 726:14f136a4641f |
---|---|
83 if( headers == null ) | 83 if( headers == null ) |
84 headers = new HashMap(); | 84 headers = new HashMap(); |
85 headers.put("Authorization",val); | 85 headers.put("Authorization",val); |
86 } | 86 } |
87 Map params = getMap(luan,map,"parameters"); | 87 Map params = getMap(luan,map,"parameters"); |
88 if( params != null ) { | 88 String enctype = getString(map,"enctype"); |
89 if( this.method==Method.POST && "multipart/form-data".equals(headers.get("Content-Type")) ) { | 89 if( enctype != null ) { |
90 multipart = new MultipartClient(params); | 90 if( !enctype.equals("multipart/form-data") ) |
91 } else { | 91 throw new LuanException( "unrecognized enctype: "+enctype ); |
92 StringBuilder sb = new StringBuilder(); | 92 if( this.method!=Method.POST ) |
93 for( Object hack : params.entrySet() ) { | 93 throw new LuanException( "multipart/form-data can only be used with POST" ); |
94 Map.Entry entry = (Map.Entry)hack; | 94 if( params==null ) |
95 String key = (String)entry.getKey(); | 95 throw new LuanException( "multipart/form-data requires parameters" ); |
96 Object val = entry.getValue(); | 96 if( params.isEmpty() ) |
97 String keyEnc = encode(key); | 97 throw new LuanException( "multipart/form-data parameters can't be empty" ); |
98 if( val instanceof String ) { | 98 multipart = new MultipartClient(params); |
99 } | |
100 else if( params != null ) { | |
101 StringBuilder sb = new StringBuilder(); | |
102 for( Object hack : params.entrySet() ) { | |
103 Map.Entry entry = (Map.Entry)hack; | |
104 String key = (String)entry.getKey(); | |
105 Object val = entry.getValue(); | |
106 String keyEnc = encode(key); | |
107 if( val instanceof String ) { | |
108 and(sb); | |
109 sb.append( keyEnc ).append( '=' ).append( encode((String)val) ); | |
110 } else { | |
111 if( !(val instanceof LuanTable) ) | |
112 throw new LuanException( "parameter '"+key+"' must be string or table" ); | |
113 LuanTable t = (LuanTable)val; | |
114 if( !t.isList() ) | |
115 throw new LuanException( "parameter '"+key+"' table must be list" ); | |
116 for( Object obj : t.asList() ) { | |
117 if( !(obj instanceof String) ) | |
118 throw new LuanException( "parameter '"+key+"' values must be strings" ); | |
99 and(sb); | 119 and(sb); |
100 sb.append( keyEnc ).append( '=' ).append( encode((String)val) ); | 120 sb.append( keyEnc ).append( '=' ).append( encode((String)obj) ); |
101 } else { | |
102 if( !(val instanceof LuanTable) ) | |
103 throw new LuanException( "parameter '"+key+"' must be string or table" ); | |
104 LuanTable t = (LuanTable)val; | |
105 if( !t.isList() ) | |
106 throw new LuanException( "parameter '"+key+"' table must be list" ); | |
107 for( Object obj : t.asList() ) { | |
108 if( !(obj instanceof String) ) | |
109 throw new LuanException( "parameter '"+key+"' values must be strings" ); | |
110 and(sb); | |
111 sb.append( keyEnc ).append( '=' ).append( encode((String)obj) ); | |
112 } | |
113 } | 121 } |
114 } | 122 } |
115 if( this.method==Method.DELETE ) | 123 } |
116 throw new LuanException( "the DELETE method cannot take parameters" ); | 124 if( this.method==Method.DELETE ) |
117 if( this.method==Method.POST ) { | 125 throw new LuanException( "the DELETE method cannot take parameters" ); |
118 content = sb.toString(); | 126 if( this.method==Method.POST ) { |
119 } else { // GET | 127 content = sb.toString(); |
120 String urlS = this.url.toString(); | 128 } else { // GET |
121 if( urlS.indexOf('?') == -1 ) { | 129 String urlS = this.url.toString(); |
122 urlS += '?'; | 130 if( urlS.indexOf('?') == -1 ) { |
123 } else { | 131 urlS += '?'; |
124 urlS += '&'; | 132 } else { |
125 } | 133 urlS += '&'; |
126 urlS += sb; | 134 } |
127 try { | 135 urlS += sb; |
128 this.url = new URL(urlS); | 136 try { |
129 } catch(IOException e) { | 137 this.url = new URL(urlS); |
130 throw new RuntimeException(e); | 138 } catch(IOException e) { |
131 } | 139 throw new RuntimeException(e); |
132 } | 140 } |
133 } | 141 } |
134 } | 142 } |
135 if( !map.isEmpty() ) | 143 if( !map.isEmpty() ) |
136 throw new LuanException( "unrecognized options: "+map ); | 144 throw new LuanException( "unrecognized options: "+map ); |
227 } else { | 235 } else { |
228 byte[] post = content.getBytes(); | 236 byte[] post = content.getBytes(); |
229 // httpCon.setRequestProperty("Content-Length",Integer.toString(post.length)); | 237 // httpCon.setRequestProperty("Content-Length",Integer.toString(post.length)); |
230 out = httpCon.getOutputStream(); | 238 out = httpCon.getOutputStream(); |
231 out.write(post); | 239 out.write(post); |
232 out.flush(); | 240 } |
233 } | 241 out.flush(); |
234 try { | 242 try { |
235 try { | 243 try { |
236 return httpCon.getInputStream(); | 244 return httpCon.getInputStream(); |
237 } catch(IOException e) { | 245 } catch(IOException e) { |
238 InputStream is = httpCon.getErrorStream(); | 246 InputStream is = httpCon.getErrorStream(); |