From fd3d9e0366a58516542be4580a89eccb9ce79e81 Mon Sep 17 00:00:00 2001 From: boedy Date: Fri, 10 May 2019 20:27:08 +0200 Subject: [PATCH] Support file responses on iOS --- src/ios/Webserver.swift | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/ios/Webserver.swift b/src/ios/Webserver.swift index 7eef4a5..907e218 100644 --- a/src/ios/Webserver.swift +++ b/src/ios/Webserver.swift @@ -31,6 +31,15 @@ ] } + func fileRequest(path: String) -> GCDWebServerResponse { + // Check if file exists, given its path + if !(FileManager.default.fileExists(atPath: path)) { + return GCDWebServerResponse(statusCode: 404); + } + + return GCDWebServerFileResponse(file: path)! + } + func processRequest(request: GCDWebServerRequest, completionBlock: GCDWebServerCompletionBlock) { var timeout = 0 // Fetch data as GCDWebserverDataRequest @@ -56,8 +65,15 @@ // We got the dict so put information in the response let responseDict = self.responses[requestUUID] as! Dictionary - let response = GCDWebServerDataResponse(text: responseDict["body"] as! String) - response?.statusCode = responseDict["status"] as! Int + + // Check if a file path is provided else use regular data response + let response = responseDict["path"] != nil + ? fileRequest(path: responseDict["path"] as! String) + : GCDWebServerDataResponse(text: responseDict["body"] as! String) + + if responseDict["status"] != nil { + response?.statusCode = responseDict["status"] as! Int + } for (key, value) in (responseDict["headers"] as! Dictionary) { response?.setValue(value, forAdditionalHeader: key)