webdav

package
v4.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2025 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package webdav 提供WebDAV服务器实现 WebDAV(Web-based Distributed Authoring and Versioning)是HTTP协议的扩展, 允许客户端在Web服务器上执行远程内容管理操作

Index

Constants

View Source
const (
	StatusMulti               = 207
	StatusUnprocessableEntity = 422
	StatusLocked              = 423
	StatusFailedDependency    = 424
	StatusInsufficientStorage = 507
)

http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11

Variables

View Source
var (
	// ErrConfirmationFailed 当锁定确认失败时由LockSystem的Confirm方法返回
	ErrConfirmationFailed = errors.New("webdav: 锁定确认失败")

	// ErrForbidden 当无权限解锁时由LockSystem的Unlock方法返回
	ErrForbidden = errors.New("webdav: 禁止访问")

	// ErrLocked 当资源已被锁定时由LockSystem的Create、Refresh和Unlock方法返回
	ErrLocked = errors.New("webdav: 资源已锁定")

	// ErrNoSuchLock 当指定的锁不存在时由LockSystem的Refresh和Unlock方法返回
	ErrNoSuchLock = errors.New("webdav: 锁不存在")
)

WebDAV锁定系统相关错误

View Source
var ErrNotImplemented = errors.New("not implemented")

ErrNotImplemented should be returned by optional interfaces if they want the original implementation to be used.

Functions

func StatusText

func StatusText(code int) string

Types

type BufferedResponseWriter

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

BufferedResponseWriter 是一个HTTP响应写入器的缓冲实现 它先将所有数据保存在内存中,然后一次性写入到实际的ResponseWriter 这对于需要在发送响应前知道完整内容的情况很有用

func NewBufferedResponseWriter

func NewBufferedResponseWriter() *BufferedResponseWriter

NewBufferedResponseWriter 创建一个新的BufferedResponseWriter实例

返回:

  • *BufferedResponseWriter: 新的缓冲响应写入器

func (*BufferedResponseWriter) Header

func (w *BufferedResponseWriter) Header() http.Header

Header 返回HTTP头部映射 实现http.ResponseWriter接口

返回:

  • http.Header: HTTP头部映射

func (*BufferedResponseWriter) Size

func (w *BufferedResponseWriter) Size() int

Size 返回当前缓冲的数据大小

返回:

  • int: 缓冲的字节数

func (*BufferedResponseWriter) StatusCode

func (w *BufferedResponseWriter) StatusCode() int

StatusCode 返回当前设置的HTTP状态码

返回:

  • int: HTTP状态码,如果未设置则为0

func (*BufferedResponseWriter) Write

func (w *BufferedResponseWriter) Write(bytes []byte) (int, error)

Write 将数据写入缓冲区 实现http.ResponseWriter接口

参数:

  • bytes: 要写入的数据

返回:

  • int: 写入的字节数
  • error: 写入错误,通常为nil

func (*BufferedResponseWriter) WriteHeader

func (w *BufferedResponseWriter) WriteHeader(statusCode int)

WriteHeader 设置HTTP状态码 实现http.ResponseWriter接口 注意:只有第一次调用会生效

参数:

  • statusCode: HTTP状态码

func (*BufferedResponseWriter) WriteToResponse

func (w *BufferedResponseWriter) WriteToResponse(rw http.ResponseWriter) (int, error)

WriteToResponse 将缓冲的内容写入到实际的HTTP响应写入器 包括状态码、头部和正文数据

参数:

  • rw: 目标HTTP响应写入器

返回:

  • int: 写入的字节数
  • error: 写入错误

type Condition

type Condition struct {
	Not   bool   // 是否为否定条件
	Token string // 锁定令牌
	ETag  string // 实体标签
}

Condition 表示WebDAV资源的匹配条件,基于令牌或ETag Token和ETag中应该有且仅有一个非空

type ContentTyper

type ContentTyper interface {
	// ContentType returns the content type for the file.
	//
	// If this returns error ErrNotImplemented then the error will
	// be ignored and the base implementation will be used
	// instead.
	ContentType(ctx context.Context) (string, error)
}

ContentTyper is an optional interface for the os.FileInfo objects returned by the FileSystem.

If this interface is defined then it will be used to read the content type from the object.

If this interface is not defined the file will be opened and the content type will be guessed from the initial contents of the file.

type DeadPropsHolder

type DeadPropsHolder interface {
	// DeadProps returns a copy of the dead properties held.
	DeadProps() (map[xml.Name]Property, error)

	// Patch patches the dead properties held.
	//
	// Patching is atomic; either all or no patches succeed. It returns (nil,
	// non-nil) if an internal server error occurred, otherwise the Propstats
	// collectively contain one Property for each proposed patch Property. If
	// all patches succeed, Patch returns a slice of length one and a Propstat
	// element with a 200 OK HTTP status code. If none succeed, for reasons
	// other than an internal server error, no Propstat has status 200 OK.
	//
	// For more details on when various HTTP status codes apply, see
	// http://www.webdav.org/specs/rfc4918.html#PROPPATCH-status
	Patch([]Proppatch) ([]Propstat, error)
}

DeadPropsHolder holds the dead properties of a resource.

Dead properties are those properties that are explicitly defined. In comparison, live properties, such as DAV:getcontentlength, are implicitly defined by the underlying resource, and cannot be explicitly overridden or removed. See the Terminology section of http://www.webdav.org/specs/rfc4918.html#rfc.section.3

There is a whitelist of the names of live properties. This package handles all live properties, and will only pass non-whitelisted names to the Patch method of DeadPropsHolder implementations.

type ETager

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)
}

ETager is an optional interface for the os.FileInfo objects returned by the FileSystem.

If this interface is defined then it will be used to read the ETag for the object.

If this interface is not defined an ETag will be computed using the ModTime() and the Size() methods of the os.FileInfo object.

type Handler

type Handler struct {
	// Prefix 是要从WebDAV资源路径中删除的URL路径前缀
	// 用于在子目录中挂载WebDAV服务
	Prefix string

	// LockSystem 是锁管理系统
	// 用于支持WebDAV的锁定功能
	LockSystem LockSystem

	// Logger 是可选的错误日志记录器
	// 如果非nil,将为所有HTTP请求调用它来记录错误
	Logger func(*http.Request, error)
}

Handler 实现WebDAV协议的HTTP处理器

func (*Handler) ServeHTTP

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

ServeHTTP 处理所有WebDAV请求 实现http.Handler接口

参数:

  • w: HTTP响应写入器
  • r: HTTP请求

type LockDetails

type LockDetails struct {
	// Root 是被锁定的根资源名称
	// 对于零深度锁,根是唯一被锁定的资源
	Root string

	// Duration 是锁的超时时间
	// 负持续时间表示无限期
	Duration time.Duration

	// OwnerXML 是LOCK HTTP请求中给出的原始<owner>XML内容
	//
	// TODO: "原始"内容是否与XML命名空间兼容?
	// OwnerXML字段是否需要更多结构?参见
	// https://codereview.appspot.com/175140043/#msg2
	OwnerXML string

	// ZeroDepth 表示锁是否具有零深度
	// 如果没有零深度,则具有无限深度
	ZeroDepth bool
}

LockDetails 表示锁的元数据信息

type LockSystem

type LockSystem interface {
	// Confirm 确认调用方可以声明指定条件的所有锁,
	// 并且持有这些锁的并集可以对所有指定资源进行独占访问。
	// 最多可以命名两个资源,空名称将被忽略。
	//
	// release和err中有且仅有一个非nil。如果release非nil,
	// 则所有请求的锁将被持有直到调用release。调用release不会
	// 在WebDAV UNLOCK的意义上解锁,但一旦Confirm确认锁定声明有效,
	// 该锁就不能再被Confirm,直到它被释放。
	//
	// 如果Confirm返回ErrConfirmationFailed,则Handler将继续尝试
	// 请求中提供的其他锁集合(WebDAV HTTP请求可以提供多个锁集合)。
	// 如果返回任何其他非nil错误,Handler将写入"500 Internal Server Error"HTTP状态。
	Confirm(now time.Time, name0, name1 string, conditions ...Condition) (release func(), err error)

	// Create 使用给定的深度、持续时间、所有者和根(name)创建锁。
	// 深度要么为负(表示无限),要么为零。
	//
	// 如果Create返回ErrLocked,则Handler将写入"423 Locked"HTTP状态。
	// 如果返回任何其他非nil错误,Handler将写入"500 Internal Server Error"HTTP状态。
	//
	// 有关何时使用每种错误的信息,请参见
	// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.10.6
	//
	// 返回的令牌标识创建的锁。它应该是由RFC 3986第4.3节定义的绝对URI。
	// 特别是,它不应包含空格。
	Create(now time.Time, details LockDetails) (token string, err error)

	// Refresh 刷新具有给定令牌的锁的持续时间。
	//
	// 如果Refresh返回ErrLocked,则Handler将写入"423 Locked"HTTP状态。
	// 如果Refresh返回ErrNoSuchLock,则Handler将写入"412 Precondition Failed"HTTP状态。
	// 如果返回任何其他非nil错误,Handler将写入"500 Internal Server Error"HTTP状态。
	//
	// 有关何时使用每种错误的信息,请参见
	// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.10.6
	Refresh(now time.Time, token string, duration time.Duration) (LockDetails, error)

	// Unlock 解锁具有给定令牌的锁。
	//
	// 如果Unlock返回ErrForbidden,则Handler将写入"403 Forbidden"HTTP状态。
	// 如果Unlock返回ErrLocked,则Handler将写入"423 Locked"HTTP状态。
	// 如果Unlock返回ErrNoSuchLock,则Handler将写入"409 Conflict"HTTP状态。
	// 如果返回任何其他非nil错误,Handler将写入"500 Internal Server Error"HTTP状态。
	//
	// 有关何时使用每种错误的信息,请参见
	// http://www.webdav.org/specs/rfc4918.html#rfc.section.9.11.1
	Unlock(now time.Time, token string) error
}

LockSystem 管理对命名资源集合的访问 锁名称中的元素由斜杠('/', U+002F)字符分隔,与主机操作系统约定无关

func NewMemLS

func NewMemLS() LockSystem

NewMemLS 返回一个新的内存中的LockSystem实现

type Property

type Property struct {
	// XMLName 是标识此属性的完全限定名称
	XMLName xml.Name

	// Lang 是可选的 xml:lang 属性
	Lang string `xml:"xml:lang,attr,omitempty"`

	// InnerXML 包含属性值的 XML 表示
	// 参见 http://www.webdav.org/specs/rfc4918.html#property_values
	//
	// 复杂类型或混合内容的属性值必须具有完全展开的 XML 命名空间
	// 或者是带有相应 XML 命名空间声明的自包含内容。它们不能依赖于
	// XML 文档范围内的任何 XML 命名空间声明,甚至包括 DAV: 命名空间。
	InnerXML []byte `xml:",innerxml"`
}

Property 表示 RFC 4918 中定义的单个 DAV 资源属性 参见 http://www.webdav.org/specs/rfc4918.html#data.model.for.resource.properties

type Proppatch

type Proppatch struct {
	// Remove specifies whether this patch removes properties. If it does not
	// remove them, it sets them.
	Remove bool
	// Props contains the properties to be set or removed.
	Props []Property
}

Proppatch describes a property update instruction as defined in RFC 4918. See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPPATCH

type Propstat

type Propstat struct {
	// Props contains the properties for which Status applies.
	Props []Property

	// Status defines the HTTP status code of the properties in Prop.
	// Allowed values include, but are not limited to the WebDAV status
	// code extensions for HTTP/1.1.
	// http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11
	Status int

	// XMLError contains the XML representation of the optional error element.
	// XML content within this field must not rely on any predefined
	// namespace declarations or prefixes. If empty, the XML error element
	// is omitted.
	XMLError string

	// ResponseDescription contains the contents of the optional
	// responsedescription field. If empty, the XML element is omitted.
	ResponseDescription string
}

Propstat describes a XML propstat element as defined in RFC 4918. See http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat

Directories

Path Synopsis
internal
xml
Package xml implements a simple XML 1.0 parser that understands XML name spaces.
Package xml implements a simple XML 1.0 parser that understands XML name spaces.

Jump to

Keyboard shortcuts

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