carddav: implement REPORT addressbook-multiget
This commit is contained in:
@@ -2,6 +2,7 @@ package carddav
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"github.com/emersion/go-vcard"
|
||||
@@ -25,9 +26,71 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
b := backend{h.Backend}
|
||||
hh := internal.Handler{&b}
|
||||
hh.ServeHTTP(w, r)
|
||||
var err error
|
||||
switch r.Method {
|
||||
case "REPORT":
|
||||
err = h.handleReport(w, r)
|
||||
default:
|
||||
b := backend{h.Backend}
|
||||
hh := internal.Handler{&b}
|
||||
hh.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
internal.ServeError(w, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) handleReport(w http.ResponseWriter, r *http.Request) error {
|
||||
t, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if t != "application/xml" && t != "text/xml" {
|
||||
return internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected application/xml REPORT request")
|
||||
}
|
||||
|
||||
var report reportReq
|
||||
if err := xml.NewDecoder(r.Body).Decode(&report); err != nil {
|
||||
return &internal.HTTPError{http.StatusBadRequest, err}
|
||||
}
|
||||
|
||||
if report.Query != nil {
|
||||
return h.handleQuery(w, report.Query)
|
||||
} else if report.Multiget != nil {
|
||||
return h.handleMultiget(w, report.Multiget)
|
||||
}
|
||||
return internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected addressbook-query or addressbook-multiget element in REPORT request")
|
||||
}
|
||||
|
||||
func (h *Handler) handleQuery(w http.ResponseWriter, query *addressbookQuery) error {
|
||||
return nil // TODO
|
||||
}
|
||||
|
||||
func (h *Handler) handleMultiget(w http.ResponseWriter, multiget *addressbookMultiget) error {
|
||||
var resps []internal.Response
|
||||
for _, href := range multiget.Hrefs {
|
||||
ao, err := h.Backend.GetAddressObject(href)
|
||||
if err != nil {
|
||||
return err // TODO: create internal.Response with error
|
||||
}
|
||||
|
||||
b := backend{h.Backend}
|
||||
propfind := internal.Propfind{
|
||||
Prop: multiget.Prop,
|
||||
// TODO: Allprop, Propnames
|
||||
}
|
||||
resp, err := b.propfindAddressObject(&propfind, ao)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resps = append(resps, *resp)
|
||||
}
|
||||
|
||||
ms := internal.NewMultistatus(resps...)
|
||||
|
||||
w.Header().Add("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
w.Write([]byte(xml.Header))
|
||||
return xml.NewEncoder(w).Encode(&ms)
|
||||
}
|
||||
|
||||
type backend struct {
|
||||
@@ -124,7 +187,8 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address
|
||||
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||
return &internal.GetContentType{Type: vcard.MIMEType}, nil
|
||||
},
|
||||
// TODO getlastmodified, getetag
|
||||
// TODO: getlastmodified, getetag
|
||||
// TODO: address-data
|
||||
}
|
||||
|
||||
return internal.NewPropfindResponse(ao.Href, propfind, props)
|
||||
|
Reference in New Issue
Block a user