2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2022-10-15 14:44:49 +02:00
|
|
|
"log"
|
2014-10-23 10:39:55 +02:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (rs *http.Response, err error) {
|
2024-01-20 12:06:08 +01:00
|
|
|
log.Printf("Client.req(%s, %s)", method, path)
|
2023-02-03 10:18:35 +01:00
|
|
|
var redo bool
|
2018-07-08 14:25:00 +02:00
|
|
|
var r *http.Request
|
2023-02-03 10:18:35 +01:00
|
|
|
var uri = PathEscape(Join(c.root, path))
|
|
|
|
auth, body := c.auth.NewAuthenticator(body)
|
|
|
|
defer auth.Close()
|
2018-07-08 14:25:00 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
for { // TODO auth.continue() strategy(true|n times|until)?
|
|
|
|
if r, err = http.NewRequest(method, uri, body); err != nil {
|
|
|
|
return
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
2020-08-18 04:47:58 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
for k, vals := range c.headers {
|
|
|
|
for _, v := range vals {
|
|
|
|
r.Header.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 04:47:58 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
if err = auth.Authorize(c.c, r, path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-23 14:10:31 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
if intercept != nil {
|
|
|
|
intercept(r)
|
|
|
|
}
|
2021-04-27 20:16:33 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
if c.interceptor != nil {
|
|
|
|
c.interceptor(method, r)
|
|
|
|
}
|
2018-06-18 17:02:01 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
if rs, err = c.c.Do(r); err != nil {
|
|
|
|
return
|
2018-06-18 17:02:01 +02:00
|
|
|
}
|
2018-07-08 14:25:00 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
if redo, err = auth.Verify(c.c, rs, path); err != nil {
|
|
|
|
rs.Body.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if redo {
|
|
|
|
rs.Body.Close()
|
|
|
|
if body, err = r.GetBody(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
2018-06-18 17:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rs, err
|
2014-10-23 14:10:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) mkcol(path string) (status int, err error) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.mkcol(%s)", path)
|
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 {
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2014-10-23 15:00:20 +02:00
|
|
|
}
|
2018-11-08 09:39:42 +01:00
|
|
|
defer rs.Body.Close()
|
2014-10-23 15:00:20 +02:00
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
status = rs.StatusCode
|
|
|
|
if status == 405 {
|
|
|
|
status = 201
|
2014-10-23 15:00:20 +02:00
|
|
|
}
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2014-10-23 15:00:20 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 15:31:34 +02:00
|
|
|
func (c *Client) options(path string) (*http.Response, error) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.options(%s)", path)
|
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 {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.propfind(%s)", path)
|
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
|
|
|
|
2022-10-16 00:39:39 +02:00
|
|
|
if rs.StatusCode != 207 {
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("PROPFIND", path, rs.StatusCode)
|
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
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) doCopyMove(
|
|
|
|
method string,
|
|
|
|
oldpath string,
|
|
|
|
newpath string,
|
|
|
|
overwrite bool,
|
|
|
|
) (
|
|
|
|
status int,
|
|
|
|
r io.ReadCloser,
|
|
|
|
err error,
|
|
|
|
) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.doCopyMove(%s, %s, %s)", method, oldpath, newpath)
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
|
2022-01-27 16:27:44 +01:00
|
|
|
rq.Header.Add("Destination", PathEscape(Join(c.root, newpath)))
|
2014-10-27 16:19:17 +01:00
|
|
|
if overwrite {
|
|
|
|
rq.Header.Add("Overwrite", "T")
|
|
|
|
} else {
|
|
|
|
rq.Header.Add("Overwrite", "F")
|
|
|
|
}
|
|
|
|
})
|
2014-10-24 14:08:42 +02:00
|
|
|
if err != nil {
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2014-10-27 16:27:57 +01:00
|
|
|
}
|
2022-01-28 17:20:35 +01:00
|
|
|
status = rs.StatusCode
|
|
|
|
r = rs.Body
|
|
|
|
return
|
2014-10-27 16:27:57 +01:00
|
|
|
}
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.copymove(%s, %s, %s)", method, oldpath, newpath)
|
2022-01-28 17:20:35 +01:00
|
|
|
s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 06:01:35 +02:00
|
|
|
if data != nil {
|
|
|
|
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 ...
|
2022-10-15 14:44:49 +02:00
|
|
|
log.Printf("TODO handle %s - %s multistatus result %s\n", 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
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError(method, oldpath, s)
|
2014-10-24 14:08:42 +02:00
|
|
|
}
|
2014-10-27 14:32:16 +01:00
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) put(path string, stream io.Reader) (status int, err error) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.put(%s)", path)
|
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 {
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2014-10-27 14:32:16 +01:00
|
|
|
}
|
2018-11-08 09:39:42 +01:00
|
|
|
defer rs.Body.Close()
|
2017-10-05 16:22:10 +02:00
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
status = rs.StatusCode
|
|
|
|
return
|
2014-10-27 14:32:16 +01:00
|
|
|
}
|
2018-07-14 01:48:30 +02:00
|
|
|
|
|
|
|
func (c *Client) createParentCollection(itemPath string) (err error) {
|
2024-01-20 12:05:02 +01:00
|
|
|
log.Printf("Client.createParentCollection(%s)", itemPath)
|
2018-07-14 01:48:30 +02:00
|
|
|
parentPath := path.Dir(itemPath)
|
2019-11-08 23:27:45 +01:00
|
|
|
if parentPath == "." || parentPath == "/" {
|
2019-02-09 20:21:47 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-14 01:48:30 +02:00
|
|
|
return c.MkdirAll(parentPath, 0755)
|
|
|
|
}
|