Skip to content

Commit

Permalink
all: encode hrefs, replace hrefs with path in public API
Browse files Browse the repository at this point in the history
Closes: #14
Closes: #16
  • Loading branch information
emersion committed Jan 22, 2020
1 parent 72c96af commit 6eeeccb
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 92 deletions.
6 changes: 3 additions & 3 deletions carddav/carddav.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type AddressBook struct {
Href string
Path string
Name string
Description string
MaxResourceSize int64
Expand All @@ -19,11 +19,11 @@ type AddressBookQuery struct {
}

type AddressBookMultiGet struct {
Hrefs []string
Paths []string
Props []string
}

type AddressObject struct {
Href string
Path string
Card vcard.Card
}
24 changes: 14 additions & 10 deletions carddav/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
return "", err
}

return prop.Href, nil
return prop.Href.Path, nil
}

func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
Expand All @@ -98,7 +98,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err

l := make([]AddressBook, 0, len(ms.Responses))
for _, resp := range ms.Responses {
href, err := resp.Href()
path, err := resp.Path()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
}

l = append(l, AddressBook{
Href: href,
Path: path,
Name: dispName.Name,
Description: desc.Description,
MaxResourceSize: maxResSize.Size,
Expand All @@ -143,7 +143,7 @@ func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, err
func decodeAddressList(ms *internal.Multistatus) ([]AddressObject, error) {
addrs := make([]AddressObject, 0, len(ms.Responses))
for _, resp := range ms.Responses {
href, err := resp.Href()
path, err := resp.Path()
if err != nil {
return nil, err
}
Expand All @@ -160,7 +160,7 @@ func decodeAddressList(ms *internal.Multistatus) ([]AddressObject, error) {
}

addrs = append(addrs, AddressObject{
Href: href,
Path: path,
Card: card,
})
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) (
return decodeAddressList(ms)
}

func (c *Client) MultiGetAddressBook(href string, multiGet *AddressBookMultiGet) ([]AddressObject, error) {
func (c *Client) MultiGetAddressBook(path string, multiGet *AddressBookMultiGet) ([]AddressObject, error) {
var addrDataReq addressDataReq
if multiGet != nil {
for _, name := range multiGet.Props {
Expand All @@ -213,13 +213,17 @@ func (c *Client) MultiGetAddressBook(href string, multiGet *AddressBookMultiGet)

addressbookMultiget := addressbookMultiget{Prop: propReq}

if multiGet == nil || len(multiGet.Hrefs) == 0 {
addressbookMultiget.Hrefs = []string{href}
if multiGet == nil || len(multiGet.Paths) == 0 {
href := internal.Href{Path: path}
addressbookMultiget.Hrefs = []internal.Href{href}
} else {
addressbookMultiget.Hrefs = multiGet.Hrefs
addressbookMultiget.Hrefs = make([]internal.Href, len(multiGet.Paths))
for i, p := range multiGet.Paths {
addressbookMultiget.Hrefs[i] = internal.Href{Path: p}
}
}

req, err := c.ic.NewXMLRequest("REPORT", href, &addressbookMultiget)
req, err := c.ic.NewXMLRequest("REPORT", path, &addressbookMultiget)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions carddav/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (

// https://tools.ietf.org/html/rfc6352#section-6.2.3
type addressbookHomeSet struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
Href string `xml:"href"`
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-home-set"`
Href internal.Href `xml:"href"`
}

type addressbookDescription struct {
Expand Down Expand Up @@ -62,9 +62,9 @@ type addressbookQuery struct {

// https://tools.ietf.org/html/rfc6352#section-8.7
type addressbookMultiget struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-multiget"`
Hrefs []string `xml:"DAV: href"`
Prop *internal.Prop `xml:"DAV: prop,omitempty"`
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:carddav addressbook-multiget"`
Hrefs []internal.Href `xml:"DAV: href"`
Prop *internal.Prop `xml:"DAV: prop,omitempty"`
// TODO: DAV:allprop | DAV:propname
}

Expand Down
8 changes: 4 additions & 4 deletions carddav/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (h *Handler) handleQuery(w http.ResponseWriter, query *addressbookQuery) er
func (h *Handler) handleMultiget(w http.ResponseWriter, multiget *addressbookMultiget) error {
var resps []internal.Response
for _, href := range multiget.Hrefs {
ao, err := h.Backend.GetAddressObject(href)
ao, err := h.Backend.GetAddressObject(href.Path)
if err != nil {
return err // TODO: create internal.Response with error
}
Expand Down Expand Up @@ -225,11 +225,11 @@ func (b *backend) propfindAddressBook(propfind *internal.Propfind, ab *AddressBo
},
// TODO: this is a principal property
addressBookHomeSetName: func(*internal.RawXMLValue) (interface{}, error) {
return &addressbookHomeSet{Href: "/"}, nil
return &addressbookHomeSet{Href: internal.Href{Path: "/"}}, nil
},
// TODO: this should be set on all resources
internal.CurrentUserPrincipalName: func(*internal.RawXMLValue) (interface{}, error) {
return &internal.CurrentUserPrincipal{Href: "/"}, nil
return &internal.CurrentUserPrincipal{Href: internal.Href{Path: "/"}}, nil
},
}

Expand Down Expand Up @@ -258,7 +258,7 @@ func (b *backend) propfindAddressObject(propfind *internal.Propfind, ao *Address
// TODO: getlastmodified, getetag
}

return internal.NewPropfindResponse(ao.Href, propfind, props)
return internal.NewPropfindResponse(ao.Path, propfind, props)
}

func (b *backend) Proppatch(r *http.Request, update *internal.Propertyupdate) (*internal.Response, error) {
Expand Down
20 changes: 5 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
return "", fmt.Errorf("webdav: unauthenticated")
}

return prop.Href, nil
return prop.Href.Path, nil
}

func fileInfoFromResponse(resp *internal.Response) (*FileInfo, error) {
href, err := resp.Href()
path, err := resp.Path()
if err != nil {
return nil, err
}

fi := &FileInfo{Href: href}
fi := &FileInfo{Path: path}

var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
Expand Down Expand Up @@ -193,12 +193,7 @@ func (c *Client) CopyAll(name, dest string, overwrite bool) error {
return err
}

dest, err = c.ic.ResolveHref(dest)
if err != nil {
return err
}
req.Header.Set("Destination", dest)

req.Header.Set("Destination", c.ic.ResolveHref(dest).String())
req.Header.Set("Overwrite", internal.FormatOverwrite(overwrite))

_, err = c.ic.Do(req)
Expand All @@ -211,12 +206,7 @@ func (c *Client) MoveAll(name, dest string, overwrite bool) error {
return err
}

dest, err = c.ic.ResolveHref(dest)
if err != nil {
return err
}
req.Header.Set("Destination", dest)

req.Header.Set("Destination", c.ic.ResolveHref(dest).String())
req.Header.Set("Overwrite", internal.FormatOverwrite(overwrite))

_, err = c.ic.Do(req)
Expand Down
29 changes: 14 additions & 15 deletions fs_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type LocalFileSystem string

func (fs LocalFileSystem) path(name string) (string, error) {
func (fs LocalFileSystem) localPath(name string) (string, error) {
if (filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0) || strings.Contains(name, "\x00") {
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: invalid character in path")
}
Expand All @@ -25,36 +25,35 @@ func (fs LocalFileSystem) path(name string) (string, error) {
return filepath.Join(string(fs), filepath.FromSlash(name)), nil
}

func (fs LocalFileSystem) href(path string) (string, error) {
rel, err := filepath.Rel(string(fs), path)
func (fs LocalFileSystem) externalPath(name string) (string, error) {
rel, err := filepath.Rel(string(fs), name)
if err != nil {
return "", err
}
href := "/" + filepath.ToSlash(rel)
return href, nil
return "/" + filepath.ToSlash(rel), nil
}

func (fs LocalFileSystem) Open(name string) (io.ReadCloser, error) {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
return os.Open(p)
}

func fileInfoFromOS(href string, fi os.FileInfo) *FileInfo {
func fileInfoFromOS(p string, fi os.FileInfo) *FileInfo {
return &FileInfo{
Href: href,
Path: p,
Size: fi.Size(),
ModTime: fi.ModTime(),
IsDir: fi.IsDir(),
// TODO: fallback to http.DetectContentType?
MIMEType: mime.TypeByExtension(path.Ext(href)),
MIMEType: mime.TypeByExtension(path.Ext(p)),
}
}

func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
Expand All @@ -66,7 +65,7 @@ func (fs LocalFileSystem) Stat(name string) (*FileInfo, error) {
}

func (fs LocalFileSystem) Readdir(name string, recursive bool) ([]FileInfo, error) {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
Expand All @@ -77,7 +76,7 @@ func (fs LocalFileSystem) Readdir(name string, recursive bool) ([]FileInfo, erro
return err
}

href, err := fs.href(p)
href, err := fs.externalPath(p)
if err != nil {
return err
}
Expand All @@ -93,15 +92,15 @@ func (fs LocalFileSystem) Readdir(name string, recursive bool) ([]FileInfo, erro
}

func (fs LocalFileSystem) Create(name string) (io.WriteCloser, error) {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
return os.Create(p)
}

func (fs LocalFileSystem) RemoveAll(name string) error {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return err
}
Expand All @@ -116,7 +115,7 @@ func (fs LocalFileSystem) RemoveAll(name string) error {
}

func (fs LocalFileSystem) Mkdir(name string) error {
p, err := fs.path(name)
p, err := fs.localPath(name)
if err != nil {
return err
}
Expand Down
41 changes: 15 additions & 26 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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,
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),
}
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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Loading

0 comments on commit 6eeeccb

Please sign in to comment.