check for request id when sending files
avoids sending the file data in response to every single request issued after the one for the file.
This commit is contained in:
parent
128933f721
commit
752148e0c8
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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, []);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user