From 519a264040a7be4c0606b6b389f9735d6f2082ec Mon Sep 17 00:00:00 2001 From: Michael Bykovski Date: Thu, 25 Jan 2018 11:14:31 +0100 Subject: [PATCH] Allow set the mimetype over the content type --- src/android/NanoHTTPDWebserver.java | 38 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/android/NanoHTTPDWebserver.java b/src/android/NanoHTTPDWebserver.java index 43c71df..65faee4 100644 --- a/src/android/NanoHTTPDWebserver.java +++ b/src/android/NanoHTTPDWebserver.java @@ -19,7 +19,7 @@ import java.util.Iterator; import java.util.Scanner; import java.util.UUID; -public class NanoHTTPDWebserver extends NanoHTTPD{ +public class NanoHTTPDWebserver extends NanoHTTPD { Webserver webserver; @@ -35,15 +35,15 @@ public class NanoHTTPDWebserver extends NanoHTTPD{ /** * Create a request object - * + *

* [ - * "requestId": requestUUID, - " body": request.jsonObject ?? "", - " headers": request.headers, - " method": request.method, - " path": request.url.path, - " query": request.url.query ?? "" - ] + * "requestId": requestUUID, + * " body": request.jsonObject ?? "", + * " headers": request.headers, + * " method": request.method, + * " path": request.url.path, + * " query": request.url.query ?? "" + * ] * * @param session * @return @@ -59,6 +59,15 @@ public class NanoHTTPDWebserver extends NanoHTTPD{ return jsonRequest; } + private String getContentType(JSONObject responseObject) throws JSONException { + if (responseObject.has("headers") && + responseObject.getJSONObject("headers").has("Content-Type")) { + return responseObject.getJSONObject("headers").getString("Content-Type"); + } else { + return "text/plain"; + } + } + @Override public Response serve(IHTTPSession session) { Log.d(this.getClass().getName(), "New request is incoming!"); @@ -86,19 +95,20 @@ public class NanoHTTPDWebserver extends NanoHTTPD{ JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID); Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString()); Response response = null; + try { response = newFixedLengthResponse( - Response.Status.lookup(responseObject.getInt("status")), - "text/plain", - responseObject.getString("body") + Response.Status.lookup(responseObject.getInt("status")), + getContentType(responseObject), + responseObject.getString("body") ); Iterator keys = responseObject.getJSONObject("headers").keys(); while (keys.hasNext()) { String key = (String) keys.next(); response.addHeader( - key, - responseObject.getJSONObject("headers").getString(key) + key, + responseObject.getJSONObject("headers").getString(key) ); }