server/webdav/prop.go

584 lines
17 KiB
Go
Raw Permalink Normal View History

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webdav
import (
"bytes"
"context"
"encoding/xml"
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
2021-09-20 13:43:00 +00:00
"path"
"path/filepath"
"strconv"
)
// Proppatch describes a property update instruction as defined in RFC 4918.
// See http://www.webdav.org/specs/rfc4918.html#METHOD_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
}
// Propstat describes a XML propstat element as defined in RFC 4918.
// See http://www.webdav.org/specs/rfc4918.html#ELEMENT_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
}
// makePropstats returns a slice containing those of x and y whose Props slice
// is non-empty. If both are empty, it returns a slice containing an otherwise
// zero Propstat whose HTTP status code is 200 OK.
func makePropstats(x, y Propstat) []Propstat {
pstats := make([]Propstat, 0, 2)
if len(x.Props) != 0 {
pstats = append(pstats, x)
}
if len(y.Props) != 0 {
pstats = append(pstats, y)
}
if len(pstats) == 0 {
pstats = append(pstats, Propstat{
Status: http.StatusOK,
})
}
return pstats
}
// 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 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)
}
// liveProps contains all supported, protected DAV: properties.
var liveProps = map[xml.Name]struct {
// findFn implements the propfind function of this property. If nil,
// it indicates a hidden property.
2021-09-20 13:43:00 +00:00
findFn func(context.Context, FileSystem, string, os.FileInfo) (string, error)
// dir is true if the property applies to directories.
dir bool
2021-10-04 10:06:42 +00:00
// file is true if the property applies to files.
file bool
}{
{Space: "DAV:", Local: "resourcetype"}: {
findFn: findResourceType,
dir: true,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "displayname"}: {
findFn: findDisplayName,
dir: true,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "getcontentlength"}: {
findFn: findContentLength,
dir: false,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "getlastmodified"}: {
findFn: findLastModified,
// http://webdav.org/specs/rfc4918.html#PROPERTY_getlastmodified
// suggests that getlastmodified should only apply to GETable
// resources, and this package does not support GET on directories.
//
// Nonetheless, some WebDAV clients expect child directories to be
// sortable by getlastmodified date, so this value is true, not false.
// See golang.org/issue/15334.
dir: true,
2021-10-04 10:06:42 +00:00
file:true,
},
{Space: "DAV:", Local: "creationdate"}: {
findFn: nil,
dir: false,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "getcontentlanguage"}: {
findFn: nil,
dir: false,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "getcontenttype"}: {
findFn: findContentType,
dir: false,
2021-10-04 10:06:42 +00:00
file: true,
},
{Space: "DAV:", Local: "getetag"}: {
findFn: findETag,
2021-10-04 10:06:42 +00:00
dir: true,
file: true,
},
{Space: "DAV:", Local: "quota-available-bytes"}: {
findFn: fnGetQuotaAvailableBytes,
dir: true,
file: false,
},
{Space: "DAV:", Local: "quota-used-bytes"}: {
findFn: fnGetQuotaUsedBytes,
dir: true,
file: false,
},
{Space: "http://owncloud.org/ns", Local: "permissions"}: {
findFn: fnGetPermissions,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "id"}: {
findFn: fnGetID,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "fileid"}: {
findFn: fnGetFileID,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "size"}: {
findFn: fnGetSize,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "favorite"}: {
findFn: fnGetFavorite,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "share-types"}: {
findFn: fnGetShareTypes,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "owner-id"}: {
findFn: fnGetOwnerID,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "owner-display-name"}: {
findFn: fnGetOwnerDisplayName,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "comments-unread"}: {
findFn: fnGetCommentsUnread,
dir: true,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "checksums"}: {
findFn: fnGetChecksums,
dir: false,
2021-10-04 10:06:42 +00:00
file: false,
},
{Space: "http://owncloud.org/ns", Local: "downloadURL"}: {
findFn: fnGetDownloadURL,
dir: false,
file: true,
},
{Space: "http://owncloud.org/ns", Local: "data-fingerprint"}: {
findFn: fnGetDataFingerprint,
dir: true,
file: true,
},
{Space: "http://nextcloud.org/ns", Local: "creation_time"}: {
findFn: fnGetCreationTime,
dir: false,
file: true,
},
{Space: "http://nextcloud.org/ns", Local: "upload_time"}: {
findFn: fnGetUploadTime,
dir: false,
file: true,
},
{Space: "http://nextcloud.org/ns", Local: "is-encrypted"}: {
findFn: fnGetIsEncrypted,
dir: true,
file: false,
},
{Space: "http://nextcloud.org/ns", Local: "has-preview"}: {
findFn: fnGetHasPreview,
dir: true,
file: true,
},
{Space: "http://nextcloud.org/ns", Local: "mount-type"}: {
findFn: fnGetMountType,
dir: true,
file: true,
},
{Space: "http://nextcloud.org/ns", Local: "rich-workspace"}: {
findFn: fnGetRichWorkspace,
dir: false,
file: false,
},
{Space: "http://nextcloud.org/ns", Local: "note"}: {
findFn: fnGetNote,
dir: true,
file: true,
},
{Space: "http://open-collaboration-services.org/ns", Local: "share-permissions"}: {
findFn: fnGetSharePermissionsOCS,
dir: true,
file: true,
},
{Space: "http://open-cloud-mesh.org/ns", Local: "share-permissions"}: {
findFn: fnGetSharePermissionsOCM,
dir: true,
file: true,
},
}
// TODO(nigeltao) merge props and allprop?
// Props returns the status of the properties named pnames for resource name.
//
// Each Propstat has a unique status and each property name will only be part
// of one Propstat element.
2021-09-20 13:43:00 +00:00
func props(ctx context.Context, fs FileSystem, name string, pnames []xml.Name) ([]Propstat, error) {
f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
isDir := fi.IsDir()
var deadProps map[xml.Name]Property
if dph, ok := f.(DeadPropsHolder); ok {
deadProps, err = dph.DeadProps()
if err != nil {
return nil, err
}
}
pstatOK := Propstat{Status: http.StatusOK}
pstatNotFound := Propstat{Status: http.StatusNotFound}
for _, pn := range pnames {
// If this file has dead properties, check if they contain pn.
if dp, ok := deadProps[pn]; ok {
pstatOK.Props = append(pstatOK.Props, dp)
continue
}
// Otherwise, it must either be a live property or we don't know it.
2021-10-04 10:06:42 +00:00
//fmt.Println(liveProps[pn], liveProps[pn].findFn, name, pn)
if prop := liveProps[pn]; prop.findFn != nil && ((prop.dir && isDir) || (prop.file && !isDir)){
2021-09-20 13:43:00 +00:00
innerXML, err := prop.findFn(ctx, fs, name, fi)
if err != nil {
return nil, err
}
pstatOK.Props = append(pstatOK.Props, Property{
XMLName: pn,
InnerXML: []byte(innerXML),
})
} else {
pstatNotFound.Props = append(pstatNotFound.Props, Property{
XMLName: pn,
})
}
}
return makePropstats(pstatOK, pstatNotFound), nil
}
// Propnames returns the property names defined for resource name.
2021-09-20 13:43:00 +00:00
func propnames(ctx context.Context, fs FileSystem, name string) ([]xml.Name, error) {
f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, err
}
isDir := fi.IsDir()
var deadProps map[xml.Name]Property
if dph, ok := f.(DeadPropsHolder); ok {
deadProps, err = dph.DeadProps()
if err != nil {
return nil, err
}
}
pnames := make([]xml.Name, 0, len(liveProps)+len(deadProps))
for pn, prop := range liveProps {
if prop.findFn != nil && (prop.dir || !isDir) {
pnames = append(pnames, pn)
}
}
for pn := range deadProps {
pnames = append(pnames, pn)
}
return pnames, nil
}
// Allprop returns the properties defined for resource name and the properties
// named in include.
//
// Note that RFC 4918 defines 'allprop' to return the DAV: properties defined
// within the RFC plus dead properties. Other live properties should only be
// returned if they are named in 'include'.
//
// See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND
2021-09-20 13:43:00 +00:00
func allprop(ctx context.Context, fs FileSystem, name string, include []xml.Name) ([]Propstat, error) {
pnames, err := propnames(ctx, fs, name)
if err != nil {
return nil, err
}
// Add names from include if they are not already covered in pnames.
nameset := make(map[xml.Name]bool)
for _, pn := range pnames {
nameset[pn] = true
}
for _, pn := range include {
if !nameset[pn] {
pnames = append(pnames, pn)
}
}
2021-09-20 13:43:00 +00:00
return props(ctx, fs, name, pnames)
}
func escapeXML(s string) string {
for i := 0; i < len(s); i++ {
// As an optimization, if s contains only ASCII letters, digits or a
// few special characters, the escaped value is s itself and we don't
// need to allocate a buffer and convert between string and []byte.
switch c := s[i]; {
case c == ' ' || c == '_' ||
('+' <= c && c <= '9') || // Digits as well as + , - . and /
('A' <= c && c <= 'Z') ||
('a' <= c && c <= 'z'):
continue
}
// Otherwise, go through the full escaping process.
var buf bytes.Buffer
xml.EscapeText(&buf, []byte(s))
return buf.String()
}
return s
}
2021-09-20 13:43:00 +00:00
func findResourceType(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
if fi.IsDir() {
2021-10-04 10:06:42 +00:00
return `<d:collection/>`, nil
}
return "", nil
}
2021-09-20 13:43:00 +00:00
func findDisplayName(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
if path.Clean(name) == "/" {
// Hide the real name of a possibly prefixed root directory.
return "", nil
}
return escapeXML(fi.Name()), nil
}
2021-09-20 13:43:00 +00:00
func findContentLength(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return strconv.FormatInt(fi.Size(), 10), nil
}
2021-09-20 13:43:00 +00:00
func findLastModified(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return fi.ModTime().UTC().Format(http.TimeFormat), nil
}
// ErrNotImplemented should be returned by optional interfaces if they
// want the original implementation to be used.
var ErrNotImplemented = errors.New("not implemented")
// 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 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)
}
2021-09-20 13:43:00 +00:00
func findContentType(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
if do, ok := fi.(ContentTyper); ok {
ctype, err := do.ContentType(ctx)
if err != ErrNotImplemented {
return ctype, err
}
}
f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
if err != nil {
return "", err
}
defer f.Close()
// This implementation is based on serveContent's code in the standard net/http package.
ctype := mime.TypeByExtension(filepath.Ext(name))
if ctype != "" {
return ctype, nil
}
// Read a chunk to decide between utf-8 text and binary.
var buf [512]byte
n, err := io.ReadFull(f, buf[:])
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
return "", err
}
ctype = http.DetectContentType(buf[:n])
// Rewind file.
_, err = f.Seek(0, os.SEEK_SET)
return ctype, err
}
2021-10-04 10:06:42 +00:00
func findETag(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size()), nil
}
2021-10-04 10:06:42 +00:00
func fnGetQuotaAvailableBytes(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "1057129933", nil
}
func fnGetQuotaUsedBytes(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "16611891", nil
}
func fnGetPermissions(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
if fi.IsDir() {
return "RGDNVCK", nil
}
2021-10-04 10:06:42 +00:00
return "RGDNVW", nil
}
func fnGetID(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return name, nil
}
func fnGetFileID(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetSize(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "16611891", nil
}
func fnGetFavorite(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetShareTypes(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetOwnerID(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "vscode", nil
}
func fnGetOwnerDisplayName(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "vscode", nil
}
func fnGetCommentsUnread(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetChecksums(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetDownloadURL(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetDataFingerprint(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetCreationTime(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetUploadTime(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetIsEncrypted(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "0", nil
}
func fnGetHasPreview(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "false", nil
}
func fnGetMountType(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetRichWorkspace(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetNote(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "", nil
}
func fnGetSharePermissionsOCS(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
if fi.IsDir() {
return "31", nil
}
return "19", nil
}
func fnGetSharePermissionsOCM(ctx context.Context, fs FileSystem, name string, fi os.FileInfo) (string, error) {
return "[\"share\",\"read\",\"write\"]", nil
2021-09-20 13:43:00 +00:00
}