webdav

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2025 License: MIT Imports: 19 Imported by: 0

README

fiber-webdav

Go Reference

A Go-Fiber library for WebDAV. Forked from emersion/go-webdav.

Installation

go get -u github.com/Tryanks/fiber-webdav

Usage

Basic WebDAV Server

Here's a simple example of how to create a WebDAV server using Fiber:

package main

import (
	"github.com/Tryanks/fiber-webdav"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/log"
	"github.com/gofiber/fiber/v2/middleware/logger"
)

func main() {
	// Create a new Fiber app with WebDAV extended methods
	app := fiber.New(fiber.Config{
		RequestMethods: webdav.ExtendedMethods,
	})

	// Add logger middleware
	app.Use(logger.New())

	// Mount WebDAV handler at root path
	app.Use("/", webdav.New(webdav.Config{
		Prefix: "/",
		Root:   webdav.LocalFileSystem("."), // Serve current directory
		Lock:   true,                        // Enable WebDAV locking
	}))

	// Start server on port 8080
	err := app.Listen(":8080")
	if err != nil {
		log.Fatal(err)
	}
}
Configuration Options

The webdav.Config struct accepts the following options:

  • Prefix: The URL path prefix to mount the WebDAV server on
  • Root: The base directory for the WebDAV server (implements webdav.FileSystem interface)
  • Lock: Boolean to enable WebDAV locking support
WebDAV Methods Support

To enable WebDAV support in your Fiber application, you must initialize the Fiber app with extended request methods:

fiber.Config{
    RequestMethods: webdav.ExtendedMethods,
}

License

MIT from emersion

Documentation

Overview

Package webdav provides a client and server WebDAV filesystem implementation.

WebDAV is defined in RFC 4918.

Index

Constants

View Source
const (
	MethodMkcol     = "MKCOL"
	MethodCopy      = "COPY"
	MethodMove      = "MOVE"
	MethodLock      = "LOCK"
	MethodUnlock    = "UNLOCK"
	MethodPropfind  = "PROPFIND"
	MethodProppatch = "PROPPATCH"
)

Variables

View Source
var ExtendedMethods = append(fiber.DefaultMethods[:], Methods...)

Functions

func New

func New(config ...Config) fiber.Handler

func NewHTTPError

func NewHTTPError(statusCode int, cause error) error

NewHTTPError creates a new error that is associated with an HTTP status code and optionally an error that lead to it. Backends can use this functions to return errors that convey some semantics (e.g. 404 not found, 403 access denied, etc.) while also providing an (optional) arbitrary error context (intended for humans).

func ServePrincipal

func ServePrincipal(w http.ResponseWriter, r *http.Request, options *ServePrincipalOptions)

ServePrincipal replies to requests for a principal URL.

Types

type Capability

type Capability string

Capability indicates the features that a server supports.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides access to a remote WebDAV filesystem.

func NewClient

func NewClient(c HTTPClient, endpoint string) (*Client, error)

NewClient creates a new WebDAV client.

If the HTTPClient is nil, http.DefaultClient is used.

To use HTTP basic authentication, HTTPClientWithBasicAuth can be used.

func (*Client) Copy

func (c *Client) Copy(ctx context.Context, name, dest string, options *CopyOptions) error

Copy copies a file.

By default, if the file is a directory, all descendants are recursively copied as well.

func (*Client) Create

func (c *Client) Create(ctx context.Context, name string) (io.WriteCloser, error)

Create writes a file's contents.

func (*Client) FindCurrentUserPrincipal

func (c *Client) FindCurrentUserPrincipal(ctx context.Context) (string, error)

FindCurrentUserPrincipal finds the current user's principal path.

func (*Client) Mkdir

func (c *Client) Mkdir(ctx context.Context, name string) error

Mkdir creates a new directory.

func (*Client) Move

func (c *Client) Move(ctx context.Context, name, dest string, options *MoveOptions) error

Move moves a file.

func (*Client) Open

func (c *Client) Open(ctx context.Context, name string) (io.ReadCloser, error)

Open fetches a file's contents.

func (*Client) ReadDir

func (c *Client) ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error)

ReadDir lists files in a directory.

func (*Client) RemoveAll

func (c *Client) RemoveAll(ctx context.Context, name string) error

RemoveAll deletes a file. If the file is a directory, all of its descendants are recursively deleted as well.

func (*Client) Stat

func (c *Client) Stat(ctx context.Context, name string) (*FileInfo, error)

Stat fetches a FileInfo for a single file.

type ConditionalMatch

type ConditionalMatch string

ConditionalMatch represents the value of a conditional header according to RFC 2068 section 14.25 and RFC 2068 section 14.26 The (optional) value can either be a wildcard or an ETag.

func (ConditionalMatch) ETag

func (val ConditionalMatch) ETag() (string, error)

func (ConditionalMatch) IsSet

func (val ConditionalMatch) IsSet() bool

func (ConditionalMatch) IsWildcard

func (val ConditionalMatch) IsWildcard() bool

func (ConditionalMatch) MatchETag

func (val ConditionalMatch) MatchETag(etag string) (bool, error)

type Config

type Config struct {
	// Prefix is the URL path prefix to mount the WebDAV server on
	Prefix string

	// Root is the base directory for the WebDAV server
	Root FileSystem

	// Lock enables WebDAV locking support
	Lock bool
}

type CopyOptions

type CopyOptions struct {
	NoRecursive bool
	NoOverwrite bool
}

type CreateOptions

type CreateOptions struct {
	IfMatch     ConditionalMatch
	IfNoneMatch ConditionalMatch
}

type FileInfo

type FileInfo struct {
	Path     string
	Size     int64
	ModTime  time.Time
	IsDir    bool
	MIMEType string
	ETag     string
}

FileInfo holds information about a WebDAV file.

type FileSystem

type FileSystem interface {
	Open(ctx context.Context, name string) (io.ReadCloser, error)
	Stat(ctx context.Context, name string) (*FileInfo, error)
	ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error)
	Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fileInfo *FileInfo, created bool, err error)
	RemoveAll(ctx context.Context, name string, opts *RemoveAllOptions) error
	Mkdir(ctx context.Context, name string) error
	Copy(ctx context.Context, name, dest string, options *CopyOptions) (created bool, err error)
	Move(ctx context.Context, name, dest string, options *MoveOptions) (created bool, err error)
}

FileSystem is a WebDAV server backend.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient performs HTTP requests. It's implemented by *http.Client.

func HTTPClientWithBasicAuth

func HTTPClientWithBasicAuth(c HTTPClient, username, password string) HTTPClient

HTTPClientWithBasicAuth returns an HTTP client that adds basic authentication to all outgoing requests. If c is nil, http.DefaultClient is used.

type Handler

type Handler struct {
	FileSystem FileSystem
	LockSystem *LockSystem
	// contains filtered or unexported fields
}

Handler handles WebDAV HTTP requests. It can be used to create a WebDAV server.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type LocalFileSystem

type LocalFileSystem string

LocalFileSystem implements FileSystem for a local directory.

func (LocalFileSystem) Copy

func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *CopyOptions) (created bool, err error)

func (LocalFileSystem) Create

func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fi *FileInfo, created bool, err error)

func (LocalFileSystem) Mkdir

func (fs LocalFileSystem) Mkdir(ctx context.Context, name string) error

func (LocalFileSystem) Move

func (fs LocalFileSystem) Move(ctx context.Context, src, dst string, options *MoveOptions) (created bool, err error)

func (LocalFileSystem) Open

func (fs LocalFileSystem) Open(ctx context.Context, name string) (io.ReadCloser, error)

func (LocalFileSystem) ReadDir

func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error)

func (LocalFileSystem) RemoveAll

func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string, opts *RemoveAllOptions) error

func (LocalFileSystem) Stat

func (fs LocalFileSystem) Stat(ctx context.Context, name string) (*FileInfo, error)

type LockSystem

type LockSystem struct {
	// contains filtered or unexported fields
}

LockSystem provides an in-memory implementation of WebDAV locks.

func GetGlobalLockSystem

func GetGlobalLockSystem() *LockSystem

GetGlobalLockSystem returns the global lock system, creating it if necessary.

func NewLockSystem

func NewLockSystem() *LockSystem

NewLockSystem creates a new in-memory lock system.

func (*LockSystem) CleanExpiredLocks

func (ls *LockSystem) CleanExpiredLocks()

CleanExpiredLocks removes expired locks.

func (*LockSystem) Lock

func (ls *LockSystem) Lock(r *http.Request, depth internal.Depth, timeout time.Duration, refreshToken string) (*internal.Lock, bool, error)

Lock creates or refreshes a lock.

func (*LockSystem) Unlock

func (ls *LockSystem) Unlock(r *http.Request, tokenHref string) error

Unlock removes a lock.

type MoveOptions

type MoveOptions struct {
	NoOverwrite bool
}

type RemoveAllOptions

type RemoveAllOptions struct {
	IfMatch     ConditionalMatch
	IfNoneMatch ConditionalMatch
}

type ServePrincipalOptions

type ServePrincipalOptions struct {
	CurrentUserPrincipalPath string
	Capabilities             []Capability
}

ServePrincipalOptions holds options for ServePrincipal.

type UserPrincipalBackend

type UserPrincipalBackend interface {
	CurrentUserPrincipal(ctx context.Context) (string, error)
}

UserPrincipalBackend can determine the current user's principal URL for a given request context.

Directories

Path Synopsis
cmd
webdav-server command
Package internal provides low-level helpers for WebDAV clients and servers.
Package internal provides low-level helpers for WebDAV clients and servers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL