wip: caldav: add support for reports

This commit is contained in:
Simon Ser 2022-02-02 13:52:14 +01:00
parent 373663f9ee
commit 0afce10e07
2 changed files with 52 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package caldav
import ( import (
"encoding/xml" "encoding/xml"
"fmt"
"time" "time"
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
@ -14,8 +15,12 @@ var (
calendarDescriptionName = xml.Name{namespace, "calendar-description"} calendarDescriptionName = xml.Name{namespace, "calendar-description"}
supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"} supportedCalendarDataName = xml.Name{namespace, "supported-calendar-data"}
supportedCalendarComponentSetName = xml.Name{namespace, "supported-calendar-component-set"}
maxResourceSizeName = xml.Name{namespace, "max-resource-size"} maxResourceSizeName = xml.Name{namespace, "max-resource-size"}
calendarQueryName = xml.Name{namespace, "calendar-query"}
calendarMultigetName = xml.Name{namespace, "calendar-multiget"}
calendarName = xml.Name{namespace, "calendar"} calendarName = xml.Name{namespace, "calendar"}
) )
@ -37,6 +42,12 @@ type supportedCalendarData struct {
Types []calendarDataType `xml:"calendar-data"` Types []calendarDataType `xml:"calendar-data"`
} }
// https://tools.ietf.org/html/rfc4791#section-5.2.3
type supportedCalendarComponentSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-component-set"`
Comp []comp `xml:"comp"`
}
// https://tools.ietf.org/html/rfc4791#section-9.6 // https://tools.ietf.org/html/rfc4791#section-9.6
type calendarDataType struct { type calendarDataType struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"`
@ -160,3 +171,25 @@ type calendarDataResp struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"` XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-data"`
Data []byte `xml:",chardata"` Data []byte `xml:",chardata"`
} }
type reportReq struct {
Query *calendarQuery
Multiget *calendarMultiget
// TODO: CALDAV:free-busy-query
}
func (r *reportReq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v interface{}
switch start.Name {
case calendarQueryName:
r.Query = &calendarQuery{}
v = r.Query
case calendarMultigetName:
r.Multiget = &calendarMultiget{}
v = r.Multiget
default:
return fmt.Errorf("caldav: unsupported REPORT root %q %q", start.Name.Space, start.Name.Local)
}
return d.DecodeElement(v, &start)
}

View File

@ -7,6 +7,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"bytes"
"log"
) )
func ServeError(w http.ResponseWriter, err error) { func ServeError(w http.ResponseWriter, err error) {
@ -38,6 +41,13 @@ func ServeXML(w http.ResponseWriter) *xml.Encoder {
func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error { func ServeMultistatus(w http.ResponseWriter, ms *Multistatus) error {
// TODO: streaming // TODO: streaming
w.WriteHeader(http.StatusMultiStatus) w.WriteHeader(http.StatusMultiStatus)
var buf bytes.Buffer
enc := xml.NewEncoder(&buf)
enc.Indent(" ", " ")
enc.Encode(&ms)
log.Println(">>", buf.String())
return ServeXML(w).Encode(ms) return ServeXML(w).Encode(ms)
} }
@ -129,6 +139,12 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error {
return err return err
} }
var buf bytes.Buffer
enc := xml.NewEncoder(&buf)
enc.Indent(" ", " ")
enc.Encode(&propfind)
log.Println("<< PROPFIND", r.URL, buf.String())
depth := DepthInfinity depth := DepthInfinity
if s := r.Header.Get("Depth"); s != "" { if s := r.Header.Get("Depth"); s != "" {
var err error var err error