carddav: add Client.HasSupport

This commit is contained in:
Simon Ser 2020-02-05 16:08:15 +01:00
parent 3ea3818dd8
commit f9d728aaeb
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 53 additions and 0 deletions

View File

@ -70,6 +70,18 @@ func (c *Client) SetBasicAuth(username, password string) {
c.ic.SetBasicAuth(username, password)
}
func (c *Client) HasSupport() error {
classes, _, err := c.ic.Options("/")
if err != nil {
return err
}
if !classes["addressbook"] {
return fmt.Errorf("carddav: server doesn't support the DAV addressbook class")
}
return nil
}
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
propfind := internal.NewPropNamePropfind(addressBookHomeSetName)
resp, err := c.ic.PropfindFlat(principal, propfind)

View File

@ -8,6 +8,8 @@ import (
"net/http"
"net/url"
"path"
"unicode"
"strings"
)
type Client struct {
@ -119,3 +121,42 @@ func (c *Client) PropfindFlat(path string, propfind *Propfind) (*Response, error
return ms.Get(path)
}
func parseCommaSeparatedSet(values []string, upper bool) map[string]bool {
m := make(map[string]bool)
for _, v := range values {
fields := strings.FieldsFunc(v, func(r rune) bool {
return unicode.IsSpace(r) || r == ','
})
for _, f := range fields {
if upper {
f = strings.ToUpper(f)
} else {
f = strings.ToLower(f)
}
m[f] = true
}
}
return m
}
func (c *Client) Options(path string) (classes map[string]bool, methods map[string]bool, err error) {
req, err := c.NewRequest(http.MethodOptions, path, nil)
if err != nil {
return nil, nil, err
}
resp, err := c.Do(req)
if err != nil {
return nil, nil, err
}
resp.Body.Close()
classes = parseCommaSeparatedSet(resp.Header["Dav"], false)
if !classes["1"] {
return nil, nil, fmt.Errorf("webdav: server doesn't support DAV class 1")
}
methods = parseCommaSeparatedSet(resp.Header["Allow"], true)
return classes, methods, nil
}