webdav: add support for PUT to server

This commit is contained in:
Simon Ser 2020-01-21 21:19:44 +01:00
parent 45774fe572
commit 7d6de88179
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 37 additions and 0 deletions

View File

@ -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")
}

View File

@ -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("")

View File

@ -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:

View File

@ -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) {