mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
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.
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
// Copyright 2024 WJQSERVER. All rights reserved.
|
|
// All rights reserved by WJQSERVER, related rights can be exercised by the infinite-iroha organization.
|
|
package webdav
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/infinite-iroha/touka"
|
|
)
|
|
|
|
func TestRegister(t *testing.T) {
|
|
r := touka.New()
|
|
cfg := &Config{
|
|
FileSystem: NewMemFS(),
|
|
}
|
|
Register(r, "/dav", cfg)
|
|
|
|
// Check if a WebDAV method is registered
|
|
req, _ := http.NewRequest("PROPFIND", "/dav/", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code == http.StatusNotFound {
|
|
t.Errorf("Expected PROPFIND to be registered, but got 404")
|
|
}
|
|
}
|
|
|
|
func TestServe(t *testing.T) {
|
|
r := touka.New()
|
|
dir, _ := os.MkdirTemp("", "webdav")
|
|
defer os.RemoveAll(dir)
|
|
|
|
if err := Serve(r, "/serve", dir); err != nil {
|
|
t.Fatalf("Serve failed: %v", err)
|
|
}
|
|
|
|
// Check if a WebDAV method is registered
|
|
req, _ := http.NewRequest("OPTIONS", "/serve/", nil)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Errorf("Expected OPTIONS to return 200, but got %d", w.Code)
|
|
}
|
|
}
|