2020-01-17 15:59:29 +00:00
|
|
|
package carddav
|
|
|
|
|
|
|
|
import (
|
2020-01-19 13:53:58 +00:00
|
|
|
"bytes"
|
2022-02-23 10:52:04 +00:00
|
|
|
"context"
|
2020-01-17 15:59:29 +00:00
|
|
|
"encoding/xml"
|
2020-01-22 14:35:36 +00:00
|
|
|
"fmt"
|
2020-01-22 14:14:49 +00:00
|
|
|
"mime"
|
2020-01-17 15:59:29 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/emersion/go-vcard"
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: add support for multiple address books
|
|
|
|
|
2022-03-17 10:19:51 +00:00
|
|
|
type PutAddressObjectOptions struct {
|
|
|
|
// IfNoneMatch indicates that the client does not want to overwrite
|
|
|
|
// an existing resource.
|
|
|
|
IfNoneMatch bool
|
|
|
|
// IfMatch provides the ETag of the resource that the client intends
|
|
|
|
// to overwrite, can be ""
|
|
|
|
IfMatch string
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-02-23 10:52:04 +00:00
|
|
|
AddressBook(ctx context.Context) (*AddressBook, error)
|
|
|
|
GetAddressObject(ctx context.Context, path string, req *AddressDataRequest) (*AddressObject, error)
|
|
|
|
ListAddressObjects(ctx context.Context, req *AddressDataRequest) ([]AddressObject, error)
|
|
|
|
QueryAddressObjects(ctx context.Context, query *AddressBookQuery) ([]AddressObject, error)
|
2022-03-17 10:19:51 +00:00
|
|
|
PutAddressObject(ctx context.Context, path string, card vcard.Card, opts *PutAddressObjectOptions) (loc string, err error)
|
2022-02-23 10:52:04 +00:00
|
|
|
DeleteAddressObject(ctx context.Context, path string) 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-10-09 14:10:33 +01:00
|
|
|
if r.URL.Path == "/.well-known/carddav" {
|
|
|
|
http.Redirect(w, r, "/", http.StatusMovedPermanently)
|
|
|
|
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 {
|
2022-02-23 10:52:04 +00:00
|
|
|
return h.handleQuery(r.Context(), w, report.Query)
|
2020-01-19 10:05:56 +00:00
|
|
|
} else if report.Multiget != nil {
|
2022-02-23 10:52:04 +00:00
|
|
|
return h.handleMultiget(r.Context(), w, report.Multiget)
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
2020-01-24 15:34:57 +00:00
|
|
|
return internal.HTTPErrorf(http.StatusBadRequest, "carddav: expected addressbook-query or addressbook-multiget element in REPORT request")
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodePropFilter(el *propFilter) (*PropFilter, error) {
|
|
|
|
pf := &PropFilter{Name: el.Name, Test: FilterTest(el.Test)}
|
|
|
|
if el.IsNotDefined != nil {
|
|
|
|
if len(el.TextMatches) > 0 || len(el.Params) > 0 {
|
|
|
|
return nil, fmt.Errorf("carddav: failed to parse prop-filter: if is-not-defined is provided, text-match or param-filter can't be provided")
|
|
|
|
}
|
|
|
|
pf.IsNotDefined = true
|
|
|
|
}
|
|
|
|
for _, tm := range el.TextMatches {
|
|
|
|
pf.TextMatches = append(pf.TextMatches, *decodeTextMatch(&tm))
|
|
|
|
}
|
|
|
|
for _, paramEl := range el.Params {
|
|
|
|
param, err := decodeParamFilter(¶mEl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pf.Params = append(pf.Params, *param)
|
|
|
|
}
|
|
|
|
return pf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeParamFilter(el *paramFilter) (*ParamFilter, error) {
|
|
|
|
pf := &ParamFilter{Name: el.Name}
|
|
|
|
if el.IsNotDefined != nil {
|
|
|
|
if el.TextMatch != nil {
|
|
|
|
return nil, fmt.Errorf("carddav: failed to parse param-filter: if is-not-defined is provided, text-match can't be provided")
|
|
|
|
}
|
|
|
|
pf.IsNotDefined = true
|
|
|
|
}
|
|
|
|
if el.TextMatch != nil {
|
|
|
|
pf.TextMatch = decodeTextMatch(el.TextMatch)
|
|
|
|
}
|
|
|
|
return pf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeTextMatch(tm *textMatch) *TextMatch {
|
|
|
|
return &TextMatch{
|
|
|
|
Text: tm.Text,
|
|
|
|
NegateCondition: bool(tm.NegateCondition),
|
|
|
|
MatchType: MatchType(tm.MatchType),
|
|
|
|
}
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 09:30:19 +00:00
|
|
|
func decodeAddressDataReq(addressData *addressDataReq) (*AddressDataRequest, error) {
|
|
|
|
if addressData.Allprop != nil && len(addressData.Props) > 0 {
|
|
|
|
return nil, internal.HTTPErrorf(http.StatusBadRequest, "carddav: only one of allprop or prop can be specified in address-data")
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &AddressDataRequest{AllProp: addressData.Allprop != nil}
|
|
|
|
for _, p := range addressData.Props {
|
|
|
|
req.Props = append(req.Props, p.Name)
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2022-02-23 10:52:04 +00:00
|
|
|
func (h *Handler) handleQuery(ctx context.Context, w http.ResponseWriter, query *addressbookQuery) error {
|
2020-01-19 11:02:18 +00:00
|
|
|
var q AddressBookQuery
|
|
|
|
if query.Prop != nil {
|
|
|
|
var addressData addressDataReq
|
2020-03-28 22:51:45 +00:00
|
|
|
if err := query.Prop.Decode(&addressData); err != nil && !internal.IsNotFound(err) {
|
2020-01-19 11:02:18 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-01-27 09:30:19 +00:00
|
|
|
req, err := decodeAddressDataReq(&addressData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-01-19 11:02:18 +00:00
|
|
|
}
|
2020-01-27 09:30:19 +00:00
|
|
|
q.DataRequest = *req
|
2020-01-19 11:02:18 +00:00
|
|
|
}
|
2020-01-24 15:34:57 +00:00
|
|
|
q.FilterTest = FilterTest(query.Filter.Test)
|
|
|
|
for _, el := range query.Filter.Props {
|
|
|
|
pf, err := decodePropFilter(&el)
|
|
|
|
if err != nil {
|
|
|
|
return &internal.HTTPError{http.StatusBadRequest, err}
|
|
|
|
}
|
|
|
|
q.PropFilters = append(q.PropFilters, *pf)
|
|
|
|
}
|
2020-01-23 09:35:06 +00:00
|
|
|
if query.Limit != nil {
|
|
|
|
q.Limit = int(query.Limit.NResults)
|
|
|
|
if q.Limit <= 0 {
|
|
|
|
return internal.ServeMultistatus(w, internal.NewMultistatus())
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 11:02:18 +00:00
|
|
|
|
2022-02-23 10:52:04 +00:00
|
|
|
aos, err := h.Backend.QueryAddressObjects(ctx, &q)
|
2020-01-19 11:02:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resps []internal.Response
|
|
|
|
for _, ao := range aos {
|
|
|
|
b := backend{h.Backend}
|
|
|
|
propfind := internal.Propfind{
|
2020-01-22 17:59:01 +00:00
|
|
|
Prop: query.Prop,
|
|
|
|
AllProp: query.AllProp,
|
|
|
|
PropName: query.PropName,
|
2020-01-19 11:02:18 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-02-23 10:52:04 +00:00
|
|
|
func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, multiget *addressbookMultiget) error {
|
2020-01-27 09:30:19 +00:00
|
|
|
var dataReq AddressDataRequest
|
|
|
|
if multiget.Prop != nil {
|
|
|
|
var addressData addressDataReq
|
2020-03-28 22:51:45 +00:00
|
|
|
if err := multiget.Prop.Decode(&addressData); err != nil && !internal.IsNotFound(err) {
|
2020-01-27 09:30:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
decoded, err := decodeAddressDataReq(&addressData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dataReq = *decoded
|
|
|
|
}
|
|
|
|
|
2020-01-19 10:05:56 +00:00
|
|
|
var resps []internal.Response
|
|
|
|
for _, href := range multiget.Hrefs {
|
2022-02-23 10:52:04 +00:00
|
|
|
ao, err := h.Backend.GetAddressObject(ctx, href.Path, &dataReq)
|
2020-01-19 10:05:56 +00:00
|
|
|
if err != nil {
|
2022-04-29 14:24:24 +01:00
|
|
|
resp := internal.NewErrorResponse(href.Path, err)
|
|
|
|
resps = append(resps, *resp)
|
|
|
|
continue
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := backend{h.Backend}
|
|
|
|
propfind := internal.Propfind{
|
2020-01-22 17:59:01 +00:00
|
|
|
Prop: multiget.Prop,
|
|
|
|
AllProp: multiget.AllProp,
|
|
|
|
PropName: multiget.PropName,
|
2020-01-19 10:05:56 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-29 17:03:47 +00:00
|
|
|
func (b *backend) Options(r *http.Request) (caps []string, allow []string, err error) {
|
|
|
|
caps = []string{"addressbook"}
|
2020-01-17 15:59:29 +00:00
|
|
|
|
|
|
|
if r.URL.Path == "/" {
|
2020-01-29 23:43:23 +00:00
|
|
|
// Note: some clients assume the address book is read-only when
|
|
|
|
// DELETE/MKCOL are missing
|
|
|
|
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 09:30:19 +00:00
|
|
|
var dataReq AddressDataRequest
|
2022-02-23 10:52:04 +00:00
|
|
|
_, err = b.Backend.GetAddressObject(r.Context(), r.URL.Path, &dataReq)
|
2020-01-17 15:59:29 +00:00
|
|
|
if httpErr, ok := err.(*internal.HTTPError); ok && httpErr.Code == http.StatusNotFound {
|
2020-01-29 17:03:47 +00:00
|
|
|
return caps, []string{http.MethodOptions, http.MethodPut}, nil
|
2020-01-17 15:59:29 +00:00
|
|
|
} else if err != nil {
|
2020-01-29 17:03:47 +00:00
|
|
|
return nil, nil, err
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 17:03:47 +00:00
|
|
|
return caps, []string{
|
|
|
|
http.MethodOptions,
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodGet,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodDelete,
|
|
|
|
"PROPFIND",
|
|
|
|
}, nil
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
return &internal.HTTPError{Code: http.StatusMethodNotAllowed}
|
|
|
|
}
|
|
|
|
|
2020-01-27 09:30:19 +00:00
|
|
|
var dataReq AddressDataRequest
|
|
|
|
if r.Method != http.MethodHead {
|
|
|
|
dataReq.AllProp = true
|
|
|
|
}
|
2022-02-23 10:52:04 +00:00
|
|
|
ao, err := b.Backend.GetAddressObject(r.Context(), r.URL.Path, &dataReq)
|
2020-01-17 15:59:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", vcard.MIMEType)
|
2022-02-24 11:41:56 +00:00
|
|
|
if ao.ETag != "" {
|
|
|
|
w.Header().Set("ETag", internal.ETag(ao.ETag).String())
|
|
|
|
}
|
|
|
|
if !ao.ModTime.IsZero() {
|
|
|
|
w.Header().Set("Last-Modified", ao.ModTime.UTC().Format(http.TimeFormat))
|
|
|
|
}
|
2020-01-17 15:59:29 +00:00
|
|
|
|
|
|
|
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) {
|
2020-01-27 09:30:19 +00:00
|
|
|
var dataReq AddressDataRequest
|
|
|
|
|
2020-01-17 15:59:29 +00:00
|
|
|
var resps []internal.Response
|
|
|
|
if r.URL.Path == "/" {
|
2022-02-23 10:52:04 +00:00
|
|
|
ab, err := b.Backend.AddressBook(r.Context())
|
2020-01-19 13:53:58 +00:00
|
|
|
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 {
|
2022-02-23 10:52:04 +00:00
|
|
|
aos, err := b.Backend.ListAddressObjects(r.Context(), &dataReq)
|
2020-01-17 15:59:29 +00:00
|
|
|
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 {
|
2022-02-23 10:52:04 +00:00
|
|
|
ao, err := b.Backend.GetAddressObject(r.Context(), r.URL.Path, &dataReq)
|
2020-01-17 15:59:29 +00:00
|
|
|
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-02-27 11:36:14 +00:00
|
|
|
supportedAddressDataName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &supportedAddressData{
|
2020-01-19 14:00:15 +00:00
|
|
|
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) {
|
2020-01-22 10:07:30 +00:00
|
|
|
return &addressbookHomeSet{Href: internal.Href{Path: "/"}}, nil
|
2020-01-19 14:10:54 +00:00
|
|
|
},
|
2020-01-20 09:56:25 +00:00
|
|
|
// TODO: this should be set on all resources
|
|
|
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
2020-01-22 10:07:30 +00:00
|
|
|
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: "/"}}, nil
|
2020-01-20 09:56:25 +00:00
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
2022-05-02 19:58:00 +01:00
|
|
|
return internal.NewPropfindResponse(ab.Path, propfind, props)
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-22 13:50:57 +00:00
|
|
|
// TODO: address-data can only be used in REPORT requests
|
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-22 14:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !ao.ModTime.IsZero() {
|
|
|
|
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
|
|
return &internal.GetLastModified{LastModified: internal.Time(ao.ModTime)}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ao.ETag != "" {
|
|
|
|
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) {
|
2020-02-03 20:48:31 +00:00
|
|
|
return &internal.GetETag{ETag: internal.ETag(ao.ETag)}, nil
|
2020-01-22 14:35:36 +00:00
|
|
|
}
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
return internal.NewPropfindResponse(ao.Path, propfind, props)
|
2020-01-17 15:59:29 +00:00
|
|
|
}
|
2020-01-21 20:19:44 +00:00
|
|
|
|
2020-01-21 22:18:27 +00:00
|
|
|
func (b *backend) Proppatch(r *http.Request, update *internal.Propertyupdate) (*internal.Response, error) {
|
2020-01-22 13:50:57 +00:00
|
|
|
// TODO: return a failed Response instead
|
|
|
|
// TODO: support PROPPATCH for address books
|
|
|
|
return nil, internal.HTTPErrorf(http.StatusForbidden, "carddav: PROPPATCH is unsupported")
|
2020-01-21 22:18:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 14:20:10 +00:00
|
|
|
func (b *backend) Put(r *http.Request) (*internal.Href, error) {
|
2022-03-17 10:19:51 +00:00
|
|
|
if inm := r.Header.Get("If-None-Match"); inm != "" && inm != "*" {
|
|
|
|
return nil, internal.HTTPErrorf(http.StatusBadRequest, "invalid value for If-None-Match header")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := PutAddressObjectOptions{
|
|
|
|
IfNoneMatch: r.Header.Get("If-None-Match") == "*",
|
|
|
|
IfMatch: r.Header.Get("If-Match"),
|
|
|
|
}
|
2020-01-22 14:14:49 +00:00
|
|
|
|
|
|
|
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
2020-01-30 14:20:10 +00:00
|
|
|
return nil, internal.HTTPErrorf(http.StatusBadRequest, "carddav: malformed Content-Type: %v", err)
|
2020-01-22 14:14:49 +00:00
|
|
|
}
|
|
|
|
if t != vcard.MIMEType {
|
|
|
|
// TODO: send CARDDAV:supported-address-data error
|
2020-01-30 14:20:10 +00:00
|
|
|
return nil, internal.HTTPErrorf(http.StatusBadRequest, "carddav: unsupporetd Content-Type %q", t)
|
2020-01-22 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check CARDDAV:max-resource-size precondition
|
|
|
|
card, err := vcard.NewDecoder(r.Body).Decode()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: send CARDDAV:valid-address-data error
|
2020-01-30 14:20:10 +00:00
|
|
|
return nil, internal.HTTPErrorf(http.StatusBadRequest, "carddav: failed to parse vCard: %v", err)
|
2020-01-22 14:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add support for the CARDDAV:no-uid-conflict error
|
2022-03-17 10:19:51 +00:00
|
|
|
loc, err := b.Backend.PutAddressObject(r.Context(), r.URL.Path, card, &opts)
|
2020-01-30 14:20:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &internal.Href{Path: loc}, nil
|
2020-01-21 20:19:44 +00:00
|
|
|
}
|
2020-01-21 20:46:01 +00:00
|
|
|
|
|
|
|
func (b *backend) Delete(r *http.Request) error {
|
2022-02-23 10:52:04 +00:00
|
|
|
return b.Backend.DeleteAddressObject(r.Context(), r.URL.Path)
|
2020-01-21 20:46:01 +00:00
|
|
|
}
|
2020-01-21 21:05:59 +00:00
|
|
|
|
|
|
|
func (b *backend) Mkcol(r *http.Request) error {
|
|
|
|
return internal.HTTPErrorf(http.StatusForbidden, "carddav: address book creation unsupported")
|
|
|
|
}
|
2020-01-22 10:43:36 +00:00
|
|
|
|
2020-01-22 12:00:42 +00:00
|
|
|
func (b *backend) Copy(r *http.Request, dest *internal.Href, recursive, overwrite bool) (created bool, err error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:43:36 +00:00
|
|
|
func (b *backend) Move(r *http.Request, dest *internal.Href, overwrite bool) (created bool, err error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2022-03-10 14:35:39 +00:00
|
|
|
|
|
|
|
// https://tools.ietf.org/rfcmarkup?doc=6352#section-6.3.2.1
|
|
|
|
type PreconditionType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
PreconditionNoUIDConflict PreconditionType = "no-uid-conflict"
|
|
|
|
PreconditionSupportedAddressData PreconditionType = "supported-address-data"
|
|
|
|
PreconditionValidAddressData PreconditionType = "valid-address-data"
|
|
|
|
PreconditionMaxResourceSize PreconditionType = "max-resource-size"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewPreconditionError(err PreconditionType) error {
|
|
|
|
name := xml.Name{"urn:ietf:params:xml:ns:carddav", string(err)}
|
|
|
|
elem := internal.NewRawXMLElement(name, nil, nil)
|
2022-05-02 10:37:45 +01:00
|
|
|
return &internal.HTTPError{
|
2022-03-10 14:35:39 +00:00
|
|
|
Code: 409,
|
2022-05-02 10:37:45 +01:00
|
|
|
Err: &internal.Error{
|
|
|
|
Raw: []internal.RawXMLValue{*elem},
|
|
|
|
},
|
2022-03-10 14:35:39 +00:00
|
|
|
}
|
|
|
|
}
|