diff --git a/carddav/server.go b/carddav/server.go index c1522f1..04c0836 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -214,22 +214,29 @@ type backend struct { Backend Backend } -func (b *backend) Options(r *http.Request) ([]string, error) { - // TODO: add DAV: addressbook +func (b *backend) Options(r *http.Request) (caps []string, allow []string, err error) { + caps = []string{"addressbook"} if r.URL.Path == "/" { - return []string{http.MethodOptions, "PROPFIND"}, nil + return caps, []string{http.MethodOptions, "PROPFIND", "REPORT"}, nil } 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 { - return []string{http.MethodOptions}, nil + return caps, []string{http.MethodOptions, http.MethodPut}, 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 { diff --git a/internal/server.go b/internal/server.go index abd6d6c..5ecbe99 100644 --- a/internal/server.go +++ b/internal/server.go @@ -75,7 +75,7 @@ func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error { } 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 Propfind(r *http.Request, pf *Propfind, depth Depth) (*Multistatus, 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 { - methods, err := h.Backend.Options(r) + caps, allow, err := h.Backend.Options(r) if err != nil { return err } + caps = append([]string{"1", "3"}, caps...) - w.Header().Add("Allow", strings.Join(methods, ", ")) - w.Header().Add("DAV", "1, 3") + w.Header().Add("DAV", strings.Join(caps, ", ")) + w.Header().Add("Allow", strings.Join(allow, ", ")) w.WriteHeader(http.StatusNoContent) return nil } diff --git a/server.go b/server.go index 09aca28..3279f5d 100644 --- a/server.go +++ b/server.go @@ -45,31 +45,27 @@ type backend struct { 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) if os.IsNotExist(err) { - return []string{http.MethodOptions}, nil + return nil, []string{http.MethodOptions, http.MethodPut, "MKCOL"}, nil } else if err != nil { - return nil, err + return nil, nil, err } - if fi.IsDir { - return []string{ - http.MethodOptions, - http.MethodDelete, - "PROPFIND", - "MKCOL", - }, nil - } else { - return []string{ - http.MethodOptions, - http.MethodHead, - http.MethodGet, - http.MethodPut, - http.MethodDelete, - "PROPFIND", - }, nil + allow = []string{ + http.MethodOptions, + http.MethodDelete, + "PROPFIND", + "COPY", + "MOVE", } + + 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 {