all: encode hrefs, replace hrefs with path in public API
Closes: https://github.com/emersion/go-webdav/issues/14 Closes: https://github.com/emersion/go-webdav/issues/16
This commit is contained in:
@@ -33,38 +33,27 @@ func (c *Client) SetBasicAuth(username, password string) {
|
||||
c.password = password
|
||||
}
|
||||
|
||||
func (c *Client) ResolveHref(href string) (string, error) {
|
||||
hrefURL, err := url.Parse(href)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("webdav: failed to parse href %q: %v", href, err)
|
||||
func (c *Client) ResolveHref(p string) *url.URL {
|
||||
return &url.URL{
|
||||
Scheme: c.endpoint.Scheme,
|
||||
User: c.endpoint.User,
|
||||
Host: c.endpoint.Host,
|
||||
Path: path.Join(c.endpoint.Path, p),
|
||||
}
|
||||
|
||||
u := url.URL{
|
||||
Scheme: c.endpoint.Scheme,
|
||||
User: c.endpoint.User,
|
||||
Host: c.endpoint.Host,
|
||||
Path: path.Join(c.endpoint.Path, hrefURL.Path),
|
||||
RawQuery: hrefURL.RawQuery,
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (c *Client) NewRequest(method string, href string, body io.Reader) (*http.Request, error) {
|
||||
href, err := c.ResolveHref(href)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return http.NewRequest(method, href, body)
|
||||
func (c *Client) NewRequest(method string, path string, body io.Reader) (*http.Request, error) {
|
||||
return http.NewRequest(method, c.ResolveHref(path).String(), body)
|
||||
}
|
||||
|
||||
func (c *Client) NewXMLRequest(method string, href string, v interface{}) (*http.Request, error) {
|
||||
func (c *Client) NewXMLRequest(method string, path string, v interface{}) (*http.Request, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(xml.Header)
|
||||
if err := xml.NewEncoder(&buf).Encode(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := c.NewRequest(method, href, &buf)
|
||||
req, err := c.NewRequest(method, path, &buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -101,8 +90,8 @@ func (c *Client) DoMultiStatus(req *http.Request) (*Multistatus, error) {
|
||||
return &ms, nil
|
||||
}
|
||||
|
||||
func (c *Client) Propfind(href string, depth Depth, propfind *Propfind) (*Multistatus, error) {
|
||||
req, err := c.NewXMLRequest("PROPFIND", href, propfind)
|
||||
func (c *Client) Propfind(path string, depth Depth, propfind *Propfind) (*Multistatus, error) {
|
||||
req, err := c.NewXMLRequest("PROPFIND", path, propfind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -113,11 +102,11 @@ func (c *Client) Propfind(href string, depth Depth, propfind *Propfind) (*Multis
|
||||
}
|
||||
|
||||
// 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)
|
||||
func (c *Client) PropfindFlat(path string, propfind *Propfind) (*Response, error) {
|
||||
ms, err := c.Propfind(path, DepthZero, propfind)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ms.Get(href)
|
||||
return ms.Get(path)
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -66,6 +67,26 @@ func (s *Status) Err() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Href url.URL
|
||||
|
||||
func (h *Href) String() string {
|
||||
u := (*url.URL)(h)
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func (h *Href) MarshalText() ([]byte, error) {
|
||||
return []byte(h.String()), nil
|
||||
}
|
||||
|
||||
func (h *Href) UnmarshalText(b []byte) error {
|
||||
u, err := url.Parse(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*h = Href(*u)
|
||||
return nil
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc4918#section-14.16
|
||||
type Multistatus struct {
|
||||
XMLName xml.Name `xml:"DAV: multistatus"`
|
||||
@@ -77,23 +98,23 @@ func NewMultistatus(resps ...Response) *Multistatus {
|
||||
return &Multistatus{Responses: resps}
|
||||
}
|
||||
|
||||
func (ms *Multistatus) Get(href string) (*Response, error) {
|
||||
func (ms *Multistatus) Get(path string) (*Response, error) {
|
||||
for i := range ms.Responses {
|
||||
resp := &ms.Responses[i]
|
||||
for _, h := range resp.Hrefs {
|
||||
if h == href {
|
||||
if h.Path == path {
|
||||
return resp, resp.Status.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("webdav: missing response for href %q", href)
|
||||
return nil, fmt.Errorf("webdav: missing response for path %q", path)
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc4918#section-14.24
|
||||
type Response struct {
|
||||
XMLName xml.Name `xml:"DAV: response"`
|
||||
Hrefs []string `xml:"href"`
|
||||
Hrefs []Href `xml:"href"`
|
||||
Propstats []Propstat `xml:"propstat,omitempty"`
|
||||
ResponseDescription string `xml:"responsedescription,omitempty"`
|
||||
Status *Status `xml:"status,omitempty"`
|
||||
@@ -101,21 +122,22 @@ type Response struct {
|
||||
Location *Location `xml:"location,omitempty"`
|
||||
}
|
||||
|
||||
func NewOKResponse(href string) *Response {
|
||||
func NewOKResponse(path string) *Response {
|
||||
href := Href{Path: path}
|
||||
return &Response{
|
||||
Hrefs: []string{href},
|
||||
Hrefs: []Href{href},
|
||||
Status: &Status{Code: http.StatusOK},
|
||||
}
|
||||
}
|
||||
|
||||
func (resp *Response) Href() (string, error) {
|
||||
func (resp *Response) Path() (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
|
||||
return resp.Hrefs[0].Path, nil
|
||||
}
|
||||
|
||||
type missingPropError struct {
|
||||
@@ -181,7 +203,7 @@ func (resp *Response) EncodeProp(code int, v interface{}) error {
|
||||
// https://tools.ietf.org/html/rfc4918#section-14.9
|
||||
type Location struct {
|
||||
XMLName xml.Name `xml:"DAV: location"`
|
||||
Href string `xml:"href"`
|
||||
Href Href `xml:"href"`
|
||||
}
|
||||
|
||||
// https://tools.ietf.org/html/rfc4918#section-14.22
|
||||
@@ -332,7 +354,7 @@ type DisplayName struct {
|
||||
// https://tools.ietf.org/html/rfc5397#section-3
|
||||
type CurrentUserPrincipal struct {
|
||||
XMLName xml.Name `xml:"DAV: current-user-principal"`
|
||||
Href string `xml:"href,omitempty"`
|
||||
Href Href `xml:"href,omitempty"`
|
||||
Unauthenticated *struct{} `xml:"unauthenticated,omitempty"`
|
||||
}
|
||||
|
||||
|
@@ -171,8 +171,8 @@ func (h *Handler) handlePropfind(w http.ResponseWriter, r *http.Request) error {
|
||||
|
||||
type PropfindFunc func(raw *RawXMLValue) (interface{}, error)
|
||||
|
||||
func NewPropfindResponse(href string, propfind *Propfind, props map[xml.Name]PropfindFunc) (*Response, error) {
|
||||
resp := NewOKResponse(href)
|
||||
func NewPropfindResponse(path string, propfind *Propfind, props map[xml.Name]PropfindFunc) (*Response, error) {
|
||||
resp := NewOKResponse(path)
|
||||
|
||||
if _, ok := props[ResourceTypeName]; !ok {
|
||||
props[ResourceTypeName] = func(*RawXMLValue) (interface{}, error) {
|
||||
|
Reference in New Issue
Block a user