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