ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
webdav.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_WEBDAV_H
5#define PROTOCORE_WEBDAV_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
9EMBED_BEGIN_DECLS
10
11/**
12 * @file webdav.h
13 * @brief WebDAV wire format (RFC 4918): method classification, header parsing,
14and the 207 Multi-Status XML builder.
15 *
16 * Mirrors the CoAP/SNMP split: this header declares the pure, host-testable core
17 * (no sockets, no filesystem - unit-tested in env:native_webdav). The
18 * filesystem-backed request handling (PROPFIND directory walk, PUT/MKCOL/DELETE/
19 * COPY/MOVE, GET via the file-serving path) lives in server/io/webdav_handler.h
20 * and runs only on a build with a real filesystem.
21 *
22 * Scope: class 1 (PROPFIND Depth 0/1, PROPPATCH, PUT, DELETE, MKCOL, COPY, MOVE)
23 * plus OPTIONS and class 2 LOCK/UNLOCK, now enforced by a small lock table (see
24 * the lock manager below): a locked resource rejects a write that does not present
25 * the matching token in its If header (423 Locked). PROPPATCH is answered 207 with every requested property refused 403
26 * (read-only live properties, no dead-property store). The filesystem-backed
27 * handler streams a PUT body straight to the file (PC's stream-body
28 * hook), so uploads are not bounded by BODY_BUF_SIZE.
29 *
30 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
31 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
32 * a caller drives every namespace the same way.
33 */
34
35// PROTOCORE_WEBDAV_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
36// it into its arena. Its size and its offset are each a static_assert, so a feature
37// combination that does not fit fails to compile rather than overrunning at run time.
38
39/** @brief Depth: infinity sentinel (a lone constant). */
40#define PROTOCORE_DAV_DEPTH_INFINITY 0x7fffffff
41
42/** @brief Maximum concurrent locks (fixed - a small structural bound, not a per-board tunable). */
43#define PROTOCORE_DAV_LOCK_MAX 8
44
45/** @brief Maximum locked-path length, including the NUL. */
46#define PROTOCORE_DAV_LOCK_PATH_MAX 128
47
48/** @brief Maximum lock-token length, including the NUL (e.g. "opaquelocktoken:xxxxxxxx-pc"). */
49#define PROTOCORE_DAV_LOCK_TOKEN_MAX 48
50
51/** @brief WebDAV request methods recognized by the server. */
52EMBED_ENUM_BYTE(WebDavMethod, DAV_M_OPTIONS, DAV_M_GET, DAV_M_HEAD, DAV_M_PUT, DAV_M_DELETE, DAV_M_PROPFIND,
53 DAV_M_PROPPATCH, DAV_M_MKCOL, DAV_M_COPY, DAV_M_MOVE, DAV_M_LOCK, DAV_M_UNLOCK,
54 DAV_M_UNSUPPORTED ///< Anything else - answered 405 Method Not Allowed.
55);
56
57/** @brief One active lock (RFC 4918 §6.4). */
58typedef struct
59{
60 char path[PROTOCORE_DAV_LOCK_PATH_MAX]; ///< the locked resource path (trailing slash normalized off)
61 char token[PROTOCORE_DAV_LOCK_TOKEN_MAX]; ///< the lock token (an opaquelocktoken URI)
62 embed_bool exclusive; ///< exclusive-write (true) or shared (false)
63 embed_bool depth_infinity; ///< the lock covers the whole subtree (Depth: infinity) vs just the resource
64 embed_bool active; ///< false = free slot
65 uint32_t expiry_s; ///< monotonic second the lock expires (0 = no timeout); swept by _sweep
66} DavLock;
67
68/** @brief The server-global lock table (one instance, not per-connection). */
73
74/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
75typedef struct
76{
77 WebDavMethod (*method)(const uint8_t *, const char *);
78 int (*depth)(const uint8_t *, const char *, int);
79 size_t (*xml_escape)(const uint8_t *, char *, size_t, const char *);
80 embed_bool (*dest_path)(const uint8_t *, const char *, char *, size_t);
81 size_t (*ms_begin)(const uint8_t *, char *, size_t, size_t);
82 size_t (*ms_entry)(const uint8_t *, char *, size_t, size_t, const char *, embed_bool, uint32_t, const char *,
83 const char *);
84 size_t (*ms_end)(const uint8_t *, char *, size_t, size_t);
85 size_t (*proppatch_ms)(const uint8_t *, char *, size_t, const char *, const char *, size_t);
86 void (*lock_init)(const uint8_t *, DavLockTable *);
87 const DavLock *(*lock_acquire)(const uint8_t *, DavLockTable *, const char *, const char *, embed_bool, embed_bool,
88 uint32_t);
89 size_t (*lock_sweep)(const uint8_t *, DavLockTable *, uint32_t);
90 const DavLock *(*lock_refresh)(const uint8_t *, DavLockTable *, const char *, uint32_t);
91 const DavLock *(*lock_find)(const uint8_t *, const DavLockTable *, const char *);
92 embed_bool (*lock_release)(const uint8_t *, DavLockTable *, const char *);
93 embed_bool (*lock_can_write)(const uint8_t *, const DavLockTable *, const char *, const char *);
94 embed_bool (*if_token)(const uint8_t *, const char *, char *, size_t);
95} WebdavNs;
96PROTOCORE_NS_LAYOUT(WebdavNs, method, depth, xml_escape, dest_path, ms_begin, ms_entry, ms_end, proppatch_ms, lock_init,
97 lock_acquire, lock_sweep, lock_refresh, lock_find, lock_release, lock_can_write, if_token);
98
99/**
100 * @brief Classify an HTTP method token (e.g. "PROPFIND") into a WebDavMethod.
101 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
102 * @param m M
103 * @return The WebDavMethod.
104 */
105WebDavMethod protocore_webdav_method(const uint8_t *work, const char *m);
106/**
107 * @brief Parse a Depth header value ("0", "1", or "infinity").
108 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
109 * @param depth_hdr Depth hdr
110 * @param dflt Dflt
111 * @return The int.
112 */
113int protocore_webdav_depth(const uint8_t *work, const char *depth_hdr, int dflt);
114/**
115 * @brief XML-escape src into dst (`&`, `<`, `>`, `"`, `'`).
116 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
117 * @param dst Dst
118 * @param cap Cap
119 * @param src Src
120 * @return The size_t.
121 */
122size_t protocore_webdav_xml_escape(const uint8_t *work, char *dst, size_t cap, const char *src);
123/**
124 * @brief Extract and percent-decode the path of a Destination header. .
125 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
126 * @param destination Destination
127 * @param out Out
128 * @param cap Cap
129 * @return EMBED_TRUE on success.
130 */
131embed_bool protocore_webdav_dest_path(const uint8_t *work, const char *destination, char *out, size_t cap);
132/**
133 * @brief Write the XML prolog and the open <multistatus> element.
134 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
135 * @param buf Buf
136 * @param cap Cap
137 * @param len Len
138 * @return The size_t.
139 */
140size_t protocore_webdav_ms_begin(const uint8_t *work, char *buf, size_t cap, size_t len);
141/**
142 * @brief Append one <response> describing a resource.
143 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
144 * @param buf Buf
145 * @param cap Cap
146 * @param len Len
147 * @param href the resource's URL path (XML-escaped here)
148 * @param is_collection true for a directory (emits <collection/>)
149 * @param size content length (files only)
150 * @param rfc1123_mtime Last-Modified string, or "" to omit
151 * @param content_type MIME type (files only), or "" to omit
152 * @return The size_t.
153 */
154size_t protocore_webdav_ms_entry(const uint8_t *work, char *buf, size_t cap, size_t len, const char *href,
155 embed_bool is_collection, uint32_t size, const char *rfc1123_mtime,
156 const char *content_type);
157/**
158 * @brief Close the <multistatus> element.
159 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
160 * @param buf Buf
161 * @param cap Cap
162 * @param len Len
163 * @return The size_t.
164 */
165size_t protocore_webdav_ms_end(const uint8_t *work, char *buf, size_t cap, size_t len);
166/**
167 * @brief Build a complete 207 Multi-Status body answering a PROPPATCH. The .
168 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
169 * @param buf destination buffer (whole document, NUL-terminated)
170 * @param cap buffer capacity
171 * @param href the resource path (XML-escaped here)
172 * @param body the PROPPATCH request body (not required to be NUL-terminated)
173 * @param body_len length of body
174 * @return The size_t.
175 */
176size_t protocore_webdav_proppatch_ms(const uint8_t *work, char *buf, size_t cap, const char *href, const char *body,
177 size_t body_len);
178/**
179 * @brief Reset a lock table (no locks held).
180 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
181 * @param t T
182 */
183void protocore_webdav_lock_init(const uint8_t *work, DavLockTable *t);
184/**
185 * @brief Acquire a lock on path with the caller-supplied token (RFC 4918 .
186 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
187 * @param t T
188 * @param path Path
189 * @param token Token
190 * @param exclusive Exclusive
191 * @param depth_infinity Depth infinity
192 * @param expiry_s the monotonic second the lock expires (0 = no timeout); protocore_dav_lock_sweep drops a
193 * @return The const DavLock *.
194 */
195const DavLock *protocore_webdav_lock_acquire(const uint8_t *work, DavLockTable *t, const char *path, const char *token,
196 embed_bool exclusive, embed_bool depth_infinity, uint32_t expiry_s);
197/**
198 * @brief Expire and drop every lock whose timeout passed (RFC 4918 .
199 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
200 * @param t T
201 * @param now_s the caller's current monotonic second
202 * @return The size_t.
203 */
204size_t protocore_webdav_lock_sweep(const uint8_t *work, DavLockTable *t, uint32_t now_s);
205/**
206 * @brief Refresh a held lock's timeout to new_expiry_s, keyed by token (a .
207 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
208 * @param t T
209 * @param token Token
210 * @param new_expiry_s New expiry s
211 * @return The const DavLock *.
212 */
213const DavLock *protocore_webdav_lock_refresh(const uint8_t *work, DavLockTable *t, const char *token,
214 uint32_t new_expiry_s);
215/**
216 * @brief Find a lock covering path: one on path itself, or a Depth-infinity .
217 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
218 * @param t T
219 * @param path Path
220 * @return The const DavLock *.
221 */
222const DavLock *protocore_webdav_lock_find(const uint8_t *work, const DavLockTable *t, const char *path);
223/**
224 * @brief Release the lock whose token equals token (UNLOCK). true if one was .
225 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
226 * @param t T
227 * @param token Token
228 * @return EMBED_TRUE on success.
229 */
230embed_bool protocore_webdav_lock_release(const uint8_t *work, DavLockTable *t, const char *token);
231/**
232 * @brief May a write to path proceed given the token the request presented .
233 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
234 * @param t T
235 * @param path Path
236 * @param presented_token Presented token
237 * @return EMBED_TRUE on success.
238 */
239embed_bool protocore_webdav_lock_can_write(const uint8_t *work, const DavLockTable *t, const char *path,
240 const char *presented_token);
241/**
242 * @brief Extract the first lock token from an If header value (RFC 4918 .
243 * @param work PROTOCORE_WEBDAV_BORROW bytes the caller took. Not held past the call.
244 * @param if_header If header
245 * @param out Out
246 * @param cap Cap
247 * @return EMBED_TRUE on success.
248 */
249embed_bool protocore_webdav_if_token(const uint8_t *work, const char *if_header, char *out, size_t cap);
250
251/** @brief Module namespace. */
253 .depth = protocore_webdav_depth,
254 .xml_escape = protocore_webdav_xml_escape,
255 .dest_path = protocore_webdav_dest_path,
256 .ms_begin = protocore_webdav_ms_begin,
257 .ms_entry = protocore_webdav_ms_entry,
258 .ms_end = protocore_webdav_ms_end,
259 .proppatch_ms = protocore_webdav_proppatch_ms,
260 .lock_init = protocore_webdav_lock_init,
261 .lock_acquire = protocore_webdav_lock_acquire,
262 .lock_sweep = protocore_webdav_lock_sweep,
263 .lock_refresh = protocore_webdav_lock_refresh,
264 .lock_find = protocore_webdav_lock_find,
265 .lock_release = protocore_webdav_lock_release,
266 .lock_can_write = protocore_webdav_lock_can_write,
267 .if_token = protocore_webdav_if_token};
268
269EMBED_END_DECLS
270
271#endif // PROTOCORE_WEBDAV_H
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
The server-global lock table (one instance, not per-connection).
Definition webdav.h:70
DavLock locks[PROTOCORE_DAV_LOCK_MAX]
Definition webdav.h:71
One active lock (RFC 4918 §6.4).
Definition webdav.h:59
embed_bool exclusive
exclusive-write (true) or shared (false)
Definition webdav.h:62
uint32_t expiry_s
monotonic second the lock expires (0 = no timeout); swept by _sweep
Definition webdav.h:65
embed_bool active
false = free slot
Definition webdav.h:64
embed_bool depth_infinity
the lock covers the whole subtree (Depth: infinity) vs just the resource
Definition webdav.h:63
char path[PROTOCORE_DAV_LOCK_PATH_MAX]
the locked resource path (trailing slash normalized off)
Definition webdav.h:60
char token[PROTOCORE_DAV_LOCK_TOKEN_MAX]
the lock token (an opaquelocktoken URI)
Definition webdav.h:61
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition webdav.h:76
size_t(* ms_entry)(const uint8_t *, char *, size_t, size_t, const char *, embed_bool, uint32_t, const char *, const char *)
Definition webdav.h:82
int(* depth)(const uint8_t *, const char *, int)
Definition webdav.h:78
size_t(* ms_end)(const uint8_t *, char *, size_t, size_t)
Definition webdav.h:84
embed_bool(* dest_path)(const uint8_t *, const char *, char *, size_t)
Definition webdav.h:80
size_t(* proppatch_ms)(const uint8_t *, char *, size_t, const char *, const char *, size_t)
Definition webdav.h:85
size_t(* xml_escape)(const uint8_t *, char *, size_t, const char *)
Definition webdav.h:79
embed_bool(* if_token)(const uint8_t *, const char *, char *, size_t)
Definition webdav.h:94
embed_bool(* lock_can_write)(const uint8_t *, const DavLockTable *, const char *, const char *)
Definition webdav.h:93
WebDavMethod(* method)(const uint8_t *, const char *)
Definition webdav.h:77
void(* lock_init)(const uint8_t *, DavLockTable *)
Definition webdav.h:86
embed_bool(* lock_release)(const uint8_t *, DavLockTable *, const char *)
Definition webdav.h:92
size_t(* ms_begin)(const uint8_t *, char *, size_t, size_t)
Definition webdav.h:81
size_t(* lock_sweep)(const uint8_t *, DavLockTable *, uint32_t)
Definition webdav.h:89
size_t protocore_webdav_lock_sweep(const uint8_t *work, DavLockTable *t, uint32_t now_s)
Expire and drop every lock whose timeout passed (RFC 4918 .
size_t protocore_webdav_ms_end(const uint8_t *work, char *buf, size_t cap, size_t len)
Close the <multistatus> element.
const DavLock * protocore_webdav_lock_find(const uint8_t *work, const DavLockTable *t, const char *path)
Find a lock covering path: one on path itself, or a Depth-infinity .
const DavLock * protocore_webdav_lock_refresh(const uint8_t *work, DavLockTable *t, const char *token, uint32_t new_expiry_s)
Refresh a held lock's timeout to new_expiry_s, keyed by token (a .
int protocore_webdav_depth(const uint8_t *work, const char *depth_hdr, int dflt)
Parse a Depth header value ("0", "1", or "infinity").
size_t protocore_webdav_ms_begin(const uint8_t *work, char *buf, size_t cap, size_t len)
Write the XML prolog and the open <multistatus> element.
#define PROTOCORE_DAV_LOCK_PATH_MAX
Maximum locked-path length, including the NUL.
Definition webdav.h:46
embed_bool protocore_webdav_lock_can_write(const uint8_t *work, const DavLockTable *t, const char *path, const char *presented_token)
May a write to path proceed given the token the request presented .
WebDavMethod protocore_webdav_method(const uint8_t *work, const char *m)
Classify an HTTP method token (e.g. "PROPFIND") into a WebDavMethod.
embed_bool protocore_webdav_if_token(const uint8_t *work, const char *if_header, char *out, size_t cap)
Extract the first lock token from an If header value (RFC 4918 .
embed_bool protocore_webdav_dest_path(const uint8_t *work, const char *destination, char *out, size_t cap)
Extract and percent-decode the path of a Destination header. .
#define PROTOCORE_DAV_LOCK_TOKEN_MAX
Maximum lock-token length, including the NUL (e.g. "opaquelocktoken:xxxxxxxx-pc").
Definition webdav.h:49
size_t protocore_webdav_xml_escape(const uint8_t *work, char *dst, size_t cap, const char *src)
XML-escape src into dst (&, <, >, ", ').
EMBED_ENUM_BYTE(WebDavMethod, DAV_M_OPTIONS, DAV_M_GET, DAV_M_HEAD, DAV_M_PUT, DAV_M_DELETE, DAV_M_PROPFIND, DAV_M_PROPPATCH, DAV_M_MKCOL, DAV_M_COPY, DAV_M_MOVE, DAV_M_LOCK, DAV_M_UNLOCK, DAV_M_UNSUPPORTED)
WebDAV request methods recognized by the server.
#define PROTOCORE_DAV_LOCK_MAX
Maximum concurrent locks (fixed - a small structural bound, not a per-board tunable).
Definition webdav.h:43
size_t protocore_webdav_proppatch_ms(const uint8_t *work, char *buf, size_t cap, const char *href, const char *body, size_t body_len)
Build a complete 207 Multi-Status body answering a PROPPATCH. The .
void protocore_webdav_lock_init(const uint8_t *work, DavLockTable *t)
Reset a lock table (no locks held).
size_t protocore_webdav_ms_entry(const uint8_t *work, char *buf, size_t cap, size_t len, const char *href, embed_bool is_collection, uint32_t size, const char *rfc1123_mtime, const char *content_type)
Append one <response> describing a resource.
embed_bool protocore_webdav_lock_release(const uint8_t *work, DavLockTable *t, const char *token)
Release the lock whose token equals token (UNLOCK). true if one was .
const DavLock * protocore_webdav_lock_acquire(const uint8_t *work, DavLockTable *t, const char *path, const char *token, embed_bool exclusive, embed_bool depth_infinity, uint32_t expiry_s)
Acquire a lock on path with the caller-supplied token (RFC 4918 .