From 4c4832e475ac4664627383c49e7974191c1c1acf Mon Sep 17 00:00:00 2001 From: anschrammh Date: Sat, 19 Dec 2020 21:14:57 +0100 Subject: [PATCH] Updated WEBServer (same changes as FTPServer) --- src/app/WEBServer.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/app/WEBServer.h b/src/app/WEBServer.h index 149316e..eb2509d 100644 --- a/src/app/WEBServer.h +++ b/src/app/WEBServer.h @@ -1,10 +1,11 @@ #ifndef WEBSERVER_H #define WEBSERVER_H +#include #include "TCPServer.h" #include "Dictionary.h" -#include "SDCardManager.h" #include "HttpConstants.h" +#include "utilities.h" //#define DEBUG_WEBS #define READ_WRITE_BUFFER_SIZE 2000 @@ -32,7 +33,7 @@ class WEBServer : public TCPServer, public HttpConstants uint16_t maxBodyBuffer; }; - WEBServer(uint16_t port = 80, SDCardManager *sdCardManager = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer(port, maxClient, clientDataBufferSize), _sdCardManager(sdCardManager) {} + WEBServer(uint16_t port = 80, SDClass *sdClass = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer(port, maxClient, clientDataBufferSize), _sdClass(sdClass) {} boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED) { @@ -57,6 +58,11 @@ class WEBServer : public TCPServer, public HttpConstants free(buffer); } } + + void setWWWDir(const char *WWWDir) + { + _WWWDir = WWWDir; + } protected: private: virtual T* createNewClient(WiFiClient wc) @@ -470,7 +476,7 @@ class WEBServer : public TCPServer, public HttpConstants boolean sendPageToClientFromSdCard(T *client) { - if(_sdCardManager != NULL) + if(_sdClass != NULL) { File pageToSend; char *filePath(NULL), *header(NULL); @@ -480,7 +486,7 @@ class WEBServer : public TCPServer, public HttpConstants switch(client->_httpRequestData.HRM) { case GET: - filePath = getFilePathByHttpResource(client->_httpRequestData.httpResource); + filePath = getFilePathByHttpResource(_WWWDir, client->_httpRequestData.httpResource); if(filePath == NULL) { sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath"); @@ -492,7 +498,7 @@ class WEBServer : public TCPServer, public HttpConstants Serial.println(filePath); #endif - pageToSend = _sdCardManager->open(filePath); + pageToSend = _sdClass->open(filePath); free(filePath);filePath = NULL; //If we couldn't open the file @@ -710,16 +716,16 @@ class WEBServer : public TCPServer, public HttpConstants return p != NULL ? p+1 : NULL; } - static char *getFilePathByHttpResource(char *res) + static char *getFilePathByHttpResource(const char *WWWDir, char *res) { - uint16_t buffSize = strlen(WWW_DIR) + (strcmp(res, "/") == 0 ? 10:strlen(res)) + 1;//10 for /index.htm +1 for \0 + uint16_t buffSize = (WWWDir ? strlen(WWWDir) : 0 /*default is / */) + (strcmp(res, "/") == 0 ? 10:strlen(res)) + 1;//10 for /index.htm +1 for \0 char *filePath = (char*) malloc( sizeof(char) * buffSize); if(filePath == NULL) return NULL; - strcpy(filePath, WWW_DIR); - strcat(filePath, strcmp(res, "/") == 0 ? "/index.htm":res); + WWWDir ? strcpy(filePath, WWWDir) : strcpy(filePath, ""); + strcat(filePath, (strcmp(res, "/") == 0) ? "/index.htm":res); #ifdef DEBUG_FILEPATH Serial.println(res); @@ -750,7 +756,8 @@ class WEBServer : public TCPServer, public HttpConstants }; Dictionary _apiDictionary; - SDCardManager *_sdCardManager; + SDClass *_sdClass; + const char *_WWWDir = NULL; //Website root folder }; #endif //WEBSERVER_H