diff --git a/src/android/NanoHTTPDWebserver.java b/src/android/NanoHTTPDWebserver.java index b6309b7..a018fe3 100644 --- a/src/android/NanoHTTPDWebserver.java +++ b/src/android/NanoHTTPDWebserver.java @@ -207,7 +207,7 @@ public class NanoHTTPDWebserver extends NanoHTTPD { pluginResult.setKeepCallback(true); this.webserver.onRequestCallbackContext.sendPluginResult(pluginResult); - while (!this.webserver.responses.containsKey(requestUUID) && !this.webserver.responses.containsKey("file")) { + while (!this.webserver.responses.containsKey(requestUUID)) { try { Thread.sleep(1); } catch (InterruptedException e) { @@ -215,45 +215,41 @@ public class NanoHTTPDWebserver extends NanoHTTPD { } } - JSONObject responseObject; + JSONObject responseObject = (JSONObject) this.webserver.responses.get(requestUUID); Response response = null; - - if (this.webserver.responses.containsKey("file")) { - // TODO should specify a more correct mime-type - try { - responseObject = (JSONObject) this.webserver.responses.get("file"); - Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString()); - return serveFile(session.getHeaders(), new File(responseObject.getString("path")), responseObject.getString("type")); - } - catch (JSONException e) { - e.printStackTrace(); - } - return response; - } - - responseObject = (JSONObject) this.webserver.responses.get(requestUUID); Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString()); - - try { - response = newFixedLengthResponse( - 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) - ); + if (responseObject.containsKey("path")) { + // TODO should specify a more correct mime-type + try { + return serveFile(session.getHeaders(), new File(responseObject.getString("path")), responseObject.getString("type")); } - - } catch (JSONException e) { - e.printStackTrace(); + catch (JSONException e) { + e.printStackTrace(); + } + return response; + } + else { + try { + response = newFixedLengthResponse( + 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) + ); + } + + } catch (JSONException e) { + e.printStackTrace(); + } + return response; } - return response; } } diff --git a/src/android/Webserver.java b/src/android/Webserver.java index b6b9d48..cf82547 100644 --- a/src/android/Webserver.java +++ b/src/android/Webserver.java @@ -44,10 +44,6 @@ public class Webserver extends CordovaPlugin { this.sendResponse(args, callbackContext); return true; } - else if ("sendFileResponse".equals(action)) { - this.sendFileResponse(args, callbackContext); - return true; - } return false; // Returning false results in a "MethodNotFound" error. } @@ -100,12 +96,6 @@ public class Webserver extends CordovaPlugin { callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); } - private void sendFileResponse(JSONArray args, CallbackContext callbackContext) throws JSONException { - Log.d(this.getClass().getName(), "Got sendResponse: " + args.toString()); - this.responses.put("file", args.get(0)); - callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK)); - } - /** * Just register the onRequest and send no result. This is needed to save the callbackContext to * invoke it later diff --git a/src/www/webserver.js b/src/www/webserver.js index 732e20c..a1d2332 100644 --- a/src/www/webserver.js +++ b/src/www/webserver.js @@ -4,7 +4,6 @@ const WEBSERVER_CLASS = 'Webserver'; const START_FUNCTION = 'start'; const ONREQUEST_FUNCTION = 'onRequest'; const SENDRESPONSE_FUNCION = 'sendResponse'; -const SENDFILE_FUNCION = 'sendFileResponse'; const STOP_FUNCTION = 'stop'; export function start(success_callback, error_callback, port) { @@ -46,21 +45,6 @@ export function sendResponse( ); } -export function sendFile( - requestId, - params, - success_callback, - error_callback -) { - exec( - success_callback, - error_callback, - WEBSERVER_CLASS, - SENDFILE_FUNCION, - [requestId, params] - ); -} - export function stop(success_callback, error_callback) { exec( success_callback, diff --git a/webserver.js b/webserver.js index 720ac86..9ac93d6 100644 --- a/webserver.js +++ b/webserver.js @@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", { exports.start = start; exports.onRequest = onRequest; exports.sendResponse = sendResponse; -exports.sendFile = sendFile; exports.stop = stop; var _exec = require('cordova/exec'); @@ -19,7 +18,6 @@ var WEBSERVER_CLASS = 'Webserver'; var START_FUNCTION = 'start'; var ONREQUEST_FUNCTION = 'onRequest'; var SENDRESPONSE_FUNCION = 'sendResponse'; -var SENDFILE_FUNCION = 'sendFileResponse'; var STOP_FUNCTION = 'stop'; function start(success_callback, error_callback, port) { @@ -40,10 +38,6 @@ function sendResponse(requestId, params, success_callback, error_callback) { (0, _exec2.default)(success_callback, error_callback, WEBSERVER_CLASS, SENDRESPONSE_FUNCION, [requestId, params]); } -function sendFile(requestId, params, success_callback, error_callback) { - (0, _exec2.default)(success_callback, error_callback, WEBSERVER_CLASS, SENDFILE_FUNCION, [requestId, params]); -} - function stop(success_callback, error_callback) { (0, _exec2.default)(success_callback, error_callback, WEBSERVER_CLASS, STOP_FUNCTION, []); }