Allow servers to return DAV capabilities in OPTIONS

This commit is contained in:
Simon Ser 2020-01-29 18:03:47 +01:00
parent 5f03e421d3
commit 8937358ac1
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 34 additions and 30 deletions

View File

@ -214,22 +214,29 @@ type backend struct {
Backend Backend Backend Backend
} }
func (b *backend) Options(r *http.Request) ([]string, error) { func (b *backend) Options(r *http.Request) (caps []string, allow []string, err error) {
// TODO: add DAV: addressbook caps = []string{"addressbook"}
if r.URL.Path == "/" { if r.URL.Path == "/" {
return []string{http.MethodOptions, "PROPFIND"}, nil return caps, []string{http.MethodOptions, "PROPFIND", "REPORT"}, nil
} }
var dataReq AddressDataRequest var dataReq AddressDataRequest
_, err := b.Backend.GetAddressObject(r.URL.Path, &dataReq) _, err = b.Backend.GetAddressObject(r.URL.Path, &dataReq)
if httpErr, ok := err.(*internal.HTTPError); ok && httpErr.Code == http.StatusNotFound { if httpErr, ok := err.(*internal.HTTPError); ok && httpErr.Code == http.StatusNotFound {
return []string{http.MethodOptions}, nil return caps, []string{http.MethodOptions, http.MethodPut}, nil
} else if err != nil { } else if err != nil {
return nil, err return nil, nil, err
} }
return []string{http.MethodOptions, http.MethodHead, http.MethodGet, "PROPFIND"}, nil return caps, []string{
http.MethodOptions,
http.MethodHead,
http.MethodGet,
http.MethodPut,
http.MethodDelete,
"PROPFIND",
}, nil
} }
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error { func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {

View File

@ -75,7 +75,7 @@ func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error {
} }
type Backend interface { type Backend interface {
Options(r *http.Request) ([]string, error) Options(r *http.Request) (caps []string, allow []string, err error)
HeadGet(w http.ResponseWriter, r *http.Request) error HeadGet(w http.ResponseWriter, r *http.Request) error
Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error) Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, error)
Proppatch(r *http.Request, pu *Propertyupdate) (*Response, error) Proppatch(r *http.Request, pu *Propertyupdate) (*Response, error)
@ -140,13 +140,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error { func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) error {
methods, err := h.Backend.Options(r) caps, allow, err := h.Backend.Options(r)
if err != nil { if err != nil {
return err return err
} }
caps = append([]string{"1", "3"}, caps...)
w.Header().Add("Allow", strings.Join(methods, ", ")) w.Header().Add("DAV", strings.Join(caps, ", "))
w.Header().Add("DAV", "1, 3") w.Header().Add("Allow", strings.Join(allow, ", "))
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
return nil return nil
} }

View File

@ -45,31 +45,27 @@ type backend struct {
FileSystem FileSystem FileSystem FileSystem
} }
func (b *backend) Options(r *http.Request) ([]string, error) { func (b *backend) Options(r *http.Request) (caps []string, allow []string, err 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) {
return []string{http.MethodOptions}, nil return nil, []string{http.MethodOptions, http.MethodPut, "MKCOL"}, nil
} else if err != nil { } else if err != nil {
return nil, err return nil, nil, err
} }
if fi.IsDir { allow = []string{
return []string{ http.MethodOptions,
http.MethodOptions, http.MethodDelete,
http.MethodDelete, "PROPFIND",
"PROPFIND", "COPY",
"MKCOL", "MOVE",
}, nil
} else {
return []string{
http.MethodOptions,
http.MethodHead,
http.MethodGet,
http.MethodPut,
http.MethodDelete,
"PROPFIND",
}, nil
} }
if !fi.IsDir {
allow = append(allow, http.MethodHead, http.MethodGet, http.MethodPut)
}
return nil, allow, nil
} }
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error { func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {