go-webdav/caldav/client.go

377 lines
9.2 KiB
Go
Raw Normal View History

2020-01-30 12:18:05 +00:00
package caldav
import (
2020-02-05 17:05:48 +00:00
"bytes"
2023-12-13 13:37:38 +00:00
"context"
2020-01-30 12:51:02 +00:00
"fmt"
2020-02-24 17:19:39 +00:00
"mime"
2020-02-24 17:13:24 +00:00
"net/http"
"net/url"
"strconv"
2020-02-24 17:19:39 +00:00
"strings"
2020-02-03 16:26:55 +00:00
"time"
2020-01-30 12:18:05 +00:00
2020-02-24 16:52:25 +00:00
"github.com/emersion/go-ical"
2020-01-30 12:18:05 +00:00
"github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/internal"
)
2023-12-27 22:16:49 +00:00
// DiscoverContextURL performs a DNS-based CardDAV service discovery as
// described in RFC 6352 section 11. It returns the URL to the CardDAV server.
func DiscoverContextURL(ctx context.Context, domain string) (string, error) {
return internal.DiscoverContextURL(ctx, "caldavs", domain)
}
2020-01-30 12:18:05 +00:00
// Client provides access to a remote CardDAV server.
type Client struct {
*webdav.Client
ic *internal.Client
}
func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) {
2020-01-30 12:18:05 +00:00
wc, err := webdav.NewClient(c, endpoint)
if err != nil {
return nil, err
}
ic, err := internal.NewClient(c, endpoint)
if err != nil {
return nil, err
}
return &Client{wc, ic}, nil
}
2023-12-13 13:37:38 +00:00
func (c *Client) FindCalendarHomeSet(ctx context.Context, principal string) (string, error) {
propfind := internal.NewPropNamePropFind(calendarHomeSetName)
2023-12-13 13:37:38 +00:00
resp, err := c.ic.PropFindFlat(ctx, principal, propfind)
2020-01-30 12:18:05 +00:00
if err != nil {
return "", err
}
var prop calendarHomeSet
if err := resp.DecodeProp(&prop); err != nil {
return "", err
}
return prop.Href.Path, nil
}
2020-01-30 12:51:02 +00:00
2023-12-13 13:37:38 +00:00
func (c *Client) FindCalendars(ctx context.Context, calendarHomeSet string) ([]Calendar, error) {
propfind := internal.NewPropNamePropFind(
2020-01-30 12:51:02 +00:00
internal.ResourceTypeName,
internal.DisplayNameName,
calendarDescriptionName,
maxResourceSizeName,
supportedCalendarComponentSetName,
2020-01-30 12:51:02 +00:00
)
2023-12-13 13:37:38 +00:00
ms, err := c.ic.PropFind(ctx, calendarHomeSet, internal.DepthOne, propfind)
2020-01-30 12:51:02 +00:00
if err != nil {
return nil, err
}
l := make([]Calendar, 0, len(ms.Responses))
for _, resp := range ms.Responses {
path, err := resp.Path()
if err != nil {
return nil, err
}
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
return nil, err
}
if !resType.Is(calendarName) {
continue
}
var desc calendarDescription
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var dispName internal.DisplayName
if err := resp.DecodeProp(&dispName); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var maxResSize maxResourceSize
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
return nil, err
}
if maxResSize.Size < 0 {
return nil, fmt.Errorf("carddav: max-resource-size must be a positive integer")
}
var supportedCompSet supportedCalendarComponentSet
if err := resp.DecodeProp(&supportedCompSet); err != nil && !internal.IsNotFound(err) {
return nil, err
}
compNames := make([]string, 0, len(supportedCompSet.Comp))
for _, comp := range supportedCompSet.Comp {
compNames = append(compNames, comp.Name)
}
2020-01-30 12:51:02 +00:00
l = append(l, Calendar{
Path: path,
Name: dispName.Name,
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
SupportedComponentSet: compNames,
2020-01-30 12:51:02 +00:00
})
}
return l, nil
}
2020-02-03 16:26:55 +00:00
func encodeCalendarCompReq(c *CalendarCompRequest) (*comp, error) {
encoded := comp{Name: c.Name}
if c.AllProps {
encoded.Allprop = &struct{}{}
}
for _, name := range c.Props {
encoded.Prop = append(encoded.Prop, prop{Name: name})
}
if c.AllComps {
encoded.Allcomp = &struct{}{}
}
for _, child := range c.Comps {
encodedChild, err := encodeCalendarCompReq(&child)
if err != nil {
return nil, err
}
encoded.Comp = append(encoded.Comp, *encodedChild)
}
return &encoded, nil
}
func encodeCalendarReq(c *CalendarCompRequest) (*internal.Prop, error) {
compReq, err := encodeCalendarCompReq(c)
if err != nil {
return nil, err
}
calDataReq := calendarDataReq{Comp: compReq}
getLastModReq := internal.NewRawXMLElement(internal.GetLastModifiedName, nil, nil)
getETagReq := internal.NewRawXMLElement(internal.GetETagName, nil, nil)
return internal.EncodeProp(&calDataReq, getLastModReq, getETagReq)
}
func encodeCompFilter(filter *CompFilter) *compFilter {
encoded := compFilter{Name: filter.Name}
if !filter.Start.IsZero() || !filter.End.IsZero() {
encoded.TimeRange = &timeRange{
Start: dateWithUTCTime(filter.Start),
End: dateWithUTCTime(filter.End),
}
}
for _, child := range filter.Comps {
encoded.CompFilters = append(encoded.CompFilters, *encodeCompFilter(&child))
}
return &encoded
}
func decodeCalendarObjectList(ms *internal.MultiStatus) ([]CalendarObject, error) {
2020-02-03 16:26:55 +00:00
addrs := make([]CalendarObject, 0, len(ms.Responses))
for _, resp := range ms.Responses {
path, err := resp.Path()
if err != nil {
return nil, err
}
var calData calendarDataResp
if err := resp.DecodeProp(&calData); err != nil {
return nil, err
}
var getLastMod internal.GetLastModified
if err := resp.DecodeProp(&getLastMod); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var getETag internal.GetETag
if err := resp.DecodeProp(&getETag); err != nil && !internal.IsNotFound(err) {
return nil, err
}
var getContentLength internal.GetContentLength
if err := resp.DecodeProp(&getContentLength); err != nil && !internal.IsNotFound(err) {
return nil, err
}
2020-02-24 16:52:25 +00:00
r := bytes.NewReader(calData.Data)
2020-02-24 20:16:45 +00:00
data, err := ical.NewDecoder(r).Decode()
2020-02-05 17:05:48 +00:00
if err != nil {
return nil, err
}
2020-02-03 16:26:55 +00:00
addrs = append(addrs, CalendarObject{
Path: path,
ModTime: time.Time(getLastMod.LastModified),
ContentLength: getContentLength.Length,
ETag: string(getETag.ETag),
Data: data,
2020-02-03 16:26:55 +00:00
})
}
return addrs, nil
}
2023-12-13 13:37:38 +00:00
func (c *Client) QueryCalendar(ctx context.Context, calendar string, query *CalendarQuery) ([]CalendarObject, error) {
propReq, err := encodeCalendarReq(&query.CompRequest)
2020-02-03 16:26:55 +00:00
if err != nil {
return nil, err
}
calendarQuery := calendarQuery{Prop: propReq}
calendarQuery.Filter.CompFilter = *encodeCompFilter(&query.CompFilter)
2020-02-03 16:26:55 +00:00
req, err := c.ic.NewXMLRequest("REPORT", calendar, &calendarQuery)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
2020-02-03 16:26:55 +00:00
2023-12-13 13:37:38 +00:00
ms, err := c.ic.DoMultiStatus(req.WithContext(ctx))
2020-02-03 16:26:55 +00:00
if err != nil {
return nil, err
}
return decodeCalendarObjectList(ms)
}
2020-02-24 17:13:24 +00:00
2023-12-13 13:37:38 +00:00
func (c *Client) MultiGetCalendar(ctx context.Context, path string, multiGet *CalendarMultiGet) ([]CalendarObject, error) {
2020-05-13 15:45:10 +01:00
propReq, err := encodeCalendarReq(&multiGet.CompRequest)
if err != nil {
return nil, err
}
calendarMultiget := calendarMultiget{Prop: propReq}
if len(multiGet.Paths) == 0 {
2020-05-13 15:45:10 +01:00
href := internal.Href{Path: path}
calendarMultiget.Hrefs = []internal.Href{href}
} else {
calendarMultiget.Hrefs = make([]internal.Href, len(multiGet.Paths))
for i, p := range multiGet.Paths {
calendarMultiget.Hrefs[i] = internal.Href{Path: p}
}
}
req, err := c.ic.NewXMLRequest("REPORT", path, &calendarMultiget)
if err != nil {
return nil, err
}
req.Header.Add("Depth", "1")
2023-12-13 13:37:38 +00:00
ms, err := c.ic.DoMultiStatus(req.WithContext(ctx))
2020-05-13 15:45:10 +01:00
if err != nil {
return nil, err
}
return decodeCalendarObjectList(ms)
}
func populateCalendarObject(co *CalendarObject, h http.Header) error {
if loc := h.Get("Location"); loc != "" {
2020-02-24 17:13:24 +00:00
u, err := url.Parse(loc)
if err != nil {
return err
}
co.Path = u.Path
}
if etag := h.Get("ETag"); etag != "" {
2020-02-24 17:13:24 +00:00
etag, err := strconv.Unquote(etag)
if err != nil {
return err
}
co.ETag = etag
}
if contentLength := h.Get("Content-Length"); contentLength != "" {
n, err := strconv.ParseInt(contentLength, 10, 64)
if err != nil {
return err
}
co.ContentLength = n
}
if lastModified := h.Get("Last-Modified"); lastModified != "" {
2020-02-24 17:13:24 +00:00
t, err := http.ParseTime(lastModified)
if err != nil {
return err
}
co.ModTime = t
}
return nil
}
2023-12-13 13:37:38 +00:00
func (c *Client) GetCalendarObject(ctx context.Context, path string) (*CalendarObject, error) {
2020-02-24 17:19:39 +00:00
req, err := c.ic.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", ical.MIMEType)
2023-12-13 13:37:38 +00:00
resp, err := c.ic.Do(req.WithContext(ctx))
2020-02-24 17:19:39 +00:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
if err != nil {
return nil, err
}
if !strings.EqualFold(mediaType, ical.MIMEType) {
return nil, fmt.Errorf("caldav: expected Content-Type %q, got %q", ical.MIMEType, mediaType)
}
2020-02-24 20:16:45 +00:00
cal, err := ical.NewDecoder(resp.Body).Decode()
2020-02-24 17:19:39 +00:00
if err != nil {
return nil, err
}
co := &CalendarObject{
Path: resp.Request.URL.Path,
Data: cal,
}
if err := populateCalendarObject(co, resp.Header); err != nil {
2020-02-24 17:19:39 +00:00
return nil, err
}
return co, nil
}
2023-12-13 13:37:38 +00:00
func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.Calendar) (*CalendarObject, error) {
2020-02-24 17:13:24 +00:00
// TODO: add support for If-None-Match and If-Match
// TODO: some servers want a Content-Length header, so we can't stream the
// request body here. See the Radicale issue:
// https://github.com/Kozea/Radicale/issues/1016
var buf bytes.Buffer
2020-02-24 20:16:45 +00:00
if err := ical.NewEncoder(&buf).Encode(cal); err != nil {
2020-02-24 17:13:24 +00:00
return nil, err
}
req, err := c.ic.NewRequest(http.MethodPut, path, &buf)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", ical.MIMEType)
2023-12-13 13:37:38 +00:00
resp, err := c.ic.Do(req.WithContext(ctx))
2020-02-24 17:13:24 +00:00
if err != nil {
return nil, err
}
resp.Body.Close()
co := &CalendarObject{Path: path}
if err := populateCalendarObject(co, resp.Header); err != nil {
2020-02-24 17:13:24 +00:00
return nil, err
}
return co, nil
}