mirror of
https://github.com/1f349/go-webdav.git
synced 2025-01-21 06:46:28 +00:00
carddav: switch to one static path layout
See #100 for details. Obsoletes #99.
This commit is contained in:
parent
6f22a649ac
commit
561012d30f
@ -92,39 +92,29 @@ func (*testBackend) DeleteAddressObject(ctx context.Context, path string) error
|
|||||||
func TestAddressBookDiscovery(t *testing.T) {
|
func TestAddressBookDiscovery(t *testing.T) {
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
|
prefix string
|
||||||
currentUserPrincipal string
|
currentUserPrincipal string
|
||||||
homeSetPath string
|
homeSetPath string
|
||||||
addressBookPath string
|
addressBookPath string
|
||||||
}{
|
}{
|
||||||
// TODO this used to work, but is currently broken.
|
|
||||||
//{
|
|
||||||
// name: "all-at-root",
|
|
||||||
// currentUserPrincipal: "/",
|
|
||||||
// homeSetPath: "/",
|
|
||||||
// addressBookPath: "/",
|
|
||||||
//},
|
|
||||||
{
|
{
|
||||||
name: "simple-home-set-path",
|
name: "simple",
|
||||||
currentUserPrincipal: "/",
|
prefix: "",
|
||||||
homeSetPath: "/contacts/",
|
|
||||||
addressBookPath: "/contacts/",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "all-at-different-paths",
|
|
||||||
currentUserPrincipal: "/",
|
|
||||||
homeSetPath: "/contacts/",
|
|
||||||
addressBookPath: "/contacts/work",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "nothing-at-root",
|
|
||||||
currentUserPrincipal: "/test/",
|
currentUserPrincipal: "/test/",
|
||||||
homeSetPath: "/test/contacts/",
|
homeSetPath: "/test/contacts/",
|
||||||
addressBookPath: "/test/contacts/private",
|
addressBookPath: "/test/contacts/private",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "prefix",
|
||||||
|
prefix: "/dav",
|
||||||
|
currentUserPrincipal: "/dav/test/",
|
||||||
|
homeSetPath: "/dav/test/contacts/",
|
||||||
|
addressBookPath: "/dav/test/contacts/private",
|
||||||
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|
||||||
h := Handler{&testBackend{}}
|
h := Handler{&testBackend{}, tc.prefix}
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
ctx = context.WithValue(ctx, currentUserPrincipalKey, tc.currentUserPrincipal)
|
ctx = context.WithValue(ctx, currentUserPrincipalKey, tc.currentUserPrincipal)
|
||||||
|
@ -7,7 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/emersion/go-vcard"
|
"github.com/emersion/go-vcard"
|
||||||
"github.com/emersion/go-webdav"
|
"github.com/emersion/go-webdav"
|
||||||
@ -42,6 +44,7 @@ type Backend interface {
|
|||||||
// server.
|
// server.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
Backend Backend
|
Backend Backend
|
||||||
|
Prefix string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP implements http.Handler.
|
// ServeHTTP implements http.Handler.
|
||||||
@ -67,7 +70,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
case "REPORT":
|
case "REPORT":
|
||||||
err = h.handleReport(w, r)
|
err = h.handleReport(w, r)
|
||||||
default:
|
default:
|
||||||
b := backend{h.Backend}
|
b := backend{
|
||||||
|
Backend: h.Backend,
|
||||||
|
Prefix: strings.TrimSuffix(h.Prefix, "/"),
|
||||||
|
}
|
||||||
hh := internal.Handler{&b}
|
hh := internal.Handler{&b}
|
||||||
hh.ServeHTTP(w, r)
|
hh.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
@ -181,7 +187,10 @@ func (h *Handler) handleQuery(ctx context.Context, w http.ResponseWriter, query
|
|||||||
|
|
||||||
var resps []internal.Response
|
var resps []internal.Response
|
||||||
for _, ao := range aos {
|
for _, ao := range aos {
|
||||||
b := backend{h.Backend}
|
b := backend{
|
||||||
|
Backend: h.Backend,
|
||||||
|
Prefix: strings.TrimSuffix(h.Prefix, "/"),
|
||||||
|
}
|
||||||
propfind := internal.PropFind{
|
propfind := internal.PropFind{
|
||||||
Prop: query.Prop,
|
Prop: query.Prop,
|
||||||
AllProp: query.AllProp,
|
AllProp: query.AllProp,
|
||||||
@ -221,7 +230,10 @@ func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, mul
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
b := backend{h.Backend}
|
b := backend{
|
||||||
|
Backend: h.Backend,
|
||||||
|
Prefix: strings.TrimSuffix(h.Prefix, "/"),
|
||||||
|
}
|
||||||
propfind := internal.PropFind{
|
propfind := internal.PropFind{
|
||||||
Prop: multiget.Prop,
|
Prop: multiget.Prop,
|
||||||
AllProp: multiget.AllProp,
|
AllProp: multiget.AllProp,
|
||||||
@ -240,22 +252,35 @@ func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, mul
|
|||||||
|
|
||||||
type backend struct {
|
type backend struct {
|
||||||
Backend Backend
|
Backend Backend
|
||||||
|
Prefix string
|
||||||
|
}
|
||||||
|
|
||||||
|
type resourceType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
resourceTypeRoot resourceType = iota
|
||||||
|
resourceTypeUserPrincipal
|
||||||
|
resourceTypeAddressBookHomeSet
|
||||||
|
resourceTypeAddressBook
|
||||||
|
resourceTypeAddressObject
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *backend) resourceTypeAtPath(reqPath string) resourceType {
|
||||||
|
p := path.Clean(reqPath)
|
||||||
|
p = strings.TrimPrefix(p, b.Prefix)
|
||||||
|
if !strings.HasPrefix(p, "/") {
|
||||||
|
p = "/" + p
|
||||||
|
}
|
||||||
|
if p == "/" {
|
||||||
|
return resourceTypeRoot
|
||||||
|
}
|
||||||
|
return resourceType(len(strings.Split(p, "/")) - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) Options(r *http.Request) (caps []string, allow []string, err error) {
|
func (b *backend) Options(r *http.Request) (caps []string, allow []string, err error) {
|
||||||
caps = []string{"addressbook"}
|
caps = []string{"addressbook"}
|
||||||
|
|
||||||
homeSetPath, err := b.Backend.AddressbookHomeSetPath(r.Context())
|
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeAddressObject {
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
principalPath, err := b.Backend.CurrentUserPrincipal(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.URL.Path == "/" || r.URL.Path == principalPath || r.URL.Path == homeSetPath {
|
|
||||||
// Note: some clients assume the address book is read-only when
|
// Note: some clients assume the address book is read-only when
|
||||||
// DELETE/MKCOL are missing
|
// DELETE/MKCOL are missing
|
||||||
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
||||||
@ -307,52 +332,79 @@ func (b *backend) HeadGet(w http.ResponseWriter, r *http.Request) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) PropFind(r *http.Request, propfind *internal.PropFind, depth internal.Depth) (*internal.MultiStatus, error) {
|
func (b *backend) PropFind(r *http.Request, propfind *internal.PropFind, depth internal.Depth) (*internal.MultiStatus, error) {
|
||||||
homeSetPath, err := b.Backend.AddressbookHomeSetPath(r.Context())
|
resType := b.resourceTypeAtPath(r.URL.Path)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
principalPath, err := b.Backend.CurrentUserPrincipal(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var dataReq AddressDataRequest
|
var dataReq AddressDataRequest
|
||||||
|
|
||||||
var resps []internal.Response
|
var resps []internal.Response
|
||||||
|
|
||||||
if r.URL.Path == principalPath {
|
switch resType {
|
||||||
resp, err := b.propFindUserPrincipal(r.Context(), propfind, homeSetPath)
|
case resourceTypeUserPrincipal:
|
||||||
|
principalPath, err := b.Backend.CurrentUserPrincipal(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resps = append(resps, *resp)
|
if r.URL.Path == principalPath {
|
||||||
} else if r.URL.Path == homeSetPath {
|
resp, err := b.propFindUserPrincipal(r.Context(), propfind)
|
||||||
ab, err := b.Backend.AddressBook(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := b.propFindAddressBook(r.Context(), propfind, ab)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
resps = append(resps, *resp)
|
|
||||||
|
|
||||||
if depth != internal.DepthZero {
|
|
||||||
aos, err := b.Backend.ListAddressObjects(r.Context(), &dataReq)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
for _, ao := range aos {
|
if depth != internal.DepthZero {
|
||||||
resp, err := b.propFindAddressObject(r.Context(), propfind, &ao)
|
resp, err := b.propFindHomeSet(r.Context(), propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resps = append(resps, *resp)
|
resps = append(resps, *resp)
|
||||||
|
if depth == internal.DepthInfinity {
|
||||||
|
resps_, err := b.propFindAllAddressBooks(r.Context(), propfind, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
case resourceTypeAddressBookHomeSet:
|
||||||
|
homeSetPath, err := b.Backend.AddressbookHomeSetPath(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if r.URL.Path == homeSetPath {
|
||||||
|
resp, err := b.propFindHomeSet(r.Context(), propfind)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
if depth != internal.DepthZero {
|
||||||
|
recurse := depth == internal.DepthInfinity
|
||||||
|
resps_, err := b.propFindAllAddressBooks(r.Context(), propfind, recurse)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case resourceTypeAddressBook:
|
||||||
|
// TODO for multiple address books, look through all of them
|
||||||
|
ab, err := b.Backend.AddressBook(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if r.URL.Path == ab.Path {
|
||||||
|
resp, err := b.propFindAddressBook(r.Context(), propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
if depth != internal.DepthZero {
|
||||||
|
resps_, err := b.propFindAllAddressObjects(r.Context(), propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case resourceTypeAddressObject:
|
||||||
ao, err := b.Backend.GetAddressObject(r.Context(), r.URL.Path, &dataReq)
|
ao, err := b.Backend.GetAddressObject(r.Context(), r.URL.Path, &dataReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -368,11 +420,15 @@ func (b *backend) PropFind(r *http.Request, propfind *internal.PropFind, depth i
|
|||||||
return internal.NewMultiStatus(resps...), nil
|
return internal.NewMultiStatus(resps...), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.PropFind, homeSetPath string) (*internal.Response, error) {
|
func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.PropFind) (*internal.Response, error) {
|
||||||
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
|
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
homeSetPath, err := b.Backend.AddressbookHomeSetPath(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
props := map[xml.Name]internal.PropFindFunc{
|
props := map[xml.Name]internal.PropFindFunc{
|
||||||
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
@ -381,10 +437,35 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
|
|||||||
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
|
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
return &addressbookHomeSet{Href: internal.Href{Path: homeSetPath}}, nil
|
return &addressbookHomeSet{Href: internal.Href{Path: homeSetPath}}, nil
|
||||||
},
|
},
|
||||||
|
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
|
return internal.NewResourceType(internal.CollectionName), nil
|
||||||
|
},
|
||||||
}
|
}
|
||||||
return internal.NewPropFindResponse(principalPath, propfind, props)
|
return internal.NewPropFindResponse(principalPath, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFind) (*internal.Response, error) {
|
||||||
|
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
homeSetPath, err := b.Backend.AddressbookHomeSetPath(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO anything else to return here?
|
||||||
|
props := map[xml.Name]internal.PropFindFunc{
|
||||||
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
|
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil
|
||||||
|
},
|
||||||
|
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
|
return internal.NewResourceType(internal.CollectionName), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return internal.NewPropFindResponse(homeSetPath, propfind, props)
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.PropFind, ab *AddressBook) (*internal.Response, error) {
|
func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.PropFind, ab *AddressBook) (*internal.Response, error) {
|
||||||
props := map[xml.Name]internal.PropFindFunc{
|
props := map[xml.Name]internal.PropFindFunc{
|
||||||
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
@ -422,6 +503,32 @@ func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.Pr
|
|||||||
return internal.NewPropFindResponse(ab.Path, propfind, props)
|
return internal.NewPropFindResponse(ab.Path, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) propFindAllAddressBooks(ctx context.Context, propfind *internal.PropFind, recurse bool) ([]internal.Response, error) {
|
||||||
|
// TODO iterate over all address books once having multiple is supported
|
||||||
|
ab, err := b.Backend.AddressBook(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
abs := []*AddressBook{ab}
|
||||||
|
|
||||||
|
var resps []internal.Response
|
||||||
|
for _, ab := range abs {
|
||||||
|
resp, err := b.propFindAddressBook(ctx, propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
if recurse {
|
||||||
|
resps_, err := b.propFindAllAddressObjects(ctx, propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resps, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.PropFind, ao *AddressObject) (*internal.Response, error) {
|
func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.PropFind, ao *AddressObject) (*internal.Response, error) {
|
||||||
props := map[xml.Name]internal.PropFindFunc{
|
props := map[xml.Name]internal.PropFindFunc{
|
||||||
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
@ -465,6 +572,24 @@ func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.
|
|||||||
return internal.NewPropFindResponse(ao.Path, propfind, props)
|
return internal.NewPropFindResponse(ao.Path, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) propFindAllAddressObjects(ctx context.Context, propfind *internal.PropFind, ab *AddressBook) ([]internal.Response, error) {
|
||||||
|
var dataReq AddressDataRequest
|
||||||
|
aos, err := b.Backend.ListAddressObjects(ctx, &dataReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var resps []internal.Response
|
||||||
|
for _, ao := range aos {
|
||||||
|
resp, err := b.propFindAddressObject(ctx, propfind, &ao)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
}
|
||||||
|
return resps, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (*internal.Response, error) {
|
func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (*internal.Response, error) {
|
||||||
homeSetPath, err := b.Backend.AddressbookHomeSetPath(r.Context())
|
homeSetPath, err := b.Backend.AddressbookHomeSetPath(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user