internal: add IsRequestEmpty

This commit is contained in:
Simon Ser 2024-02-07 17:22:41 +01:00
parent 71bd967b43
commit f1d56f2437
2 changed files with 7 additions and 8 deletions

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io"
"mime" "mime"
"net/http" "net/http"
"path" "path"
@ -703,13 +702,7 @@ func (b *backend) Mkcol(r *http.Request) error {
Path: r.URL.Path, Path: r.URL.Path,
} }
// Check if a request body was sent if !internal.IsRequestBodyEmpty(r) {
_, err := r.Body.Read(nil)
if err != nil && err != io.EOF {
return err
}
if err == nil {
// Not EOF, body is present
var m mkcolReq var m mkcolReq
if err := internal.DecodeXMLRequest(r, &m); err != nil { if err := internal.DecodeXMLRequest(r, &m); err != nil {
return internal.HTTPErrorf(http.StatusBadRequest, "carddav: error parsing mkcol request: %s", err.Error()) 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 ab.Description = m.Description.Description
// TODO ... // TODO ...
} }
return b.Backend.CreateAddressBook(r.Context(), ab) return b.Backend.CreateAddressBook(r.Context(), ab)
} }

View File

@ -44,6 +44,11 @@ func DecodeXMLRequest(r *http.Request, v interface{}) error {
return nil return nil
} }
func IsRequestBodyEmpty(r *http.Request) bool {
_, err := r.Body.Read(nil)
return err == io.EOF
}
func ServeXML(w http.ResponseWriter) *xml.Encoder { func ServeXML(w http.ResponseWriter) *xml.Encoder {
w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"") w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"")
w.Write([]byte(xml.Header)) w.Write([]byte(xml.Header))