2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Client) req(method string, path string, body io.Reader) (req *http.Request, err error) {
|
|
|
|
req, err = http.NewRequest(method, Join(c.root, path), body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, vals := range c.headers {
|
|
|
|
for _, v := range vals {
|
|
|
|
req.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2014-10-23 14:10:31 +02:00
|
|
|
func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Response, error) {
|
|
|
|
rq, err := c.req(method, path, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.c.Do(rq)
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:31:34 +02:00
|
|
|
func (c *Client) mkcol(path string) int {
|
2014-10-23 15:00:20 +02:00
|
|
|
rs, err := c.reqDo("MKCOL", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 400
|
|
|
|
}
|
|
|
|
defer rs.Body.Close()
|
|
|
|
|
|
|
|
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
|
|
|
return 201
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs.StatusCode
|
|
|
|
}
|
|
|
|
|
2014-10-23 15:31:34 +02:00
|
|
|
func (c *Client) options(path string) (*http.Response, error) {
|
2014-10-23 10:39:55 +02:00
|
|
|
rq, err := c.req("OPTIONS", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rq.Header.Add("Depth", "0")
|
|
|
|
|
|
|
|
return c.c.Do(rq)
|
|
|
|
}
|
|
|
|
|
2014-10-24 12:39:35 +02:00
|
|
|
func (c *Client) propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{}) error) error {
|
2014-10-23 10:39:55 +02:00
|
|
|
rq, err := c.req("PROPFIND", path, strings.NewReader(body))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if self {
|
|
|
|
rq.Header.Add("Depth", "0")
|
|
|
|
} else {
|
|
|
|
rq.Header.Add("Depth", "1")
|
|
|
|
}
|
|
|
|
rq.Header.Add("Content-Type", "text/xml;charset=UTF-8")
|
|
|
|
rq.Header.Add("Accept", "application/xml,text/xml")
|
|
|
|
rq.Header.Add("Accept-Charset", "utf-8")
|
|
|
|
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
|
|
|
|
rq.Header.Add("Accept-Encoding", "")
|
|
|
|
|
|
|
|
rs, err := c.c.Do(rq)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rs.Body.Close()
|
|
|
|
|
|
|
|
if rs.StatusCode != 207 {
|
|
|
|
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String()))
|
|
|
|
}
|
|
|
|
|
2014-10-24 12:39:35 +02:00
|
|
|
return parseXML(rs.Body, resp, parse)
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
2014-10-24 14:08:42 +02:00
|
|
|
|
|
|
|
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
|
|
|
rq, err := c.req(method, oldpath, nil)
|
|
|
|
if err != nil {
|
|
|
|
return newPathErrorErr(method, oldpath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rq.Header.Add("Destination", Join(c.root, newpath))
|
|
|
|
if overwrite {
|
|
|
|
rq.Header.Add("Overwrite", "T")
|
|
|
|
} else {
|
|
|
|
rq.Header.Add("Overwrite", "F")
|
|
|
|
}
|
|
|
|
|
|
|
|
rs, err := c.c.Do(rq)
|
|
|
|
if err != nil {
|
|
|
|
return newPathErrorErr(method, oldpath, err)
|
|
|
|
}
|
|
|
|
defer rs.Body.Close()
|
|
|
|
|
|
|
|
switch rs.StatusCode {
|
|
|
|
case 201, 204:
|
|
|
|
return nil
|
2014-10-24 14:09:04 +02:00
|
|
|
|
|
|
|
case 207:
|
|
|
|
// TODO handle multistat errors, worst case ...
|
|
|
|
log(String(rs.Body))
|
|
|
|
|
|
|
|
case 409:
|
|
|
|
// TODO create dst path
|
2014-10-24 14:08:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newPathError(method, oldpath, rs.StatusCode)
|
|
|
|
}
|