diff --git a/carddav/server.go b/carddav/server.go index de2015d..90e6f1b 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -264,3 +264,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address func (b *backend) Put(r *http.Request) error { panic("TODO") } + +func (b *backend) Delete(r *http.Request) error { + panic("TODO") +} diff --git a/client.go b/client.go index 46d334f..2470888 100644 --- a/client.go +++ b/client.go @@ -160,7 +160,7 @@ func (c *Client) Readdir(name string) ([]os.FileInfo, error) { } type fileWriter struct { - pw *io.PipeWriter + pw *io.PipeWriter done <-chan error } diff --git a/fs_local.go b/fs_local.go index 1b0e8a0..a3809f8 100644 --- a/fs_local.go +++ b/fs_local.go @@ -61,4 +61,12 @@ func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) { 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("") diff --git a/internal/server.go b/internal/server.go index 0ad024a..2b37d91 100644 --- a/internal/server.go +++ b/internal/server.go @@ -76,8 +76,9 @@ func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error { type Backend interface { Options(r *http.Request) ([]string, error) HeadGet(w http.ResponseWriter, r *http.Request) error - Put(r *http.Request) error Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error) + Put(r *http.Request) error + Delete(r *http.Request) error } 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 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": err = h.handlePropfind(w, r) default: diff --git a/server.go b/server.go index c816542..d6fc4c9 100644 --- a/server.go +++ b/server.go @@ -24,6 +24,7 @@ type FileSystem interface { Stat(name string) (os.FileInfo, error) Readdir(name string) ([]os.FileInfo, error) Create(name string) (io.WriteCloser, error) + RemoveAll(name string) error } // 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() { - return []string{http.MethodOptions, "PROPFIND"}, nil + return []string{http.MethodOptions, http.MethodDelete, "PROPFIND"}, nil } else { return []string{ http.MethodOptions, http.MethodHead, http.MethodGet, http.MethodPut, + http.MethodDelete, "PROPFIND", }, nil } @@ -90,20 +92,6 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error { 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) { fi, err := b.FileSystem.Stat(r.URL.Path) 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} 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) +}