2020-01-14 17:51:17 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2020-01-14 19:00:54 +00:00
|
|
|
"bytes"
|
2020-01-14 17:51:17 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-05-13 14:02:52 +01:00
|
|
|
"mime"
|
2020-01-14 17:51:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-02-05 15:08:15 +00:00
|
|
|
"strings"
|
2020-02-05 16:07:35 +00:00
|
|
|
"unicode"
|
2020-01-14 17:51:17 +00:00
|
|
|
)
|
|
|
|
|
2020-02-19 15:02:49 +00:00
|
|
|
// HTTPClient performs HTTP requests. It's implemented by *http.Client.
|
|
|
|
type HTTPClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
type Client struct {
|
2020-02-19 15:02:49 +00:00
|
|
|
http HTTPClient
|
|
|
|
endpoint *url.URL
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 15:02:49 +00:00
|
|
|
func NewClient(c HTTPClient, endpoint string) (*Client, error) {
|
2020-01-14 17:51:17 +00:00
|
|
|
if c == nil {
|
|
|
|
c = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-12 19:04:31 +00:00
|
|
|
if u.Path == "" {
|
|
|
|
// This is important to avoid issues with path.Join
|
|
|
|
u.Path = "/"
|
|
|
|
}
|
2020-01-15 22:45:37 +00:00
|
|
|
return &Client{http: c, endpoint: u}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (c *Client) ResolveHref(p string) *url.URL {
|
2020-02-12 16:13:12 +00:00
|
|
|
if !strings.HasPrefix(p, "/") {
|
|
|
|
p = path.Join(c.endpoint.Path, p)
|
2020-02-12 15:40:03 +00:00
|
|
|
}
|
2020-01-22 10:07:30 +00:00
|
|
|
return &url.URL{
|
|
|
|
Scheme: c.endpoint.Scheme,
|
|
|
|
User: c.endpoint.User,
|
|
|
|
Host: c.endpoint.Host,
|
2020-02-12 15:40:03 +00:00
|
|
|
Path: p,
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
2020-01-22 09:15:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (c *Client) NewRequest(method string, path string, body io.Reader) (*http.Request, error) {
|
|
|
|
return http.NewRequest(method, c.ResolveHref(path).String(), body)
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (c *Client) NewXMLRequest(method string, path string, v interface{}) (*http.Request, error) {
|
2020-01-14 19:00:54 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(xml.Header)
|
2020-01-14 20:38:25 +00:00
|
|
|
if err := xml.NewEncoder(&buf).Encode(v); err != nil {
|
2020-01-14 19:00:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
req, err := c.NewRequest(method, path, &buf)
|
2020-01-14 19:00:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
2020-01-22 12:22:45 +00:00
|
|
|
resp, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode/100 != 2 {
|
2020-05-13 14:02:52 +01:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
contentType := resp.Header.Get("Content-Type")
|
|
|
|
if contentType == "" {
|
|
|
|
contentType = "text/plain"
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:46:05 +00:00
|
|
|
var wrappedErr error
|
2020-05-13 14:02:52 +01:00
|
|
|
t, _, _ := mime.ParseMediaType(contentType)
|
|
|
|
if t == "application/xml" || t == "text/xml" {
|
|
|
|
var davErr Error
|
|
|
|
if err := xml.NewDecoder(resp.Body).Decode(&davErr); err != nil {
|
|
|
|
wrappedErr = err
|
|
|
|
} else {
|
|
|
|
wrappedErr = &davErr
|
|
|
|
}
|
|
|
|
} else if strings.HasPrefix(t, "text/") {
|
2020-02-12 18:46:05 +00:00
|
|
|
lr := io.LimitedReader{R: resp.Body, N: 1024}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, &lr)
|
|
|
|
resp.Body.Close()
|
|
|
|
if s := strings.TrimSpace(buf.String()); s != "" {
|
|
|
|
if lr.N == 0 {
|
|
|
|
s += " […]"
|
|
|
|
}
|
|
|
|
wrappedErr = fmt.Errorf("%v", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, &HTTPError{Code: resp.StatusCode, Err: wrappedErr}
|
2020-01-22 12:22:45 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 16:32:12 +01:00
|
|
|
func (c *Client) DoMultiStatus(req *http.Request) (*MultiStatus, error) {
|
2020-01-14 17:51:17 +00:00
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusMultiStatus {
|
|
|
|
return nil, fmt.Errorf("HTTP multi-status request failed: %v", resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: the response can be quite large, support streaming Response elements
|
2022-05-31 16:32:12 +01:00
|
|
|
var ms MultiStatus
|
2020-01-14 17:51:17 +00:00
|
|
|
if err := xml.NewDecoder(resp.Body).Decode(&ms); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
return &ms, nil
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
2020-01-14 20:43:09 +00:00
|
|
|
|
2022-05-31 16:32:12 +01:00
|
|
|
func (c *Client) PropFind(path string, depth Depth, propfind *PropFind) (*MultiStatus, error) {
|
2020-01-22 10:07:30 +00:00
|
|
|
req, err := c.NewXMLRequest("PROPFIND", path, propfind)
|
2020-01-14 20:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:30:42 +00:00
|
|
|
req.Header.Add("Depth", depth.String())
|
|
|
|
|
|
|
|
return c.DoMultiStatus(req)
|
|
|
|
}
|
2020-01-14 20:43:09 +00:00
|
|
|
|
2020-01-15 11:30:42 +00:00
|
|
|
// PropfindFlat performs a PROPFIND request with a zero depth.
|
2022-05-31 16:32:12 +01:00
|
|
|
func (c *Client) PropFindFlat(path string, propfind *PropFind) (*Response, error) {
|
|
|
|
ms, err := c.PropFind(path, DepthZero, propfind)
|
2020-01-14 20:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 11:44:03 +01:00
|
|
|
// If the client followed a redirect, the Href might be different from the request path
|
|
|
|
if len(ms.Responses) != 1 {
|
|
|
|
return nil, fmt.Errorf("PROPFIND with Depth: 0 returned %d responses", len(ms.Responses))
|
|
|
|
}
|
|
|
|
return &ms.Responses[0], nil
|
2020-01-14 20:43:09 +00:00
|
|
|
}
|
2020-02-05 15:08:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-03-29 13:08:48 +01:00
|
|
|
|
|
|
|
// SyncCollection perform a `sync-collection` REPORT operation on a resource
|
2022-05-31 16:32:12 +01:00
|
|
|
func (c *Client) SyncCollection(path, syncToken string, level Depth, limit *Limit, prop *Prop) (*MultiStatus, error) {
|
2020-03-29 13:08:48 +01:00
|
|
|
q := SyncCollectionQuery{
|
|
|
|
SyncToken: syncToken,
|
2020-09-09 14:41:34 +01:00
|
|
|
SyncLevel: level.String(),
|
2020-03-29 13:08:48 +01:00
|
|
|
Limit: limit,
|
|
|
|
Prop: prop,
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.NewXMLRequest("REPORT", path, &q)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ms, err := c.DoMultiStatus(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms, nil
|
|
|
|
}
|