1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/net/context"
21 // Proppatch describes a property update instruction as defined in RFC 4918.
22 // See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPPATCH
23 type Proppatch struct {
24 // Remove specifies whether this patch removes properties. If it does not
25 // remove them, it sets them.
27 // Props contains the properties to be set or removed.
31 // Propstat describes a XML propstat element as defined in RFC 4918.
32 // See http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat
33 type Propstat struct {
34 // Props contains the properties for which Status applies.
37 // Status defines the HTTP status code of the properties in Prop.
38 // Allowed values include, but are not limited to the WebDAV status
39 // code extensions for HTTP/1.1.
40 // http://www.webdav.org/specs/rfc4918.html#status.code.extensions.to.http11
43 // XMLError contains the XML representation of the optional error element.
44 // XML content within this field must not rely on any predefined
45 // namespace declarations or prefixes. If empty, the XML error element
49 // ResponseDescription contains the contents of the optional
50 // responsedescription field. If empty, the XML element is omitted.
51 ResponseDescription string
54 // makePropstats returns a slice containing those of x and y whose Props slice
55 // is non-empty. If both are empty, it returns a slice containing an otherwise
56 // zero Propstat whose HTTP status code is 200 OK.
57 func makePropstats(x, y Propstat) []Propstat {
58 pstats := make([]Propstat, 0, 2)
59 if len(x.Props) != 0 {
60 pstats = append(pstats, x)
62 if len(y.Props) != 0 {
63 pstats = append(pstats, y)
66 pstats = append(pstats, Propstat{
67 Status: http.StatusOK,
73 // DeadPropsHolder holds the dead properties of a resource.
75 // Dead properties are those properties that are explicitly defined. In
76 // comparison, live properties, such as DAV:getcontentlength, are implicitly
77 // defined by the underlying resource, and cannot be explicitly overridden or
78 // removed. See the Terminology section of
79 // http://www.webdav.org/specs/rfc4918.html#rfc.section.3
81 // There is a whitelist of the names of live properties. This package handles
82 // all live properties, and will only pass non-whitelisted names to the Patch
83 // method of DeadPropsHolder implementations.
84 type DeadPropsHolder interface {
85 // DeadProps returns a copy of the dead properties held.
86 DeadProps() (map[xml.Name]Property, error)
88 // Patch patches the dead properties held.
90 // Patching is atomic; either all or no patches succeed. It returns (nil,
91 // non-nil) if an internal server error occurred, otherwise the Propstats
92 // collectively contain one Property for each proposed patch Property. If
93 // all patches succeed, Patch returns a slice of length one and a Propstat
94 // element with a 200 OK HTTP status code. If none succeed, for reasons
95 // other than an internal server error, no Propstat has status 200 OK.
97 // For more details on when various HTTP status codes apply, see
98 // http://www.webdav.org/specs/rfc4918.html#PROPPATCH-status
99 Patch([]Proppatch) ([]Propstat, error)
102 // liveProps contains all supported, protected DAV: properties.
103 var liveProps = map[xml.Name]struct {
104 // findFn implements the propfind function of this property. If nil,
105 // it indicates a hidden property.
106 findFn func(context.Context, FileSystem, LockSystem, string, os.FileInfo) (string, error)
107 // dir is true if the property applies to directories.
110 {Space: "DAV:", Local: "resourcetype"}: {
111 findFn: findResourceType,
114 {Space: "DAV:", Local: "displayname"}: {
115 findFn: findDisplayName,
118 {Space: "DAV:", Local: "getcontentlength"}: {
119 findFn: findContentLength,
122 {Space: "DAV:", Local: "getlastmodified"}: {
123 findFn: findLastModified,
124 // http://webdav.org/specs/rfc4918.html#PROPERTY_getlastmodified
125 // suggests that getlastmodified should only apply to GETable
126 // resources, and this package does not support GET on directories.
128 // Nonetheless, some WebDAV clients expect child directories to be
129 // sortable by getlastmodified date, so this value is true, not false.
130 // See golang.org/issue/15334.
133 {Space: "DAV:", Local: "creationdate"}: {
137 {Space: "DAV:", Local: "getcontentlanguage"}: {
141 {Space: "DAV:", Local: "getcontenttype"}: {
142 findFn: findContentType,
145 {Space: "DAV:", Local: "getetag"}: {
147 // findETag implements ETag as the concatenated hex values of a file's
148 // modification time and size. This is not a reliable synchronization
149 // mechanism for directories, so we do not advertise getetag for DAV
154 // TODO: The lockdiscovery property requires LockSystem to list the
155 // active locks on a resource.
156 {Space: "DAV:", Local: "lockdiscovery"}: {},
157 {Space: "DAV:", Local: "supportedlock"}: {
158 findFn: findSupportedLock,
163 // TODO(nigeltao) merge props and allprop?
165 // Props returns the status of the properties named pnames for resource name.
167 // Each Propstat has a unique status and each property name will only be part
168 // of one Propstat element.
169 func props(ctx context.Context, fs FileSystem, ls LockSystem, name string, pnames []xml.Name) ([]Propstat, error) {
170 f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
181 var deadProps map[xml.Name]Property
182 if dph, ok := f.(DeadPropsHolder); ok {
183 deadProps, err = dph.DeadProps()
189 pstatOK := Propstat{Status: http.StatusOK}
190 pstatNotFound := Propstat{Status: http.StatusNotFound}
191 for _, pn := range pnames {
192 // If this file has dead properties, check if they contain pn.
193 if dp, ok := deadProps[pn]; ok {
194 pstatOK.Props = append(pstatOK.Props, dp)
197 // Otherwise, it must either be a live property or we don't know it.
198 if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !isDir) {
199 innerXML, err := prop.findFn(ctx, fs, ls, name, fi)
203 pstatOK.Props = append(pstatOK.Props, Property{
205 InnerXML: []byte(innerXML),
208 pstatNotFound.Props = append(pstatNotFound.Props, Property{
213 return makePropstats(pstatOK, pstatNotFound), nil
216 // Propnames returns the property names defined for resource name.
217 func propnames(ctx context.Context, fs FileSystem, ls LockSystem, name string) ([]xml.Name, error) {
218 f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
229 var deadProps map[xml.Name]Property
230 if dph, ok := f.(DeadPropsHolder); ok {
231 deadProps, err = dph.DeadProps()
237 pnames := make([]xml.Name, 0, len(liveProps)+len(deadProps))
238 for pn, prop := range liveProps {
239 if prop.findFn != nil && (prop.dir || !isDir) {
240 pnames = append(pnames, pn)
243 for pn := range deadProps {
244 pnames = append(pnames, pn)
249 // Allprop returns the properties defined for resource name and the properties
252 // Note that RFC 4918 defines 'allprop' to return the DAV: properties defined
253 // within the RFC plus dead properties. Other live properties should only be
254 // returned if they are named in 'include'.
256 // See http://www.webdav.org/specs/rfc4918.html#METHOD_PROPFIND
257 func allprop(ctx context.Context, fs FileSystem, ls LockSystem, name string, include []xml.Name) ([]Propstat, error) {
258 pnames, err := propnames(ctx, fs, ls, name)
262 // Add names from include if they are not already covered in pnames.
263 nameset := make(map[xml.Name]bool)
264 for _, pn := range pnames {
267 for _, pn := range include {
269 pnames = append(pnames, pn)
272 return props(ctx, fs, ls, name, pnames)
275 // Patch patches the properties of resource name. The return values are
276 // constrained in the same manner as DeadPropsHolder.Patch.
277 func patch(ctx context.Context, fs FileSystem, ls LockSystem, name string, patches []Proppatch) ([]Propstat, error) {
280 for _, patch := range patches {
281 for _, p := range patch.Props {
282 if _, ok := liveProps[p.XMLName]; ok {
289 pstatForbidden := Propstat{
290 Status: http.StatusForbidden,
291 XMLError: `<D:cannot-modify-protected-property xmlns:D="DAV:"/>`,
293 pstatFailedDep := Propstat{
294 Status: StatusFailedDependency,
296 for _, patch := range patches {
297 for _, p := range patch.Props {
298 if _, ok := liveProps[p.XMLName]; ok {
299 pstatForbidden.Props = append(pstatForbidden.Props, Property{XMLName: p.XMLName})
301 pstatFailedDep.Props = append(pstatFailedDep.Props, Property{XMLName: p.XMLName})
305 return makePropstats(pstatForbidden, pstatFailedDep), nil
308 f, err := fs.OpenFile(ctx, name, os.O_RDWR, 0)
313 if dph, ok := f.(DeadPropsHolder); ok {
314 ret, err := dph.Patch(patches)
318 // http://www.webdav.org/specs/rfc4918.html#ELEMENT_propstat says that
319 // "The contents of the prop XML element must only list the names of
320 // properties to which the result in the status element applies."
321 for _, pstat := range ret {
322 for i, p := range pstat.Props {
323 pstat.Props[i] = Property{XMLName: p.XMLName}
328 // The file doesn't implement the optional DeadPropsHolder interface, so
329 // all patches are forbidden.
330 pstat := Propstat{Status: http.StatusForbidden}
331 for _, patch := range patches {
332 for _, p := range patch.Props {
333 pstat.Props = append(pstat.Props, Property{XMLName: p.XMLName})
336 return []Propstat{pstat}, nil
339 func escapeXML(s string) string {
340 for i := 0; i < len(s); i++ {
341 // As an optimization, if s contains only ASCII letters, digits or a
342 // few special characters, the escaped value is s itself and we don't
343 // need to allocate a buffer and convert between string and []byte.
345 case c == ' ' || c == '_' ||
346 ('+' <= c && c <= '9') || // Digits as well as + , - . and /
347 ('A' <= c && c <= 'Z') ||
348 ('a' <= c && c <= 'z'):
351 // Otherwise, go through the full escaping process.
353 xml.EscapeText(&buf, []byte(s))
359 func findResourceType(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
361 return `<D:collection xmlns:D="DAV:"/>`, nil
366 func findDisplayName(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
367 if slashClean(name) == "/" {
368 // Hide the real name of a possibly prefixed root directory.
371 return escapeXML(fi.Name()), nil
374 func findContentLength(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
375 return strconv.FormatInt(fi.Size(), 10), nil
378 func findLastModified(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
379 return fi.ModTime().Format(http.TimeFormat), nil
382 func findContentType(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
383 f, err := fs.OpenFile(ctx, name, os.O_RDONLY, 0)
388 // This implementation is based on serveContent's code in the standard net/http package.
389 ctype := mime.TypeByExtension(filepath.Ext(name))
393 // Read a chunk to decide between utf-8 text and binary.
395 n, err := io.ReadFull(f, buf[:])
396 if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
399 ctype = http.DetectContentType(buf[:n])
401 _, err = f.Seek(0, os.SEEK_SET)
405 func findETag(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
406 // The Apache http 2.4 web server by default concatenates the
407 // modification time and size of a file. We replicate the heuristic
408 // with nanosecond granularity.
409 return fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size()), nil
412 func findSupportedLock(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
414 `<D:lockentry xmlns:D="DAV:">` +
415 `<D:lockscope><D:exclusive/></D:lockscope>` +
416 `<D:locktype><D:write/></D:locktype>` +
417 `</D:lockentry>`, nil