2020-01-14 17:51:17 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2020-01-14 20:29:54 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-01-14 17:51:17 +00:00
|
|
|
)
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
type Status string
|
|
|
|
|
|
|
|
func (s Status) Err() error {
|
2020-01-14 20:35:24 +00:00
|
|
|
if s == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
parts := strings.SplitN(string(s), " ", 3)
|
|
|
|
if len(parts) != 3 {
|
|
|
|
return fmt.Errorf("webdav: invalid HTTP status %q: expected 3 fields", s)
|
|
|
|
}
|
|
|
|
code, err := strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("webdav: invalid HTTP status %q: failed to parse code: %v", s, err)
|
|
|
|
}
|
|
|
|
msg := parts[2]
|
|
|
|
|
|
|
|
// TODO: handle 2xx, 3xx
|
|
|
|
if code != http.StatusOK {
|
|
|
|
return fmt.Errorf("webdav: HTTP error: %v %v", code, msg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.16
|
2020-01-14 20:29:54 +00:00
|
|
|
type Multistatus struct {
|
2020-01-14 19:27:08 +00:00
|
|
|
XMLName xml.Name `xml:"DAV: multistatus"`
|
2020-01-14 20:32:43 +00:00
|
|
|
Responses []Response `xml:"response"`
|
|
|
|
ResponseDescription string `xml:"responsedescription,omitempty"`
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
func (ms *Multistatus) Get(href string) (*Response, error) {
|
|
|
|
for i := range ms.Responses {
|
|
|
|
resp := &ms.Responses[i]
|
2020-01-14 22:13:23 +00:00
|
|
|
for _, h := range resp.Hrefs {
|
2020-01-14 20:29:54 +00:00
|
|
|
if h == href {
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("webdav: missing response for href %q", href)
|
|
|
|
}
|
|
|
|
|
2020-01-14 17:51:17 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.24
|
|
|
|
type Response struct {
|
2020-01-14 20:29:54 +00:00
|
|
|
XMLName xml.Name `xml:"DAV: response"`
|
2020-01-14 22:13:23 +00:00
|
|
|
Hrefs []string `xml:"href"`
|
2020-01-14 20:32:43 +00:00
|
|
|
Propstats []Propstat `xml:"propstat,omitempty"`
|
|
|
|
ResponseDescription string `xml:"responsedescription,omitempty"`
|
|
|
|
Status Status `xml:"status,omitempty"`
|
|
|
|
Error *RawXMLValue `xml:"error,omitempty"`
|
|
|
|
Location *Location `xml:"location,omitempty"`
|
2020-01-14 20:29:54 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 22:13:23 +00:00
|
|
|
func (resp *Response) Href() (string, error) {
|
|
|
|
if err := resp.Status.Err(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(resp.Hrefs) != 1 {
|
|
|
|
return "", fmt.Errorf("webdav: malformed response: expected exactly one href element, got %v", len(resp.Hrefs))
|
|
|
|
}
|
|
|
|
return resp.Hrefs[0], nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 20:29:54 +00:00
|
|
|
func (resp *Response) DecodeProp(name xml.Name, v interface{}) error {
|
2020-01-14 20:35:24 +00:00
|
|
|
if err := resp.Status.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-14 20:29:54 +00:00
|
|
|
for i := range resp.Propstats {
|
|
|
|
propstat := &resp.Propstats[i]
|
|
|
|
for j := range propstat.Prop.Raw {
|
|
|
|
raw := &propstat.Prop.Raw[j]
|
2020-01-14 22:13:23 +00:00
|
|
|
if start, ok := raw.tok.(xml.StartElement); ok && name == start.Name {
|
|
|
|
if err := propstat.Status.Err(); err != nil {
|
|
|
|
return err
|
2020-01-14 20:29:54 +00:00
|
|
|
}
|
2020-01-14 22:13:23 +00:00
|
|
|
return raw.Decode(v)
|
2020-01-14 20:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("webdav: missing prop %v %v in response", name.Space, name.Local)
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.9
|
|
|
|
type Location struct {
|
|
|
|
XMLName xml.Name `xml:"DAV: location"`
|
2020-01-14 20:32:43 +00:00
|
|
|
Href string `xml:"href"`
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.22
|
|
|
|
type Propstat struct {
|
2020-01-14 20:29:54 +00:00
|
|
|
XMLName xml.Name `xml:"DAV: propstat"`
|
2020-01-14 20:32:43 +00:00
|
|
|
Prop Prop `xml:"prop"`
|
|
|
|
Status Status `xml:"status"`
|
|
|
|
ResponseDescription string `xml:"responsedescription,omitempty"`
|
|
|
|
Error *RawXMLValue `xml:"error,omitempty"`
|
2020-01-14 20:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.18
|
|
|
|
type Prop struct {
|
|
|
|
XMLName xml.Name `xml:"DAV: prop"`
|
|
|
|
Raw []RawXMLValue `xml:",any"`
|
2020-01-14 17:51:17 +00:00
|
|
|
}
|
2020-01-14 19:00:54 +00:00
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc4918#section-14.20
|
|
|
|
type Propfind struct {
|
2020-01-14 20:29:54 +00:00
|
|
|
XMLName xml.Name `xml:"DAV: propfind"`
|
2020-01-14 20:32:43 +00:00
|
|
|
Prop *Prop `xml:"prop,omitempty"`
|
2020-01-14 19:00:54 +00:00
|
|
|
// TODO: propname | (allprop, include?)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPropPropfind(names ...xml.Name) *Propfind {
|
|
|
|
children := make([]RawXMLValue, len(names))
|
|
|
|
for i, name := range names {
|
|
|
|
children[i] = *NewRawXMLElement(name, nil, nil)
|
|
|
|
}
|
2020-01-14 20:29:54 +00:00
|
|
|
return &Propfind{Prop: &Prop{Raw: children}}
|
2020-01-14 19:00:54 +00:00
|
|
|
}
|
2020-01-14 22:13:23 +00:00
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc4918#section-15.9
|
|
|
|
type ResourceType struct {
|
|
|
|
XMLName xml.Name `xml:"DAV: resourcetype"`
|
|
|
|
Raw []RawXMLValue `xml:",any"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ResourceType) Is(name xml.Name) bool {
|
|
|
|
for _, raw := range t.Raw {
|
|
|
|
if start, ok := raw.tok.(xml.StartElement); ok && name == start.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var CollectionName = xml.Name{"DAV:", "collection"}
|