swimtracker-firmware/lib/webdav/AsyncWebDav.h

131 lines
4.9 KiB
C
Raw Normal View History

2019-09-10 22:05:28 +02:00
#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/>";
2020-05-16 12:33:53 +02:00
FLASH_TEXT(HTTP_204_NO_CONTENT) = "HTTP/1.1 204 No Content";
2019-09-10 22:05:28 +02:00
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);
2020-05-16 12:33:53 +02:00
Serial.println("Before rewind");
2019-09-10 22:05:28 +02:00
dir->rewind();
2020-05-16 12:33:53 +02:00
Serial.println("After rewind");
2019-09-10 22:05:28 +02:00
while (dir->next()) {
2020-05-16 12:33:53 +02:00
Serial.println("Inside dir loop");
Serial.println(folderPath);
Serial.println(dir->fileName());
2019-09-10 22:05:28 +02:00
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);
2020-05-16 12:33:53 +02:00
response->println(webdav_constants::RESPONSE_END);
2019-09-10 22:05:28 +02:00
}
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
{
2020-05-16 12:33:53 +02:00
if (request->url() == prefix_ + "/" && request->method() == HTTP_PROPFIND) {
2019-09-10 22:05:28 +02:00
Serial.println("Propfind start");
AsyncResponseStream * response = request->beginResponseStream("application/xml");
Dir dir = SPIFFS.openDir(folder_);
listFiles(response, "/", &dir);
request->send(response);
2020-05-16 12:33:53 +02:00
} else if(request->url() == prefix_ + "/" && request->method() == HTTP_GET) {
AsyncResponseStream * response = request->beginResponseStream("text/plain", 1460*10);
2019-09-10 22:05:28 +02:00
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());
if (SPIFFS.exists(path)) {
request->send(SPIFFS, path, "application/x-msgpack");
} else {
request->send(404, "text/plain", "Webdav: File not found");
}
2020-05-16 12:33:53 +02:00
} else if (request->method() == HTTP_DELETE) {
auto path = folder_ + request->url().substring(prefix_.length());
if (SPIFFS.exists(path)) {
if(SPIFFS.remove(path))
request->send(204, "text/plain", "Success");
else
request->send(404, "text/plain", "Webdav: Invalid delete request");
} else {
request->send(404, "text/plain", "Webdav: File to delete not found");
}
2019-09-10 22:05:28 +02:00
} else {
request->send(404, "text/plain", "Webdav: Invalid request");
}
}
virtual bool isRequestHandlerTrivial() override final {return false;}
private:
String prefix_;
String folder_;
};