2020-01-17 15:59:29 +00:00
|
|
|
package carddav
|
|
|
|
|
|
|
|
import (
|
2020-01-19 13:53:58 +00:00
|
|
|
"bytes"
|
2020-01-17 15:59:29 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/emersion/go-vcard"
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: add support for multiple address books
|
|
|
|
|
2020-01-21 20:01:18 +00:00
|
|
|
// Backend is a CardDAV server backend.
|
2020-01-17 15:59:29 +00:00
|
|
|
type Backend interface {
|
2020-01-19 13:53:58 +00:00
|
|
|
AddressBook() (*AddressBook, error)
|
2020-01-17 15:59:29 +00:00
|
|
|
GetAddressObject(href string) (*AddressObject, error)
|
|
|
|
ListAddressObjects() ([]AddressObject, error)
|
2020-01-19 11:02:18 +00:00
|
|
|
QueryAddressObjects(query *AddressBookQuery) ([]AddressObject, error)
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 20:01:18 +00:00
|
|
|
// Handler handles CardDAV HTTP requests. It can be used to create a CardDAV
|
|
|
|
// server.
|
2020-01-17 15:59:29 +00:00
|
|
|
type Handler struct {
|
|
|
|
Backend Backend
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:01:18 +00:00
|
|
|
// ServeHTTP implements http.Handler.
|
2020-01-17 15:59:29 +00:00
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if h.Backend == nil {
|
|
|
|
http.Error(w, "carddav: no backend available", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-19 10:05:56 +00:00
|
|
|
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 {
|
|
|
|
var report reportReq
|
2020-01-19 10:12:45 +00:00
|
|
|
if err := internal.DecodeXMLRequest(r, &report); err != nil {
|
|
|
|
return err
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-01-19 11:02:18 +00:00
|
|
|
var q AddressBookQuery
|
|
|
|
if query.Prop != nil {
|
|
|
|
var addressData addressDataReq
|
|
|
|
if err := query.Prop.Decode(&addressData); err != nil && !internal.IsMissingProp(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, p := range addressData.Props {
|
|
|
|
q.Props = append(q.Props, p.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aos, err := h.Backend.QueryAddressObjects(&q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resps []internal.Response
|
|
|
|
for _, ao := range aos {
|
|
|
|
b := backend{h.Backend}
|
|
|
|
propfind := internal.Propfind{
|
|
|
|
Prop: query.Prop,
|
|
|
|
// TODO: Allprop, Propnames
|
|
|
|
}
|
|
|
|
resp, err := b.propfindAddressObject(&propfind, &ao)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resps = append(resps, *resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
ms := internal.NewMultistatus(resps...)
|
|
|
|
return internal.ServeMultistatus(w, ms)
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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...)
|
2020-01-19 10:12:45 +00:00
|
|
|
return internal.ServeMultistatus(w, ms)
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type backend struct {
|
|
|
|
Backend Backend
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) Options(r *http.Request) ([]string, error) {
|
|
|
|
// TODO: add DAV: addressbook
|
|
|
|
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
return []string{http.MethodOptions, "PROPFIND"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.Backend.GetAddressObject(r.URL.Path)
|
|
|
|
if httpErr, ok := err.(*internal.HTTPError); ok && httpErr.Code == http.StatusNotFound {
|
|
|
|
return []string{http.MethodOptions}, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{http.MethodOptions, http.MethodHead, http.MethodGet, "PROPFIND"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
|
|
|
|
}
|
|
|
|
|
|
|
|
ao, err := b.Backend.GetAddressObject(r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", vcard.MIMEType)
|
|
|
|
// TODO: set ETag, Last-Modified
|
|
|
|
|
|
|
|
if r.Method != http.MethodHead {
|
|
|
|
return vcard.NewEncoder(w).Encode(ao.Card)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) Propfind(r *http.Request, propfind *internal.Propfind, depth internal.Depth) (*internal.Multistatus, error) {
|
|
|
|
var resps []internal.Response
|
|
|
|
if r.URL.Path == "/" {
|
2020-01-19 13:53:58 +00:00
|
|
|
ab, err := b.Backend.AddressBook()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.propfindAddressBook(propfind, ab)
|
2020-01-17 15:59:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resps = append(resps, *resp)
|
|
|
|
|
|
|
|
if depth != internal.DepthZero {
|
|
|
|
aos, err := b.Backend.ListAddressObjects()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ao := range aos {
|
|
|
|
resp, err := b.propfindAddressObject(propfind, &ao)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resps = append(resps, *resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ao, err := b.Backend.GetAddressObject(r.URL.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.propfindAddressObject(propfind, ao)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resps = append(resps, *resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return internal.NewMultistatus(resps...), nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 13:53:58 +00:00
|
|
|
func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBook) (*internal.Response, error) {
|
2020-01-17 15:59:29 +00:00
|
|
|
props := map[xml.Name]internal.PropfindFunc{
|
2020-01-17 16:09:23 +00:00
|
|
|
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
2020-01-17 15:59:29 +00:00
|
|
|
return internal.NewResourceType(internal.CollectionName, addressBookName), nil
|
|
|
|
},
|
2020-01-19 13:53:58 +00:00
|
|
|
internal.DisplayNameName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &internal.DisplayName{Name: ab.Name}, nil
|
|
|
|
},
|
|
|
|
addressBookDescriptionName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &addressbookDescription{Description: ab.Description}, nil
|
|
|
|
},
|
2020-01-19 14:00:15 +00:00
|
|
|
addressBookSupportedAddressData: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &addressbookSupportedAddressData{
|
|
|
|
Types: []addressDataType{
|
|
|
|
{ContentType: vcard.MIMEType, Version: "3.0"},
|
|
|
|
{ContentType: vcard.MIMEType, Version: "4.0"},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2020-01-19 14:10:54 +00:00
|
|
|
// TODO: this is a principal property
|
|
|
|
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &addressbookHomeSet{Href: "/"}, nil
|
|
|
|
},
|
2020-01-20 09:56:25 +00:00
|
|
|
// TODO: this should be set on all resources
|
|
|
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &internal.CurrentUserPrincipal{Href: "/"}, nil
|
|
|
|
},
|
2020-01-19 14:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ab.MaxResourceSize > 0 {
|
|
|
|
props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &maxResourceSize{Size: ab.MaxResourceSize}, nil
|
|
|
|
}
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return internal.NewPropfindResponse("/", propfind, props)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *AddressObject) (*internal.Response, error) {
|
|
|
|
props := map[xml.Name]internal.PropfindFunc{
|
2020-01-17 16:09:23 +00:00
|
|
|
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
2020-01-17 15:59:29 +00:00
|
|
|
return &internal.GetContentType{Type: vcard.MIMEType}, nil
|
|
|
|
},
|
2020-01-19 13:53:58 +00:00
|
|
|
addressDataName: func(*internal.RawXMLValue) (interface{}, error) {
|
2020-01-19 11:08:53 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := vcard.NewEncoder(&buf).Encode(ao.Card); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &addressDataResp{Data: buf.Bytes()}, nil
|
|
|
|
},
|
2020-01-19 10:05:56 +00:00
|
|
|
// TODO: getlastmodified, getetag
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return internal.NewPropfindResponse(ao.Href, propfind, props)
|
|
|
|
}
|
2020-01-21 20:19:44 +00:00
|
|
|
|
|
|
|
func (b *backend) Put(r *http.Request) error {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-01-21 20:46:01 +00:00
|
|
|
|
|
|
|
func (b *backend) Delete(r *http.Request) error {
|
|
|
|
panic("TODO")
|
|
|
|
}
|