2020-01-15 17:21:27 +00:00
|
|
|
package webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-01-15 17:39:25 +00:00
|
|
|
"path"
|
2020-01-15 17:21:27 +00:00
|
|
|
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
type File interface {
|
|
|
|
http.File
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileSystem interface {
|
|
|
|
Open(name string) (File, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
FileSystem FileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.FileSystem == nil {
|
2020-01-17 10:30:42 +00:00
|
|
|
http.Error(w, "webdav: no filesystem available", http.StatusInternalServerError)
|
|
|
|
return
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
b := backend{h.FileSystem}
|
|
|
|
hh := internal.Handler{&b}
|
|
|
|
hh.ServeHTTP(w, r)
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
type backend struct {
|
|
|
|
FileSystem FileSystem
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:41:44 +00:00
|
|
|
func (b *backend) Options(r *http.Request) ([]string, error) {
|
|
|
|
f, err := b.FileSystem.Open(r.URL.Path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return []string{http.MethodOptions}, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
|
|
|
return []string{http.MethodOptions, "PROPFIND"}, nil
|
|
|
|
} else {
|
|
|
|
return []string{http.MethodOptions, http.MethodHead, http.MethodGet, "PROPFIND"}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
f, err := b.FileSystem.Open(r.URL.Path)
|
2020-01-15 17:21:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:32:13 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
|
|
|
|
}
|
|
|
|
|
2020-01-15 17:21:27 +00:00
|
|
|
http.ServeContent(w, r, r.URL.Path, fi.ModTime(), f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
|
|
|
|
f, err := b.FileSystem.Open(r.URL.Path)
|
2020-01-15 17:21:27 +00:00
|
|
|
if err != nil {
|
2020-01-17 10:30:42 +00:00
|
|
|
return nil, err
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := f.Stat()
|
|
|
|
if err != nil {
|
2020-01-17 10:30:42 +00:00
|
|
|
return nil, err
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 17:39:25 +00:00
|
|
|
var resps []internal.Response
|
2020-01-17 10:30:42 +00:00
|
|
|
if err := b.propfind(propfind, r.URL.Path, fi, depth, &resps); err != nil {
|
|
|
|
return nil, err
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
return internal.NewMultistatus(resps...), nil
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
func (b *backend) propfind(propfind *internal.Propfind, name string, fi os.FileInfo, depth internal.Depth, resps *[]internal.Response) error {
|
2020-01-15 17:39:25 +00:00
|
|
|
// TODO: use partial error Response on error
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
resp, err := b.propfindFile(propfind, name, fi)
|
2020-01-15 17:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*resps = append(*resps, *resp)
|
|
|
|
|
|
|
|
if depth != internal.DepthZero && fi.IsDir() {
|
|
|
|
childDepth := depth
|
|
|
|
if depth == internal.DepthOne {
|
|
|
|
childDepth = internal.DepthZero
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
f, err := b.FileSystem.Open(name)
|
2020-01-15 17:39:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
children, err := f.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child := range children {
|
2020-01-17 10:30:42 +00:00
|
|
|
if err := b.propfind(propfind, path.Join(name, child.Name()), child, childDepth, resps); err != nil {
|
2020-01-15 17:39:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 10:30:42 +00:00
|
|
|
func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.FileInfo) (*internal.Response, error) {
|
2020-01-17 13:40:29 +00:00
|
|
|
props := make(map[xml.Name]internal.PropfindFunc)
|
2020-01-15 17:21:27 +00:00
|
|
|
|
2020-01-17 13:40:29 +00:00
|
|
|
props[xml.Name{"DAV:", "resourcetype"}] = func(*internal.RawXMLValue) (interface{}, error) {
|
2020-01-15 17:21:27 +00:00
|
|
|
var types []xml.Name
|
|
|
|
if fi.IsDir() {
|
|
|
|
types = append(types, internal.CollectionName)
|
|
|
|
}
|
|
|
|
return internal.NewResourceType(types...), nil
|
2020-01-17 13:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
props[xml.Name{"DAV:", "getcontentlength"}] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &internal.GetContentLength{Length: fi.Size()}, nil
|
2020-01-15 18:08:38 +00:00
|
|
|
}
|
2020-01-17 13:40:29 +00:00
|
|
|
props[xml.Name{"DAV:", "getcontenttype"}] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
t := mime.TypeByExtension(path.Ext(name))
|
|
|
|
if t == "" {
|
|
|
|
// TODO: use http.DetectContentType
|
|
|
|
return nil, &internal.HTTPError{Code: http.StatusNotFound}
|
|
|
|
}
|
|
|
|
return &internal.GetContentType{Type: t}, nil
|
2020-01-15 18:08:38 +00:00
|
|
|
}
|
2020-01-17 13:40:29 +00:00
|
|
|
props[xml.Name{"DAV:", "getlastmodified"}] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime())}, nil
|
2020-01-15 18:08:38 +00:00
|
|
|
}
|
2020-01-17 13:40:29 +00:00
|
|
|
// TODO: getetag
|
|
|
|
}
|
|
|
|
|
|
|
|
return internal.NewPropfindResponse(name, propfind, props)
|
2020-01-15 17:21:27 +00:00
|
|
|
}
|