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:00:20 +02:00
|
|
|
func (c *Client) MkCol(path string) int {
|
|
|
|
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 10:39:55 +02:00
|
|
|
func (c *Client) Options(path string) (*http.Response, error) {
|
|
|
|
rq, err := c.req("OPTIONS", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rq.Header.Add("Depth", "0")
|
|
|
|
|
|
|
|
return c.c.Do(rq)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{})) error {
|
|
|
|
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-23 11:29:26 +02:00
|
|
|
parseXML(rs.Body, resp, parse)
|
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
return nil
|
|
|
|
}
|