even more debug

This commit is contained in:
shoopea 2024-01-20 11:59:18 +01:00
parent 331e1f8e16
commit e3bd2a5fb2

View File

@ -5,6 +5,7 @@ import (
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@ -26,11 +27,13 @@ type Client struct {
// NewClient creates a new instance of client
func NewClient(uri, user, pw string) *Client {
log.Printf("NewClient(%s, %s, %s)", uri, user, pw)
return NewAuthClient(uri, NewAutoAuth(user, pw))
}
// NewAuthClient creates a new client instance with a custom Authorizer
func NewAuthClient(uri string, auth Authorizer) *Client {
log.Printf("NewAuthClient(%s)", uri)
c := &http.Client{
CheckRedirect: func(rq *http.Request, via []*http.Request) error {
if len(via) >= 10 {
@ -47,31 +50,37 @@ func NewAuthClient(uri string, auth Authorizer) *Client {
// SetHeader lets us set arbitrary headers for a given client
func (c *Client) SetHeader(key, value string) {
log.Printf("Client.SetHeader(%s, %s)", key, value)
c.headers.Add(key, value)
}
// SetInterceptor lets us set an arbitrary interceptor for a given client
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request)) {
log.Printf("Client.SetInterceptor()")
c.interceptor = interceptor
}
// SetTimeout exposes the ability to set a time limit for requests
func (c *Client) SetTimeout(timeout time.Duration) {
log.Printf("Client.SetTimeout()")
c.c.Timeout = timeout
}
// SetTransport exposes the ability to define custom transports
func (c *Client) SetTransport(transport http.RoundTripper) {
log.Printf("Client.SetTransport()")
c.c.Transport = transport
}
// SetJar exposes the ability to set a cookie jar to the client.
func (c *Client) SetJar(jar http.CookieJar) {
log.Printf("Client.SetJar()")
c.c.Jar = jar
}
// Connect connects to our dav server
func (c *Client) Connect() error {
log.Printf("Client.Connect()")
rs, err := c.options("/")
if err != nil {
return err
@ -115,6 +124,7 @@ func getProps(r *response, status string) *props {
// ReadDir reads the contents of a remote directory
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
log.Printf("Client.ReadDir(%s)", path)
path = FixSlashes(path)
files := make([]os.FileInfo, 0)
skipSelf := true
@ -182,6 +192,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
// Stat returns the file stats for a specified path
func (c *Client) Stat(path string) (os.FileInfo, error) {
log.Printf("Client.Stat(%s)", path)
var f *File
parse := func(resp interface{}) error {
r := resp.(*response)
@ -234,11 +245,13 @@ func (c *Client) Stat(path string) (os.FileInfo, error) {
// Remove removes a remote file
func (c *Client) Remove(path string) error {
log.Printf("Client.Remove(%s)", path)
return c.RemoveAll(path)
}
// RemoveAll removes remote files
func (c *Client) RemoveAll(path string) error {
log.Printf("Client.RemoveAll(%s)", path)
rs, err := c.req("DELETE", path, nil, nil)
if err != nil {
return NewPathError("Remove", path, 400)
@ -257,6 +270,7 @@ func (c *Client) RemoveAll(path string) error {
// Mkdir makes a directory
func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
log.Printf("Client.Mkdir(%s)", path)
path = FixSlashes(path)
status, err := c.mkcol(path)
if err != nil {
@ -271,6 +285,7 @@ func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
// MkdirAll like mkdir -p, but for webdav
func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
log.Printf("Client.MkdirAll(%s)", path)
path = FixSlashes(path)
status, err := c.mkcol(path)
if err != nil {
@ -303,16 +318,19 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
// Rename moves a file from A to B
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error {
log.Printf("Client.Rename(%s, %s)", oldpath, newpath)
return c.copymove("MOVE", oldpath, newpath, overwrite)
}
// Copy copies a file from A to B
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error {
log.Printf("Client.Copy(%s, %s)", oldpath, newpath)
return c.copymove("COPY", oldpath, newpath, overwrite)
}
// Read reads the contents of a remote file
func (c *Client) Read(path string) ([]byte, error) {
log.Printf("Client.Read(%s)", path)
var stream io.ReadCloser
var err error
@ -331,6 +349,7 @@ func (c *Client) Read(path string) ([]byte, error) {
// ReadStream reads the stream for a given path
func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
log.Printf("Client.ReadStream(%s)", path)
rs, err := c.req("GET", path, nil, nil)
if err != nil {
return nil, NewPathErrorErr("ReadStream", path, err)
@ -353,6 +372,7 @@ func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
// this function will emulate the behavior by skipping `offset` bytes and limiting the result
// to `length`.
func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error) {
log.Printf("Client.ReadStreamRange(%s)", path)
rs, err := c.req("GET", path, nil, func(r *http.Request) {
if length > 0 {
r.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
@ -387,6 +407,7 @@ func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadClos
// Write writes data to a given path
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
log.Printf("Client.Write(%s)", path)
s, err := c.put(path, bytes.NewReader(data))
if err != nil {
return
@ -417,6 +438,7 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
// WriteStream writes a stream
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) {
log.Printf("Client.WriteStream(%s)", path)
err = c.createParentCollection(path)
if err != nil {