Started to add a method listing the files in a directory if the rsrc asked is a folder,like apache2 does. Not finished yet

This commit is contained in:
anschrammh 2022-04-22 08:05:16 +02:00
parent 8c6dd39bd7
commit 9d831f11d9

View File

@ -55,11 +55,19 @@ class WEBServer : public TCPServer<T>, 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<T>, 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,6 +700,45 @@ class WEBServer : public TCPServer<T>, public HttpConstants
return true;
}
void sendDirectoryListing(T *client, File& pageToSend)
{
/*sendHTTPHeader(&client->_client, HttpConstants::httpMIMETypeToString(HttpConstants::TEXT_HTML));
client->_client.printf_P(PSTR( "<!DOCTYPE HTML>\r\n\
<html>\r\n\
<head>\r\n\
<title>Index of %s</title>\r\n\
</head>\r\n\
<body>\r\n\
<h1>Index of %s</h1>\r\n\
<table>\r\n\
<tr><th>Name</th><th>Created</th><th>Last modified</th><th>Size</th></tr>\r\n\
<tr><th colspan=\"4\"><hr></th></tr>\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("<tr><td><a href=\"%s\">%s</a></td><td align=\"right\">2021-07-09 15:37 </td><td align=\"right\">2021-07-09 15:37 </td><td align=\"right\"> %u</td></tr>\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( "<tr><th colspan=\"4\"><hr></th></tr>\r\n\
</table>\r\n\
<address>SAB WEBServer, Version %s at %s Port %u</address>\r\n\
</body>\r\n\
</html>"), "1.0.0", "", TCPServer<T>::getPort()); //TODO get IP*/
}
/*Static helper methods*/
static void sendInfoResponse(HTTP_CODE http_code, T *client, const char *message)
@ -752,6 +800,7 @@ class WEBServer : public TCPServer<T>, 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;
}