mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
caldav: use same static path layout as carddav
This commit is contained in:
parent
54f2a6355b
commit
5bfd6f54b2
221
caldav/server.go
221
caldav/server.go
@ -7,7 +7,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/emersion/go-ical"
|
"github.com/emersion/go-ical"
|
||||||
@ -44,6 +46,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.
|
||||||
@ -69,7 +72,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)
|
||||||
}
|
}
|
||||||
@ -215,7 +221,10 @@ func (h *Handler) handleQuery(r *http.Request, w http.ResponseWriter, query *cal
|
|||||||
|
|
||||||
var resps []internal.Response
|
var resps []internal.Response
|
||||||
for _, co := range cos {
|
for _, co := range cos {
|
||||||
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,
|
||||||
@ -256,7 +265,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,
|
||||||
@ -275,22 +287,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
|
||||||
|
resourceTypeCalendarHomeSet
|
||||||
|
resourceTypeCalendar
|
||||||
|
resourceTypeCalendarObject
|
||||||
|
)
|
||||||
|
|
||||||
|
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{"calendar-access"}
|
caps = []string{"calendar-access"}
|
||||||
|
|
||||||
homeSetPath, err := b.Backend.CalendarHomeSetPath(r.Context())
|
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeCalendarObject {
|
||||||
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 {
|
|
||||||
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,58 +365,85 @@ 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.CalendarHomeSetPath(r.Context())
|
resType := b.resourceTypeAtPath(r.URL.Path)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
var dataReq CalendarCompRequest
|
||||||
}
|
var resps []internal.Response
|
||||||
|
|
||||||
|
switch resType {
|
||||||
|
case resourceTypeUserPrincipal:
|
||||||
principalPath, err := b.Backend.CurrentUserPrincipal(r.Context())
|
principalPath, err := b.Backend.CurrentUserPrincipal(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var dataReq CalendarCompRequest
|
|
||||||
|
|
||||||
var resps []internal.Response
|
|
||||||
|
|
||||||
if r.URL.Path == principalPath {
|
if r.URL.Path == principalPath {
|
||||||
resp, err := b.propFindUserPrincipal(r.Context(), propfind, homeSetPath)
|
resp, err := b.propFindUserPrincipal(r.Context(), propfind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resps = append(resps, *resp)
|
resps = append(resps, *resp)
|
||||||
} else if r.URL.Path == homeSetPath {
|
|
||||||
cal, err := b.Backend.Calendar(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := b.propFindCalendar(r.Context(), propfind, cal)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
resps = append(resps, *resp)
|
|
||||||
|
|
||||||
if depth != internal.DepthZero {
|
if depth != internal.DepthZero {
|
||||||
cos, err := b.Backend.ListCalendarObjects(r.Context(), &dataReq)
|
resp, err := b.propFindHomeSet(r.Context(), propfind)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, co := range cos {
|
|
||||||
resp, err := b.propFindCalendarObject(r.Context(), propfind, &co)
|
|
||||||
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.propFindAllCalendars(r.Context(), propfind, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
co, err := b.Backend.GetCalendarObject(r.Context(), r.URL.Path, &dataReq)
|
case resourceTypeCalendarHomeSet:
|
||||||
|
homeSetPath, err := b.Backend.CalendarHomeSetPath(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.propFindAllCalendars(r.Context(), propfind, recurse)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case resourceTypeCalendar:
|
||||||
|
// TODO for multiple calendars, look through all of them
|
||||||
|
ab, err := b.Backend.Calendar(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if r.URL.Path == ab.Path {
|
||||||
|
resp, err := b.propFindCalendar(r.Context(), propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
if depth != internal.DepthZero {
|
||||||
|
resps_, err := b.propFindAllCalendarObjects(r.Context(), propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case resourceTypeCalendarObject:
|
||||||
|
ao, err := b.Backend.GetCalendarObject(r.Context(), r.URL.Path, &dataReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := b.propFindCalendarObject(r.Context(), propfind, co)
|
resp, err := b.propFindCalendarObject(r.Context(), propfind, ao)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -401,11 +453,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.CalendarHomeSetPath(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) {
|
||||||
@ -414,10 +470,35 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
|
|||||||
calendarHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
|
calendarHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
return &calendarHomeSet{Href: internal.Href{Path: homeSetPath}}, nil
|
return &calendarHomeSet{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.CalendarHomeSetPath(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) propFindCalendar(ctx context.Context, propfind *internal.PropFind, cal *Calendar) (*internal.Response, error) {
|
func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropFind, cal *Calendar) (*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) {
|
||||||
@ -469,6 +550,32 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
|
|||||||
return internal.NewPropFindResponse(cal.Path, propfind, props)
|
return internal.NewPropFindResponse(cal.Path, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) propFindAllCalendars(ctx context.Context, propfind *internal.PropFind, recurse bool) ([]internal.Response, error) {
|
||||||
|
// TODO iterate over all calendars once having multiple is supported
|
||||||
|
ab, err := b.Backend.Calendar(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
abs := []*Calendar{ab}
|
||||||
|
|
||||||
|
var resps []internal.Response
|
||||||
|
for _, ab := range abs {
|
||||||
|
resp, err := b.propFindCalendar(ctx, propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, *resp)
|
||||||
|
if recurse {
|
||||||
|
resps_, err := b.propFindAllCalendarObjects(ctx, propfind, ab)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resps = append(resps, resps_...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resps, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal.PropFind, co *CalendarObject) (*internal.Response, error) {
|
func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal.PropFind, co *CalendarObject) (*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) {
|
||||||
@ -481,7 +588,7 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
|
|||||||
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
return &internal.GetContentType{Type: ical.MIMEType}, nil
|
return &internal.GetContentType{Type: ical.MIMEType}, nil
|
||||||
},
|
},
|
||||||
// TODO: address-data can only be used in REPORT requests
|
// TODO: calendar-data can only be used in REPORT requests
|
||||||
calendarDataName: func(*internal.RawXMLValue) (interface{}, error) {
|
calendarDataName: func(*internal.RawXMLValue) (interface{}, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if err := ical.NewEncoder(&buf).Encode(co.Data); err != nil {
|
if err := ical.NewEncoder(&buf).Encode(co.Data); err != nil {
|
||||||
@ -512,6 +619,24 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
|
|||||||
return internal.NewPropFindResponse(co.Path, propfind, props)
|
return internal.NewPropFindResponse(co.Path, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *backend) propFindAllCalendarObjects(ctx context.Context, propfind *internal.PropFind, cal *Calendar) ([]internal.Response, error) {
|
||||||
|
var dataReq CalendarCompRequest
|
||||||
|
aos, err := b.Backend.ListCalendarObjects(ctx, &dataReq)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var resps []internal.Response
|
||||||
|
for _, ao := range aos {
|
||||||
|
resp, err := b.propFindCalendarObject(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) {
|
||||||
panic("TODO")
|
panic("TODO")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user