changeset 1258:e4d7a3114fa8

support "Content-Type: application/json; charset=utf-8"
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Sep 2018 22:11:11 -0600
parents e38f5869e9df
children e8020216dee7
files src/luan/webserver/Connection.java src/luan/webserver/RequestParser.java
diffstat 2 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/webserver/Connection.java	Thu Sep 20 21:04:41 2018 -0600
+++ b/src/luan/webserver/Connection.java	Thu Sep 20 22:11:11 2018 -0600
@@ -88,6 +88,8 @@
 							parser.parseUrlencoded();
 						} else if( contentType.startsWith("multipart/form-data;") ) {
 							parser.parseMultipart();
+						} else if( contentType.equals("application/json; charset=utf-8") ) {
+							parser.parseJson();
 						} else {
 							logger.info("unknown request content-type: "+contentType);
 						}
--- a/src/luan/webserver/RequestParser.java	Thu Sep 20 21:04:41 2018 -0600
+++ b/src/luan/webserver/RequestParser.java	Thu Sep 20 22:11:11 2018 -0600
@@ -264,4 +264,17 @@
 		}
 	}
 
+	// improve later
+	void parseJson() throws UnsupportedEncodingException {
+		if( request.body == null ) {
+			logger.warn("body is null\n"+request.rawHead);
+			return;
+		}
+		String contentType = (String)request.headers.get("content-type");
+		if( !contentType.equals("application/json; charset=utf-8") )
+			throw new RuntimeException(contentType);
+		String value = new String(request.body,"utf-8");
+		Util.add(request.parameters,"json",value);
+	}
+
 }