internal: add PropFindValue

NewPropFindResponse uses callbacks to lazily build the response.
However, some props are static: they don't require any processing
to generate. Add a small helper to reduce boilerplate a bit.
This commit is contained in:
Simon Ser 2024-04-17 17:50:33 +02:00
parent ff8598015d
commit e9a76eef05
4 changed files with 109 additions and 125 deletions

View File

@ -465,12 +465,10 @@ func (b *backend) propFindRoot(ctx context.Context, propfind *internal.PropFind)
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -486,15 +484,13 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
calendarHomeSetName: func(*internal.RawXMLValue) (interface{}, error) { calendarHomeSetName: internal.PropFindValue(&calendarHomeSet{
return &calendarHomeSet{Href: internal.Href{Path: homeSetPath}}, nil Href: internal.Href{Path: homeSetPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -511,12 +507,10 @@ func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFi
// TODO anything else to return here? // TODO anything else to return here?
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(homeSetPath, propfind, props) return internal.NewPropFindResponse(homeSetPath, propfind, props)
} }
@ -530,19 +524,15 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, calendarName)),
return internal.NewResourceType(internal.CollectionName, calendarName), nil calendarDescriptionName: internal.PropFindValue(&calendarDescription{
}, Description: cal.Description,
calendarDescriptionName: func(*internal.RawXMLValue) (interface{}, error) { }),
return &calendarDescription{Description: cal.Description}, nil supportedCalendarDataName: internal.PropFindValue(&supportedCalendarData{
},
supportedCalendarDataName: func(*internal.RawXMLValue) (interface{}, error) {
return &supportedCalendarData{
Types: []calendarDataType{ Types: []calendarDataType{
{ContentType: ical.MIMEType, Version: "2.0"}, {ContentType: ical.MIMEType, Version: "2.0"},
}, },
}, nil }),
},
supportedCalendarComponentSetName: func(*internal.RawXMLValue) (interface{}, error) { supportedCalendarComponentSetName: func(*internal.RawXMLValue) (interface{}, error) {
components := []comp{} components := []comp{}
if cal.SupportedComponentSet != nil { if cal.SupportedComponentSet != nil {
@ -559,19 +549,19 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF
} }
if cal.Name != "" { if cal.Name != "" {
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.DisplayNameName] = internal.PropFindValue(&internal.DisplayName{
return &internal.DisplayName{Name: cal.Name}, nil Name: cal.Name,
} })
} }
if cal.Description != "" { if cal.Description != "" {
props[calendarDescriptionName] = func(*internal.RawXMLValue) (interface{}, error) { props[calendarDescriptionName] = internal.PropFindValue(&calendarDescription{
return &calendarDescription{Description: cal.Description}, nil Description: cal.Description,
} })
} }
if cal.MaxResourceSize > 0 { if cal.MaxResourceSize > 0 {
props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) { props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
return &maxResourceSize{Size: cal.MaxResourceSize}, nil Size: cal.MaxResourceSize,
} })
} }
// TODO: CALDAV:calendar-timezone, CALDAV:supported-calendar-component-set, CALDAV:min-date-time, CALDAV:max-date-time, CALDAV:max-instances, CALDAV:max-attendees-per-instance // TODO: CALDAV:calendar-timezone, CALDAV:supported-calendar-component-set, CALDAV:min-date-time, CALDAV:max-date-time, CALDAV:max-instances, CALDAV:max-attendees-per-instance
@ -612,9 +602,9 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.GetContentTypeName: internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: ical.MIMEType}, nil Type: ical.MIMEType,
}, }),
// TODO: calendar-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
@ -627,20 +617,20 @@ func (b *backend) propFindCalendarObject(ctx context.Context, propfind *internal
} }
if co.ContentLength > 0 { if co.ContentLength > 0 {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: co.ContentLength}, nil Length: co.ContentLength,
} })
} }
if !co.ModTime.IsZero() { if !co.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(co.ModTime)}, nil LastModified: internal.Time(co.ModTime),
} })
} }
if co.ETag != "" { if co.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(co.ETag)}, nil ETag: internal.ETag(co.ETag),
} })
} }
return internal.NewPropFindResponse(co.Path, propfind, props) return internal.NewPropFindResponse(co.Path, propfind, props)

View File

@ -431,12 +431,10 @@ func (b *backend) propFindRoot(ctx context.Context, propfind *internal.PropFind)
} }
props := map[xml.Name]internal.PropFindFunc{ props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) { internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil Href: internal.Href{Path: principalPath},
}, }),
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(principalPath, propfind, props) return internal.NewPropFindResponse(principalPath, propfind, props)
} }
@ -446,30 +444,24 @@ func (b *backend) propFindUserPrincipal(ctx context.Context, propfind *internal.
if err != nil { if err != nil {
return nil, err return nil, err
} }
props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: internal.PropFindValue(&internal.CurrentUserPrincipal{
Href: internal.Href{Path: principalPath},
}),
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx) homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
props := map[xml.Name]internal.PropFindFunc{
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil
},
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) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
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) { 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) homeSetPath, err := b.Backend.AddressBookHomeSetPath(ctx)
if err != nil { if err != nil {
return nil, err return nil, err
@ -478,11 +470,13 @@ func (b *backend) propFindHomeSet(ctx context.Context, propfind *internal.PropFi
// TODO anything else to return here? // TODO anything else to return here?
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) {
principalPath, err := b.Backend.CurrentUserPrincipal(ctx)
if err != nil {
return nil, err
}
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: principalPath}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName)),
return internal.NewResourceType(internal.CollectionName), nil
},
} }
return internal.NewPropFindResponse(homeSetPath, propfind, props) return internal.NewPropFindResponse(homeSetPath, propfind, props)
} }
@ -496,33 +490,29 @@ func (b *backend) propFindAddressBook(ctx context.Context, propfind *internal.Pr
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.ResourceTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.ResourceTypeName: internal.PropFindValue(internal.NewResourceType(internal.CollectionName, addressBookName)),
return internal.NewResourceType(internal.CollectionName, addressBookName), nil supportedAddressDataName: internal.PropFindValue(&supportedAddressData{
},
supportedAddressDataName: func(*internal.RawXMLValue) (interface{}, error) {
return &supportedAddressData{
Types: []addressDataType{ Types: []addressDataType{
{ContentType: vcard.MIMEType, Version: "3.0"}, {ContentType: vcard.MIMEType, Version: "3.0"},
{ContentType: vcard.MIMEType, Version: "4.0"}, {ContentType: vcard.MIMEType, Version: "4.0"},
}, },
}, nil }),
},
} }
if ab.Name != "" { if ab.Name != "" {
props[internal.DisplayNameName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.DisplayNameName] = internal.PropFindValue(&internal.DisplayName{
return &internal.DisplayName{Name: ab.Name}, nil Name: ab.Name,
} })
} }
if ab.Description != "" { if ab.Description != "" {
props[addressBookDescriptionName] = func(*internal.RawXMLValue) (interface{}, error) { props[addressBookDescriptionName] = internal.PropFindValue(&addressbookDescription{
return &addressbookDescription{Description: ab.Description}, nil Description: ab.Description,
} })
} }
if ab.MaxResourceSize > 0 { if ab.MaxResourceSize > 0 {
props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) { props[maxResourceSizeName] = internal.PropFindValue(&maxResourceSize{
return &maxResourceSize{Size: ab.MaxResourceSize}, nil Size: ab.MaxResourceSize,
} })
} }
return internal.NewPropFindResponse(ab.Path, propfind, props) return internal.NewPropFindResponse(ab.Path, propfind, props)
@ -561,9 +551,9 @@ func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.
} }
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil return &internal.CurrentUserPrincipal{Href: internal.Href{Path: path}}, nil
}, },
internal.GetContentTypeName: func(*internal.RawXMLValue) (interface{}, error) { internal.GetContentTypeName: internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: vcard.MIMEType}, nil Type: vcard.MIMEType,
}, }),
// TODO: address-data can only be used in REPORT requests // TODO: address-data can only be used in REPORT requests
addressDataName: func(*internal.RawXMLValue) (interface{}, error) { addressDataName: func(*internal.RawXMLValue) (interface{}, error) {
var buf bytes.Buffer var buf bytes.Buffer
@ -576,20 +566,20 @@ func (b *backend) propFindAddressObject(ctx context.Context, propfind *internal.
} }
if ao.ContentLength > 0 { if ao.ContentLength > 0 {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: ao.ContentLength}, nil Length: ao.ContentLength,
} })
} }
if !ao.ModTime.IsZero() { if !ao.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(ao.ModTime)}, nil LastModified: internal.Time(ao.ModTime),
} })
} }
if ao.ETag != "" { if ao.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(ao.ETag)}, nil ETag: internal.ETag(ao.ETag),
} })
} }
return internal.NewPropFindResponse(ao.Path, propfind, props) return internal.NewPropFindResponse(ao.Path, propfind, props)

View File

@ -162,13 +162,17 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error {
type PropFindFunc func(raw *RawXMLValue) (interface{}, error) type PropFindFunc func(raw *RawXMLValue) (interface{}, error)
func PropFindValue(value interface{}) PropFindFunc {
return func(raw *RawXMLValue) (interface{}, error) {
return value, nil
}
}
func NewPropFindResponse(path string, propfind *PropFind, props map[xml.Name]PropFindFunc) (*Response, error) { func NewPropFindResponse(path string, propfind *PropFind, props map[xml.Name]PropFindFunc) (*Response, error) {
resp := &Response{Hrefs: []Href{Href{Path: path}}} resp := &Response{Hrefs: []Href{Href{Path: path}}}
if _, ok := props[ResourceTypeName]; !ok { if _, ok := props[ResourceTypeName]; !ok {
props[ResourceTypeName] = func(*RawXMLValue) (interface{}, error) { props[ResourceTypeName] = PropFindValue(NewResourceType())
return NewResourceType(), nil
}
} }
if propfind.PropName != nil { if propfind.PropName != nil {

View File

@ -162,26 +162,26 @@ func (b *backend) propFindFile(propfind *internal.PropFind, fi *FileInfo) (*inte
} }
if !fi.IsDir { if !fi.IsDir {
props[internal.GetContentLengthName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentLengthName] = internal.PropFindValue(&internal.GetContentLength{
return &internal.GetContentLength{Length: fi.Size}, nil Length: fi.Size,
} })
if !fi.ModTime.IsZero() { if !fi.ModTime.IsZero() {
props[internal.GetLastModifiedName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetLastModifiedName] = internal.PropFindValue(&internal.GetLastModified{
return &internal.GetLastModified{LastModified: internal.Time(fi.ModTime)}, nil LastModified: internal.Time(fi.ModTime),
} })
} }
if fi.MIMEType != "" { if fi.MIMEType != "" {
props[internal.GetContentTypeName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetContentTypeName] = internal.PropFindValue(&internal.GetContentType{
return &internal.GetContentType{Type: fi.MIMEType}, nil Type: fi.MIMEType,
} })
} }
if fi.ETag != "" { if fi.ETag != "" {
props[internal.GetETagName] = func(*internal.RawXMLValue) (interface{}, error) { props[internal.GetETagName] = internal.PropFindValue(&internal.GetETag{
return &internal.GetETag{ETag: internal.ETag(fi.ETag)}, nil ETag: internal.ETag(fi.ETag),
} })
} }
} }