changeset 1197:886e14903c1e

better Content-Type handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 27 Feb 2018 01:35:09 -0700
parents 44491798e431
children 9d3835e88204
files src/luan/webserver/Connection.java src/luan/webserver/RequestParser.java
diffstat 2 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
diff -r 44491798e431 -r 886e14903c1e src/luan/webserver/Connection.java
--- a/src/luan/webserver/Connection.java	Mon Feb 26 21:06:28 2018 -0700
+++ b/src/luan/webserver/Connection.java	Tue Feb 27 01:35:09 2018 -0700
@@ -60,7 +60,7 @@
 						}
 					}
 					String rawHead = new String(a,0,endOfHeader);
-//System.out.println(rawHead);
+					//System.out.println(rawHead);
 					request.rawHead = rawHead;
 					RequestParser parser = new RequestParser(request);
 					parser.parseHead();
@@ -79,21 +79,17 @@
 							size += n;
 						}
 						request.body = body;
-//System.out.println(new String(request.body));
+						//System.out.println(new String(request.body));
 					}
 	
 					String contentType = (String)request.headers.get("content-type");
 					if( contentType != null ) {
-						if( request.body == null ) {
-							logger.error("body is null\n"+request.rawHead);
+						if( "application/x-www-form-urlencoded".equals(contentType) ) {
+							parser.parseUrlencoded();
+						} else if( contentType.startsWith("multipart/form-data;") ) {
+							parser.parseMultipart();
 						} else {
-							if( "application/x-www-form-urlencoded".equals(contentType) ) {
-								parser.parseUrlencoded();
-							} else if( contentType.startsWith("multipart/form-data;") ) {
-								parser.parseMultipart();
-							} else {
-								logger.error("unknown content type: "+contentType);
-							}
+							logger.warn("unknown content type: "+contentType);
 						}
 					}
 				}
diff -r 44491798e431 -r 886e14903c1e src/luan/webserver/RequestParser.java
--- a/src/luan/webserver/RequestParser.java	Mon Feb 26 21:06:28 2018 -0700
+++ b/src/luan/webserver/RequestParser.java	Tue Feb 27 01:35:09 2018 -0700
@@ -2,11 +2,14 @@
 
 import java.util.List;
 import java.util.ArrayList;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import luan.lib.parser.Parser;
 import luan.lib.parser.ParseException;
 
 
 final class RequestParser {
+	private static final Logger logger = LoggerFactory.getLogger(Connection.class);
 	private final Request request;
 	private Parser parser;
 
@@ -15,6 +18,10 @@
 	}
 
 	void parseUrlencoded() throws ParseException {
+		if( request.body == null ) {
+			logger.error("body is null\n"+request.rawHead);
+			return;
+		}
 		this.parser = new Parser(Util.toString(request.body));
 		parseQuery();
 		require( parser.endOfInput() );
@@ -187,6 +194,10 @@
 	private static final String contentTypeStart = "multipart/form-data; boundary=";
 
 	void parseMultipart() throws ParseException {
+		if( request.body == null ) {
+			logger.error("body is null\n"+request.rawHead);
+			return;
+		}
 		String contentType = (String)request.headers.get("content-type");
 		if( !contentType.startsWith(contentTypeStart) )
 			throw new RuntimeException(contentType);