123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- package webdav
- import (
- "bytes"
- "context"
- "encoding/xml"
- "errors"
- "fmt"
- "io"
- "mime"
- "net/http"
- "os"
- "path/filepath"
- "strconv"
- )
- type Proppatch struct {
-
-
- Remove bool
-
- Props []Property
- }
- type Propstat struct {
-
- Props []Property
-
-
-
-
- Status int
-
-
-
-
- XMLError string
-
-
- ResponseDescription string
- }
- 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
- }
- type DeadPropsHolder interface {
-
- DeadProps() (map[xml.Name]Property, error)
-
-
-
-
-
-
-
-
-
-
-
- Patch([]Proppatch) ([]Propstat, error)
- }
- var liveProps = map[xml.Name]struct {
-
-
- findFn func(context.Context, FileSystem, LockSystem, string, os.FileInfo) (string, error)
-
- dir bool
- }{
- {Space: "DAV:", Local: "resourcetype"}: {
- findFn: findResourceType,
- dir: true,
- },
- {Space: "DAV:", Local: "displayname"}: {
- findFn: findDisplayName,
- dir: true,
- },
- {Space: "DAV:", Local: "getcontentlength"}: {
- findFn: findContentLength,
- dir: false,
- },
- {Space: "DAV:", Local: "getlastmodified"}: {
- findFn: findLastModified,
-
-
-
-
-
-
-
- dir: true,
- },
- {Space: "DAV:", Local: "creationdate"}: {
- findFn: nil,
- dir: false,
- },
- {Space: "DAV:", Local: "getcontentlanguage"}: {
- findFn: nil,
- dir: false,
- },
- {Space: "DAV:", Local: "getcontenttype"}: {
- findFn: findContentType,
- dir: false,
- },
- {Space: "DAV:", Local: "getetag"}: {
- findFn: findETag,
-
-
-
-
- dir: false,
- },
-
-
- {Space: "DAV:", Local: "lockdiscovery"}: {},
- {Space: "DAV:", Local: "supportedlock"}: {
- findFn: findSupportedLock,
- dir: true,
- },
- }
- func props(ctx context.Context, fs FileSystem, ls LockSystem, 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 dp, ok := deadProps[pn]; ok {
- pstatOK.Props = append(pstatOK.Props, dp)
- continue
- }
-
- if prop := liveProps[pn]; prop.findFn != nil && (prop.dir || !isDir) {
- innerXML, err := prop.findFn(ctx, fs, ls, 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
- }
- func propnames(ctx context.Context, fs FileSystem, ls LockSystem, 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
- }
- func allprop(ctx context.Context, fs FileSystem, ls LockSystem, name string, include []xml.Name) ([]Propstat, error) {
- pnames, err := propnames(ctx, fs, ls, name)
- if err != nil {
- return nil, err
- }
-
- nameset := make(map[xml.Name]bool)
- for _, pn := range pnames {
- nameset[pn] = true
- }
- for _, pn := range include {
- if !nameset[pn] {
- pnames = append(pnames, pn)
- }
- }
- return props(ctx, fs, ls, name, pnames)
- }
- func patch(ctx context.Context, fs FileSystem, ls LockSystem, name string, patches []Proppatch) ([]Propstat, error) {
- conflict := false
- loop:
- for _, patch := range patches {
- for _, p := range patch.Props {
- if _, ok := liveProps[p.XMLName]; ok {
- conflict = true
- break loop
- }
- }
- }
- if conflict {
- pstatForbidden := Propstat{
- Status: http.StatusForbidden,
- XMLError: `<D:cannot-modify-protected-property xmlns:D="DAV:"/>`,
- }
- pstatFailedDep := Propstat{
- Status: StatusFailedDependency,
- }
- for _, patch := range patches {
- for _, p := range patch.Props {
- if _, ok := liveProps[p.XMLName]; ok {
- pstatForbidden.Props = append(pstatForbidden.Props, Property{XMLName: p.XMLName})
- } else {
- pstatFailedDep.Props = append(pstatFailedDep.Props, Property{XMLName: p.XMLName})
- }
- }
- }
- return makePropstats(pstatForbidden, pstatFailedDep), nil
- }
- f, err := fs.OpenFile(ctx, name, os.O_RDWR, 0)
- if err != nil {
- return nil, err
- }
- defer f.Close()
- if dph, ok := f.(DeadPropsHolder); ok {
- ret, err := dph.Patch(patches)
- if err != nil {
- return nil, err
- }
-
-
-
- for _, pstat := range ret {
- for i, p := range pstat.Props {
- pstat.Props[i] = Property{XMLName: p.XMLName}
- }
- }
- return ret, nil
- }
-
-
- pstat := Propstat{Status: http.StatusForbidden}
- for _, patch := range patches {
- for _, p := range patch.Props {
- pstat.Props = append(pstat.Props, Property{XMLName: p.XMLName})
- }
- }
- return []Propstat{pstat}, nil
- }
- func escapeXML(s string) string {
- for i := 0; i < len(s); i++ {
-
-
-
- switch c := s[i]; {
- case c == ' ' || c == '_' ||
- ('+' <= c && c <= '9') ||
- ('A' <= c && c <= 'Z') ||
- ('a' <= c && c <= 'z'):
- continue
- }
-
- var buf bytes.Buffer
- xml.EscapeText(&buf, []byte(s))
- return buf.String()
- }
- return s
- }
- func findResourceType(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- if fi.IsDir() {
- return `<D:collection xmlns:D="DAV:"/>`, nil
- }
- return "", nil
- }
- func findDisplayName(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- if slashClean(name) == "/" {
-
- return "", nil
- }
- return escapeXML(fi.Name()), nil
- }
- func findContentLength(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- return strconv.FormatInt(fi.Size(), 10), nil
- }
- func findLastModified(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- return fi.ModTime().UTC().Format(http.TimeFormat), nil
- }
- var ErrNotImplemented = errors.New("not implemented")
- type ContentTyper interface {
-
-
-
-
-
- ContentType(ctx context.Context) (string, error)
- }
- func findContentType(ctx context.Context, fs FileSystem, ls LockSystem, 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()
-
- ctype := mime.TypeByExtension(filepath.Ext(name))
- if ctype != "" {
- return ctype, nil
- }
-
- 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])
-
- _, err = f.Seek(0, io.SeekStart)
- return ctype, err
- }
- type ETager interface {
-
-
-
-
-
-
- ETag(ctx context.Context) (string, error)
- }
- func findETag(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- if do, ok := fi.(ETager); ok {
- etag, err := do.ETag(ctx)
- if err != ErrNotImplemented {
- return etag, err
- }
- }
-
-
-
- return fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size()), nil
- }
- func findSupportedLock(ctx context.Context, fs FileSystem, ls LockSystem, name string, fi os.FileInfo) (string, error) {
- return `` +
- `<D:lockentry xmlns:D="DAV:">` +
- `<D:lockscope><D:exclusive/></D:lockscope>` +
- `<D:locktype><D:write/></D:locktype>` +
- `</D:lockentry>`, nil
- }
|