webdav: add MKCOL support to server

This commit is contained in:
Simon Ser 2020-01-21 22:05:59 +01:00
parent a2ad695145
commit e9e1f102de
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 36 additions and 1 deletions

View File

@ -268,3 +268,7 @@ func (b *backend) Put(r *http.Request) error {
func (b *backend) Delete(r *http.Request) error {
panic("TODO")
}
func (b *backend) Mkcol(r *http.Request) error {
return internal.HTTPErrorf(http.StatusForbidden, "carddav: address book creation unsupported")
}

View File

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

View File

@ -79,6 +79,7 @@ type Backend interface {
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
Put(r *http.Request) error
Delete(r *http.Request) error
Mkcol(r *http.Request) error
}
type Handler struct {
@ -111,6 +112,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
case "PROPFIND":
err = h.handlePropfind(w, r)
case "MKCOL":
err = h.Backend.Mkcol(r)
if err == nil {
w.WriteHeader(http.StatusCreated)
}
default:
err = HTTPErrorf(http.StatusMethodNotAllowed, "webdav: unsupported method")
}

View File

@ -25,6 +25,7 @@ type FileSystem interface {
Readdir(name string) ([]os.FileInfo, error)
Create(name string) (io.WriteCloser, error)
RemoveAll(name string) error
Mkdir(name string) error
}
// Handler handles WebDAV HTTP requests. It can be used to create a WebDAV
@ -58,7 +59,12 @@ func (b *backend) Options(r *http.Request) ([]string, error) {
}
if fi.IsDir() {
return []string{http.MethodOptions, http.MethodDelete, "PROPFIND"}, nil
return []string{
http.MethodOptions,
http.MethodDelete,
"PROPFIND",
"MKCOL",
}, nil
} else {
return []string{
http.MethodOptions,
@ -192,3 +198,14 @@ func (b *backend) Delete(r *http.Request) error {
}
return err
}
func (b *backend) Mkcol(r *http.Request) error {
if r.Header.Get("Content-Type") != "" {
return internal.HTTPErrorf(http.StatusUnsupportedMediaType, "webdav: request body not supported in MKCOL request")
}
err := b.FileSystem.Mkdir(r.URL.Path)
if os.IsNotExist(err) {
return &internal.HTTPError{Code: http.StatusConflict, Err: err}
}
return err
}