Allow set the mimetype over the content type

This commit is contained in:
Michael Bykovski 2018-01-25 11:14:31 +01:00
parent f77fd4d4b0
commit 519a264040

View File

@ -19,7 +19,7 @@ import java.util.Iterator;
import java.util.Scanner; import java.util.Scanner;
import java.util.UUID; import java.util.UUID;
public class NanoHTTPDWebserver extends NanoHTTPD{ public class NanoHTTPDWebserver extends NanoHTTPD {
Webserver webserver; Webserver webserver;
@ -35,15 +35,15 @@ public class NanoHTTPDWebserver extends NanoHTTPD{
/** /**
* Create a request object * Create a request object
* * <p>
* [ * [
* "requestId": requestUUID, * "requestId": requestUUID,
" body": request.jsonObject ?? "", * " body": request.jsonObject ?? "",
" headers": request.headers, * " headers": request.headers,
" method": request.method, * " method": request.method,
" path": request.url.path, * " path": request.url.path,
" query": request.url.query ?? "" * " query": request.url.query ?? ""
] * ]
* *
* @param session * @param session
* @return * @return
@ -59,6 +59,15 @@ public class NanoHTTPDWebserver extends NanoHTTPD{
return jsonRequest; 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 @Override
public Response serve(IHTTPSession session) { public Response serve(IHTTPSession session) {
Log.d(this.getClass().getName(), "New request is incoming!"); 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); JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID);
Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString()); Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
Response response = null; Response response = null;
try { try {
response = newFixedLengthResponse( response = newFixedLengthResponse(
Response.Status.lookup(responseObject.getInt("status")), Response.Status.lookup(responseObject.getInt("status")),
"text/plain", getContentType(responseObject),
responseObject.getString("body") responseObject.getString("body")
); );
Iterator<?> keys = responseObject.getJSONObject("headers").keys(); Iterator<?> keys = responseObject.getJSONObject("headers").keys();
while (keys.hasNext()) { while (keys.hasNext()) {
String key = (String) keys.next(); String key = (String) keys.next();
response.addHeader( response.addHeader(
key, key,
responseObject.getJSONObject("headers").getString(key) responseObject.getJSONObject("headers").getString(key)
); );
} }