Generate PROPFIND request body

Instead of a hardcoded string, generate it with encoding/xml.
This commit is contained in:
Simon Ser 2020-01-14 20:00:54 +01:00
parent 6f9ff62747
commit 87a88d6723
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
4 changed files with 47 additions and 10 deletions

View File

@ -1,9 +1,9 @@
package webdav
import (
"encoding/xml"
"fmt"
"net/http"
"strings"
"github.com/emersion/go-webdav/internal"
)
@ -21,21 +21,15 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
}
func (c *Client) FindCurrentUserPrincipal() (string, error) {
r := strings.NewReader(`<?xml version="1.0" encoding="utf-8"?>
<D:propfind xmlns:D="DAV:">
<D:prop>
<D:current-user-principal/>
</D:prop>
</D:propfind>
`)
name := xml.Name{"DAV:", "current-user-principal"}
propfind := internal.NewPropPropfind(name)
req, err := c.c.NewRequest("PROPFIND", "", r)
req, err := c.c.NewXMLRequest("PROPFIND", "", propfind)
if err != nil {
return "", err
}
req.Header.Add("Depth", "0")
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
resps, err := c.c.DoMultiStatus(req)
if err != nil {

View File

@ -1,6 +1,7 @@
package internal
import (
"bytes"
"encoding/xml"
"fmt"
"io"
@ -36,6 +37,27 @@ func (c *Client) NewRequest(method string, p string, body io.Reader) (*http.Requ
return http.NewRequest(method, u.String(), body)
}
func (c *Client) NewXMLRequest(method string, p string, v interface{}) (*http.Request, error) {
var buf bytes.Buffer
buf.WriteString(xml.Header)
enc := xml.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
if err := enc.Flush(); err != nil {
return nil, err
}
req, err := c.NewRequest(method, p, &buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
return req, nil
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
// TODO: remove this quirk
req.SetBasicAuth("simon", "")

View File

@ -27,3 +27,19 @@ type Propstat struct {
Status string `xml:"DAV: status"`
// TODO: error?, responsedescription?
}
// https://tools.ietf.org/html/rfc4918#section-14.20
type Propfind struct {
XMLName xml.Name `xml:"DAV: propfind"`
Prop *RawXMLValue `xml:"DAV: prop,omitempty"`
// 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)
}
prop := NewRawXMLElement(xml.Name{"DAV:", "prop"}, nil, children)
return &Propfind{Prop: prop}
}

View File

@ -13,6 +13,11 @@ type RawXMLValue struct {
children []RawXMLValue
}
// NewRawXMLElement creates a new RawXMLValue for an element.
func NewRawXMLElement(name xml.Name, attr []xml.Attr, children []RawXMLValue) *RawXMLValue {
return &RawXMLValue{tok: xml.StartElement{name, attr}, children: children}
}
// UnmarshalXML implements xml.Unmarshaler.
func (val *RawXMLValue) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
val.tok = start