Package webdav provides a WebDAV server implementation.
import "golang.org/x/net/webdav"const (
StatusMulti = 207
StatusUnprocessableEntity = 422
StatusLocked = 423
StatusFailedDependency = 424
StatusInsufficientStorage = 507
)var (
ErrConfirmationFailed = errors.New("webdav: confirmation failed")
ErrForbidden = errors.New("webdav: forbidden")
ErrLocked = errors.New("webdav: locked")
ErrNoSuchLock = errors.New("webdav: no such lock")
)
var ErrNotImplemented = errors.New("not implemented")func StatusText(code int) string// Handler is a WebDAV HTTP handler
type Handler struct {
FileSystem FileSystem // Required: backing store for the collection
LockSystem LockSystem // Optional: manages locks
Logger func(*http.Request, error)
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)// FileSystem is a file system tree for WebDAV operations
type FileSystem interface {
Mkdir(ctx context.Context, name string, perm os.FileMode) error
OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (File, error)
RemoveAll(ctx context.Context, name string) error
Rename(ctx context.Context, oldName, newName string) error
Stat(ctx context.Context, name string) (os.FileInfo, error)
}
// NewMemFS returns a new in-memory FileSystem implementation
func NewMemFS() FileSystem
// Dir returns a FileSystem for the tree of files rooted at directory dir
func Dir(dir string) FileSystem// File represents an open file
type File interface {
io.Closer
io.Reader
io.Writer
io.Seeker
Readdir(count int) ([]os.FileInfo, error)
Stat() (os.FileInfo, error)
}// LockSystem manages access to a collection of named resources
type LockSystem interface {
Confirm(now time.Time, name0, name1 string, conditions ...Condition) (release func(), err error)
Create(now time.Time, details LockDetails) (token string, err error)
Refresh(now time.Time, token string, duration time.Duration) (LockDetails, error)
Unlock(now time.Time, token string) error
}
// NewMemLS returns a new in-memory LockSystem
func NewMemLS() LockSystem// LockDetails represents lock metadata
type LockDetails struct {
Root string
Duration time.Duration
OwnerXML string
ZeroDepth bool
}// Condition can match a WebDAV resource based on token or ETag
type Condition struct {
Not bool
Token string
ETag string
}// Property represents a single DAV resource property
type Property struct {
XMLName xml.Name
Lang string `xml:"xml:lang,attr,omitempty"`
InnerXML []byte `xml:",innerxml"`
}// Propstat represents the result of setting or removing a property
type Propstat struct {
Props []Property
Status int
Error *xmlError
ResponseDescription string
}// DeadPropsHolder manages dead properties for a resource
type DeadPropsHolder interface {
DeadProps() (map[xml.Name]Property, error)
Patch([]Proppatch) ([]Propstat, error)
}// ContentTyper is an optional interface for FileInfo objects
type ContentTyper interface {
ContentType(ctx context.Context) (string, error)
}// ETager is an optional interface for os.FileInfo objects returned by the FileSystem
type ETager interface {
// ETag returns an ETag for the file. This should be of the form "value" or W/"value"
// If this returns error ErrNotImplemented then the error will be ignored
// and the base implementation will be used instead.
ETag(ctx context.Context) (string, error)
}import (
"golang.org/x/net/webdav"
"net/http"
)
func startWebDAVServer() error {
handler := &webdav.Handler{
FileSystem: webdav.Dir("/tmp/webdav"),
LockSystem: webdav.NewMemLS(),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Printf("WebDAV error: %v", err)
}
},
}
return http.ListenAndServe(":8080", handler)
}func startMemoryWebDAV() error {
handler := &webdav.Handler{
FileSystem: webdav.NewMemFS(),
LockSystem: webdav.NewMemLS(),
}
return http.ListenAndServe(":8080", handler)
}type customFS struct {
// Custom implementation
}
func (fs *customFS) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
// Implementation
return nil
}
func (fs *customFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
// Implementation
return nil, nil
}
func (fs *customFS) RemoveAll(ctx context.Context, name string) error {
// Implementation
return nil
}
func (fs *customFS) Rename(ctx context.Context, oldName, newName string) error {
// Implementation
return nil
}
func (fs *customFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
// Implementation
return nil, nil
}
func startCustomWebDAV() error {
handler := &webdav.Handler{
FileSystem: &customFS{},
LockSystem: webdav.NewMemLS(),
}
return http.ListenAndServe(":8080", handler)
}