mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
webdav: add DELETE support to server
This commit is contained in:
parent
69f88b075a
commit
41b68829e8
@ -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")
|
||||||
|
}
|
||||||
|
@ -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("")
|
||||||
|
@ -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:
|
||||||
|
45
server.go
45
server.go
@ -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)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user