Compare commits
No commits in common. "main" and "v0.8.1" have entirely different histories.
@ -4,8 +4,6 @@
|
|||||||
package caldav
|
package caldav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -81,16 +79,6 @@ type Calendar struct {
|
|||||||
// client sets it when the server reports a DAV:current-user-privilege-set
|
// client sets it when the server reports a DAV:current-user-privilege-set
|
||||||
// that doesn't grant DAV:write.
|
// that doesn't grant DAV:write.
|
||||||
ReadOnly bool
|
ReadOnly bool
|
||||||
// CTag is the value of the getctag property in the
|
|
||||||
// http://calendarserver.org/ns/ namespace. It changes whenever any object
|
|
||||||
// in the calendar changes.
|
|
||||||
CTag string
|
|
||||||
// SyncToken is the value of the DAV:sync-token property (RFC 6578). It
|
|
||||||
// must be a URI (RFC 6578 section 6.2).
|
|
||||||
SyncToken string
|
|
||||||
// Transparent marks a calendar whose events don't count as busy time. It
|
|
||||||
// is served as CALDAV:schedule-calendar-transp (RFC 6638 section 9.1).
|
|
||||||
Transparent bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CalendarCompRequest struct {
|
type CalendarCompRequest struct {
|
||||||
@ -161,54 +149,9 @@ type SyncQuery struct {
|
|||||||
Limit int // <= 0 means unlimited
|
Limit int // <= 0 means unlimited
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncResponse contains the returned sync-token for next time. A server's
|
// SyncResponse contains the returned sync-token for next time
|
||||||
// sync-token must be a URI (RFC 6578 section 6.2).
|
|
||||||
type SyncResponse struct {
|
type SyncResponse struct {
|
||||||
SyncToken string
|
SyncToken string
|
||||||
Updated []CalendarObject
|
Updated []CalendarObject
|
||||||
Deleted []string
|
Deleted []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrInvalidSyncToken is returned by SyncBackend.SyncCalendar when the token
|
|
||||||
// is unknown or no longer valid. The client then starts again with an empty
|
|
||||||
// token.
|
|
||||||
var ErrInvalidSyncToken = errors.New("caldav: invalid sync token")
|
|
||||||
|
|
||||||
// SyncBackend is an optional Backend interface answering the sync-collection
|
|
||||||
// REPORT (RFC 6578) on calendars.
|
|
||||||
type SyncBackend interface {
|
|
||||||
// SyncCalendar returns the objects changed and deleted in the calendar at
|
|
||||||
// path since token, and the token for the next sync. An empty token asks
|
|
||||||
// for every object. Tokens must be URIs (RFC 6578 section 6.2).
|
|
||||||
SyncCalendar(ctx context.Context, path, token string, req *CalendarCompRequest) (*SyncResponse, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrincipalBackend is an optional Backend interface serving the scheduling
|
|
||||||
// properties of the current user principal (RFC 6638 section 2).
|
|
||||||
type PrincipalBackend interface {
|
|
||||||
// CalendarUserAddresses returns the email addresses of the current user,
|
|
||||||
// served as mailto: URIs.
|
|
||||||
CalendarUserAddresses(ctx context.Context) ([]string, error)
|
|
||||||
ScheduleInboxPath(ctx context.Context) (string, error)
|
|
||||||
ScheduleOutboxPath(ctx context.Context) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrincipalNameBackend is an optional Backend interface naming the current
|
|
||||||
// user principal, served as its DAV:displayname.
|
|
||||||
type PrincipalNameBackend interface {
|
|
||||||
PrincipalDisplayName(ctx context.Context) (string, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// FreeBusyBackend is an optional Backend interface answering free/busy
|
|
||||||
// requests POSTed to the schedule outbox (RFC 6638 section 5). The Backend
|
|
||||||
// must also implement PrincipalBackend.
|
|
||||||
//
|
|
||||||
// Implementing it advertises calendar-auto-schedule in the DAV header, so
|
|
||||||
// clients stop sending invitations themselves: the Backend then owns
|
|
||||||
// scheduling for every object it stores.
|
|
||||||
type FreeBusyBackend interface {
|
|
||||||
// FreeBusy answers a VFREEBUSY request with one VFREEBUSY per ATTENDEE of
|
|
||||||
// the request, each carrying that ATTENDEE. A VFREEBUSY may carry a
|
|
||||||
// REQUEST-STATUS; it defaults to "2.0;Success".
|
|
||||||
FreeBusy(ctx context.Context, req *ical.Calendar) (*ical.Calendar, error)
|
|
||||||
}
|
|
||||||
|
|||||||
@ -25,14 +25,6 @@ var (
|
|||||||
calendarDataName = xml.Name{namespace, "calendar-data"}
|
calendarDataName = xml.Name{namespace, "calendar-data"}
|
||||||
|
|
||||||
calendarColorName = xml.Name{"http://apple.com/ns/ical/", "calendar-color"}
|
calendarColorName = xml.Name{"http://apple.com/ns/ical/", "calendar-color"}
|
||||||
getCTagName = xml.Name{Space: "http://calendarserver.org/ns/", Local: "getctag"}
|
|
||||||
|
|
||||||
calendarUserAddressSetName = xml.Name{Space: namespace, Local: "calendar-user-address-set"}
|
|
||||||
scheduleInboxURLName = xml.Name{Space: namespace, Local: "schedule-inbox-URL"}
|
|
||||||
scheduleOutboxURLName = xml.Name{Space: namespace, Local: "schedule-outbox-URL"}
|
|
||||||
scheduleInboxName = xml.Name{Space: namespace, Local: "schedule-inbox"}
|
|
||||||
scheduleOutboxName = xml.Name{Space: namespace, Local: "schedule-outbox"}
|
|
||||||
scheduleCalendarTranspName = xml.Name{Space: namespace, Local: "schedule-calendar-transp"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4791#section-6.2.1
|
// https://tools.ietf.org/html/rfc4791#section-6.2.1
|
||||||
@ -58,57 +50,6 @@ type calendarColor struct {
|
|||||||
Color string `xml:",chardata"`
|
Color string `xml:",chardata"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// getctag is not defined by any RFC. It comes from the
|
|
||||||
// http://calendarserver.org/ns/ namespace.
|
|
||||||
type getCTag struct {
|
|
||||||
XMLName xml.Name `xml:"http://calendarserver.org/ns/ getctag"`
|
|
||||||
CTag string `xml:",chardata"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-2.4.1
|
|
||||||
type calendarUserAddressSet struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-user-address-set"`
|
|
||||||
Hrefs []internal.Href `xml:"DAV: href"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-2.2
|
|
||||||
type scheduleInboxURL struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-inbox-URL"`
|
|
||||||
Href internal.Href `xml:"DAV: href"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-2.1
|
|
||||||
type scheduleOutboxURL struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-outbox-URL"`
|
|
||||||
Href internal.Href `xml:"DAV: href"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-9.1
|
|
||||||
type scheduleCalendarTransp struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-calendar-transp"`
|
|
||||||
Transparent *struct{} `xml:"urn:ietf:params:xml:ns:caldav transparent,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-10.3
|
|
||||||
type scheduleResponse struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav schedule-response"`
|
|
||||||
Responses []scheduleResponseStatus `xml:"response"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-10.4
|
|
||||||
type scheduleResponseStatus struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav response"`
|
|
||||||
Recipient recipient `xml:"recipient"`
|
|
||||||
RequestStatus string `xml:"urn:ietf:params:xml:ns:caldav request-status"`
|
|
||||||
CalendarData *calendarDataResp `xml:"calendar-data,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6638#section-10.5
|
|
||||||
type recipient struct {
|
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav recipient"`
|
|
||||||
Href internal.Href `xml:"DAV: href"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4791#section-5.2.4
|
// https://tools.ietf.org/html/rfc4791#section-5.2.4
|
||||||
type supportedCalendarData struct {
|
type supportedCalendarData struct {
|
||||||
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"`
|
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"`
|
||||||
@ -285,7 +226,6 @@ type calendarDataResp struct {
|
|||||||
type reportReq struct {
|
type reportReq struct {
|
||||||
Query *calendarQuery
|
Query *calendarQuery
|
||||||
Multiget *calendarMultiget
|
Multiget *calendarMultiget
|
||||||
SyncCollection *internal.SyncCollectionQuery
|
|
||||||
// TODO: CALDAV:free-busy-query
|
// TODO: CALDAV:free-busy-query
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -298,9 +238,6 @@ func (r *reportReq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|||||||
case calendarMultigetName:
|
case calendarMultigetName:
|
||||||
r.Multiget = &calendarMultiget{}
|
r.Multiget = &calendarMultiget{}
|
||||||
v = r.Multiget
|
v = r.Multiget
|
||||||
case internal.SyncCollectionName:
|
|
||||||
r.SyncCollection = &internal.SyncCollectionQuery{}
|
|
||||||
v = r.SyncCollection
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("caldav: unsupported REPORT root %q %q", start.Name.Space, start.Name.Local)
|
return fmt.Errorf("caldav: unsupported REPORT root %q %q", start.Name.Space, start.Name.Local)
|
||||||
}
|
}
|
||||||
|
|||||||
281
caldav/server.go
281
caldav/server.go
@ -4,11 +4,9 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -75,8 +73,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "REPORT":
|
case "REPORT":
|
||||||
err = h.handleReport(w, r)
|
err = h.handleReport(w, r)
|
||||||
case http.MethodPost:
|
|
||||||
err = h.handlePost(w, r)
|
|
||||||
default:
|
default:
|
||||||
b := backend{
|
b := backend{
|
||||||
Backend: h.Backend,
|
Backend: h.Backend,
|
||||||
@ -101,10 +97,8 @@ func (h *Handler) handleReport(w http.ResponseWriter, r *http.Request) error {
|
|||||||
return h.handleQuery(r, w, report.Query)
|
return h.handleQuery(r, w, report.Query)
|
||||||
} else if report.Multiget != nil {
|
} else if report.Multiget != nil {
|
||||||
return h.handleMultiget(r.Context(), w, report.Multiget)
|
return h.handleMultiget(r.Context(), w, report.Multiget)
|
||||||
} else if report.SyncCollection != nil {
|
|
||||||
return h.handleSyncCollection(r, w, report.SyncCollection)
|
|
||||||
}
|
}
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: expected calendar-query, calendar-multiget or sync-collection element in REPORT request")
|
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: expected calendar-query or calendar-multiget element in REPORT request")
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeParamFilter(el *paramFilter) (*ParamFilter, error) {
|
func decodeParamFilter(el *paramFilter) (*ParamFilter, error) {
|
||||||
@ -250,26 +244,23 @@ func (h *Handler) handleQuery(r *http.Request, w http.ResponseWriter, query *cal
|
|||||||
return internal.ServeMultiStatus(w, ms)
|
return internal.ServeMultiStatus(w, ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeCalendarDataProp(prop *internal.Prop) (*CalendarCompRequest, error) {
|
|
||||||
if prop == nil {
|
|
||||||
return &CalendarCompRequest{}, nil
|
|
||||||
}
|
|
||||||
var calendarData calendarDataReq
|
|
||||||
if err := prop.Decode(&calendarData); err != nil && !internal.IsNotFound(err) {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return decodeCalendarDataReq(&calendarData)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, multiget *calendarMultiget) error {
|
func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, multiget *calendarMultiget) error {
|
||||||
dataReq, err := decodeCalendarDataProp(multiget.Prop)
|
var dataReq CalendarCompRequest
|
||||||
|
if multiget.Prop != nil {
|
||||||
|
var calendarData calendarDataReq
|
||||||
|
if err := multiget.Prop.Decode(&calendarData); err != nil && !internal.IsNotFound(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
decoded, err := decodeCalendarDataReq(&calendarData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
dataReq = *decoded
|
||||||
|
}
|
||||||
|
|
||||||
var resps []internal.Response
|
var resps []internal.Response
|
||||||
for _, href := range multiget.Hrefs {
|
for _, href := range multiget.Hrefs {
|
||||||
co, err := h.Backend.GetCalendarObject(ctx, href.Path, dataReq)
|
co, err := h.Backend.GetCalendarObject(ctx, href.Path, &dataReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp := internal.NewErrorResponse(href.Path, err)
|
resp := internal.NewErrorResponse(href.Path, err)
|
||||||
resps = append(resps, *resp)
|
resps = append(resps, *resp)
|
||||||
@ -296,148 +287,6 @@ func (h *Handler) handleMultiget(ctx context.Context, w http.ResponseWriter, mul
|
|||||||
return internal.ServeMultiStatus(w, ms)
|
return internal.ServeMultiStatus(w, ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleSyncCollection(r *http.Request, w http.ResponseWriter, sync *internal.SyncCollectionQuery) error {
|
|
||||||
sb, ok := h.Backend.(SyncBackend)
|
|
||||||
if !ok {
|
|
||||||
return internal.HTTPErrorf(http.StatusForbidden, "caldav: sync-collection REPORT not supported")
|
|
||||||
}
|
|
||||||
// Calendars hold no nested collections, so infinite is answered as 1.
|
|
||||||
if sync.SyncLevel != "" && sync.SyncLevel != "1" && sync.SyncLevel != "infinite" {
|
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: invalid sync-level %q", sync.SyncLevel)
|
|
||||||
}
|
|
||||||
|
|
||||||
b := backend{
|
|
||||||
Backend: h.Backend,
|
|
||||||
Prefix: strings.TrimSuffix(h.Prefix, "/"),
|
|
||||||
}
|
|
||||||
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeCalendar {
|
|
||||||
return internal.HTTPErrorf(http.StatusForbidden, "caldav: sync-collection REPORT is only supported on calendars")
|
|
||||||
}
|
|
||||||
boxPath, _, err := b.scheduleBox(r.Context(), r.URL.Path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
} else if boxPath != "" {
|
|
||||||
return internal.HTTPErrorf(http.StatusForbidden, "caldav: sync-collection REPORT is only supported on calendars")
|
|
||||||
}
|
|
||||||
|
|
||||||
dataReq, err := decodeCalendarDataProp(sync.Prop)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: honour DAV:limit (RFC 6578 section 3.7)
|
|
||||||
res, err := sb.SyncCalendar(r.Context(), r.URL.Path, sync.SyncToken, dataReq)
|
|
||||||
if errors.Is(err, ErrInvalidSyncToken) {
|
|
||||||
return &internal.HTTPError{
|
|
||||||
Code: http.StatusForbidden,
|
|
||||||
Err: &internal.Error{
|
|
||||||
Raw: []internal.RawXMLValue{*internal.NewRawXMLElement(internal.ValidSyncTokenName, nil, nil)},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
propfind := internal.PropFind{Prop: sync.Prop}
|
|
||||||
var resps []internal.Response
|
|
||||||
for _, co := range res.Updated {
|
|
||||||
resp, err := b.propFindCalendarObject(r.Context(), &propfind, &co)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
resps = append(resps, *resp)
|
|
||||||
}
|
|
||||||
for _, p := range res.Deleted {
|
|
||||||
resps = append(resps, internal.Response{
|
|
||||||
Hrefs: []internal.Href{{Path: p}},
|
|
||||||
Status: &internal.Status{Code: http.StatusNotFound},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
ms := internal.NewMultiStatus(resps...)
|
|
||||||
ms.SyncToken = res.SyncToken
|
|
||||||
return internal.ServeMultiStatus(w, ms)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *Handler) handlePost(w http.ResponseWriter, r *http.Request) error {
|
|
||||||
fb, ok := h.Backend.(FreeBusyBackend)
|
|
||||||
pb, ok2 := h.Backend.(PrincipalBackend)
|
|
||||||
if !ok || !ok2 {
|
|
||||||
return internal.HTTPErrorf(http.StatusMethodNotAllowed, "caldav: unsupported method")
|
|
||||||
}
|
|
||||||
outboxPath, err := pb.ScheduleOutboxPath(r.Context())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if path.Clean(r.URL.Path) != path.Clean(outboxPath) {
|
|
||||||
return internal.HTTPErrorf(http.StatusMethodNotAllowed, "caldav: POST is only allowed on the schedule outbox")
|
|
||||||
}
|
|
||||||
|
|
||||||
t, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
|
||||||
if err != nil || t != ical.MIMEType {
|
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: expected %s request", ical.MIMEType)
|
|
||||||
}
|
|
||||||
req, err := ical.NewDecoder(r.Body).Decode()
|
|
||||||
if err != nil {
|
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: failed to parse iCalendar: %v", err)
|
|
||||||
}
|
|
||||||
var query *ical.Component
|
|
||||||
for _, child := range req.Children {
|
|
||||||
if child.Name == ical.CompFreeBusy {
|
|
||||||
query = child
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if query == nil {
|
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: expected a VFREEBUSY request")
|
|
||||||
}
|
|
||||||
|
|
||||||
reply, err := fb.FreeBusy(r.Context(), req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var resp scheduleResponse
|
|
||||||
for _, attendee := range query.Props.Values(ical.PropAttendee) {
|
|
||||||
u, err := url.Parse(attendee.Value)
|
|
||||||
if err != nil {
|
|
||||||
return internal.HTTPErrorf(http.StatusBadRequest, "caldav: invalid ATTENDEE %q: %v", attendee.Value, err)
|
|
||||||
}
|
|
||||||
status := scheduleResponseStatus{
|
|
||||||
Recipient: recipient{Href: internal.Href(*u)},
|
|
||||||
RequestStatus: "3.7;Invalid calendar user",
|
|
||||||
}
|
|
||||||
for _, comp := range reply.Children {
|
|
||||||
if comp.Name != ical.CompFreeBusy {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if p := comp.Props.Get(ical.PropAttendee); p == nil || !strings.EqualFold(p.Value, attendee.Value) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
status.RequestStatus = "2.0;Success"
|
|
||||||
if p := comp.Props.Get(ical.PropRequestStatus); p != nil {
|
|
||||||
status.RequestStatus = p.Value
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(status.RequestStatus, "2.") {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
cal := ical.NewCalendar()
|
|
||||||
cal.Props = reply.Props
|
|
||||||
cal.Children = []*ical.Component{comp}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := ical.NewEncoder(&buf).Encode(cal); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
status.CalendarData = &calendarDataResp{Data: buf.Bytes()}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
resp.Responses = append(resp.Responses, status)
|
|
||||||
}
|
|
||||||
|
|
||||||
return internal.ServeXML(w).Encode(&resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
type backend struct {
|
type backend struct {
|
||||||
Backend Backend
|
Backend Backend
|
||||||
Prefix string
|
Prefix string
|
||||||
@ -467,9 +316,6 @@ func (b *backend) resourceTypeAtPath(reqPath string) resourceType {
|
|||||||
|
|
||||||
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"}
|
||||||
if _, ok := b.Backend.(FreeBusyBackend); ok {
|
|
||||||
caps = append(caps, "calendar-auto-schedule")
|
|
||||||
}
|
|
||||||
|
|
||||||
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeCalendarObject {
|
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeCalendarObject {
|
||||||
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil
|
||||||
@ -526,13 +372,6 @@ func (b *backend) PropFind(r *http.Request, propfind *internal.PropFind, depth i
|
|||||||
var dataReq CalendarCompRequest
|
var dataReq CalendarCompRequest
|
||||||
var resps []internal.Response
|
var resps []internal.Response
|
||||||
|
|
||||||
resp, err := b.propFindScheduleBox(r.Context(), propfind, r.URL.Path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if resp != nil {
|
|
||||||
return internal.NewMultiStatus(*resp), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
switch resType {
|
switch resType {
|
||||||
case resourceTypeRoot:
|
case resourceTypeRoot:
|
||||||
resp, err := b.propFindRoot(r.Context(), propfind)
|
resp, err := b.propFindRoot(r.Context(), propfind)
|
||||||
@ -651,96 +490,11 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
|
|||||||
calendarHomeSetName: internal.PropFindValue(&calendarHomeSet{
|
calendarHomeSetName: internal.PropFindValue(&calendarHomeSet{
|
||||||
Href: internal.Href{Path: homeSetPath},
|
Href: internal.Href{Path: homeSetPath},
|
||||||
}),
|
}),
|
||||||
internal.PrincipalURLName: internal.PropFindValue(&internal.PrincipalURL{
|
|
||||||
Href: internal.Href{Path: principalPath},
|
|
||||||
}),
|
|
||||||
internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, internal.PrincipalName)),
|
internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, internal.PrincipalName)),
|
||||||
}
|
}
|
||||||
if nb, ok := b.Backend.(PrincipalNameBackend); ok {
|
|
||||||
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
name, err := nb.PrincipalDisplayName(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &internal.DisplayName{Name: name}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if pb, ok := b.Backend.(PrincipalBackend); ok {
|
|
||||||
props[calendarUserAddressSetName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
addrs, err := pb.CalendarUserAddresses(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
set := &calendarUserAddressSet{}
|
|
||||||
for _, addr := range addrs {
|
|
||||||
set.Hrefs = append(set.Hrefs, internal.Href{Scheme: "mailto", Opaque: addr})
|
|
||||||
}
|
|
||||||
return set, nil
|
|
||||||
}
|
|
||||||
props[scheduleInboxURLName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
p, err := pb.ScheduleInboxPath(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &scheduleInboxURL{Href: internal.Href{Path: p}}, nil
|
|
||||||
}
|
|
||||||
props[scheduleOutboxURLName] = func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
p, err := pb.ScheduleOutboxPath(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &scheduleOutboxURL{Href: internal.Href{Path: p}}, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return internal.NewPropFindResponse(principalPath, propfind, props)
|
return internal.NewPropFindResponse(principalPath, propfind, props)
|
||||||
}
|
}
|
||||||
|
|
||||||
// scheduleBox returns an empty boxPath if reqPath is neither the schedule
|
|
||||||
// inbox nor the schedule outbox.
|
|
||||||
func (b *backend) scheduleBox(ctx context.Context, reqPath string) (boxPath string, boxType xml.Name, err error) {
|
|
||||||
pb, ok := b.Backend.(PrincipalBackend)
|
|
||||||
if !ok {
|
|
||||||
return "", xml.Name{}, nil
|
|
||||||
}
|
|
||||||
inboxPath, err := pb.ScheduleInboxPath(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", xml.Name{}, err
|
|
||||||
}
|
|
||||||
outboxPath, err := pb.ScheduleOutboxPath(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return "", xml.Name{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch path.Clean(reqPath) {
|
|
||||||
case path.Clean(inboxPath):
|
|
||||||
return inboxPath, scheduleInboxName, nil
|
|
||||||
case path.Clean(outboxPath):
|
|
||||||
return outboxPath, scheduleOutboxName, nil
|
|
||||||
}
|
|
||||||
return "", xml.Name{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// propFindScheduleBox returns nil if reqPath is neither the schedule inbox nor
|
|
||||||
// the schedule outbox.
|
|
||||||
func (b *backend) propFindScheduleBox(ctx context.Context, propfind *internal.PropFind, reqPath string) (*internal.Response, error) {
|
|
||||||
boxPath, boxType, err := b.scheduleBox(ctx, reqPath)
|
|
||||||
if err != nil || boxPath == "" {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
props := map[xml.Name]internal.PropFindFunc{
|
|
||||||
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
|
|
||||||
path, err := b.Backend.CurrentUserPrincipal(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
|
|
||||||
},
|
|
||||||
internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, boxType)),
|
|
||||||
}
|
|
||||||
return internal.NewPropFindResponse(boxPath, propfind, props)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFind) (*internal.Response, error) {
|
func (b *backend) propFindHomeSet(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 {
|
||||||
@ -810,19 +564,6 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
|
|||||||
Color: cal.Color,
|
Color: cal.Color,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
if cal.CTag != "" {
|
|
||||||
props[getCTagName] = internal.PropFindValue(&getCTag{CTag: cal.CTag})
|
|
||||||
}
|
|
||||||
// RFC 6578 section 4: DAV:sync-token is not returned for DAV:allprop.
|
|
||||||
if cal.SyncToken != "" && propfind.AllProp == nil {
|
|
||||||
props[internal.SyncTokenName] = internal.PropFindValue(&internal.SyncToken{Token: cal.SyncToken})
|
|
||||||
}
|
|
||||||
if cal.Transparent {
|
|
||||||
props[scheduleCalendarTranspName] = internal.PropFindValue(&scheduleCalendarTransp{Transparent: &struct{}{}})
|
|
||||||
}
|
|
||||||
if _, ok := b.Backend.(SyncBackend); ok {
|
|
||||||
props[internal.SupportedReportSetName] = internal.PropFindValue(internal.NewSupportedReportSet(calendarQueryName, calendarMultigetName, internal.SyncCollectionName))
|
|
||||||
}
|
|
||||||
if cal.MaxResourceSize > 0 {
|
if cal.MaxResourceSize > 0 {
|
||||||
props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
|
props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
|
||||||
Size: cal.MaxResourceSize,
|
Size: cal.MaxResourceSize,
|
||||||
|
|||||||
@ -2,11 +2,9 @@ package caldav
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/xml"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -266,293 +264,3 @@ func TestFindCalendarsColor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type schedBackend struct {
|
|
||||||
testBackend
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) SyncCalendar(ctx context.Context, path, token string, req *CalendarCompRequest) (*SyncResponse, error) {
|
|
||||||
switch token {
|
|
||||||
case "":
|
|
||||||
return &SyncResponse{SyncToken: "sync-2", Updated: []CalendarObject{syncObject("a"), syncObject("b")}}, nil
|
|
||||||
case "sync-1":
|
|
||||||
return &SyncResponse{SyncToken: "sync-2", Updated: []CalendarObject{syncObject("b")}, Deleted: []string{"/user/calendars/cal/gone.ics"}}, nil
|
|
||||||
}
|
|
||||||
return nil, ErrInvalidSyncToken
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) CalendarUserAddresses(ctx context.Context) ([]string, error) {
|
|
||||||
return []string{"wilfredo@example.com"}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) PrincipalDisplayName(ctx context.Context) (string, error) {
|
|
||||||
return "Wilfredo", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) ScheduleInboxPath(ctx context.Context) (string, error) {
|
|
||||||
return "/user/inbox/", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) ScheduleOutboxPath(ctx context.Context) (string, error) {
|
|
||||||
return "/user/outbox/", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (schedBackend) FreeBusy(ctx context.Context, req *ical.Calendar) (*ical.Calendar, error) {
|
|
||||||
reply := ical.NewCalendar()
|
|
||||||
reply.Props.SetText(ical.PropVersion, "2.0")
|
|
||||||
reply.Props.SetText(ical.PropProductID, "-//Example//Server//EN")
|
|
||||||
reply.Props.SetText(ical.PropMethod, "REPLY")
|
|
||||||
for _, attendee := range req.Children[0].Props.Values(ical.PropAttendee) {
|
|
||||||
fb := ical.NewComponent(ical.CompFreeBusy)
|
|
||||||
fb.Props.SetText(ical.PropUID, "4FD3AD926350")
|
|
||||||
fb.Props.SetDateTime(ical.PropDateTimeStamp, time.Date(2026, 11, 26, 20, 7, 33, 0, time.UTC))
|
|
||||||
fb.Props.Set(&ical.Prop{Name: ical.PropAttendee, Params: ical.Params{}, Value: attendee.Value})
|
|
||||||
if strings.HasPrefix(attendee.Value, "mailto:bernard") {
|
|
||||||
fb.Props.Set(&ical.Prop{Name: ical.PropRequestStatus, Params: ical.Params{}, Value: "3.7;Invalid calendar user"})
|
|
||||||
}
|
|
||||||
fb.Props.Set(&ical.Prop{Name: ical.PropFreeBusy, Params: ical.Params{}, Value: "20261127T010000Z/20261127T020000Z"})
|
|
||||||
reply.Children = append(reply.Children, fb)
|
|
||||||
}
|
|
||||||
return reply, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func syncObject(name string) CalendarObject {
|
|
||||||
event := ical.NewEvent()
|
|
||||||
event.Props.SetText(ical.PropUID, name)
|
|
||||||
event.Props.SetText(ical.PropSummary, "Event "+name)
|
|
||||||
event.Props.SetDateTime(ical.PropDateTimeStamp, time.Date(2026, 11, 26, 9, 0, 0, 0, time.UTC))
|
|
||||||
event.Props.SetDateTime(ical.PropDateTimeStart, time.Date(2026, 11, 27, 9, 0, 0, 0, time.UTC))
|
|
||||||
cal := ical.NewCalendar()
|
|
||||||
cal.Props.SetText(ical.PropVersion, "2.0")
|
|
||||||
cal.Props.SetText(ical.PropProductID, "-//Example//Server//EN")
|
|
||||||
cal.Children = []*ical.Component{event.Component}
|
|
||||||
return CalendarObject{Path: "/user/calendars/cal/" + name + ".ics", ETag: "etag-" + name, Data: cal}
|
|
||||||
}
|
|
||||||
|
|
||||||
func serve(t *testing.T, h http.Handler, method, path, contentType, body string) (*http.Response, string) {
|
|
||||||
t.Helper()
|
|
||||||
req := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
||||||
if contentType != "" {
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
|
||||||
}
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
h.ServeHTTP(w, req)
|
|
||||||
res := w.Result()
|
|
||||||
data, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
return res, string(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPropFindCalendarCTagSyncToken(t *testing.T) {
|
|
||||||
h := &Handler{Backend: schedBackend{testBackend{calendars: []Calendar{
|
|
||||||
{Path: "/user/calendars/cal/", CTag: "ctag-1", SyncToken: "sync-1", Transparent: true},
|
|
||||||
}}}}
|
|
||||||
_, resp := serve(t, h, "PROPFIND", "/user/calendars/cal/", "application/xml", `
|
|
||||||
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:cs="http://calendarserver.org/ns/">
|
|
||||||
<d:prop><cs:getctag/><d:sync-token/><c:schedule-calendar-transp/><d:supported-report-set/></d:prop>
|
|
||||||
</d:propfind>`)
|
|
||||||
for _, want := range []string{
|
|
||||||
`<getctag xmlns="http://calendarserver.org/ns/">ctag-1</getctag>`,
|
|
||||||
`<sync-token xmlns="DAV:">sync-1</sync-token>`,
|
|
||||||
`<transparent xmlns="urn:ietf:params:xml:ns:caldav"></transparent>`,
|
|
||||||
`<sync-collection xmlns="DAV:"></sync-collection>`,
|
|
||||||
} {
|
|
||||||
if !strings.Contains(resp, want) {
|
|
||||||
t.Errorf("missing %s in response:\n%s", want, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, resp = serve(t, h, "PROPFIND", "/user/calendars/cal/", "", "")
|
|
||||||
if strings.Contains(resp, "sync-token") {
|
|
||||||
t.Errorf("allprop PROPFIND returned sync-token:\n%s", resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
h = &Handler{Backend: testBackend{calendars: []Calendar{{Path: "/user/calendars/cal/"}}}}
|
|
||||||
_, resp = serve(t, h, "PROPFIND", "/user/calendars/cal/", "", "")
|
|
||||||
for _, unwanted := range []string{"getctag", "sync-token", "schedule-calendar-transp", "supported-report-set"} {
|
|
||||||
if strings.Contains(resp, unwanted) {
|
|
||||||
t.Errorf("unexpected %s in response:\n%s", unwanted, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const syncCollectionRequest = `
|
|
||||||
<d:sync-collection xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
|
||||||
<d:sync-token>%s</d:sync-token>
|
|
||||||
<d:sync-level>%s</d:sync-level>
|
|
||||||
<d:prop><d:getetag/><c:calendar-data/></d:prop>
|
|
||||||
</d:sync-collection>`
|
|
||||||
|
|
||||||
func TestSyncCollection(t *testing.T) {
|
|
||||||
h := &Handler{Backend: schedBackend{testBackend{calendars: []Calendar{{Path: "/user/calendars/cal/"}}}}}
|
|
||||||
|
|
||||||
res, resp := serve(t, h, "REPORT", "/user/calendars/cal/", "application/xml", fmt.Sprintf(syncCollectionRequest, "", "1"))
|
|
||||||
if res.StatusCode != http.StatusMultiStatus {
|
|
||||||
t.Fatalf("full sync: status %d:\n%s", res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
if ct := res.Header.Get("Content-Type"); ct != "application/xml; charset=utf-8" {
|
|
||||||
t.Errorf("full sync: Content-Type %q", ct)
|
|
||||||
}
|
|
||||||
for _, want := range []string{"/user/calendars/cal/a.ics", "/user/calendars/cal/b.ics", "SUMMARY:Event a", "etag-b", `<sync-token>sync-2</sync-token>`} {
|
|
||||||
if !strings.Contains(resp, want) {
|
|
||||||
t.Errorf("full sync: missing %s in response:\n%s", want, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res, resp = serve(t, h, "REPORT", "/user/calendars/cal/", "application/xml", fmt.Sprintf(syncCollectionRequest, "sync-1", "infinite"))
|
|
||||||
if res.StatusCode != http.StatusMultiStatus {
|
|
||||||
t.Fatalf("delta sync: status %d:\n%s", res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
if strings.Contains(resp, "a.ics") {
|
|
||||||
t.Errorf("delta sync: unchanged object returned:\n%s", resp)
|
|
||||||
}
|
|
||||||
for _, want := range []string{
|
|
||||||
"SUMMARY:Event b",
|
|
||||||
`<response xmlns="DAV:"><href>/user/calendars/cal/gone.ics</href><status>HTTP/1.1 404 Not Found</status></response>`,
|
|
||||||
`<sync-token>sync-2</sync-token>`,
|
|
||||||
} {
|
|
||||||
if !strings.Contains(resp, want) {
|
|
||||||
t.Errorf("delta sync: missing %s in response:\n%s", want, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
res, resp = serve(t, h, "REPORT", "/user/calendars/cal/", "application/xml", fmt.Sprintf(syncCollectionRequest, "stale", "1"))
|
|
||||||
if res.StatusCode != http.StatusForbidden || !strings.Contains(resp, "valid-sync-token") {
|
|
||||||
t.Errorf("unknown token: status %d, want 403 with valid-sync-token:\n%s", res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
if ct := res.Header.Get("Content-Type"); ct != "application/xml; charset=utf-8" {
|
|
||||||
t.Errorf("unknown token: Content-Type %q", ct)
|
|
||||||
}
|
|
||||||
|
|
||||||
noLevel := strings.Replace(fmt.Sprintf(syncCollectionRequest, "", ""), "<d:sync-level></d:sync-level>", "", 1)
|
|
||||||
res, resp = serve(t, h, "REPORT", "/user/calendars/cal/", "application/xml", noLevel)
|
|
||||||
if res.StatusCode != http.StatusMultiStatus {
|
|
||||||
t.Errorf("missing sync-level: status %d, want 207:\n%s", res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, path := range []string{"/user/calendars/", "/user/inbox/", "/user/calendars/cal/a.ics"} {
|
|
||||||
res, resp = serve(t, h, "REPORT", path, "application/xml", fmt.Sprintf(syncCollectionRequest, "", "1"))
|
|
||||||
if res.StatusCode != http.StatusForbidden {
|
|
||||||
t.Errorf("%s: status %d, want 403:\n%s", path, res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPropFindPrincipalScheduling(t *testing.T) {
|
|
||||||
h := &Handler{Backend: schedBackend{}}
|
|
||||||
_, resp := serve(t, h, "PROPFIND", "/user/", "application/xml", `
|
|
||||||
<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
|
|
||||||
<d:prop><c:calendar-user-address-set/><c:schedule-inbox-URL/><c:schedule-outbox-URL/></d:prop>
|
|
||||||
</d:propfind>`)
|
|
||||||
for _, want := range []string{
|
|
||||||
`<calendar-user-address-set xmlns="urn:ietf:params:xml:ns:caldav"><href xmlns="DAV:">mailto:wilfredo@example.com</href></calendar-user-address-set>`,
|
|
||||||
`<schedule-inbox-URL xmlns="urn:ietf:params:xml:ns:caldav"><href xmlns="DAV:">/user/inbox/</href></schedule-inbox-URL>`,
|
|
||||||
`<schedule-outbox-URL xmlns="urn:ietf:params:xml:ns:caldav"><href xmlns="DAV:">/user/outbox/</href></schedule-outbox-URL>`,
|
|
||||||
} {
|
|
||||||
if !strings.Contains(resp, want) {
|
|
||||||
t.Errorf("missing %s in response:\n%s", want, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for path, box := range map[string]string{"/user/inbox/": "schedule-inbox", "/user/outbox/": "schedule-outbox"} {
|
|
||||||
res, resp := serve(t, h, "PROPFIND", path, "", "")
|
|
||||||
want := fmt.Sprintf(`<%s xmlns="urn:ietf:params:xml:ns:caldav"></%s>`, box, box)
|
|
||||||
if res.StatusCode != http.StatusMultiStatus || !strings.Contains(resp, want) || !strings.Contains(resp, "<href>"+path+"</href>") {
|
|
||||||
t.Errorf("%s: status %d, want resourcetype %s:\n%s", path, res.StatusCode, box, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, resp = serve(t, &Handler{Backend: testBackend{}}, "PROPFIND", "/user/", "", "")
|
|
||||||
if strings.Contains(resp, "schedule-inbox-URL") {
|
|
||||||
t.Errorf("scheduling property served without PrincipalBackend:\n%s", resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPropFindPrincipalNameAndURL(t *testing.T) {
|
|
||||||
_, resp := serve(t, &Handler{Backend: schedBackend{}}, "PROPFIND", "/user/", "application/xml", `
|
|
||||||
<d:propfind xmlns:d="DAV:"><d:prop><d:principal-URL/><d:displayname/></d:prop></d:propfind>`)
|
|
||||||
for _, want := range []string{
|
|
||||||
`<principal-URL xmlns="DAV:"><href>/user/</href></principal-URL>`,
|
|
||||||
`<displayname xmlns="DAV:">Wilfredo</displayname>`,
|
|
||||||
} {
|
|
||||||
if !strings.Contains(resp, want) {
|
|
||||||
t.Errorf("missing %s in response:\n%s", want, resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, resp = serve(t, &Handler{Backend: testBackend{}}, "PROPFIND", "/user/", "application/xml", `
|
|
||||||
<d:propfind xmlns:d="DAV:"><d:prop><d:principal-URL/><d:displayname/></d:prop></d:propfind>`)
|
|
||||||
if !strings.Contains(resp, "principal-URL") || strings.Contains(resp, "Wilfredo") {
|
|
||||||
t.Errorf("principal-URL is always served, displayname only with PrincipalNameBackend:\n%s", resp)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestOptionsAutoSchedule(t *testing.T) {
|
|
||||||
res, _ := serve(t, &Handler{Backend: schedBackend{}}, http.MethodOptions, "/user/", "", "")
|
|
||||||
if dav := res.Header.Get("DAV"); !strings.Contains(dav, "calendar-auto-schedule") {
|
|
||||||
t.Errorf("DAV header %q lacks calendar-auto-schedule", dav)
|
|
||||||
}
|
|
||||||
res, _ = serve(t, &Handler{Backend: testBackend{}}, http.MethodOptions, "/user/", "", "")
|
|
||||||
if dav := res.Header.Get("DAV"); strings.Contains(dav, "calendar-auto-schedule") {
|
|
||||||
t.Errorf("DAV header %q has calendar-auto-schedule without FreeBusyBackend", dav)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const freeBusyRequest = `BEGIN:VCALENDAR
|
|
||||||
VERSION:2.0
|
|
||||||
PRODID:-//Example//Client//EN
|
|
||||||
METHOD:REQUEST
|
|
||||||
BEGIN:VFREEBUSY
|
|
||||||
UID:4FD3AD926350
|
|
||||||
DTSTAMP:20261126T200733Z
|
|
||||||
DTSTART:20261127T000000Z
|
|
||||||
DTEND:20261128T000000Z
|
|
||||||
ORGANIZER:mailto:wilfredo@example.com
|
|
||||||
ATTENDEE:mailto:cyrus@example.com
|
|
||||||
ATTENDEE:mailto:bernard@example.com
|
|
||||||
END:VFREEBUSY
|
|
||||||
END:VCALENDAR
|
|
||||||
`
|
|
||||||
|
|
||||||
func TestFreeBusyPost(t *testing.T) {
|
|
||||||
h := &Handler{Backend: schedBackend{}}
|
|
||||||
body := strings.ReplaceAll(freeBusyRequest, "\n", "\r\n")
|
|
||||||
res, resp := serve(t, h, http.MethodPost, "/user/outbox/", "text/calendar; charset=utf-8", body)
|
|
||||||
if res.StatusCode != http.StatusOK {
|
|
||||||
t.Fatalf("status %d:\n%s", res.StatusCode, resp)
|
|
||||||
}
|
|
||||||
|
|
||||||
var sr struct {
|
|
||||||
Responses []struct {
|
|
||||||
Recipient string `xml:"recipient>href"`
|
|
||||||
RequestStatus string `xml:"request-status"`
|
|
||||||
CalendarData string `xml:"calendar-data"`
|
|
||||||
} `xml:"response"`
|
|
||||||
}
|
|
||||||
if err := xml.Unmarshal([]byte(resp), &sr); err != nil {
|
|
||||||
t.Fatalf("decode schedule-response: %v\n%s", err, resp)
|
|
||||||
}
|
|
||||||
if len(sr.Responses) != 2 {
|
|
||||||
t.Fatalf("got %d responses, want 2:\n%s", len(sr.Responses), resp)
|
|
||||||
}
|
|
||||||
cyrus, bernard := sr.Responses[0], sr.Responses[1]
|
|
||||||
if cyrus.Recipient != "mailto:cyrus@example.com" || cyrus.RequestStatus != "2.0;Success" {
|
|
||||||
t.Errorf("cyrus: recipient %q status %q", cyrus.Recipient, cyrus.RequestStatus)
|
|
||||||
}
|
|
||||||
if strings.Count(cyrus.CalendarData, "BEGIN:VFREEBUSY") != 1 || !strings.Contains(cyrus.CalendarData, "FREEBUSY:20261127T010000Z/20261127T020000Z") || strings.Contains(cyrus.CalendarData, "bernard") {
|
|
||||||
t.Errorf("cyrus: calendar-data is not one VFREEBUSY for cyrus:\n%s", cyrus.CalendarData)
|
|
||||||
}
|
|
||||||
if bernard.Recipient != "mailto:bernard@example.com" || bernard.RequestStatus != "3.7;Invalid calendar user" {
|
|
||||||
t.Errorf("bernard: recipient %q status %q", bernard.Recipient, bernard.RequestStatus)
|
|
||||||
}
|
|
||||||
if bernard.CalendarData != "" {
|
|
||||||
t.Errorf("bernard: calendar-data on a failed request-status:\n%s", bernard.CalendarData)
|
|
||||||
}
|
|
||||||
|
|
||||||
res, _ = serve(t, &Handler{Backend: testBackend{}}, http.MethodPost, "/user/outbox/", "text/calendar", body)
|
|
||||||
if res.StatusCode != http.StatusMethodNotAllowed {
|
|
||||||
t.Errorf("POST without FreeBusyBackend: status %d, want 405", res.StatusCode)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -25,13 +25,7 @@ var (
|
|||||||
PrincipalName = xml.Name{Namespace, "principal"}
|
PrincipalName = xml.Name{Namespace, "principal"}
|
||||||
|
|
||||||
CurrentUserPrincipalName = xml.Name{Namespace, "current-user-principal"}
|
CurrentUserPrincipalName = xml.Name{Namespace, "current-user-principal"}
|
||||||
PrincipalURLName = xml.Name{Namespace, "principal-URL"}
|
|
||||||
CurrentUserPrivilegeSetName = xml.Name{Namespace, "current-user-privilege-set"}
|
CurrentUserPrivilegeSetName = xml.Name{Namespace, "current-user-privilege-set"}
|
||||||
|
|
||||||
SyncTokenName = xml.Name{Space: Namespace, Local: "sync-token"}
|
|
||||||
SyncCollectionName = xml.Name{Space: Namespace, Local: "sync-collection"}
|
|
||||||
ValidSyncTokenName = xml.Name{Space: Namespace, Local: "valid-sync-token"}
|
|
||||||
SupportedReportSetName = xml.Name{Space: Namespace, Local: "supported-report-set"}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Status struct {
|
type Status struct {
|
||||||
@ -418,12 +412,6 @@ func (err *Error) Error() string {
|
|||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc3744#section-4.2
|
|
||||||
type PrincipalURL struct {
|
|
||||||
XMLName xml.Name `xml:"DAV: principal-URL"`
|
|
||||||
Href Href `xml:"href"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc4918#section-15.2
|
// https://tools.ietf.org/html/rfc4918#section-15.2
|
||||||
type DisplayName struct {
|
type DisplayName struct {
|
||||||
XMLName xml.Name `xml:"DAV: displayname"`
|
XMLName xml.Name `xml:"DAV: displayname"`
|
||||||
@ -465,23 +453,6 @@ type SyncCollectionQuery struct {
|
|||||||
Prop *Prop `xml:"prop"`
|
Prop *Prop `xml:"prop"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc6578#section-6.2
|
|
||||||
type SyncToken struct {
|
|
||||||
XMLName xml.Name `xml:"DAV: sync-token"`
|
|
||||||
Token string `xml:",chardata"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewSupportedReportSet returns a DAV:supported-report-set (RFC 3253 section
|
|
||||||
// 3.1.5) listing the given reports.
|
|
||||||
func NewSupportedReportSet(reports ...xml.Name) *RawXMLValue {
|
|
||||||
supported := make([]RawXMLValue, len(reports))
|
|
||||||
for i, name := range reports {
|
|
||||||
report := NewRawXMLElement(xml.Name{Space: Namespace, Local: "report"}, nil, []RawXMLValue{*NewRawXMLElement(name, nil, nil)})
|
|
||||||
supported[i] = *NewRawXMLElement(xml.Name{Space: Namespace, Local: "supported-report"}, nil, []RawXMLValue{*report})
|
|
||||||
}
|
|
||||||
return NewRawXMLElement(SupportedReportSetName, nil, supported)
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://tools.ietf.org/html/rfc5323#section-5.17
|
// https://tools.ietf.org/html/rfc5323#section-5.17
|
||||||
type Limit struct {
|
type Limit struct {
|
||||||
XMLName xml.Name `xml:"DAV: limit"`
|
XMLName xml.Name `xml:"DAV: limit"`
|
||||||
|
|||||||
@ -21,7 +21,8 @@ func ServeError(w http.ResponseWriter, err error) {
|
|||||||
|
|
||||||
var errElt *Error
|
var errElt *Error
|
||||||
if errors.As(err, &errElt) {
|
if errors.As(err, &errElt) {
|
||||||
serveXMLStatus(w, code).Encode(errElt)
|
w.WriteHeader(code)
|
||||||
|
ServeXML(w).Encode(errElt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,21 +51,15 @@ func IsRequestBodyEmpty(r *http.Request) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ServeXML(w http.ResponseWriter) *xml.Encoder {
|
func ServeXML(w http.ResponseWriter) *xml.Encoder {
|
||||||
return serveXMLStatus(w, http.StatusOK)
|
w.Header().Add("Content-Type", "application/xml; charset=\"utf-8\"")
|
||||||
}
|
|
||||||
|
|
||||||
// serveXMLStatus sets Content-Type before WriteHeader, which ignores later
|
|
||||||
// header changes.
|
|
||||||
func serveXMLStatus(w http.ResponseWriter, code int) *xml.Encoder {
|
|
||||||
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
|
|
||||||
w.WriteHeader(code)
|
|
||||||
w.Write([]byte(xml.Header))
|
w.Write([]byte(xml.Header))
|
||||||
return xml.NewEncoder(w)
|
return xml.NewEncoder(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ServeMultiStatus(w http.ResponseWriter, ms *MultiStatus) error {
|
func ServeMultiStatus(w http.ResponseWriter, ms *MultiStatus) error {
|
||||||
// TODO: streaming
|
// TODO: streaming
|
||||||
return serveXMLStatus(w, http.StatusMultiStatus).Encode(ms)
|
w.WriteHeader(http.StatusMultiStatus)
|
||||||
|
return ServeXML(w).Encode(ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Backend interface {
|
type Backend interface {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user