mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-02-03 00:41:10 +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. - `OSFS`: A secure, OS-based filesystem that interacts with the local disk and includes path traversal protection. - A `LockSystem` interface with an in-memory implementation (`MemLock`) to support resource locking (DAV Class 2). - 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 commit is contained in:
parent
8e10d51d6d
commit
33e5d5474d
4 changed files with 68 additions and 17 deletions
|
|
@ -36,7 +36,13 @@ func (fs *MemFS) findNode(path string) (*memNode, error) {
|
||||||
current := fs.root
|
current := fs.root
|
||||||
parts := strings.Split(path, "/")
|
parts := strings.Split(path, "/")
|
||||||
for _, part := range parts {
|
for _, part := range parts {
|
||||||
if part == "" {
|
if part == "" || part == "." {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if part == ".." {
|
||||||
|
if current.parent != nil {
|
||||||
|
current = current.parent
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if current.children == nil {
|
if current.children == nil {
|
||||||
|
|
@ -105,6 +111,7 @@ func (fs *MemFS) OpenFile(ctx context.Context, name string, flag int, perm os.Fi
|
||||||
|
|
||||||
if flag&os.O_TRUNC != 0 {
|
if flag&os.O_TRUNC != 0 {
|
||||||
node.data = nil
|
node.data = nil
|
||||||
|
node.size = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return &memFile{
|
return &memFile{
|
||||||
|
|
@ -234,14 +241,21 @@ func (f *memFile) Write(p []byte) (n int, err error) {
|
||||||
func (f *memFile) Seek(offset int64, whence int) (int64, error) {
|
func (f *memFile) Seek(offset int64, whence int) (int64, error) {
|
||||||
f.fs.mu.Lock()
|
f.fs.mu.Lock()
|
||||||
defer f.fs.mu.Unlock()
|
defer f.fs.mu.Unlock()
|
||||||
|
var newOffset int64
|
||||||
switch whence {
|
switch whence {
|
||||||
case 0:
|
case io.SeekStart:
|
||||||
f.offset = offset
|
newOffset = offset
|
||||||
case 1:
|
case io.SeekCurrent:
|
||||||
f.offset += offset
|
newOffset = f.offset + offset
|
||||||
case 2:
|
case io.SeekEnd:
|
||||||
f.offset = int64(len(f.node.data)) + offset
|
newOffset = f.node.size + offset
|
||||||
|
default:
|
||||||
|
return 0, os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
if newOffset < 0 {
|
||||||
|
return 0, os.ErrInvalid
|
||||||
|
}
|
||||||
|
f.offset = newOffset
|
||||||
return f.offset, nil
|
return f.offset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,9 +28,24 @@ type lock struct {
|
||||||
|
|
||||||
// NewMemLock creates a new in-memory lock system.
|
// NewMemLock creates a new in-memory lock system.
|
||||||
func NewMemLock() *MemLock {
|
func NewMemLock() *MemLock {
|
||||||
return &MemLock{
|
l := &MemLock{
|
||||||
locks: make(map[string]*lock),
|
locks: make(map[string]*lock),
|
||||||
}
|
}
|
||||||
|
go l.cleanup()
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *MemLock) cleanup() {
|
||||||
|
for {
|
||||||
|
time.Sleep(1 * time.Minute)
|
||||||
|
l.mu.Lock()
|
||||||
|
for token, lock := range l.locks {
|
||||||
|
if time.Now().After(lock.expires) {
|
||||||
|
delete(l.locks, token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
l.mu.Unlock()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create creates a new lock.
|
// Create creates a new lock.
|
||||||
|
|
@ -39,7 +54,9 @@ func (l *MemLock) Create(ctx context.Context, path string, info LockInfo) (strin
|
||||||
defer l.mu.Unlock()
|
defer l.mu.Unlock()
|
||||||
|
|
||||||
token := make([]byte, 16)
|
token := make([]byte, 16)
|
||||||
rand.Read(token)
|
if _, err := rand.Read(token); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
tokenStr := hex.EncodeToString(token)
|
tokenStr := hex.EncodeToString(token)
|
||||||
|
|
||||||
l.locks[tokenStr] = &lock{
|
l.locks[tokenStr] = &lock{
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,16 @@ func NewOSFS(rootDir string) (*OSFS, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *OSFS) resolve(name string) (string, error) {
|
func (fs *OSFS) resolve(name string) (string, error) {
|
||||||
|
if filepath.IsAbs(name) {
|
||||||
|
return "", os.ErrPermission
|
||||||
|
}
|
||||||
path := filepath.Join(fs.RootDir, name)
|
path := filepath.Join(fs.RootDir, name)
|
||||||
if !strings.HasPrefix(path, fs.RootDir) {
|
|
||||||
|
rel, err := filepath.Rel(fs.RootDir, path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(rel, "..") {
|
||||||
return "", os.ErrPermission
|
return "", os.ErrPermission
|
||||||
}
|
}
|
||||||
return path, nil
|
return path, nil
|
||||||
|
|
|
||||||
|
|
@ -585,11 +585,11 @@ func (h *Handler) handleProppatch(c *touka.Context) {
|
||||||
c.Status(http.StatusNotImplemented)
|
c.Status(http.StatusNotImplemented)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) stripPrefix(path string) string {
|
func (h *Handler) stripPrefix(p string) string {
|
||||||
if h.Prefix == "/" {
|
if h.Prefix == "/" {
|
||||||
return path
|
return p
|
||||||
}
|
}
|
||||||
return "/" + strings.TrimPrefix(path, h.Prefix)
|
return strings.TrimPrefix(p, h.Prefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleLock(c *touka.Context) {
|
func (h *Handler) handleLock(c *touka.Context) {
|
||||||
|
|
@ -599,7 +599,15 @@ func (h *Handler) handleLock(c *touka.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
path, _ := c.Get("webdav_path")
|
path, _ := c.Get("webdav_path")
|
||||||
token := c.GetReqHeader("If")
|
tokenHeader := c.GetReqHeader("If")
|
||||||
|
var token string
|
||||||
|
if tokenHeader != "" {
|
||||||
|
// Basic parsing for <opaquelocktoken:c2134f...>
|
||||||
|
if strings.HasPrefix(tokenHeader, "(<") && strings.HasSuffix(tokenHeader, ">)") {
|
||||||
|
token = strings.TrimPrefix(tokenHeader, "(<")
|
||||||
|
token = strings.TrimSuffix(token, ">)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Refresh lock
|
// Refresh lock
|
||||||
if token != "" {
|
if token != "" {
|
||||||
|
|
@ -666,7 +674,7 @@ func parseTimeout(timeoutStr string) (time.Duration, error) {
|
||||||
return seconds, nil
|
return seconds, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0, nil
|
return 0, os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleUnlock(c *touka.Context) {
|
func (h *Handler) handleUnlock(c *touka.Context) {
|
||||||
|
|
@ -675,12 +683,16 @@ func (h *Handler) handleUnlock(c *touka.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := c.GetReqHeader("Lock-Token")
|
tokenHeader := c.GetReqHeader("Lock-Token")
|
||||||
if token == "" {
|
if tokenHeader == "" {
|
||||||
c.Status(http.StatusBadRequest)
|
c.Status(http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Basic parsing for <urn:uuid:f81d4fae...>
|
||||||
|
token := strings.TrimPrefix(tokenHeader, "<")
|
||||||
|
token = strings.TrimSuffix(token, ">")
|
||||||
|
|
||||||
if err := h.LockSystem.Unlock(c.Context(), token); err != nil {
|
if err := h.LockSystem.Unlock(c.Context(), token); err != nil {
|
||||||
c.Status(http.StatusConflict)
|
c.Status(http.StatusConflict)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue