The WEB Server root directory is now copied so that the parameter doesn't have to live during the whole life of the program

This commit is contained in:
anschrammh 2022-04-29 07:50:33 +02:00
parent 2449ab6013
commit 966177a209

View File

@ -42,6 +42,11 @@ class WEBServer : public TCPServer<T>, public HttpConstants
WEBServer(uint16_t port = 80, SDClass *sdClass = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 256) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdClass(sdClass) {} WEBServer(uint16_t port = 80, SDClass *sdClass = NULL, uint8_t maxClient = MAX_CLIENT, uint16_t clientDataBufferSize = 256) : TCPServer<T>(port, maxClient, clientDataBufferSize), _sdClass(sdClass) {}
virtual ~WEBServer()
{
free(_WWWDir);
}
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)
{ {
return _apiDictionary.add(uri, new ApiRoutine({apiRoutine, pData, HRM})); return _apiDictionary.add(uri, new ApiRoutine({apiRoutine, pData, HRM}));
@ -72,7 +77,12 @@ class WEBServer : public TCPServer<T>, public HttpConstants
void setWWWDir(const char *WWWDir) void setWWWDir(const char *WWWDir)
{ {
_WWWDir = WWWDir; if(WWWDir != nullptr)
{
free(_WWWDir);
_WWWDir = (char *)malloc((strlen(WWWDir) * sizeof(char)) + 1);
strcpy(_WWWDir, WWWDir);
}
} }
protected: protected:
private: private:
@ -165,11 +175,14 @@ class WEBServer : public TCPServer<T>, public HttpConstants
case HttpParserStatus::PARSE_HTTP_RESOURCE: case HttpParserStatus::PARSE_HTTP_RESOURCE:
{ {
char *pRsrc(strchr((char *)client->_data, ' ')), *pRsrcQuery(strchr((char *)client->_data, '?')); char *pRsrc(strchr((char *)client->_data, ' ')), *pRsrcQuery(strchr((char *)client->_data, '?'));
//!\ the ? should be present before ' ' if ' ' is found !!!!
if(pRsrc && pRsrcQuery)
if(pRsrcQuery > pRsrc)pRsrcQuery = nullptr;
//The case where we have the resource complete or not complete with query parameters like : GET /some/path/resource.rsrc?param1=one&param2 HTTP/1.1 //The case where we have the resource complete or not complete with query parameters like : GET /some/path/resource.rsrc?param1=one&param2 HTTP/1.1
if(pRsrc || pRsrcQuery) if(pRsrc || pRsrcQuery)
{ {
uint16_t rawLengthOfResource(0); uint16_t rawLengthOfResource(0);
if(pRsrcQuery) if(pRsrcQuery )
{ {
*pRsrcQuery = '\0'; // The ? is the end of the resource string *pRsrcQuery = '\0'; // The ? is the end of the resource string
rawLengthOfResource = pRsrcQuery - (char *)client->_data; rawLengthOfResource = pRsrcQuery - (char *)client->_data;
@ -182,7 +195,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
*pRsrc = '\0'; *pRsrc = '\0';
rawLengthOfResource = pRsrc - (char *)client->_data; rawLengthOfResource = pRsrc - (char *)client->_data;
#ifdef DEBUG_WEBS #ifdef DEBUG_WEBS
Serial.printf("Resource w/o query\n"); Serial.printf("Resource w/o query\nRaw length : %u\n",rawLengthOfResource);
#endif #endif
} }
@ -937,7 +950,7 @@ class WEBServer : public TCPServer<T>, public HttpConstants
Dictionary<ApiRoutine> _apiDictionary; Dictionary<ApiRoutine> _apiDictionary;
SDClass *_sdClass; SDClass *_sdClass;
const char *_WWWDir = NULL; //Website root folder char *_WWWDir = nullptr; //Website root folder
}; };
#endif //WEBSERVER_H #endif //WEBSERVER_H