118 lines
4.3 KiB
C++
118 lines
4.3 KiB
C++
#include <ESPAsyncWebServer.h>
|
|
#include <FS.h>
|
|
|
|
#define FLASH_TEXT(name) const char *name
|
|
|
|
namespace webdav_constants {
|
|
FLASH_TEXT(MULTISTATUS_START) = "<?xml version=\"1.0\" ?><D:multistatus xmlns:D=\"DAV:\">";
|
|
FLASH_TEXT(MULTISTATUS_END) = "</D:multistatus>";
|
|
FLASH_TEXT(RESPONSE_START) = "<D:response>";
|
|
FLASH_TEXT(RESPONSE_END) = "</D:response>";
|
|
FLASH_TEXT(HREF_START) = "<D:href>";
|
|
FLASH_TEXT(HREF_END) = "</D:href>";
|
|
FLASH_TEXT(PROPSTAT_START) = "<D:propstat>";
|
|
FLASH_TEXT(PROPSTAT_END) = "</D:propstat>";
|
|
FLASH_TEXT(PROP_START) = "<D:prop>";
|
|
FLASH_TEXT(PROP_END) = "</D:prop>";
|
|
FLASH_TEXT(RESOURCETYPE_START) = "<D:resourcetype>";
|
|
FLASH_TEXT(RESOURCETYPE_END) = "</D:resourcetype>";
|
|
FLASH_TEXT(RESOURCE_COLLECTION) = "<D:collection/>";
|
|
|
|
FLASH_TEXT(CONTENTLEN_START) = "<D:getcontentlength>";
|
|
FLASH_TEXT(CONTENTLEN_END) = "</D:getcontentlength>";
|
|
FLASH_TEXT(CREATEDATE_START) = "<D:creationdate>";
|
|
FLASH_TEXT(CREATEDATE_END) = "</D:creationdate>";
|
|
FLASH_TEXT(MODDATE_START) = "<D:getlastmodified>";
|
|
FLASH_TEXT(MODDATE_END) = "</D:getlastmodified>";
|
|
FLASH_TEXT(STATUS_OK) = "<D:status>HTTP/1.1 200 OK</D:status>";
|
|
}
|
|
|
|
void listFiles(AsyncResponseStream * response, const char *folderPath, Dir * dir)
|
|
{
|
|
using namespace webdav_constants;
|
|
response->println(MULTISTATUS_START);
|
|
dir->rewind();
|
|
while (dir->next()) {
|
|
response->print(RESPONSE_START);
|
|
response->print(HREF_START);
|
|
response->print(folderPath);
|
|
response->print(dir->fileName());
|
|
response->print(HREF_END);
|
|
response->print(PROPSTAT_START);
|
|
response->print(PROP_START);
|
|
if (dir->isDirectory()) {
|
|
response->print(RESOURCETYPE_START);
|
|
response->print(RESOURCE_COLLECTION);
|
|
response->print(RESOURCETYPE_END);
|
|
} else {
|
|
response->print(CONTENTLEN_START);
|
|
response->print(dir->fileSize(), DEC);
|
|
response->print(CONTENTLEN_END);
|
|
}
|
|
|
|
response->print(PROP_END);
|
|
response->print(STATUS_OK);
|
|
response->print(PROPSTAT_END);
|
|
response->print(webdav_constants::RESPONSE_END);
|
|
}
|
|
response->println(MULTISTATUS_END);
|
|
}
|
|
|
|
|
|
class SpiffsWebDavHandler : public AsyncWebHandler
|
|
{
|
|
public:
|
|
SpiffsWebDavHandler(const String & prefix, const String & folder)
|
|
: prefix_(prefix), folder_(folder)
|
|
{}
|
|
|
|
virtual bool canHandle(AsyncWebServerRequest *request) override final
|
|
{
|
|
Serial.print("Can handle for url : ");
|
|
Serial.println(request->url());
|
|
return request->url().startsWith(prefix_);
|
|
}
|
|
|
|
virtual void handleRequest(AsyncWebServerRequest *request) override final
|
|
{
|
|
if (request->url() == prefix_ && request->method() == HTTP_PROPFIND) {
|
|
Serial.println("Propfind start");
|
|
AsyncResponseStream * response = request->beginResponseStream("application/xml");
|
|
Dir dir = SPIFFS.openDir(folder_);
|
|
listFiles(response, "/", &dir);
|
|
request->send(response);
|
|
} else if(request->url() == prefix_ && request->method() == HTTP_GET) {
|
|
AsyncResponseStream * response = request->beginResponseStream("text/plain");
|
|
Dir dir = SPIFFS.openDir(folder_);
|
|
Serial.print("Opening folder ");
|
|
Serial.println(folder_);
|
|
while (dir.next()) {
|
|
Serial.print(" File: ");
|
|
Serial.println(dir.fileName());
|
|
response->println(dir.fileName());
|
|
}
|
|
request->send(response);
|
|
}
|
|
else if (request->method() == HTTP_GET) {
|
|
auto path = folder_ + request->url().substring(prefix_.length());
|
|
Serial.print("Testing if path exists: ");
|
|
Serial.println(path);
|
|
if (SPIFFS.exists(path)) {
|
|
Serial.println("Exists!");
|
|
request->send(SPIFFS, path, "application/x-msgpack");
|
|
} else {
|
|
Serial.println("Does not exist :(");
|
|
request->send(404, "text/plain", "Webdav: File not found");
|
|
}
|
|
} else {
|
|
request->send(404, "text/plain", "Webdav: Invalid request");
|
|
}
|
|
}
|
|
virtual bool isRequestHandlerTrivial() override final {return false;}
|
|
|
|
private:
|
|
String prefix_;
|
|
String folder_;
|
|
};
|
|
|