Updated WEBServer (same changes as FTPServer)

This commit is contained in:
anschrammh 2020-12-19 21:14:57 +01:00
parent 4a6dde3bc3
commit 4c4832e475

View File

@ -1,10 +1,11 @@
#ifndef WEBSERVER_H #ifndef WEBSERVER_H
#define WEBSERVER_H #define WEBSERVER_H
#include <SD.h>
#include "TCPServer.h" #include "TCPServer.h"
#include "Dictionary.h" #include "Dictionary.h"
#include "SDCardManager.h"
#include "HttpConstants.h" #include "HttpConstants.h"
#include "utilities.h"
//#define DEBUG_WEBS //#define DEBUG_WEBS
#define READ_WRITE_BUFFER_SIZE 2000 #define READ_WRITE_BUFFER_SIZE 2000
@ -32,7 +33,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
uint16_t maxBodyBuffer; uint16_t maxBodyBuffer;
}; };
WEBServer(uint16_t port = 80, SDCardManager *sdCardManager = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdCardManager(sdCardManager) {} WEBServer(uint16_t port = 80, SDClass *sdClass = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 255) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdClass(sdClass) {}
boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED) boolean addApiRoutine(const char *uri, boolean (*apiRoutine)(HttpRequestData&, WiFiClient*, void*), void *pData, HttpRequestMethod HRM = UNDEFINED)
{ {
@ -57,6 +58,11 @@ class WEBServer : public TCPServer<T>, public HttpConstants
free(buffer); free(buffer);
} }
} }
void setWWWDir(const char *WWWDir)
{
_WWWDir = WWWDir;
}
protected: protected:
private: private:
virtual T* createNewClient(WiFiClient wc) virtual T* createNewClient(WiFiClient wc)
@ -470,7 +476,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
boolean sendPageToClientFromSdCard(T *client) boolean sendPageToClientFromSdCard(T *client)
{ {
if(_sdCardManager != NULL) if(_sdClass != NULL)
{ {
File pageToSend; File pageToSend;
char *filePath(NULL), *header(NULL); char *filePath(NULL), *header(NULL);
@ -480,7 +486,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
switch(client->_httpRequestData.HRM) switch(client->_httpRequestData.HRM)
{ {
case GET: case GET:
filePath = getFilePathByHttpResource(client->_httpRequestData.httpResource); filePath = getFilePathByHttpResource(_WWWDir, client->_httpRequestData.httpResource);
if(filePath == NULL) if(filePath == NULL)
{ {
sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath"); sendInfoResponse(HTTP_CODE::HTTP_CODE_INTERNAL_SERVER_ERROR, client, "Failed to allocate memory for the filePath");
@ -492,7 +498,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
Serial.println(filePath); Serial.println(filePath);
#endif #endif
pageToSend = _sdCardManager->open(filePath); pageToSend = _sdClass->open(filePath);
free(filePath);filePath = NULL; free(filePath);filePath = NULL;
//If we couldn't open the file //If we couldn't open the file
@ -710,16 +716,16 @@ class WEBServer : public TCPServer<T>, public HttpConstants
return p != NULL ? p+1 : NULL; 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); char *filePath = (char*) malloc( sizeof(char) * buffSize);
if(filePath == NULL) if(filePath == NULL)
return NULL; return NULL;
strcpy(filePath, WWW_DIR); WWWDir ? strcpy(filePath, WWWDir) : strcpy(filePath, "");
strcat(filePath, strcmp(res, "/") == 0 ? "/index.htm":res); strcat(filePath, (strcmp(res, "/") == 0) ? "/index.htm":res);
#ifdef DEBUG_FILEPATH #ifdef DEBUG_FILEPATH
Serial.println(res); Serial.println(res);
@ -750,7 +756,8 @@ class WEBServer : public TCPServer<T>, public HttpConstants
}; };
Dictionary<ApiRoutine> _apiDictionary; Dictionary<ApiRoutine> _apiDictionary;
SDCardManager *_sdCardManager; SDClass *_sdClass;
const char *_WWWDir = NULL; //Website root folder
}; };
#endif //WEBSERVER_H #endif //WEBSERVER_H