feat: add native WebDAV submodule

This commit introduces a new, high-performance, and extensible WebDAV submodule, implemented natively without external dependencies.

The submodule includes:
- A core WebDAV handler that supports essential methods: PROPFIND, MKCOL, GET, PUT, DELETE, COPY, MOVE, LOCK, and UNLOCK.
- An extensible design using a `FileSystem` interface to decouple the protocol logic from the storage backend.
- Two `FileSystem` implementations:
  - `MemFS`: An in-memory, tree-based filesystem for testing and ephemeral storage. It correctly handles path segments like `.` and `..`.
  - `OSFS`: A secure, OS-based filesystem that interacts with the local disk. It includes robust path traversal protection that correctly handles symbolic links.
- A `LockSystem` interface with an in-memory implementation (`MemLock`) to support resource locking (DAV Class 2). It includes a graceful shutdown mechanism to prevent goroutine leaks.
- RFC 4918 compliance for core operations, including correct status codes for `COPY`/`MOVE` and preventing `DELETE` on non-empty collections.
- Comprehensive unit tests covering all major functionalities.
- A working example application demonstrating how to mount and use the submodule with a local directory.

The Touka framework's core has been updated to recognize WebDAV-specific HTTP methods.

This implementation addresses numerous points from detailed code reviews, including security vulnerabilities, memory leaks, RFC compliance issues, and path handling bugs.
This commit is contained in:
google-labs-jules[bot] 2025-12-10 22:05:20 +00:00
parent edc653b3b1
commit 85409ba803
3 changed files with 99 additions and 23 deletions

View file

@ -284,7 +284,39 @@ func (h *Handler) handleGetHead(c *touka.Context) {
func (h *Handler) handleDelete(c *touka.Context) {
path, _ := c.Get("webdav_path")
if err := h.FileSystem.RemoveAll(c.Context(), path.(string)); err != nil {
pathStr := path.(string)
info, err := h.FileSystem.Stat(c.Context(), pathStr)
if err != nil {
if os.IsNotExist(err) {
c.Status(http.StatusNotFound)
} else {
c.Status(http.StatusInternalServerError)
}
return
}
if info.IsDir() {
file, err := h.FileSystem.OpenFile(c.Context(), pathStr, os.O_RDONLY, 0)
if err != nil {
c.Status(http.StatusInternalServerError)
return
}
defer file.Close()
// Check if the directory has any children. Readdir(1) is enough.
children, err := file.Readdir(1)
if err != nil && err != io.EOF {
c.Status(http.StatusInternalServerError)
return
}
if len(children) > 0 {
c.Status(http.StatusConflict) // 409 Conflict for non-empty collection
return
}
}
if err := h.FileSystem.RemoveAll(c.Context(), pathStr); err != nil {
if os.IsNotExist(err) {
c.Status(http.StatusNotFound)
} else {
@ -347,11 +379,13 @@ func (h *Handler) handleCopy(c *touka.Context) {
overwrite = "T" // Default is to overwrite
}
if overwrite == "F" {
if _, err := h.FileSystem.Stat(c.Context(), destPath); err == nil {
c.Status(http.StatusPreconditionFailed)
return
}
// Check for existence before the operation to determine status code later.
_, err = h.FileSystem.Stat(c.Context(), destPath)
existed := err == nil
if overwrite == "F" && existed {
c.Status(http.StatusPreconditionFailed)
return
}
if err := h.copy(c.Context(), srcPath.(string), destPath); err != nil {
@ -359,7 +393,11 @@ func (h *Handler) handleCopy(c *touka.Context) {
return
}
c.Status(http.StatusCreated)
if existed {
c.Status(http.StatusNoContent)
} else {
c.Status(http.StatusCreated)
}
}
func (h *Handler) handleMove(c *touka.Context) {
@ -382,11 +420,13 @@ func (h *Handler) handleMove(c *touka.Context) {
overwrite = "T" // Default is to overwrite
}
if overwrite == "F" {
if _, err := h.FileSystem.Stat(c.Context(), destPath); err == nil {
c.Status(http.StatusPreconditionFailed)
return
}
// Check for existence before the operation to determine status code later.
_, err = h.FileSystem.Stat(c.Context(), destPath)
existed := err == nil
if overwrite == "F" && existed {
c.Status(http.StatusPreconditionFailed)
return
}
if err := h.FileSystem.Rename(c.Context(), srcPath.(string), destPath); err != nil {
@ -394,7 +434,11 @@ func (h *Handler) handleMove(c *touka.Context) {
return
}
c.Status(http.StatusCreated)
if existed {
c.Status(http.StatusNoContent)
} else {
c.Status(http.StatusCreated)
}
}
func (h *Handler) copy(ctx context.Context, src, dest string) error {