diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift index 36d1467..3bbc934 100644 --- a/src/ios/Webserver.swift +++ b/src/ios/Webserver.swift @@ -9,6 +9,8 @@ override func pluginInitialize() { self.webServer = GCDWebServer() self.onRequestCommand = nil + self.responses = [:] + self.initHTTPRequestHandlers() } func requestToRequestDict(requestUUID: String, request: GCDWebServerDataRequest) -> Dictionary { @@ -22,44 +24,58 @@ ] } - func processRequest(request: GCDWebServerRequest) -> GCDWebServerResponse { + func processRequest(request: GCDWebServerRequest, completionBlock: GCDWebServerCompletionBlock) { var timeout = 0 // Fetch data as GCDWebserverDataRequest - let dataRequest = request as! GCDWebServerDataRequest let requestUUID = UUID().uuidString // Transform it into an dictionary for the javascript plugin - let requestDict = self.requestToRequestDict(requestUUID: requestUUID, request: dataRequest) + let requestDict = self.requestToRequestDict(requestUUID: requestUUID, request: request as! GCDWebServerDataRequest) // Do a call to the onRequestCommand to inform the JS plugin if (self.onRequestCommand != nil) { + print("Sending to JS Context") + let pluginResult = CDVPluginResult(status: CDVCommandStatus_OK, messageAs: requestDict) + pluginResult?.setKeepCallbackAs(true) self.commandDelegate.send( - CDVPluginResult(status: CDVCommandStatus_OK, messageAs: requestDict), + pluginResult, callbackId: self.onRequestCommand?.callbackId ) } // Here we have to wait until the javascript block fetches the message and do a response - while self.responses[requestUUID] == nil && timeout < self.TIMEOUT { + while self.responses[requestUUID] == nil { timeout += 2000 usleep(2000) } - let response = GCDWebServerResponse() - return response + let responseDict = self.responses[requestUUID] as! Dictionary + let response = GCDWebServerDataResponse(text: responseDict["body"] as! String) + response?.statusCode = responseDict["status"] as! Int + + for (key, value) in (responseDict["headers"] as! Dictionary) { + response?.setValue(value, forAdditionalHeader: key) + } + completionBlock(response!) } func onRequest(_ command: CDVInvokedUrlCommand) { self.onRequestCommand = command + let pluginResult = CDVPluginResult(status: CDVCommandStatus_NO_RESULT) + pluginResult?.setKeepCallbackAs(true) + self.commandDelegate.send( + pluginResult, + callbackId: self.onRequestCommand?.callbackId + ) } func initHTTPRequestHandlers() { - for methodType in ["GET", "POST", "PUT", "PATCH", "DELETE"] { - self.webServer.addDefaultHandler( - forMethod: methodType, - request: GCDWebServerDataRequest.self, - processBlock: self.processRequest - ) - } + self.webServer.addHandler( + match: { + (requestMethod, requestURL, requestHeaders, urlPath, urlQuery) -> GCDWebServerRequest? in + return GCDWebServerDataRequest(method: requestMethod, url: requestURL, headers: requestHeaders, path: urlPath, query: urlQuery) + }, + asyncProcessBlock: self.processRequest + ) } func sendResponse(_ command: CDVInvokedUrlCommand) { diff --git a/tests/tests.js b/tests/tests.js index 91a5799..00194cf 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,3 +1,4 @@ +cordova.define("cordova-plugin-webserver-tests.tests", function(require, exports, module) { exports.defineAutoTests = function() { describe('Webserver (window.webserver)', function () { @@ -35,12 +36,21 @@ exports.defineManualTests = function(contentEl, createActionButton) { createActionButton('Start bljad Webserver', function() { console.log("Starting webserver..."); - console.log(webserver); - webserver.onRequest( function(request) { - console.log(request); - webserver.sendResponse(request.requestId, {}); + console.log('Received request'); + console.log('Sending response'); + webserver.sendResponse( + request["requestId"], + { + status: 200, + headers: { + 'Content-Type': 'application/json', + 'TestHeader': 'Just a testheader' + }, + body: '{"hello":"from js!"}' + } + ); } ); @@ -81,3 +91,5 @@ exports.defineManualTests = function(contentEl, createActionButton) { ); }); }; + +}); diff --git a/webserver.js b/webserver.js index ba7f18d..ef1efc9 100644 --- a/webserver.js +++ b/webserver.js @@ -1,6 +1,7 @@ -var WEBSERVER_CLASS = "Webserver"; +cordova.define("cordova-plugin-webserver.webserver", function(require, exports, module) { + var WEBSERVER_CLASS = "Webserver"; -exports.start = function(success_callback, error_callback, port) { + exports.start = function(success_callback, error_callback, port) { cordova.exec( success_callback, error_callback, @@ -8,34 +9,34 @@ exports.start = function(success_callback, error_callback, port) { "start", [port] ); -}; + }; -exports.onRequest = function(success_callback) { + exports.onRequest = function(success_callback) { cordova.exec( success_callback, - function(error) {console.error(error);} + function(error) {console.error(error)}, WEBSERVER_CLASS, "onRequest", [] ); -}; + }; -exports.sendResponse = function( - responseId, - params, - success_callback, - error_callback -) { + exports.sendResponse = function( + requestId, + params, + success_callback, + error_callback + ) { cordova.exec( success_callback, error_callback, WEBSERVER_CLASS, - "send", + "sendResponse", [requestId, params] ); -}; + }; -exports.stop = function(success_callback, error_callback) { + exports.stop = function(success_callback, error_callback) { cordova.exec( success_callback, error_callback, @@ -43,4 +44,6 @@ exports.stop = function(success_callback, error_callback) { "stop", [] ); -}; + }; + +});