changeset 1463:fb003c4003dd

better application/json handling
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 30 Mar 2020 18:31:18 -0600
parents bfbf9c9c1586
children 465b4a0dae4a
files src/goodjava/webserver/Connection.java src/goodjava/webserver/RequestParser.java src/goodjava/webserver/handlers/ContentTypeHandler.java
diffstat 3 files changed, 8 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
diff -r bfbf9c9c1586 -r fb003c4003dd src/goodjava/webserver/Connection.java
--- a/src/goodjava/webserver/Connection.java	Sun Mar 29 09:55:59 2020 -0600
+++ b/src/goodjava/webserver/Connection.java	Mon Mar 30 18:31:18 2020 -0600
@@ -85,14 +85,16 @@
 					String contentType = (String)request.headers.get("content-type");
 					if( contentType != null ) {
 						contentType = contentType.toLowerCase();
-						if( "application/x-www-form-urlencoded".equals(contentType) ) {
+						if( contentType.equals("application/x-www-form-urlencoded") ) {
 							parser.parseUrlencoded(null);
-						} else if( "application/x-www-form-urlencoded; charset=utf-8".equals(contentType) ) {
+						} else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) {
 							parser.parseUrlencoded("utf-8");
 						} else if( contentType.startsWith("multipart/form-data;") ) {
 							parser.parseMultipart();
+						} else if( contentType.equals("application/json") ) {
+							parser.parseJson(null);
 						} else if( contentType.equals("application/json; charset=utf-8") ) {
-							parser.parseJson();
+							parser.parseJson("utf-8");
 						} else {
 							logger.info("unknown request content-type: "+contentType);
 						}
diff -r bfbf9c9c1586 -r fb003c4003dd src/goodjava/webserver/RequestParser.java
--- a/src/goodjava/webserver/RequestParser.java	Sun Mar 29 09:55:59 2020 -0600
+++ b/src/goodjava/webserver/RequestParser.java	Mon Mar 30 18:31:18 2020 -0600
@@ -272,15 +272,12 @@
 	}
 
 	// improve later
-	void parseJson() throws UnsupportedEncodingException {
+	void parseJson(String charset) 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");
+		String value = Util.toString(request.body,charset);
 		Util.add(request.parameters,"json",value);
 	}
 
diff -r bfbf9c9c1586 -r fb003c4003dd src/goodjava/webserver/handlers/ContentTypeHandler.java
--- a/src/goodjava/webserver/handlers/ContentTypeHandler.java	Sun Mar 29 09:55:59 2020 -0600
+++ b/src/goodjava/webserver/handlers/ContentTypeHandler.java	Mon Mar 30 18:31:18 2020 -0600
@@ -31,6 +31,7 @@
 		map.put( "txt", textType );
 		map.put( "css", "text/css" );
 		map.put( "js", "application/javascript" );
+		map.put( "json", "application/json" + attrs );
 		map.put( "mp4", "video/mp4" );
 		// add more as need
 	}