diff --git a/carddav/server.go b/carddav/server.go index 52e3523..5e0b259 100644 --- a/carddav/server.go +++ b/carddav/server.go @@ -5,7 +5,6 @@ import ( "context" "encoding/xml" "fmt" - "io" "mime" "net/http" "path" @@ -703,13 +702,7 @@ func (b *backend) Mkcol(r *http.Request) error { Path: r.URL.Path, } - // Check if a request body was sent - _, err := r.Body.Read(nil) - if err != nil && err != io.EOF { - return err - } - if err == nil { - // Not EOF, body is present + if !internal.IsRequestBodyEmpty(r) { var m mkcolReq if err := internal.DecodeXMLRequest(r, &m); err != nil { return internal.HTTPErrorf(http.StatusBadRequest, "carddav: error parsing mkcol request: %s", err.Error()) @@ -722,6 +715,7 @@ func (b *backend) Mkcol(r *http.Request) error { ab.Description = m.Description.Description // TODO ... } + return b.Backend.CreateAddressBook(r.Context(), ab) } diff --git a/internal/server.go b/internal/server.go index f9c7b68..85ed1bc 100644 --- a/internal/server.go +++ b/internal/server.go @@ -44,6 +44,11 @@ func DecodeXMLRequest(r *http.Request, v interface{}) error { return nil } +func IsRequestBodyEmpty(r *http.Request) bool { + _, err := r.Body.Read(nil) + return err == io.EOF +} + func ServeXML(w http.ResponseWriter) *xml.Encoder { w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"") w.Write([]byte(xml.Header))