touka/touka.go
google-labs-jules[bot] b92f1face5 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.
2025-12-11 02:53:19 +00:00

91 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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 touka
import (
"net/http"
)
const (
defaultMemory = 32 << 20 // 32 MB, Gin 的默认值,用于 ParseMultipartForm
)
type H map[string]interface{} // map简写, 类似gin.H
type Handle func(http.ResponseWriter, *http.Request, Params)
// HandlerFunc 定义框架处理函数的类型,包括中间件和最终的路由处理函数。
type HandlerFunc func(*Context)
// HandlersChain 定义处理函数链(中间件栈)的类型。
type HandlersChain []HandlerFunc
// IRouter 定义了路由注册的接口提供路由分组和HTTP方法注册的能力。
type IRouter interface {
Group(relativePath string, handlers ...HandlerFunc) IRouter // 创建路由分组
Use(middleware ...HandlerFunc) IRouter // 应用中间件到当前组或子组
Handle(httpMethod, relativePath string, handlers ...HandlerFunc) // 注册通用HTTP方法
GET(relativePath string, handlers ...HandlerFunc)
POST(relativePath string, handlers ...HandlerFunc)
PUT(relativePath string, handlers ...HandlerFunc)
DELETE(relativePath string, handlers ...HandlerFunc)
PATCH(relativePath string, handlers ...HandlerFunc)
HEAD(relativePath string, handlers ...HandlerFunc)
OPTIONS(relativePath string, handlers ...HandlerFunc)
ANY(relativePath string, handlers ...HandlerFunc) // 注册所有HTTP方法
}
// RouteInfo 包含一个已注册路由的详细信息。
// 由 Router.GetRouters() 方法返回。
type RouteInfo struct {
Method string // HTTP 方法 (GET, POST, PUT, DELETE 等)
Path string // 路由路径
Handler string // 处理函数名称
Group string // 路由分组
}
// 维护一个Methods列表
var (
MethodGet = "GET"
MethodHead = "HEAD"
MethodPost = "POST"
MethodPut = "PUT"
MethodPatch = "PATCH"
MethodDelete = "DELETE"
MethodConnect = "CONNECT"
MethodOptions = "OPTIONS"
MethodTrace = "TRACE"
)
var (
// WebDAV methods
MethodPropfind = "PROPFIND"
MethodProppatch = "PROPPATCH"
MethodMkcol = "MKCOL"
MethodCopy = "COPY"
MethodMove = "MOVE"
MethodLock = "LOCK"
MethodUnlock = "UNLOCK"
)
var MethodsSet = map[string]struct{}{
MethodGet: {},
MethodHead: {},
MethodPost: {},
MethodPut: {},
MethodPatch: {},
MethodDelete: {},
MethodConnect: {},
MethodOptions: {},
MethodTrace: {},
MethodPropfind: {},
MethodProppatch: {},
MethodMkcol: {},
MethodCopy: {},
MethodMove: {},
MethodLock: {},
MethodUnlock: {},
}