diff --git a/src/app/WEBServer.h b/src/app/WEBServer.h index 56ed206..0602110 100644 --- a/src/app/WEBServer.h +++ b/src/app/WEBServer.h @@ -55,11 +55,19 @@ class WEBServer : public TCPServer, public HttpConstants } //Helper function used for the webApi - static void sendHTTPHeader(WiFiClient *client, const char *contentType, const size_t contentLength, HttpVersion version = HttpVersion::HTTP_1_1, HTTP_CODE HTTPCode = HTTP_CODE::HTTP_CODE_OK) + static void sendHTTPHeader(WiFiClient *client, const char *contentType, const size_t contentLength = 0, HttpVersion version = HttpVersion::HTTP_1_1, HTTP_CODE HTTPCode = HTTP_CODE::HTTP_CODE_OK) { if(!client) return; (void)HTTPCode; - client->printf("%s 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", httpVersionToString(version), contentType, contentLength); + + client->printf("%s 200 OK\r\nContent-Type: %s", httpVersionToString(version), contentType); + + if(contentLength) + { + client->printf("\r\nContent-Length: %d", contentLength); + } + + client->print("\r\n\r\n"); } void setWWWDir(const char *WWWDir) @@ -601,8 +609,9 @@ class WEBServer : public TCPServer, public HttpConstants Serial.println(pageToSend.name()); #endif - if(pageToSend.isDirectory()) //To DO : List the files present in the directory + if(pageToSend.isDirectory()) //TODO : List the files present in the directory { + //sendDirectoryListing(client, pageToSend); //Sends the content of the directory like what apache does by default. CRASHING FOR NOW, needs to be checked further. pageToSend.close(); sendInfoResponse(HTTP_CODE::HTTP_CODE_FORBIDDEN, client, "The file you want to access is a folder"); @@ -691,8 +700,47 @@ class WEBServer : public TCPServer, public HttpConstants return true; } - /*Static helper methods*/ + void sendDirectoryListing(T *client, File& pageToSend) + { + /*sendHTTPHeader(&client->_client, HttpConstants::httpMIMETypeToString(HttpConstants::TEXT_HTML)); + client->_client.printf_P(PSTR( "\r\n\ + \r\n\ + \r\n\ + Index of %s\r\n\ + \r\n\ + \r\n\ +

Index of %s

\r\n\ + \r\n\ + \r\n\ + \r\n") + , client->_httpRequestData.httpResource, client->_httpRequestData.httpResource);*/ + //File nextFile; + for (;;) + { + //if(!(nextFile = pageToSend.openNextFile()))break; + File nextFile = pageToSend.openNextFile(); + if (!nextFile) //No more files in the directory + break; + //time_t creationTime = nextFile.getCreationTime(), lastModified = nextFile.getLastWrite(); + + /*client->_client.printf_P(PSTR("\r\n"), + nextFile.name(), + nextFile.name(), + nextFile.size() + );*/ + Serial.printf("%s %u\r\n", nextFile.name(), nextFile.size()); + nextFile.close(); + } + + /*client->_client.printf_P(PSTR( "\r\n\ +
NameCreatedLast modifiedSize

%s2021-07-09 15:37 2021-07-09 15:37 %u

\r\n\ +
SAB WEBServer, Version %s at %s Port %u
\r\n\ + \r\n\ + "), "1.0.0", "", TCPServer::getPort()); //TODO get IP*/ + } + /*Static helper methods*/ + static void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message) { char codeLiteral[100]; @@ -752,6 +800,7 @@ class WEBServer : public TCPServer, public HttpConstants else if(strcmp(extension, "js") == 0) return TEXT_JAVASCRIPT; else if(strcmp(extension, "png") == 0) return IMAGE_PNG; else if(strcmp(extension, "jpg") == 0) return IMAGE_JPEG; + else if(strcmp(extension, "ico") == 0) return IMAGE_X_ICO; else if(strcmp(extension, "mp3") == 0) return AUDIO_MPEG; else return UNKNOWN_MIME; }