diff --git a/carddav/server.go b/carddav/server.go index 24257f8..de2015d 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -260,3 +260,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address return internal.NewPropfindResponse(ao.Href, propfind, props) } + +func (b *backend) Put(r *http.Request) error { + panic("TODO") +} diff --git a/fs_local.go b/fs_local.go index 97b10d7..1b0e8a0 100644 --- a/fs_local.go +++ b/fs_local.go @@ -1,6 +1,7 @@ package webdav import ( + "io" "net/http" "os" "path" @@ -52,4 +53,12 @@ func (fs LocalFileSystem) Readdir(name string) ([]os.FileInfo, error) { return f.Readdir(-1) } +func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) { + p, err := fs.path(name) + if err != nil { + return nil, err + } + return os.Create(p) +} + var _ FileSystem = LocalFileSystem("") diff --git a/internal/server.go b/internal/server.go index 4dbfdf9..0ad024a 100644 --- a/internal/server.go +++ b/internal/server.go @@ -76,6 +76,7 @@ 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) } @@ -93,6 +94,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { err = h.handleOptions(w, r) case http.MethodGet, http.MethodHead: err = h.Backend.HeadGet(w, r) + case http.MethodPut: + err = h.Backend.Put(r) + if err == nil { + // TODO: Last-Modified, ETag, Content-Type if the request has + // been copied verbatim + // TODO: Location if the server has mutated the href + w.WriteHeader(http.StatusCreated) + } case "PROPFIND": err = h.handlePropfind(w, r) default: diff --git a/server.go b/server.go index 852fcdf..f2c08a9 100644 --- a/server.go +++ b/server.go @@ -23,6 +23,7 @@ type FileSystem interface { Open(name string) (File, error) Stat(name string) (os.FileInfo, error) Readdir(name string) ([]os.FileInfo, error) + Create(name string) (io.WriteCloser, error) } // Handler handles WebDAV HTTP requests. It can be used to create a WebDAV @@ -83,6 +84,20 @@ 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) {