comparison src/luan/webserver/RequestParser.java @ 1259:e8020216dee7

add Luan.to_luan and fix multipart/form-data
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 23 Sep 2018 17:58:40 -0600
parents e4d7a3114fa8
children 05934fbf635a
comparison
equal deleted inserted replaced
1258:e4d7a3114fa8 1259:e8020216dee7
199 String contentType = (String)request.headers.get("content-type"); 199 String contentType = (String)request.headers.get("content-type");
200 if( !contentType.startsWith(contentTypeStart) ) 200 if( !contentType.startsWith(contentTypeStart) )
201 throw new RuntimeException(contentType); 201 throw new RuntimeException(contentType);
202 String boundary = "--"+contentType.substring(contentTypeStart.length()); 202 String boundary = "--"+contentType.substring(contentTypeStart.length());
203 this.parser = new Parser(Util.toString(request.body)); 203 this.parser = new Parser(Util.toString(request.body));
204 //System.out.println(this.parser.text);
204 require( parser.match(boundary) ); 205 require( parser.match(boundary) );
205 boundary = "\r\n" + boundary; 206 boundary = "\r\n" + boundary;
206 while( !parser.match("--\r\n") ) { 207 while( !parser.match("--\r\n") ) {
207 require( parser.match("\r\n") ); 208 require( parser.match("\r\n") );
208 require( parser.match("Content-Disposition: form-data; name=") ); 209 require( parser.match("Content-Disposition: form-data; name=") );
211 boolean isBinary = false; 212 boolean isBinary = false;
212 if( parser.match("; filename=") ) { 213 if( parser.match("; filename=") ) {
213 filename = quotedString(); 214 filename = quotedString();
214 require( parser.match("\r\n") ); 215 require( parser.match("\r\n") );
215 require( parser.match("Content-Type: ") ); 216 require( parser.match("Content-Type: ") );
216 if( parser.match("application/octet-stream") ) { 217 int start = parser.currentIndex();
218 if( parser.match("application/") ) {
217 isBinary = true; 219 isBinary = true;
218 } else if( parser.match("text/plain") ) { 220 } else if( parser.match("text/") ) {
219 isBinary = false; 221 isBinary = false;
220 } else 222 } else
221 throw new ParseException(parser,"bad file content-type"); 223 throw new ParseException(parser,"bad file content-type");
224 while( parser.inCharRange('a','z') || parser.anyOf("-.") );
225 contentType = parser.textFrom(start);
222 } 226 }
223 require( parser.match("\r\n") ); 227 require( parser.match("\r\n") );
224 require( parser.match("\r\n") ); 228 require( parser.match("\r\n") );
225 int start = parser.currentIndex(); 229 int start = parser.currentIndex();
226 while( !parser.test(boundary) ) { 230 while( !parser.test(boundary) ) {
229 String value = parser.textFrom(start); 233 String value = parser.textFrom(start);
230 if( filename == null ) { 234 if( filename == null ) {
231 Util.add(request.parameters,name,value); 235 Util.add(request.parameters,name,value);
232 } else { 236 } else {
233 Object content = isBinary ? Util.toBytes(value) : value; 237 Object content = isBinary ? Util.toBytes(value) : value;
234 Request.MultipartFile mf = new Request.MultipartFile(filename,content); 238 Request.MultipartFile mf = new Request.MultipartFile(filename,contentType,content);
235 Util.add(request.parameters,name,mf); 239 Util.add(request.parameters,name,mf);
236 } 240 }
237 require( parser.match(boundary) ); 241 require( parser.match(boundary) );
238 } 242 }
239 } 243 }