webdav: add DELETE support to server

This commit is contained in:
Simon Ser 2020-01-21 21:46:01 +01:00
parent 69f88b075a
commit 41b68829e8
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
5 changed files with 51 additions and 17 deletions

View File

@ -264,3 +264,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address
func (b *backend) Put(r *http.Request) error { func (b *backend) Put(r *http.Request) error {
panic("TODO") panic("TODO")
} }
func (b *backend) Delete(r *http.Request) error {
panic("TODO")
}

View File

@ -160,7 +160,7 @@ func (c *Client) Readdir(name string) ([]os.FileInfo, error) {
} }
type fileWriter struct { type fileWriter struct {
pw *io.PipeWriter pw *io.PipeWriter
done <-chan error done <-chan error
} }

View File

@ -61,4 +61,12 @@ func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {
return os.Create(p) return os.Create(p)
} }
func (fs LocalFileSystem) RemoveAll(name string) error {
p, err := fs.path(name)
if err != nil {
return err
}
return os.RemoveAll(p)
}
var _ FileSystem = LocalFileSystem("") var _ FileSystem = LocalFileSystem("")

View File

@ -76,8 +76,9 @@ func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error {
type Backend interface { type Backend interface {
Options(r *http.Request) ([]string, error) Options(r *http.Request) ([]string, error)
HeadGet(w http.ResponseWriter, r *http.Request) error HeadGet(w http.ResponseWriter, r *http.Request) error
Put(r *http.Request) error
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error) Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
Put(r *http.Request) error
Delete(r *http.Request) error
} }
type Handler struct { type Handler struct {
@ -102,6 +103,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// TODO: Location if the server has mutated the href // TODO: Location if the server has mutated the href
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
} }
case http.MethodDelete:
// TODO: send a multistatus in case of partial failure
err = h.Backend.Delete(r)
if err == nil {
w.WriteHeader(http.StatusNoContent)
}
case "PROPFIND": case "PROPFIND":
err = h.handlePropfind(w, r) err = h.handlePropfind(w, r)
default: default:

View File

@ -24,6 +24,7 @@ type FileSystem interface {
Stat(name string) (os.FileInfo, error) Stat(name string) (os.FileInfo, error)
Readdir(name string) ([]os.FileInfo, error) Readdir(name string) ([]os.FileInfo, error)
Create(name string) (io.WriteCloser, error) Create(name string) (io.WriteCloser, error)
RemoveAll(name string) error
} }
// Handler handles WebDAV HTTP requests. It can be used to create a WebDAV // Handler handles WebDAV HTTP requests. It can be used to create a WebDAV
@ -57,13 +58,14 @@ func (b *backend) Options(r *http.Request) ([]string, error) {
} }
if fi.IsDir() { if fi.IsDir() {
return []string{http.MethodOptions, "PROPFIND"}, nil return []string{http.MethodOptions, http.MethodDelete, "PROPFIND"}, nil
} else { } else {
return []string{ return []string{
http.MethodOptions, http.MethodOptions,
http.MethodHead, http.MethodHead,
http.MethodGet, http.MethodGet,
http.MethodPut, http.MethodPut,
http.MethodDelete,
"PROPFIND", "PROPFIND",
}, nil }, nil
} }
@ -90,20 +92,6 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
return nil return nil
} }
func (b *backend) Put(r *http.Request) error {
wc, err := b.FileSystem.Create(r.URL.Path)
if err != nil {
return err
}
defer wc.Close()
if _, err := io.Copy(wc, r.Body); err != nil {
return err
}
return wc.Close()
}
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) { func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
fi, err := b.FileSystem.Stat(r.URL.Path) fi, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@ -182,3 +170,30 @@ func (b *backend) propfindFile(propfind *internal.Propfind, name string, fi os.F
u := url.URL{Path: name} u := url.URL{Path: name}
return internal.NewPropfindResponse(u.String(), propfind, props) return internal.NewPropfindResponse(u.String(), propfind, props)
} }
func (b *backend) Put(r *http.Request) error {
wc, err := b.FileSystem.Create(r.URL.Path)
if err != nil {
return err
}
defer wc.Close()
if _, err := io.Copy(wc, r.Body); err != nil {
return err
}
return wc.Close()
}
func (b *backend) Delete(r *http.Request) error {
// WebDAV semantics are that it should return a "404 Not Found" error in
// case the resource doesn't exist. We need to Stat before RemoveAll.
_, err := b.FileSystem.Stat(r.URL.Path)
if os.IsNotExist(err) {
return &internal.HTTPError{Code: http.StatusNotFound, Err: err}
} else if err != nil {
return err
}
return b.FileSystem.RemoveAll(r.URL.Path)
}