Layer: L7 Application ยท Build flags: PC_ENABLE_WEBDAV
What this example teaches
WebDAV (RFC 4918) extends HTTP with file-management methods, so a remote client can mount and edit the device's filesystem like a network drive. server.dav(url_prefix, fs, fs_root) mounts a LittleFS subtree as a WebDAV share in one call: here /dav on disk is exposed at the URL /dav.
One call mounts the share:
server.dav("/dav", LittleFS, "/dav");
Supported methods: OPTIONS, PROPFIND (Depth 0/1), PROPPATCH, GET, HEAD, PUT, DELETE, MKCOL, COPY (files + collections), MOVE, and advisory LOCK/UNLOCK. PROPPATCH is answered 207 Multi-Status with each requested property refused 403 Forbidden (the properties are read-only) - this keeps Windows Explorer and macOS Finder, which PROPPATCH a timestamp right after a PUT, from erroring on a 405. PUT streams the body straight to the file (so an upload is not bounded by BODY_BUF_SIZE) and the locks are advisory only.
Security note. A writable share is dangerous on an open network: add per-route auth (Foundation/Sysadmin), HTTPS (L4-Transport/HTTPS), and the per-IP throttle (L4-Transport/PerIpThrottle) before exposing it. The sketch seeds one file so a fresh share is not empty.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_WEBDAV=1 -DMAX_CONNS=3 -DMAX_WS_CONNS=1 -DMAX_SSE_CONNS=1 -DMAX_ROUTES=8 -DPC_WEBDAV_MAX_ENTRIES=8 -DPC_WEBDAV_BUF_SIZE=1024" \
--lib="." examples/L7-Application/WebDav/WebDav.ino
curl -X PROPFIND -H "Depth: 1" http://<ip>/dav/ # list
curl -T file.txt http://<ip>/dav/file.txt # upload (PUT)
curl -X PROPPATCH --data-binary @patch.xml http://<ip>/dav/file.txt # 207 (props refused 403)
curl -X MKCOL http://<ip>/dav/sub # make a collection
curl -X MOVE -H "Destination: /dav/b.txt" http://<ip>/dav/file.txt
curl -X DELETE http://<ip>/dav/b.txt
rclone lsd :webdav: --webdav-url http://<ip>/dav --webdav-vendor other
Annotated source
The complete sketch (WebDav.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_WEBDAV 1
#include <LittleFS.h>
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void setup()
{
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
if (!LittleFS.begin(true))
{
Serial.println("LittleFS mount failed");
return;
}
LittleFS.mkdir("/dav");
File f = LittleFS.open("/dav/hello.txt", "w");
if (f)
{
f.print("hello from ProtoCore\n");
f.close();
}
server.dav("/dav", LittleFS, "/dav");
Serial.println("WebDAV share at http://<ip>/dav");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.