Compare commits
67 Commits
6
...
dev-bodycl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d8627db50 | ||
|
|
c42caf78a2 | ||
|
|
341db84788 | ||
|
|
b51247bb2c | ||
|
|
3f8721cd4b | ||
|
|
adba8dc051 | ||
|
|
2f2cda4122 | ||
|
|
73a7f0bf37 | ||
|
|
aff231de53 | ||
|
|
e5dd1e70b1 | ||
|
|
29e74efa70 | ||
|
|
741fdbda3d | ||
|
|
a3a86976a1 | ||
|
|
a2cbdfa976 | ||
|
|
9a1ba21162 | ||
|
|
7ff61aa87b | ||
|
|
86f8378cf1 | ||
|
|
4145fa842c | ||
|
|
8244b5a5f5 | ||
|
|
d02a1ebcd2 | ||
|
|
3ed042db71 | ||
|
|
bdacfab947 | ||
|
|
617404b525 | ||
|
|
9380631c29 | ||
|
|
a93005d73c | ||
|
|
321978fa73 | ||
|
|
c4c707907d | ||
|
|
9f625b1b8e | ||
|
|
e53b818e1b | ||
|
|
ff7f737904 | ||
|
|
38f79aeaf1 | ||
|
|
6c32839dbd | ||
|
|
4d70d7ea28 | ||
|
|
8bcb1b383c | ||
|
|
cba565a9dc | ||
|
|
425530b55e | ||
|
|
7493d8befb | ||
|
|
e29bc0f031 | ||
|
|
c8fc9ca590 | ||
|
|
a68e21e92b | ||
|
|
02aa9bdaeb | ||
|
|
8de8ce169b | ||
|
|
3cd755d6c4 | ||
|
|
83e3d1e31e | ||
|
|
28039fda22 | ||
|
|
45a56c2115 | ||
|
|
f821ab73e9 | ||
|
|
ec1263db2f | ||
|
|
95706c0747 | ||
|
|
f43a0a4cf8 | ||
|
|
68824ef55e | ||
|
|
6ca20e2a70 | ||
|
|
790397514e | ||
|
|
4ca2f77e2b | ||
|
|
008b27eb0f | ||
|
|
d1ebcbebf2 | ||
|
|
4f450cfd02 | ||
|
|
876ef52924 | ||
|
|
21d86ab356 | ||
|
|
97a0b83aeb | ||
|
|
1fe9163c92 | ||
|
|
8bab650703 | ||
|
|
c4c24955e1 | ||
|
|
2593a81bf0 | ||
|
|
b45378c08f | ||
|
|
aebc3ef9d2 | ||
|
|
9ff8e33634 |
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
27
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
---
|
||||||
|
name: Bug report
|
||||||
|
about: Create a report to help us improve
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Hello Collaborators,
|
||||||
|
|
||||||
|
**Describe the bug**
|
||||||
|
A short description of what you think the bug is.
|
||||||
|
|
||||||
|
**Software**
|
||||||
|
- OS:
|
||||||
|
- Golang:
|
||||||
|
- Version:
|
||||||
|
|
||||||
|
**To Reproduce**
|
||||||
|
1. Go to '...'
|
||||||
|
2. Click on '....'
|
||||||
|
3. Scroll down to '....'
|
||||||
|
4. See error
|
||||||
|
|
||||||
|
**Expected**
|
||||||
|
A short description of what you expected to happen.
|
||||||
|
|
||||||
|
**Additional context**
|
||||||
|
Add any other context about the problem here.
|
||||||
46
.github/workflows/artifacts.yml
vendored
Normal file
46
.github/workflows/artifacts.yml
vendored
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
name: Build Artifacts
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
paths-ignore:
|
||||||
|
- "**.md"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build_artifacts:
|
||||||
|
name: Build Artifcats
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
goos:
|
||||||
|
- linux
|
||||||
|
- windows
|
||||||
|
- darwin
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
steps:
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: "^1.17"
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Get dependencies
|
||||||
|
run: go get ./...
|
||||||
|
- name: Build Client (${{ matrix.goos }}-${{ matrix.goarch }})
|
||||||
|
env:
|
||||||
|
GOOS: ${{ matrix.goos }}
|
||||||
|
GOARCH: ${{ matrix.goarch }}
|
||||||
|
run: go build -v -o ./bin/gowebdav-${{ matrix.goos }}-${{ matrix.goarch }} ./cmd/gowebdav/main.go
|
||||||
|
- name: Rename Windows Binary
|
||||||
|
if: ${{ matrix.goos == 'windows' }}
|
||||||
|
env:
|
||||||
|
FNAME: ./bin/gowebdav-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||||
|
run: mv ${{ env.FNAME }} ${{ env.FNAME }}.exe
|
||||||
|
- name: Upload Artifcats
|
||||||
|
uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: ${{ matrix.goos }}-${{ matrix.goarch }}
|
||||||
|
path: ./bin/
|
||||||
30
.github/workflows/tests.yml
vendored
Normal file
30
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
name: Unit Tests
|
||||||
|
on:
|
||||||
|
workflow_dispatch:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "*"
|
||||||
|
paths-ignore:
|
||||||
|
- "**.md"
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
unit_tests:
|
||||||
|
name: Unit Tests
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
goversion:
|
||||||
|
- "1.17"
|
||||||
|
- "1.16"
|
||||||
|
steps:
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v1
|
||||||
|
with:
|
||||||
|
go-version: ${{ matrix.goversion }}
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Get dependencies
|
||||||
|
run: go get ./...
|
||||||
|
- name: Run Unit Tests
|
||||||
|
run: go test -v -cover -race ./...
|
||||||
17
.gitignore
vendored
17
.gitignore
vendored
@@ -1,4 +1,21 @@
|
|||||||
|
# Folders to ignore
|
||||||
/src
|
/src
|
||||||
/bin
|
/bin
|
||||||
/pkg
|
/pkg
|
||||||
/gowebdav
|
/gowebdav
|
||||||
|
/.idea
|
||||||
|
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, build with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
||||||
|
|
||||||
|
.vscode/
|
||||||
@@ -2,3 +2,9 @@ language: go
|
|||||||
|
|
||||||
go:
|
go:
|
||||||
- "1.x"
|
- "1.x"
|
||||||
|
|
||||||
|
install:
|
||||||
|
- go get ./...
|
||||||
|
|
||||||
|
script:
|
||||||
|
- go test -v --short ./...
|
||||||
289
README.md
289
README.md
@@ -4,84 +4,147 @@
|
|||||||
[](https://godoc.org/github.com/studio-b12/gowebdav)
|
[](https://godoc.org/github.com/studio-b12/gowebdav)
|
||||||
[](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
|
[](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
|
||||||
|
|
||||||
A golang WebDAV client library and command line tool.
|
A golang WebDAV client library.
|
||||||
|
|
||||||
## Install command line tool
|
## Main features
|
||||||
|
`gowebdav` library allows to perform following actions on the remote WebDAV server:
|
||||||
```sh
|
* [create path](#create-path-on-a-webdav-server)
|
||||||
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
* [get files list](#get-files-list)
|
||||||
```
|
* [download file](#download-file-to-byte-array)
|
||||||
|
* [upload file](#upload-file-from-byte-array)
|
||||||
|
* [get information about specified file/folder](#get-information-about-specified-filefolder)
|
||||||
|
* [move file to another location](#move-file-to-another-location)
|
||||||
|
* [copy file to another location](#copy-file-to-another-location)
|
||||||
|
* [delete file](#delete-file)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```sh
|
First of all you should create `Client` instance using `NewClient()` function:
|
||||||
$ gowebdav --help
|
|
||||||
Usage of gowebdav
|
|
||||||
-X string
|
|
||||||
Method:
|
|
||||||
LS <PATH>
|
|
||||||
STAT <PATH>
|
|
||||||
|
|
||||||
MKDIR <PATH>
|
```go
|
||||||
MKDIRALL <PATH>
|
root := "https://webdav.mydomain.me"
|
||||||
|
user := "user"
|
||||||
|
password := "password"
|
||||||
|
|
||||||
GET <PATH> [<FILE>]
|
c := gowebdav.NewClient(root, user, password)
|
||||||
PUT <PATH> [<FILE>]
|
|
||||||
|
|
||||||
MV <OLD> <NEW>
|
|
||||||
CP <OLD> <NEW>
|
|
||||||
|
|
||||||
DEL <PATH>
|
|
||||||
|
|
||||||
-netrc-file string
|
|
||||||
read login from netrc file (default "~/.netrc")
|
|
||||||
-pw string
|
|
||||||
Password [ENV.PASSWORD]
|
|
||||||
-root string
|
|
||||||
WebDAV Endpoint [ENV.ROOT]
|
|
||||||
-user string
|
|
||||||
User [ENV.USER] (default "$USER")
|
|
||||||
```
|
```
|
||||||
|
|
||||||
*gowebdav wrapper script*
|
After you can use this `Client` to perform actions, described below.
|
||||||
|
|
||||||
Create a wrapper script for example `$EDITOR ./dav && chmod a+x ./dav` for your
|
**NOTICE:** we will not check errors in examples, to focus you on the `gowebdav` library's code, but you should do it in your code!
|
||||||
server and use [pass](https://www.passwordstore.org/ "the standard unix password manager")
|
|
||||||
or similar tools to retrieve the password.
|
|
||||||
|
|
||||||
```sh
|
### Create path on a WebDAV server
|
||||||
#!/bin/sh
|
```go
|
||||||
|
err := c.Mkdir("folder", 0644)
|
||||||
ROOT="https://my.dav.server/" \
|
```
|
||||||
USER="foo" \
|
In case you want to create several folders you can use `c.MkdirAll()`:
|
||||||
PASSWORD="$(pass dav/foo@my.dav.server)" \
|
```go
|
||||||
gowebdav $@
|
err := c.MkdirAll("folder/subfolder/subfolder2", 0644)
|
||||||
```
|
```
|
||||||
|
|
||||||
*Examples*
|
### Get files list
|
||||||
|
```go
|
||||||
Using the `dav` wrapper:
|
files, _ := c.ReadDir("folder/subfolder")
|
||||||
|
for _, file := range files {
|
||||||
```sh
|
//notice that [file] has os.FileInfo type
|
||||||
$ ./dav -X LS /
|
fmt.Println(file.Name())
|
||||||
|
}
|
||||||
$ echo hi dav! > hello && ./dav -X PUT /hello
|
|
||||||
|
|
||||||
$ ./dav -X STAT /hello
|
|
||||||
|
|
||||||
$ ./dav -X PUT /hello_dav hello
|
|
||||||
|
|
||||||
$ ./dav -X GET /hello_dav
|
|
||||||
|
|
||||||
$ ./dav -X GET /hello_dav hello.txt
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## LINKS
|
### Download file to byte array
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
localFilePath := "/tmp/webdav/file.txt"
|
||||||
|
|
||||||
* [RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV](http://www.faqs.org/rfcs/rfc2518.html "RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV")
|
bytes, _ := c.Read(webdavFilePath)
|
||||||
|
ioutil.WriteFile(localFilePath, bytes, 0644)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Download file via reader
|
||||||
|
Also you can use `c.ReadStream()` method:
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
localFilePath := "/tmp/webdav/file.txt"
|
||||||
|
|
||||||
|
reader, _ := c.ReadStream(webdavFilePath)
|
||||||
|
|
||||||
|
file, _ := os.Create(localFilePath)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
io.Copy(file, reader)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Upload file from byte array
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
localFilePath := "/tmp/webdav/file.txt"
|
||||||
|
|
||||||
|
bytes, _ := ioutil.ReadFile(localFilePath)
|
||||||
|
|
||||||
|
c.Write(webdavFilePath, bytes, 0644)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Upload file via writer
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
localFilePath := "/tmp/webdav/file.txt"
|
||||||
|
|
||||||
|
file, _ := os.Open(localFilePath)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
c.WriteStream(webdavFilePath, file, 0644)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Get information about specified file/folder
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
|
||||||
|
info := c.Stat(webdavFilePath)
|
||||||
|
//notice that [info] has os.FileInfo type
|
||||||
|
fmt.Println(info)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Move file to another location
|
||||||
|
```go
|
||||||
|
oldPath := "folder/subfolder/file.txt"
|
||||||
|
newPath := "folder/subfolder/moved.txt"
|
||||||
|
isOverwrite := true
|
||||||
|
|
||||||
|
c.Rename(oldPath, newPath, isOverwrite)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Copy file to another location
|
||||||
|
```go
|
||||||
|
oldPath := "folder/subfolder/file.txt"
|
||||||
|
newPath := "folder/subfolder/file-copy.txt"
|
||||||
|
isOverwrite := true
|
||||||
|
|
||||||
|
c.Copy(oldPath, newPath, isOverwrite)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Delete file
|
||||||
|
```go
|
||||||
|
webdavFilePath := "folder/subfolder/file.txt"
|
||||||
|
|
||||||
|
c.Remove(webdavFilePath)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
More details about WebDAV server you can read from following resources:
|
||||||
|
|
||||||
|
* [RFC 4918 - HTTP Extensions for Web Distributed Authoring and Versioning (WebDAV)](https://tools.ietf.org/html/rfc4918)
|
||||||
|
* [RFC 5689 - Extended MKCOL for Web Distributed Authoring and Versioning (WebDAV)](https://tools.ietf.org/html/rfc5689)
|
||||||
* [RFC 2616 - HTTP/1.1 Status Code Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html "HTTP/1.1 Status Code Definitions")
|
* [RFC 2616 - HTTP/1.1 Status Code Definitions](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html "HTTP/1.1 Status Code Definitions")
|
||||||
* [WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseaul](https://books.google.de/books?isbn=0130652083 "WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseault")
|
* [WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseaul](https://books.google.de/books?isbn=0130652083 "WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseault")
|
||||||
|
|
||||||
|
**NOTICE**: RFC 2518 is obsoleted by RFC 4918 in June 2007
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
All contributing are welcome. If you have any suggestions or find some bug - please create an Issue to let us make this project better. We appreciate your help!
|
||||||
|
|
||||||
|
## License
|
||||||
|
This library is distributed under the BSD 3-Clause license found in the [LICENSE](https://github.com/studio-b12/gowebdav/blob/master/LICENSE) file.
|
||||||
## API
|
## API
|
||||||
|
|
||||||
`import "github.com/studio-b12/gowebdav"`
|
`import "github.com/studio-b12/gowebdav"`
|
||||||
@@ -104,7 +167,7 @@ included.
|
|||||||
* [func String(r io.Reader) string](#String)
|
* [func String(r io.Reader) string](#String)
|
||||||
* [type Authenticator](#Authenticator)
|
* [type Authenticator](#Authenticator)
|
||||||
* [type BasicAuth](#BasicAuth)
|
* [type BasicAuth](#BasicAuth)
|
||||||
* [func (b *BasicAuth) Authorize(c *Client, method string, path string)](#BasicAuth.Authorize)
|
* [func (b *BasicAuth) Authorize(req *http.Request, method string, path string)](#BasicAuth.Authorize)
|
||||||
* [func (b *BasicAuth) Pass() string](#BasicAuth.Pass)
|
* [func (b *BasicAuth) Pass() string](#BasicAuth.Pass)
|
||||||
* [func (b *BasicAuth) Type() string](#BasicAuth.Type)
|
* [func (b *BasicAuth) Type() string](#BasicAuth.Type)
|
||||||
* [func (b *BasicAuth) User() string](#BasicAuth.User)
|
* [func (b *BasicAuth) User() string](#BasicAuth.User)
|
||||||
@@ -121,13 +184,14 @@ included.
|
|||||||
* [func (c *Client) RemoveAll(path string) error](#Client.RemoveAll)
|
* [func (c *Client) RemoveAll(path string) error](#Client.RemoveAll)
|
||||||
* [func (c *Client) Rename(oldpath, newpath string, overwrite bool) error](#Client.Rename)
|
* [func (c *Client) Rename(oldpath, newpath string, overwrite bool) error](#Client.Rename)
|
||||||
* [func (c *Client) SetHeader(key, value string)](#Client.SetHeader)
|
* [func (c *Client) SetHeader(key, value string)](#Client.SetHeader)
|
||||||
|
* [func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request))](#Client.SetInterceptor)
|
||||||
* [func (c *Client) SetTimeout(timeout time.Duration)](#Client.SetTimeout)
|
* [func (c *Client) SetTimeout(timeout time.Duration)](#Client.SetTimeout)
|
||||||
* [func (c *Client) SetTransport(transport http.RoundTripper)](#Client.SetTransport)
|
* [func (c *Client) SetTransport(transport http.RoundTripper)](#Client.SetTransport)
|
||||||
* [func (c *Client) Stat(path string) (os.FileInfo, error)](#Client.Stat)
|
* [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) 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) WriteStream(path string, stream io.Reader, _ os.FileMode) error](#Client.WriteStream)
|
||||||
* [type DigestAuth](#DigestAuth)
|
* [type DigestAuth](#DigestAuth)
|
||||||
* [func (d *DigestAuth) Authorize(c *Client, method string, path string)](#DigestAuth.Authorize)
|
* [func (d *DigestAuth) Authorize(req *http.Request, method string, path string)](#DigestAuth.Authorize)
|
||||||
* [func (d *DigestAuth) Pass() string](#DigestAuth.Pass)
|
* [func (d *DigestAuth) Pass() string](#DigestAuth.Pass)
|
||||||
* [func (d *DigestAuth) Type() string](#DigestAuth.Type)
|
* [func (d *DigestAuth) Type() string](#DigestAuth.Type)
|
||||||
* [func (d *DigestAuth) User() string](#DigestAuth.User)
|
* [func (d *DigestAuth) User() string](#DigestAuth.User)
|
||||||
@@ -138,11 +202,12 @@ included.
|
|||||||
* [func (f File) ModTime() time.Time](#File.ModTime)
|
* [func (f File) ModTime() time.Time](#File.ModTime)
|
||||||
* [func (f File) Mode() os.FileMode](#File.Mode)
|
* [func (f File) Mode() os.FileMode](#File.Mode)
|
||||||
* [func (f File) Name() string](#File.Name)
|
* [func (f File) Name() string](#File.Name)
|
||||||
|
* [func (f File) Path() string](#File.Path)
|
||||||
* [func (f File) Size() int64](#File.Size)
|
* [func (f File) Size() int64](#File.Size)
|
||||||
* [func (f File) String() string](#File.String)
|
* [func (f File) String() string](#File.String)
|
||||||
* [func (f File) Sys() interface{}](#File.Sys)
|
* [func (f File) Sys() interface{}](#File.Sys)
|
||||||
* [type NoAuth](#NoAuth)
|
* [type NoAuth](#NoAuth)
|
||||||
* [func (n *NoAuth) Authorize(c *Client, method string, path string)](#NoAuth.Authorize)
|
* [func (n *NoAuth) Authorize(req *http.Request, method string, path string)](#NoAuth.Authorize)
|
||||||
* [func (n *NoAuth) Pass() string](#NoAuth.Pass)
|
* [func (n *NoAuth) Pass() string](#NoAuth.Pass)
|
||||||
* [func (n *NoAuth) Type() string](#NoAuth.Type)
|
* [func (n *NoAuth) Type() string](#NoAuth.Type)
|
||||||
* [func (n *NoAuth) User() string](#NoAuth.User)
|
* [func (n *NoAuth) User() string](#NoAuth.User)
|
||||||
@@ -190,18 +255,18 @@ func String(r io.Reader) string
|
|||||||
```
|
```
|
||||||
String pulls a string out of our io.Reader
|
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=285:398#L24)
|
### <a name="Authenticator">type</a> [Authenticator](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=381:500#L28)
|
||||||
``` go
|
``` go
|
||||||
type Authenticator interface {
|
type Authenticator interface {
|
||||||
Type() string
|
Type() string
|
||||||
User() string
|
User() string
|
||||||
Pass() string
|
Pass() string
|
||||||
Authorize(*Client, string, string)
|
Authorize(*http.Request, string, string)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Authenticator stub
|
Authenticator stub
|
||||||
|
|
||||||
### <a name="BasicAuth">type</a> [BasicAuth](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=94:145#L8)
|
### <a name="BasicAuth">type</a> [BasicAuth](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=106:157#L9)
|
||||||
``` go
|
``` go
|
||||||
type BasicAuth struct {
|
type BasicAuth struct {
|
||||||
// contains filtered or unexported fields
|
// contains filtered or unexported fields
|
||||||
@@ -209,31 +274,31 @@ type BasicAuth struct {
|
|||||||
```
|
```
|
||||||
BasicAuth structure holds our credentials
|
BasicAuth structure holds our credentials
|
||||||
|
|
||||||
#### <a name="BasicAuth.Authorize">func</a> (\*BasicAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=461:529#L29)
|
#### <a name="BasicAuth.Authorize">func</a> (\*BasicAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=473:549#L30)
|
||||||
``` go
|
``` go
|
||||||
func (b *BasicAuth) Authorize(c *Client, method string, path string)
|
func (b *BasicAuth) Authorize(req *http.Request, method string, path string)
|
||||||
```
|
```
|
||||||
Authorize the current request
|
Authorize the current request
|
||||||
|
|
||||||
#### <a name="BasicAuth.Pass">func</a> (\*BasicAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=376:409#L24)
|
#### <a name="BasicAuth.Pass">func</a> (\*BasicAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=388:421#L25)
|
||||||
``` go
|
``` go
|
||||||
func (b *BasicAuth) Pass() string
|
func (b *BasicAuth) Pass() string
|
||||||
```
|
```
|
||||||
Pass holds the BasicAuth password
|
Pass holds the BasicAuth password
|
||||||
|
|
||||||
#### <a name="BasicAuth.Type">func</a> (\*BasicAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=189:222#L14)
|
#### <a name="BasicAuth.Type">func</a> (\*BasicAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=201:234#L15)
|
||||||
``` go
|
``` go
|
||||||
func (b *BasicAuth) Type() string
|
func (b *BasicAuth) Type() string
|
||||||
```
|
```
|
||||||
Type identifies the BasicAuthenticator
|
Type identifies the BasicAuthenticator
|
||||||
|
|
||||||
#### <a name="BasicAuth.User">func</a> (\*BasicAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=285:318#L19)
|
#### <a name="BasicAuth.User">func</a> (\*BasicAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=297:330#L20)
|
||||||
``` go
|
``` go
|
||||||
func (b *BasicAuth) User() string
|
func (b *BasicAuth) User() string
|
||||||
```
|
```
|
||||||
User holds the BasicAuth username
|
User holds the BasicAuth username
|
||||||
|
|
||||||
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=157:261#L16)
|
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=165:357#L17)
|
||||||
``` go
|
``` go
|
||||||
type Client struct {
|
type Client struct {
|
||||||
// contains filtered or unexported fields
|
// contains filtered or unexported fields
|
||||||
@@ -241,103 +306,109 @@ type Client struct {
|
|||||||
```
|
```
|
||||||
Client defines our structure
|
Client defines our structure
|
||||||
|
|
||||||
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=902:946#L57)
|
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1012:1056#L61)
|
||||||
``` go
|
``` go
|
||||||
func NewClient(uri, user, pw string) *Client
|
func NewClient(uri, user, pw string) *Client
|
||||||
```
|
```
|
||||||
NewClient creates a new instance of 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=1516:1548#L77)
|
#### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1836:1868#L86)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Connect() error
|
func (c *Client) Connect() error
|
||||||
```
|
```
|
||||||
Connect connects to our dav server
|
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=6960:7028#L314)
|
#### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6695:6763#L312)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error
|
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error
|
||||||
```
|
```
|
||||||
Copy copies a file from A to B
|
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=6051:6107#L273)
|
#### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5786:5842#L271)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Mkdir(path string, _ os.FileMode) error
|
func (c *Client) Mkdir(path string, _ os.FileMode) error
|
||||||
```
|
```
|
||||||
Mkdir makes a directory
|
Mkdir makes a directory
|
||||||
|
|
||||||
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6286:6345#L284)
|
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6021:6080#L282)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) MkdirAll(path string, _ os.FileMode) error
|
func (c *Client) MkdirAll(path string, _ os.FileMode) error
|
||||||
```
|
```
|
||||||
MkdirAll like mkdir -p, but for webdav
|
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=7134:7184#L319)
|
#### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6869:6919#L317)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Read(path string) ([]byte, error)
|
func (c *Client) Read(path string) ([]byte, error)
|
||||||
```
|
```
|
||||||
Read reads the contents of a remote file
|
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=3126:3186#L131)
|
#### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2862:2922#L129)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) ReadDir(path string) ([]os.FileInfo, error)
|
func (c *Client) ReadDir(path string) ([]os.FileInfo, error)
|
||||||
```
|
```
|
||||||
ReadDir reads the contents of a remote directory
|
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=7495:7558#L337)
|
#### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7230:7293#L335)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) ReadStream(path string) (io.ReadCloser, error)
|
func (c *Client) ReadStream(path string) (io.ReadCloser, error)
|
||||||
```
|
```
|
||||||
ReadStream reads the stream for a given path
|
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=5557:5599#L250)
|
#### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5292:5334#L248)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Remove(path string) error
|
func (c *Client) Remove(path string) error
|
||||||
```
|
```
|
||||||
Remove removes a remote file
|
Remove removes a remote file
|
||||||
|
|
||||||
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5665:5710#L255)
|
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5400:5445#L253)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) RemoveAll(path string) error
|
func (c *Client) RemoveAll(path string) error
|
||||||
```
|
```
|
||||||
RemoveAll removes remote files
|
RemoveAll removes remote files
|
||||||
|
|
||||||
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6794:6864#L309)
|
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6529:6599#L307)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error
|
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error
|
||||||
```
|
```
|
||||||
Rename moves a file from A to B
|
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=1099:1144#L62)
|
#### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1228:1273#L66)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) SetHeader(key, value string)
|
func (c *Client) SetHeader(key, value string)
|
||||||
```
|
```
|
||||||
SetHeader lets us set arbitrary headers for a given client
|
SetHeader lets us set arbitrary headers for a given client
|
||||||
|
|
||||||
#### <a name="Client.SetTimeout">func</a> (\*Client) [SetTimeout](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1244:1294#L67)
|
#### <a name="Client.SetInterceptor">func</a> (\*Client) [SetInterceptor](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1380:1462#L71)
|
||||||
|
``` 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)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) SetTimeout(timeout time.Duration)
|
func (c *Client) SetTimeout(timeout time.Duration)
|
||||||
```
|
```
|
||||||
SetTimeout exposes the ability to set a time limit for requests
|
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=1387:1445#L72)
|
#### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1707:1765#L81)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) SetTransport(transport http.RoundTripper)
|
func (c *Client) SetTransport(transport http.RoundTripper)
|
||||||
```
|
```
|
||||||
SetTransport exposes the ability to define custom transports
|
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=4513:4568#L198)
|
#### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4248:4303#L196)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Stat(path string) (os.FileInfo, error)
|
func (c *Client) Stat(path string) (os.FileInfo, error)
|
||||||
```
|
```
|
||||||
Stat returns the file stats for a specified path
|
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=7849:7918#L352)
|
#### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7584:7653#L350)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) error
|
func (c *Client) Write(path string, data []byte, _ os.FileMode) error
|
||||||
```
|
```
|
||||||
Write writes data to a given path
|
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=8320:8400#L374)
|
#### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=8009:8089#L373)
|
||||||
``` go
|
``` go
|
||||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error
|
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error
|
||||||
```
|
```
|
||||||
@@ -351,9 +422,9 @@ type DigestAuth struct {
|
|||||||
```
|
```
|
||||||
DigestAuth structure holds our credentials
|
DigestAuth structure holds our credentials
|
||||||
|
|
||||||
#### <a name="DigestAuth.Authorize">func</a> (\*DigestAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=577:646#L36)
|
#### <a name="DigestAuth.Authorize">func</a> (\*DigestAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=577:654#L36)
|
||||||
``` go
|
``` go
|
||||||
func (d *DigestAuth) Authorize(c *Client, method string, path string)
|
func (d *DigestAuth) Authorize(req *http.Request, method string, path string)
|
||||||
```
|
```
|
||||||
Authorize the current request
|
Authorize the current request
|
||||||
|
|
||||||
@@ -383,61 +454,67 @@ type File struct {
|
|||||||
```
|
```
|
||||||
File is our structure for a given file
|
File is our structure for a given file
|
||||||
|
|
||||||
#### <a name="File.ContentType">func</a> (File) [ContentType](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=388:422#L26)
|
#### <a name="File.ContentType">func</a> (File) [ContentType](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=476:510#L31)
|
||||||
``` go
|
``` go
|
||||||
func (f File) ContentType() string
|
func (f File) ContentType() string
|
||||||
```
|
```
|
||||||
ContentType returns the content type of a file
|
ContentType returns the content type of a file
|
||||||
|
|
||||||
#### <a name="File.ETag">func</a> (File) [ETag](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=841:868#L51)
|
#### <a name="File.ETag">func</a> (File) [ETag](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=929:956#L56)
|
||||||
``` go
|
``` go
|
||||||
func (f File) ETag() string
|
func (f File) ETag() string
|
||||||
```
|
```
|
||||||
ETag returns the ETag of a file
|
ETag returns the ETag of a file
|
||||||
|
|
||||||
#### <a name="File.IsDir">func</a> (File) [IsDir](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=947:973#L56)
|
#### <a name="File.IsDir">func</a> (File) [IsDir](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1035:1061#L61)
|
||||||
``` go
|
``` go
|
||||||
func (f File) IsDir() bool
|
func (f File) IsDir() bool
|
||||||
```
|
```
|
||||||
IsDir let us see if a given file is a directory or not
|
IsDir let us see if a given file is a directory or not
|
||||||
|
|
||||||
#### <a name="File.ModTime">func</a> (File) [ModTime](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=748:781#L46)
|
#### <a name="File.ModTime">func</a> (File) [ModTime](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=836:869#L51)
|
||||||
``` go
|
``` go
|
||||||
func (f File) ModTime() time.Time
|
func (f File) ModTime() time.Time
|
||||||
```
|
```
|
||||||
ModTime returns the modified time of a file
|
ModTime returns the modified time of a file
|
||||||
|
|
||||||
#### <a name="File.Mode">func</a> (File) [Mode](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=577:609#L36)
|
#### <a name="File.Mode">func</a> (File) [Mode](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=665:697#L41)
|
||||||
``` go
|
``` go
|
||||||
func (f File) Mode() os.FileMode
|
func (f File) Mode() os.FileMode
|
||||||
```
|
```
|
||||||
Mode will return the mode of a given file
|
Mode will return the mode of a given file
|
||||||
|
|
||||||
#### <a name="File.Name">func</a> (File) [Name](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=290:317#L21)
|
#### <a name="File.Name">func</a> (File) [Name](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=378:405#L26)
|
||||||
``` go
|
``` go
|
||||||
func (f File) Name() string
|
func (f File) Name() string
|
||||||
```
|
```
|
||||||
Name returns the name of a file
|
Name returns the name of a file
|
||||||
|
|
||||||
#### <a name="File.Size">func</a> (File) [Size](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=485:511#L31)
|
#### <a name="File.Path">func</a> (File) [Path](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=295:322#L21)
|
||||||
|
``` go
|
||||||
|
func (f File) Path() string
|
||||||
|
```
|
||||||
|
Path returns the full path of a file
|
||||||
|
|
||||||
|
#### <a name="File.Size">func</a> (File) [Size](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=573:599#L36)
|
||||||
``` go
|
``` go
|
||||||
func (f File) Size() int64
|
func (f File) Size() int64
|
||||||
```
|
```
|
||||||
Size returns the size of a file
|
Size returns the size of a file
|
||||||
|
|
||||||
#### <a name="File.String">func</a> (File) [String](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1095:1124#L66)
|
#### <a name="File.String">func</a> (File) [String](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1183:1212#L71)
|
||||||
``` go
|
``` go
|
||||||
func (f File) String() string
|
func (f File) String() string
|
||||||
```
|
```
|
||||||
String lets us see file information
|
String lets us see file information
|
||||||
|
|
||||||
#### <a name="File.Sys">func</a> (File) [Sys](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1007:1038#L61)
|
#### <a name="File.Sys">func</a> (File) [Sys](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1095:1126#L66)
|
||||||
``` go
|
``` go
|
||||||
func (f File) Sys() interface{}
|
func (f File) Sys() interface{}
|
||||||
```
|
```
|
||||||
Sys ????
|
Sys ????
|
||||||
|
|
||||||
### <a name="NoAuth">type</a> [NoAuth](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=442:490#L32)
|
### <a name="NoAuth">type</a> [NoAuth](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=544:592#L36)
|
||||||
``` go
|
``` go
|
||||||
type NoAuth struct {
|
type NoAuth struct {
|
||||||
// contains filtered or unexported fields
|
// contains filtered or unexported fields
|
||||||
@@ -445,25 +522,25 @@ type NoAuth struct {
|
|||||||
```
|
```
|
||||||
NoAuth structure holds our credentials
|
NoAuth structure holds our credentials
|
||||||
|
|
||||||
#### <a name="NoAuth.Authorize">func</a> (\*NoAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=785:850#L53)
|
#### <a name="NoAuth.Authorize">func</a> (\*NoAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=887:960#L57)
|
||||||
``` go
|
``` go
|
||||||
func (n *NoAuth) Authorize(c *Client, method string, path string)
|
func (n *NoAuth) Authorize(req *http.Request, method string, path string)
|
||||||
```
|
```
|
||||||
Authorize the current request
|
Authorize the current request
|
||||||
|
|
||||||
#### <a name="NoAuth.Pass">func</a> (\*NoAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=703:733#L48)
|
#### <a name="NoAuth.Pass">func</a> (\*NoAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=805:835#L52)
|
||||||
``` go
|
``` go
|
||||||
func (n *NoAuth) Pass() string
|
func (n *NoAuth) Pass() string
|
||||||
```
|
```
|
||||||
Pass returns the current password
|
Pass returns the current password
|
||||||
|
|
||||||
#### <a name="NoAuth.Type">func</a> (\*NoAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=529:559#L38)
|
#### <a name="NoAuth.Type">func</a> (\*NoAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=631:661#L42)
|
||||||
``` go
|
``` go
|
||||||
func (n *NoAuth) Type() string
|
func (n *NoAuth) Type() string
|
||||||
```
|
```
|
||||||
Type identifies the authenticator
|
Type identifies the authenticator
|
||||||
|
|
||||||
#### <a name="NoAuth.User">func</a> (\*NoAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=615:645#L43)
|
#### <a name="NoAuth.User">func</a> (\*NoAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=717:747#L47)
|
||||||
``` go
|
``` go
|
||||||
func (n *NoAuth) User() string
|
func (n *NoAuth) User() string
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package gowebdav
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BasicAuth structure holds our credentials
|
// BasicAuth structure holds our credentials
|
||||||
@@ -26,8 +27,8 @@ func (b *BasicAuth) Pass() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Authorize the current request
|
// Authorize the current request
|
||||||
func (b *BasicAuth) Authorize(c *Client, method string, path string) {
|
func (b *BasicAuth) Authorize(req *http.Request, method string, path string) {
|
||||||
a := b.user + ":" + b.pw
|
a := b.user + ":" + b.pw
|
||||||
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
|
||||||
c.headers.Set("Authorization", auth)
|
req.Header.Set("Authorization", auth)
|
||||||
}
|
}
|
||||||
|
|||||||
85
client.go
85
client.go
@@ -3,12 +3,14 @@ package gowebdav
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
pathpkg "path"
|
pathpkg "path"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +18,10 @@ import (
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
root string
|
root string
|
||||||
headers http.Header
|
headers http.Header
|
||||||
|
interceptor func(method string, rq *http.Request)
|
||||||
c *http.Client
|
c *http.Client
|
||||||
|
|
||||||
|
authMutex sync.Mutex
|
||||||
auth Authenticator
|
auth Authenticator
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,7 +30,7 @@ type Authenticator interface {
|
|||||||
Type() string
|
Type() string
|
||||||
User() string
|
User() string
|
||||||
Pass() string
|
Pass() string
|
||||||
Authorize(*Client, string, string)
|
Authorize(*http.Request, string, string)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoAuth structure holds our credentials
|
// NoAuth structure holds our credentials
|
||||||
@@ -50,12 +55,12 @@ func (n *NoAuth) Pass() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Authorize the current request
|
// Authorize the current request
|
||||||
func (n *NoAuth) Authorize(c *Client, method string, path string) {
|
func (n *NoAuth) Authorize(req *http.Request, method string, path string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
return &Client{FixSlash(uri), make(http.Header), &http.Client{}, &NoAuth{user, pw}}
|
return &Client{FixSlash(uri), make(http.Header), nil, &http.Client{}, sync.Mutex{}, &NoAuth{user, pw}}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHeader lets us set arbitrary headers for a given client
|
// SetHeader lets us set arbitrary headers for a given client
|
||||||
@@ -63,6 +68,11 @@ func (c *Client) SetHeader(key, value string) {
|
|||||||
c.headers.Add(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)) {
|
||||||
|
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) {
|
||||||
c.c.Timeout = timeout
|
c.c.Timeout = timeout
|
||||||
@@ -85,18 +95,7 @@ func (c *Client) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
|
if rs.StatusCode != 200 {
|
||||||
if strings.Index(rs.Header.Get("Www-Authenticate"), "Digest") > -1 {
|
|
||||||
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
|
|
||||||
} else if strings.Index(rs.Header.Get("Www-Authenticate"), "Basic") > -1 {
|
|
||||||
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
|
|
||||||
} else {
|
|
||||||
return newPathError("Authorize", c.root, rs.StatusCode)
|
|
||||||
}
|
|
||||||
return c.Connect()
|
|
||||||
} else if rs.StatusCode == 401 {
|
|
||||||
return newPathError("Authorize", c.root, rs.StatusCode)
|
|
||||||
} else if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
|
|
||||||
return newPathError("Connect", c.root, rs.StatusCode)
|
return newPathError("Connect", c.root, rs.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +145,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
|
|||||||
|
|
||||||
if p := getProps(r, "200"); p != nil {
|
if p := getProps(r, "200"); p != nil {
|
||||||
f := new(File)
|
f := new(File)
|
||||||
if ps, err := url.QueryUnescape(r.Href); err == nil {
|
if ps, err := url.PathUnescape(r.Href); err == nil {
|
||||||
f.name = pathpkg.Base(ps)
|
f.name = pathpkg.Base(ps)
|
||||||
} else {
|
} else {
|
||||||
f.name = p.Name
|
f.name = p.Name
|
||||||
@@ -348,6 +347,43 @@ func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
|
|||||||
return nil, newPathError("ReadStream", path, rs.StatusCode)
|
return nil, newPathError("ReadStream", path, rs.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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`.
|
||||||
|
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 err != nil {
|
||||||
|
return nil, newPathErrorErr("ReadStreamRange", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs.StatusCode == http.StatusPartialContent {
|
||||||
|
// server supported partial content, return as-is.
|
||||||
|
return rs.Body, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// server returned success, but did not support partial content, so we have the whole
|
||||||
|
// stream in rs.Body
|
||||||
|
if rs.StatusCode == 200 {
|
||||||
|
// discard first 'offset' bytes.
|
||||||
|
if _, err := io.Copy(io.Discard, io.LimitReader(rs.Body, offset)); err != nil {
|
||||||
|
return nil, newPathErrorErr("ReadStreamRange", path, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// return a io.ReadCloser that is limited to `length` bytes.
|
||||||
|
return &limitedReadCloser{rs.Body, int(length)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rs.Body.Close()
|
||||||
|
return nil, newPathError("ReadStream", path, rs.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
// Write writes data to a given path
|
// Write writes data to a given path
|
||||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||||
s := c.put(path, bytes.NewReader(data))
|
s := c.put(path, bytes.NewReader(data))
|
||||||
@@ -357,23 +393,30 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
|||||||
return nil
|
return nil
|
||||||
|
|
||||||
case 409:
|
case 409:
|
||||||
if i := strings.LastIndex(path, "/"); i > -1 {
|
err := c.createParentCollection(path)
|
||||||
if err := c.MkdirAll(path[0:i+1], 0755); err == nil {
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
s = c.put(path, bytes.NewReader(data))
|
s = c.put(path, bytes.NewReader(data))
|
||||||
if s == 200 || s == 201 || s == 204 {
|
if s == 200 || s == 201 || s == 204 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return newPathError("Write", path, s)
|
return newPathError("Write", path, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteStream writes a stream
|
// 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) error {
|
||||||
// TODO check if parent collection exists
|
|
||||||
|
err := c.createParentCollection(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
s := c.put(path, stream)
|
s := c.put(path, stream)
|
||||||
|
|
||||||
switch s {
|
switch s {
|
||||||
case 200, 201, 204:
|
case 200, 201, 204:
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
103
cmd/gowebdav/README.md
Normal file
103
cmd/gowebdav/README.md
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
# Description
|
||||||
|
Command line tool for [gowebdav](https://github.com/studio-b12/gowebdav) library.
|
||||||
|
|
||||||
|
# Prerequisites
|
||||||
|
## Software
|
||||||
|
* **OS**: all, which are supported by `Golang`
|
||||||
|
* **Golang**: version 1.x
|
||||||
|
* **Git**: version 2.14.2 at higher (required to install via `go get`)
|
||||||
|
|
||||||
|
# Install
|
||||||
|
```sh
|
||||||
|
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
||||||
|
```
|
||||||
|
|
||||||
|
# Usage
|
||||||
|
It is recommended to set following environment variables to improve your experience with this tool:
|
||||||
|
* `ROOT` is an URL of target WebDAV server (e.g. `https://webdav.mydomain.me/user_root_folder`)
|
||||||
|
* `USER` is a login to connect to specified server (e.g. `user`)
|
||||||
|
* `PASSWORD` is a password to connect to specified server (e.g. `p@s$w0rD`)
|
||||||
|
|
||||||
|
In following examples we suppose that:
|
||||||
|
* environment variable `ROOT` is set to `https://webdav.mydomain.me/ufolder`
|
||||||
|
* environment variable `USER` is set to `user`
|
||||||
|
* environment variable `PASSWORD` is set `p@s$w0rD`
|
||||||
|
* folder `/ufolder/temp` exists on the server
|
||||||
|
* file `/ufolder/temp/file.txt` exists on the server
|
||||||
|
* file `/ufolder/temp/document.rtf` exists on the server
|
||||||
|
* file `/tmp/webdav/to_upload.txt` exists on the local machine
|
||||||
|
* folder `/tmp/webdav/` is used to download files from the server
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
#### Get content of specified folder
|
||||||
|
```sh
|
||||||
|
gowebdav -X LS temp
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Get info about file/folder
|
||||||
|
```sh
|
||||||
|
gowebdav -X STAT temp
|
||||||
|
gowebdav -X STAT temp/file.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Create folder on the remote server
|
||||||
|
```sh
|
||||||
|
gowebdav -X MKDIR temp2
|
||||||
|
gowebdav -X MKDIRALL all/folders/which-you-want/to_create
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Download file
|
||||||
|
```sh
|
||||||
|
gowebdav -X GET temp/document.rtf /tmp/webdav/document.rtf
|
||||||
|
```
|
||||||
|
|
||||||
|
You may do not specify target local path, in this case file will be downloaded to the current folder with the
|
||||||
|
|
||||||
|
#### Upload file
|
||||||
|
```sh
|
||||||
|
gowebdav -X PUT temp/uploaded.txt /tmp/webdav/to_upload.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Move file on the remote server
|
||||||
|
```sh
|
||||||
|
gowebdav -X MV temp/file.txt temp/moved_file.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Copy file to another location
|
||||||
|
```sh
|
||||||
|
gowebdav -X MV temp/file.txt temp/file-copy.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Delete file from the remote server
|
||||||
|
```sh
|
||||||
|
gowebdav -X DEL temp/file.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
# Wrapper script
|
||||||
|
|
||||||
|
You can create wrapper script for your server (via `$EDITOR ./dav && chmod a+x ./dav`) and add following content to it:
|
||||||
|
```sh
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
ROOT="https://my.dav.server/" \
|
||||||
|
USER="foo" \
|
||||||
|
PASSWORD="$(pass dav/foo@my.dav.server)" \
|
||||||
|
gowebdav $@
|
||||||
|
```
|
||||||
|
|
||||||
|
It allows you to use [pass](https://www.passwordstore.org/ "the standard unix password manager") or similar tools to retrieve the password.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Using the `dav` wrapper:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ ./dav -X LS /
|
||||||
|
|
||||||
|
$ echo hi dav! > hello && ./dav -X PUT /hello
|
||||||
|
$ ./dav -X STAT /hello
|
||||||
|
$ ./dav -X PUT /hello_dav hello
|
||||||
|
$ ./dav -X GET /hello_dav
|
||||||
|
$ ./dav -X GET /hello_dav hello.txt
|
||||||
|
```
|
||||||
@@ -9,13 +9,14 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
|
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
|
||||||
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
user := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
||||||
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
password := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
||||||
netrc := flag.String("netrc-file", filepath.Join(getHome(), ".netrc"), "read login from netrc file")
|
netrc := flag.String("netrc-file", filepath.Join(getHome(), ".netrc"), "read login from netrc file")
|
||||||
method := flag.String("X", "", `Method:
|
method := flag.String("X", "", `Method:
|
||||||
LS <PATH>
|
LS <PATH>
|
||||||
@@ -38,21 +39,18 @@ func main() {
|
|||||||
fail("Set WebDAV ROOT")
|
fail("Set WebDAV ROOT")
|
||||||
}
|
}
|
||||||
|
|
||||||
if l := len(flag.Args()); l == 0 || l > 2 {
|
if argsLength := len(flag.Args()); argsLength == 0 || argsLength > 2 {
|
||||||
fail("Unsupported arguments")
|
fail("Unsupported arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
if *pw == "" {
|
if *password == "" {
|
||||||
if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
|
if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
|
||||||
usr = &u
|
user = &u
|
||||||
pw = &p
|
password = &p
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c := d.NewClient(*root, *usr, *pw)
|
c := d.NewClient(*root, *user, *password)
|
||||||
if err := c.Connect(); err != nil {
|
|
||||||
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd := getCmd(*method)
|
cmd := getCmd(*method)
|
||||||
|
|
||||||
@@ -69,10 +67,21 @@ func fail(err interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getHome() string {
|
func getHome() string {
|
||||||
if u, e := user.Current(); e != nil {
|
u, e := user.Current()
|
||||||
|
if e != nil {
|
||||||
|
return os.Getenv("HOME")
|
||||||
|
}
|
||||||
|
|
||||||
|
if u != nil {
|
||||||
return u.HomeDir
|
return u.HomeDir
|
||||||
}
|
}
|
||||||
return os.Getenv("HOME")
|
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "windows":
|
||||||
|
return ""
|
||||||
|
default:
|
||||||
|
return "~/"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
|
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
|
||||||
@@ -214,8 +223,12 @@ func writeFile(path string, bytes []byte, mode os.FileMode) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getStream(pathOrString string) (io.ReadCloser, error) {
|
func getStream(pathOrString string) (io.ReadCloser, error) {
|
||||||
|
|
||||||
fi, err := os.Stat(pathOrString)
|
fi, err := os.Stat(pathOrString)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if fi.IsDir() {
|
if fi.IsDir() {
|
||||||
return nil, &os.PathError{
|
return nil, &os.PathError{
|
||||||
Op: "Open",
|
Op: "Open",
|
||||||
@@ -223,23 +236,15 @@ func getStream(pathOrString string) (io.ReadCloser, error) {
|
|||||||
Err: errors.New("Path: '" + pathOrString + "' is a directory"),
|
Err: errors.New("Path: '" + pathOrString + "' is a directory"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Open(pathOrString)
|
f, err := os.Open(pathOrString)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, &os.PathError{
|
return nil, &os.PathError{
|
||||||
Op: "Open",
|
Op: "Open",
|
||||||
Path: pathOrString,
|
Path: pathOrString,
|
||||||
Err: err,
|
Err: err,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nopCloser{strings.NewReader(pathOrString)}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type nopCloser struct {
|
|
||||||
io.Reader
|
|
||||||
}
|
|
||||||
|
|
||||||
func (nopCloser) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,23 +33,26 @@ func (d *DigestAuth) Pass() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Authorize the current request
|
// Authorize the current request
|
||||||
func (d *DigestAuth) Authorize(c *Client, method string, path string) {
|
func (d *DigestAuth) Authorize(req *http.Request, method string, path string) {
|
||||||
d.digestParts["uri"] = path
|
d.digestParts["uri"] = path
|
||||||
d.digestParts["method"] = method
|
d.digestParts["method"] = method
|
||||||
d.digestParts["username"] = d.user
|
d.digestParts["username"] = d.user
|
||||||
d.digestParts["password"] = d.pw
|
d.digestParts["password"] = d.pw
|
||||||
c.headers.Set("Authorization", getDigestAuthorization(d.digestParts))
|
req.Header.Set("Authorization", getDigestAuthorization(d.digestParts))
|
||||||
}
|
}
|
||||||
|
|
||||||
func digestParts(resp *http.Response) map[string]string {
|
func digestParts(resp *http.Response) map[string]string {
|
||||||
result := map[string]string{}
|
result := map[string]string{}
|
||||||
if len(resp.Header["Www-Authenticate"]) > 0 {
|
if len(resp.Header["Www-Authenticate"]) > 0 {
|
||||||
wantedHeaders := []string{"nonce", "realm", "qop", "opaque"}
|
wantedHeaders := []string{"nonce", "realm", "qop", "opaque", "algorithm", "entityBody"}
|
||||||
responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
|
responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
|
||||||
for _, r := range responseHeaders {
|
for _, r := range responseHeaders {
|
||||||
for _, w := range wantedHeaders {
|
for _, w := range wantedHeaders {
|
||||||
if strings.Contains(r, w) {
|
if strings.Contains(r, w) {
|
||||||
result[w] = strings.Split(r, `"`)[1]
|
result[w] = strings.Trim(
|
||||||
|
strings.SplitN(r, `=`, 2)[1],
|
||||||
|
`"`,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,12 +75,72 @@ func getCnonce() string {
|
|||||||
func getDigestAuthorization(digestParts map[string]string) string {
|
func getDigestAuthorization(digestParts map[string]string) string {
|
||||||
d := digestParts
|
d := digestParts
|
||||||
// These are the correct ha1 and ha2 for qop=auth. We should probably check for other types of qop.
|
// These are the correct ha1 and ha2 for qop=auth. We should probably check for other types of qop.
|
||||||
ha1 := getMD5(d["username"] + ":" + d["realm"] + ":" + d["password"])
|
|
||||||
ha2 := getMD5(d["method"] + ":" + d["uri"])
|
var (
|
||||||
nonceCount := 00000001
|
ha1 string
|
||||||
cnonce := getCnonce()
|
ha2 string
|
||||||
response := getMD5(fmt.Sprintf("%s:%s:%v:%s:%s:%s", ha1, d["nonce"], nonceCount, cnonce, d["qop"], ha2))
|
nonceCount = 00000001
|
||||||
authorization := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", cnonce="%s", nc="%v", qop="%s", response="%s", opaque="%s"`,
|
cnonce = getCnonce()
|
||||||
d["username"], d["realm"], d["nonce"], d["uri"], cnonce, nonceCount, d["qop"], response, d["opaque"])
|
response string
|
||||||
|
)
|
||||||
|
|
||||||
|
// 'ha1' value depends on value of "algorithm" field
|
||||||
|
switch d["algorithm"] {
|
||||||
|
case "MD5", "":
|
||||||
|
ha1 = getMD5(d["username"] + ":" + d["realm"] + ":" + d["password"])
|
||||||
|
case "MD5-sess":
|
||||||
|
ha1 = getMD5(
|
||||||
|
fmt.Sprintf("%s:%v:%s",
|
||||||
|
getMD5(d["username"]+":"+d["realm"]+":"+d["password"]),
|
||||||
|
nonceCount,
|
||||||
|
cnonce,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 'ha2' value depends on value of "qop" field
|
||||||
|
switch d["qop"] {
|
||||||
|
case "auth", "":
|
||||||
|
ha2 = getMD5(d["method"] + ":" + d["uri"])
|
||||||
|
case "auth-int":
|
||||||
|
if d["entityBody"] != "" {
|
||||||
|
ha2 = getMD5(d["method"] + ":" + d["uri"] + ":" + getMD5(d["entityBody"]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 'response' value depends on value of "qop" field
|
||||||
|
switch d["qop"] {
|
||||||
|
case "":
|
||||||
|
response = getMD5(
|
||||||
|
fmt.Sprintf("%s:%s:%s",
|
||||||
|
ha1,
|
||||||
|
d["nonce"],
|
||||||
|
ha2,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
case "auth", "auth-int":
|
||||||
|
response = getMD5(
|
||||||
|
fmt.Sprintf("%s:%s:%v:%s:%s:%s",
|
||||||
|
ha1,
|
||||||
|
d["nonce"],
|
||||||
|
nonceCount,
|
||||||
|
cnonce,
|
||||||
|
d["qop"],
|
||||||
|
ha2,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
authorization := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", nc=%v, cnonce="%s", response="%s"`,
|
||||||
|
d["username"], d["realm"], d["nonce"], d["uri"], nonceCount, cnonce, response)
|
||||||
|
|
||||||
|
if d["qop"] != "" {
|
||||||
|
authorization += fmt.Sprintf(`, qop=%s`, d["qop"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if d["opaque"] != "" {
|
||||||
|
authorization += fmt.Sprintf(`, opaque="%s"`, d["opaque"])
|
||||||
|
}
|
||||||
|
|
||||||
return authorization
|
return authorization
|
||||||
}
|
}
|
||||||
|
|||||||
5
file.go
5
file.go
@@ -17,6 +17,11 @@ type File struct {
|
|||||||
isdir bool
|
isdir bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path returns the full path of a file
|
||||||
|
func (f File) Path() string {
|
||||||
|
return f.path
|
||||||
|
}
|
||||||
|
|
||||||
// Name returns the name of a file
|
// Name returns the name of a file
|
||||||
func (f File) Name() string {
|
func (f File) Name() string {
|
||||||
return f.name
|
return f.name
|
||||||
|
|||||||
104
requests.go
104
requests.go
@@ -1,14 +1,49 @@
|
|||||||
package gowebdav
|
package gowebdav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
|
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
|
||||||
r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), body)
|
var r *http.Request
|
||||||
|
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
|
||||||
|
// cursor to the start.
|
||||||
|
// Otherwise, copy the stream into a buffer while uploading
|
||||||
|
// and use the buffers content on retry.
|
||||||
|
if sk, ok := body.(io.Seeker); ok {
|
||||||
|
if _, err = sk.Seek(0, io.SeekStart); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
retryBuf = body
|
||||||
|
} else {
|
||||||
|
buff := &bytes.Buffer{}
|
||||||
|
retryBuf = buff
|
||||||
|
body = io.TeeReader(body, buff)
|
||||||
|
}
|
||||||
|
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), body)
|
||||||
|
} else {
|
||||||
|
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -19,21 +54,58 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.auth.Authorize(c, method, path)
|
// make sure we read 'c.auth' only once since it will be substituted below
|
||||||
|
// and that is unsafe to do when multiple goroutines are running at the same time.
|
||||||
|
c.authMutex.Lock()
|
||||||
|
auth := c.auth
|
||||||
|
c.authMutex.Unlock()
|
||||||
|
|
||||||
|
auth.Authorize(r, method, path)
|
||||||
|
|
||||||
if intercept != nil {
|
if intercept != nil {
|
||||||
intercept(r)
|
intercept(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.c.Do(r)
|
if c.interceptor != nil {
|
||||||
|
c.interceptor(method, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
rs, err := c.c.Do(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if rs.StatusCode == 401 && auth.Type() == "NoAuth" {
|
||||||
|
wwwAuthenticateHeader := strings.ToLower(rs.Header.Get("Www-Authenticate"))
|
||||||
|
|
||||||
|
if strings.Index(wwwAuthenticateHeader, "digest") > -1 {
|
||||||
|
c.authMutex.Lock()
|
||||||
|
c.auth = &DigestAuth{auth.User(), auth.Pass(), digestParts(rs)}
|
||||||
|
c.authMutex.Unlock()
|
||||||
|
} else if strings.Index(wwwAuthenticateHeader, "basic") > -1 {
|
||||||
|
c.authMutex.Lock()
|
||||||
|
c.auth = &BasicAuth{auth.User(), auth.Pass()}
|
||||||
|
c.authMutex.Unlock()
|
||||||
|
} else {
|
||||||
|
return rs, newPathError("Authorize", c.root, rs.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// retryBuf will be nil if body was nil initially so no check
|
||||||
|
// for body == nil is required here.
|
||||||
|
return c.req(method, path, retryBuf, intercept)
|
||||||
|
} else if rs.StatusCode == 401 {
|
||||||
|
return rs, newPathError("Authorize", c.root, rs.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
return rs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) mkcol(path string) int {
|
func (c *Client) mkcol(path string) int {
|
||||||
rs, err := c.req("MKCOL", path, nil, nil)
|
rs, err := c.req("MKCOL", path, nil, nil)
|
||||||
defer rs.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 400
|
return 400
|
||||||
}
|
}
|
||||||
|
defer rs.Body.Close()
|
||||||
|
|
||||||
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
||||||
return 201
|
return 201
|
||||||
@@ -55,16 +127,16 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
|
|||||||
} else {
|
} else {
|
||||||
rq.Header.Add("Depth", "1")
|
rq.Header.Add("Depth", "1")
|
||||||
}
|
}
|
||||||
rq.Header.Add("Content-Type", "text/xml;charset=UTF-8")
|
rq.Header.Add("Content-Type", "application/xml;charset=UTF-8")
|
||||||
rq.Header.Add("Accept", "application/xml,text/xml")
|
rq.Header.Add("Accept", "application/xml,text/xml")
|
||||||
rq.Header.Add("Accept-Charset", "utf-8")
|
rq.Header.Add("Accept-Charset", "utf-8")
|
||||||
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
|
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
|
||||||
rq.Header.Add("Accept-Encoding", "")
|
rq.Header.Add("Accept-Encoding", "")
|
||||||
})
|
})
|
||||||
defer rs.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer rs.Body.Close()
|
||||||
|
|
||||||
if rs.StatusCode != 207 {
|
if rs.StatusCode != 207 {
|
||||||
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
|
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
|
||||||
@@ -90,7 +162,9 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
|
|||||||
|
|
||||||
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
||||||
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||||
|
if data != nil {
|
||||||
defer data.Close()
|
defer data.Close()
|
||||||
|
}
|
||||||
|
|
||||||
switch s {
|
switch s {
|
||||||
case 201, 204:
|
case 201, 204:
|
||||||
@@ -101,7 +175,12 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
|||||||
log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data)))
|
log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data)))
|
||||||
|
|
||||||
case 409:
|
case 409:
|
||||||
// TODO create dst path
|
err := c.createParentCollection(newpath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.copymove(method, oldpath, newpath, overwrite)
|
||||||
}
|
}
|
||||||
|
|
||||||
return newPathError(method, oldpath, s)
|
return newPathError(method, oldpath, s)
|
||||||
@@ -109,10 +188,19 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
|||||||
|
|
||||||
func (c *Client) put(path string, stream io.Reader) int {
|
func (c *Client) put(path string, stream io.Reader) int {
|
||||||
rs, err := c.req("PUT", path, stream, nil)
|
rs, err := c.req("PUT", path, stream, nil)
|
||||||
defer rs.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 400
|
return 400
|
||||||
}
|
}
|
||||||
|
defer rs.Body.Close()
|
||||||
|
|
||||||
return rs.StatusCode
|
return rs.StatusCode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) createParentCollection(itemPath string) (err error) {
|
||||||
|
parentPath := path.Dir(itemPath)
|
||||||
|
if parentPath == "." || parentPath == "/" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.MkdirAll(parentPath, 0755)
|
||||||
|
}
|
||||||
|
|||||||
41
utils.go
41
utils.go
@@ -32,7 +32,7 @@ func newPathErrorErr(op string, path string, err error) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PathEscape escapes all segemnts of a given path
|
// PathEscape escapes all segments of a given path
|
||||||
func PathEscape(path string) string {
|
func PathEscape(path string) string {
|
||||||
s := strings.Split(path, "/")
|
s := strings.Split(path, "/")
|
||||||
for i, e := range s {
|
for i, e := range s {
|
||||||
@@ -51,9 +51,10 @@ func FixSlash(s string) string {
|
|||||||
|
|
||||||
// FixSlashes appends and prepends a / if they are missing
|
// FixSlashes appends and prepends a / if they are missing
|
||||||
func FixSlashes(s string) string {
|
func FixSlashes(s string) string {
|
||||||
if s[0] != '/' {
|
if !strings.HasPrefix(s, "/") {
|
||||||
s = "/" + s
|
s = "/" + s
|
||||||
}
|
}
|
||||||
|
|
||||||
return FixSlash(s)
|
return FixSlash(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,3 +108,39 @@ func parseXML(data io.Reader, resp interface{}, parse func(resp interface{}) err
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// limitedReadCloser wraps a io.ReadCloser and limits the number of bytes that can be read from it.
|
||||||
|
type limitedReadCloser struct {
|
||||||
|
rc io.ReadCloser
|
||||||
|
remaining int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *limitedReadCloser) Read(buf []byte) (int, error) {
|
||||||
|
if l.remaining <= 0 {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf) > l.remaining {
|
||||||
|
buf = buf[0:l.remaining]
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := l.rc.Read(buf)
|
||||||
|
l.remaining -= n
|
||||||
|
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -43,3 +43,25 @@ func TestEscapeURL(t *testing.T) {
|
|||||||
t.Error("expected: " + ex + " got: " + u.String())
|
t.Error("expected: " + ex + " got: " + u.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFixSlashes(t *testing.T) {
|
||||||
|
expected := "/"
|
||||||
|
|
||||||
|
if got := FixSlashes(""); got != expected {
|
||||||
|
t.Errorf("expected: %q, got: %q", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
expected = "/path/"
|
||||||
|
|
||||||
|
if got := FixSlashes("path"); got != expected {
|
||||||
|
t.Errorf("expected: %q, got: %q", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got := FixSlashes("/path"); got != expected {
|
||||||
|
t.Errorf("expected: %q, got: %q", expected, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got := FixSlashes("path/"); got != expected {
|
||||||
|
t.Errorf("expected: %q, got: %q", expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user