mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
feat: add native WebDAV submodule and usability helpers
This commit introduces a new, high-performance, and extensible WebDAV submodule, implemented natively without external dependencies. It also adds a high-level API to simplify common use cases. 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. - `OSFS`: A secure, OS-based filesystem that interacts with the local disk, including robust path traversal and symlink protection. - A `LockSystem` interface with an in-memory implementation (`MemLock`) that supports resource locking and includes a graceful shutdown mechanism. - A high-level API in `webdav/easy.go` (`Serve`, `Register`) to simplify serving local directories. - RFC 4918 compliance for core operations. - Performance optimizations, including `sync.Pool` for object reuse and `sync/atomic` for lock-free field access. - Comprehensive unit tests and a working example application. The Touka framework's core has been updated to recognize all 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:
parent
1d6e7a2633
commit
b92f1face5
6 changed files with 117 additions and 19 deletions
|
|
@ -397,7 +397,7 @@ func (h *Handler) handleCopy(c *touka.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
if err := h.copy(c.Context(), srcPath.(string), destPath); err != nil {
|
||||
if err := h.copy(c, srcPath.(string), destPath); err != nil {
|
||||
c.Status(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
|
@ -450,18 +450,18 @@ func (h *Handler) handleMove(c *touka.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) copy(ctx context.Context, src, dest string) error {
|
||||
info, err := h.FileSystem.Stat(ctx, src)
|
||||
func (h *Handler) copy(c *touka.Context, src, dest string) error {
|
||||
info, err := h.FileSystem.Stat(c.Context(), src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
if err := h.FileSystem.Mkdir(ctx, dest, info.Mode()); err != nil {
|
||||
if err := h.FileSystem.Mkdir(c.Context(), dest, info.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
srcFile, err := h.FileSystem.OpenFile(&touka.Context{Request: &http.Request{}}, src, os.O_RDONLY, 0)
|
||||
srcFile, err := h.FileSystem.OpenFile(c, src, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -473,20 +473,20 @@ func (h *Handler) copy(ctx context.Context, src, dest string) error {
|
|||
}
|
||||
|
||||
for _, child := range children {
|
||||
if err := h.copy(ctx, path.Join(src, child.Name()), path.Join(dest, child.Name())); err != nil {
|
||||
if err := h.copy(c, path.Join(src, child.Name()), path.Join(dest, child.Name())); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
srcFile, err := h.FileSystem.OpenFile(&touka.Context{Request: &http.Request{}}, src, os.O_RDONLY, 0)
|
||||
srcFile, err := h.FileSystem.OpenFile(c, src, os.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
destFile, err := h.FileSystem.OpenFile(&touka.Context{Request: &http.Request{}}, dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
|
||||
destFile, err := h.FileSystem.OpenFile(c, dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue