mirror of
https://github.com/1f349/go-webdav.git
synced 2024-12-22 16:24:14 +00:00
Generate PROPFIND request body
Instead of a hardcoded string, generate it with encoding/xml.
This commit is contained in:
parent
6f9ff62747
commit
87a88d6723
14
client.go
14
client.go
@ -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 {
|
||||
|
@ -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", "")
|
||||
|
@ -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}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user