diff --git a/src/app/WEBServerManager.cpp b/src/app/WEBServerManager.cpp index 3efedba..f83e741 100644 --- a/src/app/WEBServerManager.cpp +++ b/src/app/WEBServerManager.cpp @@ -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(), Dictionary(), 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\r\n\r\n

Failed to malloc filePath

\r\n")); + wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Failed to malloc filePath

\r\n")); 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\r\n\r\n

Page not found for :

\r\n

\r\n") + 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\r\n\r\n

Failed to malloc filePath

\r\n")); + wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Failed to malloc response

\r\n")); return false; } sprintf(response, "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Page not found for :

\r\n

%s

\r\n", _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\r\n\r\n

Failed to malloc filePath

\r\n")); + wifiClient->print(F("HTTP/1.1 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n\r\n\r\n

Failed to malloc header

\r\n")); 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\r\n\r\n

SDCardManager is NULL
Check code

\r\n")); } @@ -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; }