Compare commits
15 Commits
dev-bodycl
...
cmd_test
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f74925694 | ||
|
|
200a600c02 | ||
|
|
17255f2e74 | ||
|
|
937a18c9a3 | ||
|
|
d2a480ffa9 | ||
|
|
8528c01163 | ||
|
|
bf6102194f | ||
|
|
2c20e7e763 | ||
|
|
fbeb69f25b | ||
|
|
4adca27344 | ||
|
|
8190232c06 | ||
|
|
e70a598e94 | ||
|
|
c7b1ff8a5e | ||
|
|
a047320e42 | ||
|
|
b5bd04e2b5 |
5
.github/workflows/tests.yml
vendored
5
.github/workflows/tests.yml
vendored
@@ -15,8 +15,9 @@ jobs:
|
||||
strategy:
|
||||
matrix:
|
||||
goversion:
|
||||
- "1.19"
|
||||
- "1.18"
|
||||
- "1.17"
|
||||
- "1.16"
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v1
|
||||
@@ -27,4 +28,4 @@ jobs:
|
||||
- name: Get dependencies
|
||||
run: go get ./...
|
||||
- name: Run Unit Tests
|
||||
run: go test -v -cover -race ./...
|
||||
run: go test -modfile=go_test.mod -v -cover -race ./...
|
||||
|
||||
10
.travis.yml
10
.travis.yml
@@ -1,10 +0,0 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- "1.x"
|
||||
|
||||
install:
|
||||
- go get ./...
|
||||
|
||||
script:
|
||||
- go test -v --short ./...
|
||||
4
Makefile
4
Makefile
@@ -9,7 +9,7 @@ ${BIN}: ${SRC}
|
||||
go build -o $@ ./cmd/gowebdav
|
||||
|
||||
test:
|
||||
go test -v --short ./...
|
||||
go test -modfile=go_test.mod -v -short -cover ./...
|
||||
|
||||
api:
|
||||
@sed '/^## API$$/,$$d' -i README.md
|
||||
@@ -25,7 +25,7 @@ check:
|
||||
@echo
|
||||
gocyclo -over 15 .
|
||||
@echo
|
||||
golint ./...
|
||||
go vet -modfile=go_test.mod ./...
|
||||
|
||||
clean:
|
||||
@rm -f ${BIN}
|
||||
|
||||
137
README.md
137
README.md
@@ -1,12 +1,14 @@
|
||||
# GoWebDAV
|
||||
|
||||
[](https://travis-ci.org/studio-b12/gowebdav)
|
||||
[](https://github.com/studio-b12/gowebdav/actions/workflows/tests.yml)
|
||||
[](https://github.com/studio-b12/gowebdav/actions/workflows/artifacts.yml)
|
||||
[](https://godoc.org/github.com/studio-b12/gowebdav)
|
||||
[](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
|
||||
|
||||
A golang WebDAV client library.
|
||||
|
||||
## Main features
|
||||
|
||||
`gowebdav` library allows to perform following actions on the remote WebDAV server:
|
||||
* [create path](#create-path-on-a-webdav-server)
|
||||
* [get files list](#get-files-list)
|
||||
@@ -161,6 +163,8 @@ included.
|
||||
### <a name="pkg-index">Index</a>
|
||||
* [func FixSlash(s string) string](#FixSlash)
|
||||
* [func FixSlashes(s string) string](#FixSlashes)
|
||||
* [func IsErrCode(err error, code int) bool](#IsErrCode)
|
||||
* [func IsErrNotFound(err error) bool](#IsErrNotFound)
|
||||
* [func Join(path0 string, path1 string) string](#Join)
|
||||
* [func PathEscape(path string) string](#PathEscape)
|
||||
* [func ReadConfig(uri, netrc string) (string, string)](#ReadConfig)
|
||||
@@ -175,11 +179,12 @@ included.
|
||||
* [func NewClient(uri, user, pw string) *Client](#NewClient)
|
||||
* [func (c *Client) Connect() error](#Client.Connect)
|
||||
* [func (c *Client) Copy(oldpath, newpath string, overwrite bool) error](#Client.Copy)
|
||||
* [func (c *Client) Mkdir(path string, _ os.FileMode) error](#Client.Mkdir)
|
||||
* [func (c *Client) MkdirAll(path string, _ os.FileMode) error](#Client.MkdirAll)
|
||||
* [func (c *Client) Mkdir(path string, _ os.FileMode) (err error)](#Client.Mkdir)
|
||||
* [func (c *Client) MkdirAll(path string, _ os.FileMode) (err error)](#Client.MkdirAll)
|
||||
* [func (c *Client) Read(path string) ([]byte, error)](#Client.Read)
|
||||
* [func (c *Client) ReadDir(path string) ([]os.FileInfo, error)](#Client.ReadDir)
|
||||
* [func (c *Client) ReadStream(path string) (io.ReadCloser, error)](#Client.ReadStream)
|
||||
* [func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error)](#Client.ReadStreamRange)
|
||||
* [func (c *Client) Remove(path string) error](#Client.Remove)
|
||||
* [func (c *Client) RemoveAll(path string) error](#Client.RemoveAll)
|
||||
* [func (c *Client) Rename(oldpath, newpath string, overwrite bool) error](#Client.Rename)
|
||||
@@ -188,8 +193,8 @@ included.
|
||||
* [func (c *Client) SetTimeout(timeout time.Duration)](#Client.SetTimeout)
|
||||
* [func (c *Client) SetTransport(transport http.RoundTripper)](#Client.SetTransport)
|
||||
* [func (c *Client) Stat(path string) (os.FileInfo, error)](#Client.Stat)
|
||||
* [func (c *Client) Write(path string, data []byte, _ os.FileMode) error](#Client.Write)
|
||||
* [func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error](#Client.WriteStream)
|
||||
* [func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error)](#Client.Write)
|
||||
* [func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error)](#Client.WriteStream)
|
||||
* [type DigestAuth](#DigestAuth)
|
||||
* [func (d *DigestAuth) Authorize(req *http.Request, method string, path string)](#DigestAuth.Authorize)
|
||||
* [func (d *DigestAuth) Pass() string](#DigestAuth.Pass)
|
||||
@@ -211,36 +216,53 @@ included.
|
||||
* [func (n *NoAuth) Pass() string](#NoAuth.Pass)
|
||||
* [func (n *NoAuth) Type() string](#NoAuth.Type)
|
||||
* [func (n *NoAuth) User() string](#NoAuth.User)
|
||||
* [type StatusError](#StatusError)
|
||||
* [func (se StatusError) Error() string](#StatusError.Error)
|
||||
|
||||
##### <a name="pkg-examples">Examples</a>
|
||||
* [PathEscape](#example_PathEscape)
|
||||
|
||||
##### <a name="pkg-files">Package files</a>
|
||||
[basicAuth.go](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go) [client.go](https://github.com/studio-b12/gowebdav/blob/master/client.go) [digestAuth.go](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go) [doc.go](https://github.com/studio-b12/gowebdav/blob/master/doc.go) [file.go](https://github.com/studio-b12/gowebdav/blob/master/file.go) [netrc.go](https://github.com/studio-b12/gowebdav/blob/master/netrc.go) [requests.go](https://github.com/studio-b12/gowebdav/blob/master/requests.go) [utils.go](https://github.com/studio-b12/gowebdav/blob/master/utils.go)
|
||||
[basicAuth.go](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go) [client.go](https://github.com/studio-b12/gowebdav/blob/master/client.go) [digestAuth.go](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go) [doc.go](https://github.com/studio-b12/gowebdav/blob/master/doc.go) [errors.go](https://github.com/studio-b12/gowebdav/blob/master/errors.go) [file.go](https://github.com/studio-b12/gowebdav/blob/master/file.go) [netrc.go](https://github.com/studio-b12/gowebdav/blob/master/netrc.go) [requests.go](https://github.com/studio-b12/gowebdav/blob/master/requests.go) [utils.go](https://github.com/studio-b12/gowebdav/blob/master/utils.go)
|
||||
|
||||
### <a name="FixSlash">func</a> [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=707:737#L45)
|
||||
### <a name="FixSlash">func</a> [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=354:384#L23)
|
||||
``` go
|
||||
func FixSlash(s string) string
|
||||
```
|
||||
FixSlash appends a trailing / to our string
|
||||
|
||||
### <a name="FixSlashes">func</a> [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=859:891#L53)
|
||||
### <a name="FixSlashes">func</a> [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=506:538#L31)
|
||||
``` go
|
||||
func FixSlashes(s string) string
|
||||
```
|
||||
FixSlashes appends and prepends a / if they are missing
|
||||
|
||||
### <a name="Join">func</a> [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=976:1020#L61)
|
||||
### <a name="IsErrCode">func</a> [IsErrCode](https://github.com/studio-b12/gowebdav/blob/master/errors.go?s=355:395#L21)
|
||||
``` go
|
||||
func IsErrCode(err error, code int) bool
|
||||
```
|
||||
IsErrCode returns true if the given error
|
||||
is an os.PathError wrapping a StatusError
|
||||
with the given status code.
|
||||
|
||||
### <a name="IsErrNotFound">func</a> [IsErrNotFound](https://github.com/studio-b12/gowebdav/blob/master/errors.go?s=587:621#L31)
|
||||
``` go
|
||||
func IsErrNotFound(err error) bool
|
||||
```
|
||||
IsErrNotFound is shorthand for IsErrCode
|
||||
for status 404.
|
||||
|
||||
### <a name="Join">func</a> [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=639:683#L40)
|
||||
``` go
|
||||
func Join(path0 string, path1 string) string
|
||||
```
|
||||
Join joins two paths
|
||||
|
||||
### <a name="PathEscape">func</a> [PathEscape](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=506:541#L36)
|
||||
### <a name="PathEscape">func</a> [PathEscape](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=153:188#L14)
|
||||
``` go
|
||||
func PathEscape(path string) string
|
||||
```
|
||||
PathEscape escapes all segemnts of a given path
|
||||
PathEscape escapes all segments of a given path
|
||||
|
||||
### <a name="ReadConfig">func</a> [ReadConfig](https://github.com/studio-b12/gowebdav/blob/master/netrc.go?s=428:479#L27)
|
||||
``` go
|
||||
@@ -249,13 +271,13 @@ func ReadConfig(uri, netrc string) (string, string)
|
||||
ReadConfig reads login and password configuration from ~/.netrc
|
||||
machine foo.com login username password 123456
|
||||
|
||||
### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=1150:1181#L66)
|
||||
### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=813:844#L45)
|
||||
``` go
|
||||
func String(r io.Reader) string
|
||||
```
|
||||
String pulls a string out of our io.Reader
|
||||
|
||||
### <a name="Authenticator">type</a> [Authenticator](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=381:500#L28)
|
||||
### <a name="Authenticator">type</a> [Authenticator](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=388:507#L29)
|
||||
``` go
|
||||
type Authenticator interface {
|
||||
Type() string
|
||||
@@ -271,6 +293,7 @@ Authenticator stub
|
||||
type BasicAuth struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
|
||||
```
|
||||
BasicAuth structure holds our credentials
|
||||
|
||||
@@ -298,119 +321,133 @@ func (b *BasicAuth) User() string
|
||||
```
|
||||
User holds the BasicAuth username
|
||||
|
||||
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=165:357#L17)
|
||||
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=172:364#L18)
|
||||
``` go
|
||||
type Client struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
|
||||
```
|
||||
Client defines our structure
|
||||
|
||||
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1012:1056#L61)
|
||||
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1019:1063#L62)
|
||||
``` go
|
||||
func NewClient(uri, user, pw string) *Client
|
||||
```
|
||||
NewClient creates a new instance of client
|
||||
|
||||
#### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1836:1868#L86)
|
||||
#### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1843:1875#L87)
|
||||
``` go
|
||||
func (c *Client) Connect() error
|
||||
```
|
||||
Connect connects to our dav server
|
||||
|
||||
#### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6695:6763#L312)
|
||||
#### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6818:6886#L323)
|
||||
``` go
|
||||
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error
|
||||
```
|
||||
Copy copies a file from A to B
|
||||
|
||||
#### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5786:5842#L271)
|
||||
#### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5793:5855#L272)
|
||||
``` go
|
||||
func (c *Client) Mkdir(path string, _ os.FileMode) error
|
||||
func (c *Client) Mkdir(path string, _ os.FileMode) (err error)
|
||||
```
|
||||
Mkdir makes a directory
|
||||
|
||||
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6021:6080#L282)
|
||||
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6068:6133#L286)
|
||||
``` go
|
||||
func (c *Client) MkdirAll(path string, _ os.FileMode) error
|
||||
func (c *Client) MkdirAll(path string, _ os.FileMode) (err error)
|
||||
```
|
||||
MkdirAll like mkdir -p, but for webdav
|
||||
|
||||
#### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6869:6919#L317)
|
||||
#### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6992:7042#L328)
|
||||
``` go
|
||||
func (c *Client) Read(path string) ([]byte, error)
|
||||
```
|
||||
Read reads the contents of a remote file
|
||||
|
||||
#### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2862:2922#L129)
|
||||
#### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2869:2929#L130)
|
||||
``` go
|
||||
func (c *Client) ReadDir(path string) ([]os.FileInfo, error)
|
||||
```
|
||||
ReadDir reads the contents of a remote directory
|
||||
|
||||
#### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7230:7293#L335)
|
||||
#### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7353:7416#L346)
|
||||
``` go
|
||||
func (c *Client) ReadStream(path string) (io.ReadCloser, error)
|
||||
```
|
||||
ReadStream reads the stream for a given path
|
||||
|
||||
#### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5292:5334#L248)
|
||||
#### <a name="Client.ReadStreamRange">func</a> (\*Client) [ReadStreamRange](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=8165:8255#L368)
|
||||
``` go
|
||||
func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error)
|
||||
```
|
||||
ReadStreamRange reads the stream representing a subset of bytes for a given path,
|
||||
utilizing HTTP Range Requests if the server supports it.
|
||||
The range is expressed as offset from the start of the file and length, for example
|
||||
offset=10, length=10 will return bytes 10 through 19.
|
||||
|
||||
If the server does not support partial content requests and returns full content instead,
|
||||
this function will emulate the behavior by skipping `offset` bytes and limiting the result
|
||||
to `length`.
|
||||
|
||||
#### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5299:5341#L249)
|
||||
``` go
|
||||
func (c *Client) Remove(path string) error
|
||||
```
|
||||
Remove removes a remote file
|
||||
|
||||
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5400:5445#L253)
|
||||
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5407:5452#L254)
|
||||
``` go
|
||||
func (c *Client) RemoveAll(path string) error
|
||||
```
|
||||
RemoveAll removes remote files
|
||||
|
||||
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6529:6599#L307)
|
||||
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6652:6722#L318)
|
||||
``` go
|
||||
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error
|
||||
```
|
||||
Rename moves a file from A to B
|
||||
|
||||
#### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1228:1273#L66)
|
||||
#### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1235:1280#L67)
|
||||
``` go
|
||||
func (c *Client) SetHeader(key, value string)
|
||||
```
|
||||
SetHeader lets us set arbitrary headers for a given client
|
||||
|
||||
#### <a name="Client.SetInterceptor">func</a> (\*Client) [SetInterceptor](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1380:1462#L71)
|
||||
#### <a name="Client.SetInterceptor">func</a> (\*Client) [SetInterceptor](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1387:1469#L72)
|
||||
``` go
|
||||
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request))
|
||||
```
|
||||
SetInterceptor lets us set an arbitrary interceptor for a given client
|
||||
|
||||
#### <a name="Client.SetTimeout">func</a> (\*Client) [SetTimeout](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1564:1614#L76)
|
||||
#### <a name="Client.SetTimeout">func</a> (\*Client) [SetTimeout](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1571:1621#L77)
|
||||
``` go
|
||||
func (c *Client) SetTimeout(timeout time.Duration)
|
||||
```
|
||||
SetTimeout exposes the ability to set a time limit for requests
|
||||
|
||||
#### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1707:1765#L81)
|
||||
#### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1714:1772#L82)
|
||||
``` go
|
||||
func (c *Client) SetTransport(transport http.RoundTripper)
|
||||
```
|
||||
SetTransport exposes the ability to define custom transports
|
||||
|
||||
#### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4248:4303#L196)
|
||||
#### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4255:4310#L197)
|
||||
``` go
|
||||
func (c *Client) Stat(path string) (os.FileInfo, error)
|
||||
```
|
||||
Stat returns the file stats for a specified path
|
||||
|
||||
#### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7584:7653#L350)
|
||||
#### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=9260:9335#L402)
|
||||
``` go
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) error
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error)
|
||||
```
|
||||
Write writes data to a given path
|
||||
|
||||
#### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=8009:8089#L373)
|
||||
#### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=9759:9845#L432)
|
||||
``` go
|
||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error
|
||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error)
|
||||
```
|
||||
WriteStream writes a stream
|
||||
|
||||
@@ -419,6 +456,7 @@ WriteStream writes a stream
|
||||
type DigestAuth struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
|
||||
```
|
||||
DigestAuth structure holds our credentials
|
||||
|
||||
@@ -451,6 +489,7 @@ User holds the DigestAuth username
|
||||
type File struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
|
||||
```
|
||||
File is our structure for a given file
|
||||
|
||||
@@ -514,37 +553,53 @@ func (f File) Sys() interface{}
|
||||
```
|
||||
Sys ????
|
||||
|
||||
### <a name="NoAuth">type</a> [NoAuth](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=544:592#L36)
|
||||
### <a name="NoAuth">type</a> [NoAuth](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=551:599#L37)
|
||||
``` go
|
||||
type NoAuth struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
|
||||
```
|
||||
NoAuth structure holds our credentials
|
||||
|
||||
#### <a name="NoAuth.Authorize">func</a> (\*NoAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=887:960#L57)
|
||||
#### <a name="NoAuth.Authorize">func</a> (\*NoAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=894:967#L58)
|
||||
``` go
|
||||
func (n *NoAuth) Authorize(req *http.Request, method string, path string)
|
||||
```
|
||||
Authorize the current request
|
||||
|
||||
#### <a name="NoAuth.Pass">func</a> (\*NoAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=805:835#L52)
|
||||
#### <a name="NoAuth.Pass">func</a> (\*NoAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=812:842#L53)
|
||||
``` go
|
||||
func (n *NoAuth) Pass() string
|
||||
```
|
||||
Pass returns the current password
|
||||
|
||||
#### <a name="NoAuth.Type">func</a> (\*NoAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=631:661#L42)
|
||||
#### <a name="NoAuth.Type">func</a> (\*NoAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=638:668#L43)
|
||||
``` go
|
||||
func (n *NoAuth) Type() string
|
||||
```
|
||||
Type identifies the authenticator
|
||||
|
||||
#### <a name="NoAuth.User">func</a> (\*NoAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=717:747#L47)
|
||||
#### <a name="NoAuth.User">func</a> (\*NoAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=724:754#L48)
|
||||
``` go
|
||||
func (n *NoAuth) User() string
|
||||
```
|
||||
User returns the current user
|
||||
|
||||
### <a name="StatusError">type</a> [StatusError](https://github.com/studio-b12/gowebdav/blob/master/errors.go?s=114:153#L10)
|
||||
``` go
|
||||
type StatusError struct {
|
||||
Status int
|
||||
}
|
||||
|
||||
```
|
||||
StatusError implements error and wraps
|
||||
an erroneous status code.
|
||||
|
||||
#### <a name="StatusError.Error">func</a> (StatusError) [Error](https://github.com/studio-b12/gowebdav/blob/master/errors.go?s=155:191#L14)
|
||||
``` go
|
||||
func (se StatusError) Error() string
|
||||
```
|
||||
|
||||
- - -
|
||||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
|
||||
|
||||
58
client.go
58
client.go
@@ -269,9 +269,12 @@ func (c *Client) RemoveAll(path string) error {
|
||||
}
|
||||
|
||||
// Mkdir makes a directory
|
||||
func (c *Client) Mkdir(path string, _ os.FileMode) error {
|
||||
func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
|
||||
path = FixSlashes(path)
|
||||
status := c.mkcol(path)
|
||||
status, err := c.mkcol(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status == 201 {
|
||||
return nil
|
||||
}
|
||||
@@ -280,12 +283,16 @@ func (c *Client) Mkdir(path string, _ os.FileMode) error {
|
||||
}
|
||||
|
||||
// MkdirAll like mkdir -p, but for webdav
|
||||
func (c *Client) MkdirAll(path string, _ os.FileMode) error {
|
||||
func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
|
||||
path = FixSlashes(path)
|
||||
status := c.mkcol(path)
|
||||
status, err := c.mkcol(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status == 201 {
|
||||
return nil
|
||||
} else if status == 409 {
|
||||
}
|
||||
if status == 409 {
|
||||
paths := strings.Split(path, "/")
|
||||
sub := "/"
|
||||
for _, e := range paths {
|
||||
@@ -293,7 +300,10 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) error {
|
||||
continue
|
||||
}
|
||||
sub += e + "/"
|
||||
status = c.mkcol(sub)
|
||||
status, err = c.mkcol(sub)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if status != 201 {
|
||||
return newPathError("MkdirAll", sub, status)
|
||||
}
|
||||
@@ -357,7 +367,11 @@ func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
|
||||
// to `length`.
|
||||
func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error) {
|
||||
rs, err := c.req("GET", path, nil, func(r *http.Request) {
|
||||
r.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, offset+length-1))
|
||||
if length > 0 {
|
||||
r.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
|
||||
} else {
|
||||
r.Header.Add("Range", fmt.Sprintf("bytes=%d-", offset))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, newPathErrorErr("ReadStreamRange", path, err)
|
||||
@@ -385,22 +399,29 @@ 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) error {
|
||||
s := c.put(path, bytes.NewReader(data))
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
|
||||
s, err := c.put(path, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch s {
|
||||
|
||||
case 200, 201, 204:
|
||||
return nil
|
||||
|
||||
case 409:
|
||||
err := c.createParentCollection(path)
|
||||
case 404, 409:
|
||||
err = c.createParentCollection(path)
|
||||
if err != nil {
|
||||
return err
|
||||
return
|
||||
}
|
||||
|
||||
s = c.put(path, bytes.NewReader(data))
|
||||
s, err = c.put(path, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if s == 200 || s == 201 || s == 204 {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,14 +429,17 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||
}
|
||||
|
||||
// WriteStream writes a stream
|
||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error {
|
||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) {
|
||||
|
||||
err := c.createParentCollection(path)
|
||||
err = c.createParentCollection(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := c.put(path, stream)
|
||||
s, err := c.put(path, stream)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch s {
|
||||
case 200, 201, 204:
|
||||
|
||||
445
client_test.go
Normal file
445
client_test.go
Normal file
@@ -0,0 +1,445 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/webdav"
|
||||
)
|
||||
|
||||
func basicAuth(h http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if user, passwd, ok := r.BasicAuth(); ok {
|
||||
if user == "user" && passwd == "password" {
|
||||
h.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "not authorized", 403)
|
||||
} else {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="x"`)
|
||||
w.WriteHeader(401)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fillFs(t *testing.T, fs webdav.FileSystem) context.Context {
|
||||
ctx := context.Background()
|
||||
f, err := fs.OpenFile(ctx, "hello.txt", os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate file: %v", err)
|
||||
}
|
||||
f.Write([]byte("hello gowebdav\n"))
|
||||
f.Close()
|
||||
err = fs.Mkdir(ctx, "/test", 0755)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate directory: %v", err)
|
||||
}
|
||||
f, err = fs.OpenFile(ctx, "/test/test.txt", os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate file: %v", err)
|
||||
}
|
||||
f.Write([]byte("test test gowebdav\n"))
|
||||
f.Close()
|
||||
return ctx
|
||||
}
|
||||
|
||||
func newServer(t *testing.T) (*Client, *httptest.Server, webdav.FileSystem, context.Context) {
|
||||
mux := http.NewServeMux()
|
||||
fs := webdav.NewMemFS()
|
||||
ctx := fillFs(t, fs)
|
||||
mux.HandleFunc("/", basicAuth(&webdav.Handler{
|
||||
FileSystem: fs,
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
}))
|
||||
srv := httptest.NewServer(mux)
|
||||
cli := NewClient(srv.URL, "user", "password")
|
||||
return cli, srv, fs, ctx
|
||||
}
|
||||
|
||||
func TestConnect(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
if err := cli.Connect(); err != nil {
|
||||
t.Fatalf("got error: %v, want nil", err)
|
||||
}
|
||||
|
||||
cli = NewClient(srv.URL, "no", "no")
|
||||
if err := cli.Connect(); err == nil {
|
||||
t.Fatalf("got nil, want error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadDirConcurrent(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
f, err := cli.ReadDir("/")
|
||||
if err != nil {
|
||||
errs <- errors.New(fmt.Sprintf("got error: %v, want file listing: %v", err, f))
|
||||
}
|
||||
if len(f) != 2 {
|
||||
errs <- errors.New(fmt.Sprintf("f: %v err: %v", f, err))
|
||||
}
|
||||
if f[0].Name() != "hello.txt" && f[1].Name() != "hello.txt" {
|
||||
errs <- errors.New(fmt.Sprintf("got: %v, want file: %s", f, "hello.txt"))
|
||||
}
|
||||
if f[0].Name() != "test" && f[1].Name() != "test" {
|
||||
errs <- errors.New(fmt.Sprintf("got: %v, want directory: %s", f, "test"))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
data, err := cli.Read("/hello.txt")
|
||||
if err != nil || bytes.Compare(data, []byte("hello gowebdav\n")) != 0 {
|
||||
t.Fatalf("got: %v, want data: %s", err, []byte("hello gowebdav\n"))
|
||||
}
|
||||
|
||||
data, err = cli.Read("/404.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", data, err)
|
||||
}
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("got: %v, want 404 error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStream(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
stream, err := cli.ReadStream("/hello.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want data: %v", err, stream)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(stream)
|
||||
if buf.String() != "hello gowebdav\n" {
|
||||
t.Fatalf("got: %v, want stream: hello gowebdav", buf.String())
|
||||
}
|
||||
|
||||
stream, err = cli.ReadStream("/404/hello.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", stream, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStreamRange(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
stream, err := cli.ReadStreamRange("/hello.txt", 4, 4)
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want data: %v", err, stream)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(stream)
|
||||
if buf.String() != "o go" {
|
||||
t.Fatalf("got: %v, want stream: o go", buf.String())
|
||||
}
|
||||
|
||||
stream, err = cli.ReadStream("/404/hello.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", stream, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadStreamRangeUnkownLength(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
stream, err := cli.ReadStreamRange("/hello.txt", 6, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want data: %v", err, stream)
|
||||
}
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(stream)
|
||||
if buf.String() != "gowebdav\n" {
|
||||
t.Fatalf("got: %v, want stream: gowebdav\n", buf.String())
|
||||
}
|
||||
|
||||
stream, err = cli.ReadStream("/404/hello.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", stream, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStat(t *testing.T) {
|
||||
cli, srv, _, _ := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
info, err := cli.Stat("/hello.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want os.Info: %v", err, info)
|
||||
}
|
||||
if info.Name() != "hello.txt" {
|
||||
t.Fatalf("got: %v, want file hello.txt", info)
|
||||
}
|
||||
|
||||
info, err = cli.Stat("/404.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
if !IsErrNotFound(err) {
|
||||
t.Fatalf("got: %v, want 404 error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMkdir(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
info, err := cli.Stat("/newdir")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.Mkdir("/newdir", 0755); err != nil {
|
||||
t.Fatalf("got: %v, want mkdir /newdir", err)
|
||||
}
|
||||
|
||||
if err := cli.Mkdir("/newdir", 0755); err != nil {
|
||||
t.Fatalf("got: %v, want mkdir /newdir", err)
|
||||
}
|
||||
|
||||
info, err = fs.Stat(ctx, "/newdir")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want dir info: %v", err, info)
|
||||
}
|
||||
|
||||
if err := cli.Mkdir("/404/newdir", 0755); err == nil {
|
||||
t.Fatalf("expected Mkdir error due to missing parent directory")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMkdirAll(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
if err := cli.MkdirAll("/dir/dir/dir", 0755); err != nil {
|
||||
t.Fatalf("got: %v, want mkdirAll /dir/dir/dir", err)
|
||||
}
|
||||
|
||||
info, err := fs.Stat(ctx, "/dir/dir/dir")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want dir info: %v", err, info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCopy(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
info, err := fs.Stat(ctx, "/copy.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.Copy("/hello.txt", "/copy.txt", false); err != nil {
|
||||
t.Fatalf("got: %v, want copy /hello.txt to /copy.txt", err)
|
||||
}
|
||||
|
||||
info, err = fs.Stat(ctx, "/copy.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 15 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 15)
|
||||
}
|
||||
|
||||
info, err = fs.Stat(ctx, "/hello.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 15 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 15)
|
||||
}
|
||||
|
||||
if err := cli.Copy("/hello.txt", "/copy.txt", false); err == nil {
|
||||
t.Fatalf("expected copy error due to overwrite false")
|
||||
}
|
||||
|
||||
if err := cli.Copy("/hello.txt", "/copy.txt", true); err != nil {
|
||||
t.Fatalf("got: %v, want overwrite /copy.txt with /hello.txt", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRename(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
info, err := fs.Stat(ctx, "/copy.txt")
|
||||
if err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.Rename("/hello.txt", "/copy.txt", false); err != nil {
|
||||
t.Fatalf("got: %v, want mv /hello.txt to /copy.txt", err)
|
||||
}
|
||||
|
||||
info, err = fs.Stat(ctx, "/copy.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 15 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 15)
|
||||
}
|
||||
|
||||
if info, err = fs.Stat(ctx, "/hello.txt"); err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.Rename("/test/test.txt", "/copy.txt", true); err != nil {
|
||||
t.Fatalf("got: %v, want overwrite /copy.txt with /hello.txt", err)
|
||||
}
|
||||
info, err = fs.Stat(ctx, "/copy.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 19 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 19)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemove(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
if err := cli.Remove("/hello.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
if info, err := fs.Stat(ctx, "/hello.txt"); err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.Remove("/404.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveAll(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
if err := cli.RemoveAll("/test/test.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
if info, err := fs.Stat(ctx, "/test/test.txt"); err == nil {
|
||||
t.Fatalf("got: %v, want error: %v", info, err)
|
||||
}
|
||||
|
||||
if err := cli.RemoveAll("/404.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
if err := cli.RemoveAll("/404/404/404.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrite(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
if err := cli.Write("/newfile.txt", []byte("foo bar\n"), 0660); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
info, err := fs.Stat(ctx, "/newfile.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 8 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 8)
|
||||
}
|
||||
|
||||
if err := cli.Write("/404/newfile.txt", []byte("foo bar\n"), 0660); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStream(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
if err := cli.WriteStream("/newfile.txt", strings.NewReader("foo bar\n"), 0660); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
info, err := fs.Stat(ctx, "/newfile.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 8 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 8)
|
||||
}
|
||||
|
||||
if err := cli.WriteStream("/404/works.txt", strings.NewReader("foo bar\n"), 0660); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
if info, err := fs.Stat(ctx, "/404/works.txt"); err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteStreamFromPipe(t *testing.T) {
|
||||
cli, srv, fs, ctx := newServer(t)
|
||||
defer srv.Close()
|
||||
|
||||
r, w := io.Pipe()
|
||||
|
||||
go func() {
|
||||
defer w.Close()
|
||||
fmt.Fprint(w, "foo")
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Fprint(w, " ")
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Fprint(w, "bar\n")
|
||||
}()
|
||||
|
||||
if err := cli.WriteStream("/newfile.txt", r, 0660); err != nil {
|
||||
t.Fatalf("got: %v, want nil", err)
|
||||
}
|
||||
|
||||
info, err := fs.Stat(ctx, "/newfile.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("got: %v, want file info: %v", err, info)
|
||||
}
|
||||
if info.Size() != 8 {
|
||||
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 8)
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,16 @@ import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
d "github.com/studio-b12/gowebdav"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
d "github.com/studio-b12/gowebdav"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -190,8 +193,18 @@ func cmdCp(c *d.Client, p0, p1 string) (err error) {
|
||||
|
||||
func cmdPut(c *d.Client, p0, p1 string) (err error) {
|
||||
if p1 == "" {
|
||||
p1 = filepath.Join(".", p0)
|
||||
p1 = path.Join(".", p0)
|
||||
} else {
|
||||
var fi fs.FileInfo
|
||||
fi, err = c.Stat(p0)
|
||||
if err != nil && !d.IsErrNotFound(err) {
|
||||
return
|
||||
}
|
||||
if !d.IsErrNotFound(err) && fi.IsDir() {
|
||||
p0 = path.Join(p0, p1)
|
||||
}
|
||||
}
|
||||
|
||||
stream, err := getStream(p1)
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
78
cmd/gowebdav/main_test.go
Normal file
78
cmd/gowebdav/main_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/webdav"
|
||||
)
|
||||
|
||||
func basicAuth(h http.Handler) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if user, passwd, ok := r.BasicAuth(); ok {
|
||||
if user == "user" && passwd == "password" {
|
||||
h.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "not authorized", 403)
|
||||
} else {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="x"`)
|
||||
w.WriteHeader(401)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newServer(t *testing.T) (*httptest.Server, webdav.FileSystem, context.Context) {
|
||||
mux := http.NewServeMux()
|
||||
fs := webdav.NewMemFS()
|
||||
ctx := fillFs(t, fs)
|
||||
mux.HandleFunc("/", basicAuth(&webdav.Handler{
|
||||
FileSystem: fs,
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
}))
|
||||
srv := httptest.NewServer(mux)
|
||||
os.Setenv("ROOT", srv.URL)
|
||||
os.Setenv("USER", "user")
|
||||
os.Setenv("PASSWORD", "password")
|
||||
return srv, fs, ctx
|
||||
}
|
||||
|
||||
func fillFs(t *testing.T, fs webdav.FileSystem) context.Context {
|
||||
ctx := context.Background()
|
||||
f, err := fs.OpenFile(ctx, "hello.txt", os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate file: %v", err)
|
||||
}
|
||||
f.Write([]byte("hello gowebdav\n"))
|
||||
f.Close()
|
||||
err = fs.Mkdir(ctx, "/test", 0755)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate directory: %v", err)
|
||||
}
|
||||
f, err = fs.OpenFile(ctx, "/test/test.txt", os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
t.Errorf("fail to crate file: %v", err)
|
||||
}
|
||||
f.Write([]byte("test test gowebdav\n"))
|
||||
f.Close()
|
||||
return ctx
|
||||
}
|
||||
|
||||
func TestLs(t *testing.T) {
|
||||
oldArgs := os.Args
|
||||
defer func() { os.Args = oldArgs }()
|
||||
|
||||
srv, _, _ := newServer(t)
|
||||
|
||||
defer srv.Close()
|
||||
|
||||
flag.CommandLine = flag.NewFlagSet("ls", flag.ExitOnError)
|
||||
|
||||
os.Args = []string{"ls", "-X", "ls", "/"}
|
||||
main()
|
||||
}
|
||||
49
errors.go
Normal file
49
errors.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// StatusError implements error and wraps
|
||||
// an erroneous status code.
|
||||
type StatusError struct {
|
||||
Status int
|
||||
}
|
||||
|
||||
func (se StatusError) Error() string {
|
||||
return fmt.Sprintf("%d", se.Status)
|
||||
}
|
||||
|
||||
// IsErrCode returns true if the given error
|
||||
// is an os.PathError wrapping a StatusError
|
||||
// with the given status code.
|
||||
func IsErrCode(err error, code int) bool {
|
||||
if pe, ok := err.(*os.PathError); ok {
|
||||
se, ok := pe.Err.(StatusError)
|
||||
return ok && se.Status == code
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsErrNotFound is shorthand for IsErrCode
|
||||
// for status 404.
|
||||
func IsErrNotFound(err error) bool {
|
||||
return IsErrCode(err, 404)
|
||||
}
|
||||
|
||||
func newPathError(op string, path string, statusCode int) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: path,
|
||||
Err: StatusError{statusCode},
|
||||
}
|
||||
}
|
||||
|
||||
func newPathErrorErr(op string, path string, err error) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: path,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
5
go_test.mod
Normal file
5
go_test.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module github.com/studio-b12/gowebdav
|
||||
|
||||
go 1.17
|
||||
|
||||
require golang.org/x/net v0.0.0-20221014081412-f15817d10f9b
|
||||
7
go_test.sum
Normal file
7
go_test.sum
Normal file
@@ -0,0 +1,7 @@
|
||||
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
|
||||
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
59
requests.go
59
requests.go
@@ -2,8 +2,8 @@ package gowebdav
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
@@ -14,15 +14,6 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
||||
var retryBuf io.Reader
|
||||
|
||||
if body != nil {
|
||||
// Because Request#Do closes closable streams, Seeker#Seek
|
||||
// will fail on retry because stream is already closed.
|
||||
// This inhibits the closing of the passed stream on passing
|
||||
// it to the RoundTripper and closes the stream after we
|
||||
// are done with the body content.
|
||||
if cl, ok := body.(io.Closer); ok {
|
||||
body = closeInhibitor{body}
|
||||
defer cl.Close()
|
||||
}
|
||||
// If the authorization fails, we will need to restart reading
|
||||
// from the passed body stream.
|
||||
// When body is seekable, use seek to reset the streams
|
||||
@@ -100,18 +91,19 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
||||
return rs, err
|
||||
}
|
||||
|
||||
func (c *Client) mkcol(path string) int {
|
||||
func (c *Client) mkcol(path string) (status int, err error) {
|
||||
rs, err := c.req("MKCOL", path, nil, nil)
|
||||
if err != nil {
|
||||
return 400
|
||||
return
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
||||
return 201
|
||||
status = rs.StatusCode
|
||||
if status == 405 {
|
||||
status = 201
|
||||
}
|
||||
|
||||
return rs.StatusCode
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) options(path string) (*http.Response, error) {
|
||||
@@ -139,15 +131,24 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode != 207 {
|
||||
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
|
||||
return newPathError("PROPFIND", path, rs.StatusCode)
|
||||
}
|
||||
|
||||
return parseXML(rs.Body, resp, parse)
|
||||
}
|
||||
|
||||
func (c *Client) doCopyMove(method string, oldpath string, newpath string, overwrite bool) (int, io.ReadCloser) {
|
||||
func (c *Client) doCopyMove(
|
||||
method string,
|
||||
oldpath string,
|
||||
newpath string,
|
||||
overwrite bool,
|
||||
) (
|
||||
status int,
|
||||
r io.ReadCloser,
|
||||
err error,
|
||||
) {
|
||||
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
|
||||
rq.Header.Add("Destination", Join(c.root, newpath))
|
||||
rq.Header.Add("Destination", PathEscape(Join(c.root, newpath)))
|
||||
if overwrite {
|
||||
rq.Header.Add("Overwrite", "T")
|
||||
} else {
|
||||
@@ -155,13 +156,18 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return 400, nil
|
||||
return
|
||||
}
|
||||
return rs.StatusCode, rs.Body
|
||||
status = rs.StatusCode
|
||||
r = rs.Body
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
||||
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
|
||||
s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if data != nil {
|
||||
defer data.Close()
|
||||
}
|
||||
@@ -172,7 +178,7 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
||||
|
||||
case 207:
|
||||
// TODO handle multistat errors, worst case ...
|
||||
log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data)))
|
||||
log.Printf("TODO handle %s - %s multistatus result %s\n", method, oldpath, String(data))
|
||||
|
||||
case 409:
|
||||
err := c.createParentCollection(newpath)
|
||||
@@ -186,14 +192,15 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
||||
return newPathError(method, oldpath, s)
|
||||
}
|
||||
|
||||
func (c *Client) put(path string, stream io.Reader) int {
|
||||
func (c *Client) put(path string, stream io.Reader) (status int, err error) {
|
||||
rs, err := c.req("PUT", path, stream, nil)
|
||||
if err != nil {
|
||||
return 400
|
||||
return
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
return rs.StatusCode
|
||||
status = rs.StatusCode
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Client) createParentCollection(itemPath string) (err error) {
|
||||
|
||||
33
utils.go
33
utils.go
@@ -3,35 +3,13 @@ package gowebdav
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func log(msg interface{}) {
|
||||
fmt.Println(msg)
|
||||
}
|
||||
|
||||
func newPathError(op string, path string, statusCode int) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: path,
|
||||
Err: fmt.Errorf("%d", statusCode),
|
||||
}
|
||||
}
|
||||
|
||||
func newPathErrorErr(op string, path string, err error) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: path,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
|
||||
// PathEscape escapes all segments of a given path
|
||||
func PathEscape(path string) string {
|
||||
s := strings.Split(path, "/")
|
||||
@@ -133,14 +111,3 @@ func (l *limitedReadCloser) Read(buf []byte) (int, error) {
|
||||
func (l *limitedReadCloser) Close() error {
|
||||
return l.rc.Close()
|
||||
}
|
||||
|
||||
// closeInhibitor implements io.Closer and
|
||||
// wraps a Reader. When Close() is performed
|
||||
// on it, it will simply be silently rejected.
|
||||
type closeInhibitor struct {
|
||||
io.Reader
|
||||
}
|
||||
|
||||
func (ci closeInhibitor) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user