Bug fixe with the getFilePathByHttpResource() method

This commit is contained in:
Th3maz1ng 2019-05-01 11:38:09 +02:00
parent 4bf746db65
commit 513007f2da

View File

@ -5,6 +5,7 @@
//#define DEBUG_PARAMETER
//#define DEBUG_CONTENT_LENGTH
//#define DEBUG_RAW
//#define DEBUG_FILEPATH
WEBServerManager::WEBServerManager(unsigned int port, SDCardManager *sdCardManager) : _wifiServer(port), _sdCardManager(sdCardManager), _httpRequestData({UNDEFINED, UNKNOWN, UNKNOWN_MIME, Dictionary<DictionaryHelper::StringEntity>(), Dictionary<DictionaryHelper::StringEntity>(), NULL,NULL}), _httpParserState(INIT), _clientState(WAITING_FOR_CLIENT), _port(port), _clientTimeout(0)
{
@ -320,7 +321,7 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
filePath = getFilePathByHttpResource(_httpRequestData.httpResource);
if(filePath == NULL)
{
wifiClient->print(F("HTTP/1.1 500 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<p>Failed to malloc filePath</p>\r\n</html>"));
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<p>Failed to malloc filePath</p>\r\n</html>"));
return false;
}
@ -338,7 +339,7 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
response = (char *) malloc(sizeof(char) * (strlen("HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Page not found for : </h1>\r\n<h4></h4>\r\n</html>") + strlen(_httpRequestData.httpResource) + 1));
if(response == NULL)
{
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc filePath</h1>\r\n</html>"));
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc response</h1>\r\n</html>"));
return false;
}
sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Page not found for : </h1>\r\n<h4>%s</h4>\r\n</html>", _httpRequestData.httpResource);
@ -360,7 +361,7 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
header = getHTTPHeader(getMIMETypeByExtension(getFileExtension(pageToSend.name())), pageToSend.size());
if(header == NULL)
{
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc filePath</h1>\r\n</html>"));
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>Failed to malloc header</h1>\r\n</html>"));
pageToSend.close();
return false;
}
@ -382,8 +383,6 @@ boolean WEBServerManager::sendPageToClientFromSdCard(WiFiClient *wifiClient)
}
}else
{
//Test if it does correspond to a user defined API call
wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n<h1>SDCardManager is NULL<br \\>Check code</h1>\r\n</html>"));
}
@ -481,12 +480,22 @@ char *WEBServerManager::getFileExtension(char *name)
char *WEBServerManager::getFilePathByHttpResource(const char *res)
{
char *filePath = (char*) malloc( sizeof(char) * (strlen(WWW_DIR)+ strlen(strcmp(res, "/") == 0 ? "/index.web":res) + 1) ); //Do not forget \0
uint16_t buffSize = strlen(WWW_DIR) + (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;
sprintf(filePath,"%s%s",WWW_DIR, strcmp(_httpRequestData.httpResource, "/") == 0 ? "/index.htm":_httpRequestData.httpResource);
strcpy(filePath, WWW_DIR);
strcat(filePath, strcmp(res, "/") == 0 ? "/index.htm":res);
//sprintf(filePath,"%s%s",WWW_DIR, strcmp(res, "/") == 0 ? "/index.htm":res);
#ifdef DEBUG_FILEPATH
Serial.println(res);
Serial.print("Reserved space : ");Serial.println(buffSize);
Serial.print("Actual size : ");Serial.println(strlen(filePath));
Serial.println(filePath);
#endif
return filePath;
}