2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
2018-06-18 17:02:01 +02:00
|
|
|
"bytes"
|
2014-10-23 10:39:55 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2018-07-14 01:48:30 +02:00
|
|
|
"path"
|
2018-07-14 01:55:58 +02:00
|
|
|
"strings"
|
2014-10-23 10:39:55 +02:00
|
|
|
)
|
|
|
|
|
2017-10-05 16:22:10 +02:00
|
|
|
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
|
2018-06-18 17:02:01 +02:00
|
|
|
// Tee the body, because if authorization fails we will need to read from it again.
|
2018-07-08 14:25:00 +02:00
|
|
|
var r *http.Request
|
2018-06-18 17:02:01 +02:00
|
|
|
var ba bytes.Buffer
|
|
|
|
bb := io.TeeReader(body, &ba)
|
|
|
|
|
2018-07-08 14:25:00 +02:00
|
|
|
if body == nil {
|
|
|
|
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
|
|
|
|
} else {
|
|
|
|
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), bb)
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-07 15:37:48 +02:00
|
|
|
|
2018-06-10 11:01:51 +02:00
|
|
|
c.auth.Authorize(c, method, path)
|
2018-06-18 17:02:01 +02:00
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
for k, vals := range c.headers {
|
|
|
|
for _, v := range vals {
|
2014-10-27 16:19:17 +01:00
|
|
|
r.Header.Add(k, v)
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 16:19:17 +01:00
|
|
|
if intercept != nil {
|
|
|
|
intercept(r)
|
2014-10-23 14:10:31 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 08:43:13 +02:00
|
|
|
rs, err := c.c.Do(r)
|
2018-09-04 22:48:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-18 17:02:01 +02:00
|
|
|
|
|
|
|
if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
|
2019-10-05 21:13:01 +02:00
|
|
|
|
|
|
|
wwwAuthenticateHeader := strings.ToLower(rs.Header.Get("Www-Authenticate"))
|
|
|
|
|
|
|
|
if strings.Index(wwwAuthenticateHeader, "digest") > -1 {
|
2018-06-18 17:02:01 +02:00
|
|
|
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
|
2019-10-05 21:13:01 +02:00
|
|
|
|
|
|
|
} else if strings.Index(wwwAuthenticateHeader, "basic") > -1 {
|
2018-06-18 17:02:01 +02:00
|
|
|
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
|
2019-10-05 21:13:01 +02:00
|
|
|
|
2018-06-18 17:02:01 +02:00
|
|
|
} else {
|
|
|
|
return rs, newPathError("Authorize", c.root, rs.StatusCode)
|
|
|
|
}
|
2018-07-08 14:25:00 +02:00
|
|
|
|
|
|
|
if body == nil {
|
|
|
|
return c.req(method, path, nil, intercept)
|
|
|
|
} else {
|
|
|
|
return c.req(method, path, &ba, intercept)
|
|
|
|
}
|
|
|
|
|
2018-06-18 17:02:01 +02:00
|
|
|
} else if rs.StatusCode == 401 {
|
|
|
|
return rs, newPathError("Authorize", c.root, rs.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs, err
|
2014-10-23 14:10:31 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 15:31:34 +02:00
|
|
|
func (c *Client) mkcol(path string) int {
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req("MKCOL", path, nil, nil)
|
2014-10-23 15:00:20 +02:00
|
|
|
if err != nil {
|
|
|
|
return 400
|
|
|
|
}
|
2018-11-08 09:39:42 +01:00
|
|
|
defer rs.Body.Close()
|
2014-10-23 15:00:20 +02:00
|
|
|
|
|
|
|
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-27 16:19:17 +01:00
|
|
|
return c.req("OPTIONS", path, nil, func(rq *http.Request) {
|
|
|
|
rq.Header.Add("Depth", "0")
|
|
|
|
})
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
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-27 16:19:17 +01:00
|
|
|
rs, err := c.req("PROPFIND", path, strings.NewReader(body), func(rq *http.Request) {
|
|
|
|
if self {
|
|
|
|
rq.Header.Add("Depth", "0")
|
|
|
|
} else {
|
|
|
|
rq.Header.Add("Depth", "1")
|
|
|
|
}
|
2018-12-29 21:04:04 +01:00
|
|
|
rq.Header.Add("Content-Type", "application/xml;charset=UTF-8")
|
2014-10-27 16:19:17 +01:00
|
|
|
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", "")
|
|
|
|
})
|
2014-10-23 10:39:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-08 09:39:42 +01:00
|
|
|
defer rs.Body.Close()
|
2014-10-23 10:39:55 +02:00
|
|
|
|
|
|
|
if rs.StatusCode != 207 {
|
2017-10-05 16:22:10 +02:00
|
|
|
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-10-27 16:27:57 +01:00
|
|
|
func (c *Client) doCopyMove(method string, oldpath string, newpath string, overwrite bool) (int, io.ReadCloser) {
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
|
|
|
|
rq.Header.Add("Destination", Join(c.root, newpath))
|
|
|
|
if overwrite {
|
|
|
|
rq.Header.Add("Overwrite", "T")
|
|
|
|
} else {
|
|
|
|
rq.Header.Add("Overwrite", "F")
|
|
|
|
}
|
|
|
|
})
|
2014-10-24 14:08:42 +02:00
|
|
|
if err != nil {
|
2014-10-27 16:27:57 +01:00
|
|
|
return 400, nil
|
|
|
|
}
|
|
|
|
return rs.StatusCode, rs.Body
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
|
|
|
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
2017-10-05 16:22:10 +02:00
|
|
|
defer data.Close()
|
2014-10-24 14:08:42 +02:00
|
|
|
|
2014-10-27 16:27:57 +01:00
|
|
|
switch s {
|
2014-10-24 14:08:42 +02:00
|
|
|
case 201, 204:
|
|
|
|
return nil
|
2014-10-24 14:09:04 +02:00
|
|
|
|
|
|
|
case 207:
|
|
|
|
// TODO handle multistat errors, worst case ...
|
2014-10-27 16:27:57 +01:00
|
|
|
log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data)))
|
2014-10-24 14:09:04 +02:00
|
|
|
|
|
|
|
case 409:
|
2018-07-14 01:48:30 +02:00
|
|
|
err := c.createParentCollection(newpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.copymove(method, oldpath, newpath, overwrite)
|
2014-10-24 14:08:42 +02:00
|
|
|
}
|
|
|
|
|
2014-10-27 16:27:57 +01:00
|
|
|
return newPathError(method, oldpath, s)
|
2014-10-24 14:08:42 +02:00
|
|
|
}
|
2014-10-27 14:32:16 +01:00
|
|
|
|
|
|
|
func (c *Client) put(path string, stream io.Reader) int {
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req("PUT", path, stream, nil)
|
2014-10-27 14:32:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return 400
|
|
|
|
}
|
2018-11-08 09:39:42 +01:00
|
|
|
defer rs.Body.Close()
|
2017-10-05 16:22:10 +02:00
|
|
|
|
2014-10-27 14:32:16 +01:00
|
|
|
return rs.StatusCode
|
|
|
|
}
|
2018-07-14 01:48:30 +02:00
|
|
|
|
|
|
|
func (c *Client) createParentCollection(itemPath string) (err error) {
|
|
|
|
parentPath := path.Dir(itemPath)
|
|
|
|
return c.MkdirAll(parentPath, 0755)
|
|
|
|
}
|