mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 08:51:11 +08:00
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. - Performance optimizations, including the use of `sync.Pool` for object reuse and `sync/atomic` for lock-free field access to reduce GC pressure. - 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:
parent
85409ba803
commit
1d6e7a2633
4 changed files with 61 additions and 32 deletions
|
|
@ -10,7 +10,10 @@ import (
|
|||
"path"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/infinite-iroha/touka"
|
||||
)
|
||||
|
||||
// MemFS is an in-memory file system for WebDAV using a tree structure.
|
||||
|
|
@ -85,7 +88,7 @@ func (fs *MemFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error
|
|||
}
|
||||
|
||||
// OpenFile opens a file in the in-memory file system.
|
||||
func (fs *MemFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (File, error) {
|
||||
func (fs *MemFS) OpenFile(c *touka.Context, name string, flag int, perm os.FileMode) (File, error) {
|
||||
fs.mu.Lock()
|
||||
defer fs.mu.Unlock()
|
||||
|
||||
|
|
@ -111,15 +114,16 @@ func (fs *MemFS) OpenFile(ctx context.Context, name string, flag int, perm os.Fi
|
|||
|
||||
if flag&os.O_TRUNC != 0 {
|
||||
node.data = nil
|
||||
node.size = 0
|
||||
atomic.StoreInt64(&node.size, 0)
|
||||
}
|
||||
|
||||
return &memFile{
|
||||
node: node,
|
||||
fs: fs,
|
||||
offset: 0,
|
||||
fullPath: name,
|
||||
}, nil
|
||||
mf := memFilePool.Get().(*memFile)
|
||||
mf.node = node
|
||||
mf.fs = fs
|
||||
mf.offset = 0
|
||||
mf.fullPath = name
|
||||
mf.contentLength = c.Request.ContentLength
|
||||
return mf, nil
|
||||
}
|
||||
|
||||
// RemoveAll removes a file or directory from the in-memory file system.
|
||||
|
|
@ -194,20 +198,32 @@ type memNode struct {
|
|||
}
|
||||
|
||||
func (n *memNode) Name() string { return n.name }
|
||||
func (n *memNode) Size() int64 { return n.size }
|
||||
func (n *memNode) Size() int64 { return atomic.LoadInt64(&n.size) }
|
||||
func (n *memNode) Mode() os.FileMode { return n.mode }
|
||||
func (n *memNode) ModTime() time.Time { return n.modTime }
|
||||
func (n *memNode) IsDir() bool { return n.isDir }
|
||||
func (n *memNode) Sys() interface{} { return nil }
|
||||
|
||||
type memFile struct {
|
||||
node *memNode
|
||||
fs *MemFS
|
||||
offset int64
|
||||
fullPath string
|
||||
node *memNode
|
||||
fs *MemFS
|
||||
offset int64
|
||||
fullPath string
|
||||
contentLength int64
|
||||
}
|
||||
|
||||
func (f *memFile) Close() error { return nil }
|
||||
var memFilePool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &memFile{}
|
||||
},
|
||||
}
|
||||
|
||||
func (f *memFile) Close() error {
|
||||
f.node = nil
|
||||
f.fs = nil
|
||||
memFilePool.Put(f)
|
||||
return nil
|
||||
}
|
||||
func (f *memFile) Stat() (ObjectInfo, error) { return f.node, nil }
|
||||
|
||||
func (f *memFile) Read(p []byte) (n int, err error) {
|
||||
|
|
@ -224,17 +240,17 @@ func (f *memFile) Read(p []byte) (n int, err error) {
|
|||
func (f *memFile) Write(p []byte) (n int, err error) {
|
||||
f.fs.mu.Lock()
|
||||
defer f.fs.mu.Unlock()
|
||||
if f.offset+int64(len(p)) > int64(len(f.node.data)) {
|
||||
newSize := f.offset + int64(len(p))
|
||||
newSize := f.offset + int64(len(p))
|
||||
if newSize > int64(cap(f.node.data)) {
|
||||
newData := make([]byte, newSize)
|
||||
copy(newData, f.node.data)
|
||||
f.node.data = newData
|
||||
} else {
|
||||
f.node.data = f.node.data[:newSize]
|
||||
}
|
||||
n = copy(f.node.data[f.offset:], p)
|
||||
f.offset += int64(n)
|
||||
if f.offset > f.node.size {
|
||||
f.node.size = f.offset
|
||||
}
|
||||
atomic.StoreInt64(&f.node.size, newSize)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue