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"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2020-01-15 11:30:42 +00:00
|
|
|
// Depth indicates whether a request applies to the resource's members. It's
|
|
|
|
// defined in RFC 4918 section 10.2.
|
|
|
|
type Depth int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DepthZero indicates that the request applies only to the resource.
|
|
|
|
DepthZero Depth = 0
|
|
|
|
// DepthOne indicates that the request applies to the resource and its
|
|
|
|
// internal members only.
|
|
|
|
DepthOne Depth = 1
|
|
|
|
// DepthInfinity indicates that the request applies to the resource and all
|
|
|
|
// of its members.
|
|
|
|
DepthInfinity Depth = -1
|
|
|
|
)
|
|
|
|
|
2020-01-15 17:21:27 +00:00
|
|
|
// ParseDepth parses a Depth header.
|
|
|
|
func ParseDepth(s string) (Depth, error) {
|
|
|
|
switch s {
|
|
|
|
case "0":
|
|
|
|
return DepthZero, nil
|
|
|
|
case "1":
|
|
|
|
return DepthOne, nil
|
|
|
|
case "infinity":
|
|
|
|
return DepthInfinity, nil
|
|
|
|
}
|
|
|
|
return 0, fmt.Errorf("webdav: invalid Depth value")
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:30:42 +00:00
|
|
|
// String formats the depth.
|
|
|
|
func (d Depth) String() string {
|
|
|
|
switch d {
|
|
|
|
case DepthZero:
|
|
|
|
return "0"
|
|
|
|
case DepthOne:
|
|
|
|
return "1"
|
|
|
|
case DepthInfinity:
|
|
|
|
return "infinity"
|
|
|
|
}
|
|
|
|
panic("webdav: invalid Depth value")
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
type Client struct {
|
|
|
|
http *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(c *http.Client, endpoint string) (*Client, error) {
|
|
|
|
if c == nil {
|
|
|
|
c = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{c, u}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:43:09 +00:00
|
|
|
func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.Request, error) {
|
2020-01-14 17:51:17 +00:00
|
|
|
u := url.URL{
|
|
|
|
Scheme: c.endpoint.Scheme,
|
|
|
|
User: c.endpoint.User,
|
|
|
|
Host: c.endpoint.Host,
|
2020-01-14 20:43:09 +00:00
|
|
|
Path: path.Join(c.endpoint.Path, href),
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
return http.NewRequest(method, u.String(), body)
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:43:09 +00:00
|
|
|
func (c *Client) NewXMLRequest(method string, href 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-14 20:43:09 +00:00
|
|
|
req, err := c.NewRequest(method, href, &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) {
|
|
|
|
// TODO: remove this quirk
|
2020-01-14 22:13:23 +00:00
|
|
|
req.SetBasicAuth("emersion", "")
|
2020-01-14 17:51:17 +00:00
|
|
|
return c.http.Do(req)
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00: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
|
2020-01-14 20:29:54 +00: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
|
|
|
|
2020-01-15 11:30:42 +00:00
|
|
|
func (c *Client) Propfind(href string, depth Depth, propfind *Propfind) (*Multistatus, error) {
|
2020-01-14 20:43:09 +00:00
|
|
|
req, err := c.NewXMLRequest("PROPFIND", href, propfind)
|
|
|
|
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.
|
|
|
|
func (c *Client) PropfindFlat(href string, propfind *Propfind) (*Response, error) {
|
|
|
|
ms, err := c.Propfind(href, DepthZero, propfind)
|
2020-01-14 20:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ms.Get(href)
|
|
|
|
}
|