Compare commits
78 Commits
5
...
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 | ||
|
|
a33240e4ab | ||
|
|
fbcb29d33e | ||
|
|
6d8c168f72 | ||
|
|
5bedad6f1e | ||
|
|
31e0b57e53 | ||
|
|
ba3a71318b | ||
|
|
fa51555f16 | ||
|
|
e0b778960b | ||
|
|
a98da9745e | ||
|
|
1786d37966 | ||
|
|
32d5561fb6 |
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
|
||||
/bin
|
||||
/pkg
|
||||
/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:
|
||||
- "1.x"
|
||||
|
||||
install:
|
||||
- go get ./...
|
||||
|
||||
script:
|
||||
- go test -v --short ./...
|
||||
11
Makefile
11
Makefile
@@ -9,7 +9,7 @@ ${BIN}: ${SRC}
|
||||
go build -o $@ ./cmd/gowebdav
|
||||
|
||||
test:
|
||||
go test -v ./...
|
||||
go test -v --short ./...
|
||||
|
||||
api:
|
||||
@sed '/^## API$$/,$$d' -i README.md
|
||||
@@ -20,7 +20,14 @@ api:
|
||||
sed 's/\/src\/target\//https:\/\/github.com\/studio-b12\/gowebdav\/blob\/master\//g' |\
|
||||
sed 's/^#/##/g' >> README.md
|
||||
|
||||
check:
|
||||
gofmt -w -s $(SRC)
|
||||
@echo
|
||||
gocyclo -over 15 .
|
||||
@echo
|
||||
golint ./...
|
||||
|
||||
clean:
|
||||
@rm -f ${BIN}
|
||||
|
||||
.PHONY: all cmd clean test api
|
||||
.PHONY: all cmd clean test api check
|
||||
|
||||
368
README.md
368
README.md
@@ -1,60 +1,150 @@
|
||||
# GoWebDAV
|
||||
|
||||
[](https://travis-ci.org/studio-b12/gowebdav)
|
||||
[](https://godoc.org/github.com/studio-b12/gowebdav)
|
||||
[](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
|
||||
|
||||
A WebDAV client and library for golang.
|
||||
A golang WebDAV client library.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
||||
```
|
||||
## 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)
|
||||
* [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
|
||||
|
||||
```sh
|
||||
$ gowebdav --help
|
||||
Usage of gowebdav
|
||||
-X string
|
||||
Method:
|
||||
LS <PATH>
|
||||
STAT <PATH>
|
||||
First of all you should create `Client` instance using `NewClient()` function:
|
||||
|
||||
MKDIR <PATH>
|
||||
MKDIRALL <PATH>
|
||||
```go
|
||||
root := "https://webdav.mydomain.me"
|
||||
user := "user"
|
||||
password := "password"
|
||||
|
||||
GET <PATH> <FILE>
|
||||
PUT <PATH> <FILE>
|
||||
|
||||
MV <OLD> <NEW>
|
||||
CP <OLD> <NEW>
|
||||
|
||||
DEL <PATH>
|
||||
|
||||
-pw string
|
||||
Password [ENV.PASSWORD]
|
||||
-root string
|
||||
WebDAV Endpoint [ENV.ROOT]
|
||||
-user string
|
||||
User [ENV.USER]
|
||||
c := gowebdav.NewClient(root, user, password)
|
||||
```
|
||||
|
||||
*Example*
|
||||
After you can use this `Client` to perform actions, described below.
|
||||
|
||||
```sh
|
||||
ROOT="https://webdav.server/" \
|
||||
USER="foo" \
|
||||
PASSWORD="bar" \
|
||||
./gowebdav -X LS /
|
||||
**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!
|
||||
|
||||
### Create path on a WebDAV server
|
||||
```go
|
||||
err := c.Mkdir("folder", 0644)
|
||||
```
|
||||
In case you want to create several folders you can use `c.MkdirAll()`:
|
||||
```go
|
||||
err := c.MkdirAll("folder/subfolder/subfolder2", 0644)
|
||||
```
|
||||
|
||||
## LINKS
|
||||
### Get files list
|
||||
```go
|
||||
files, _ := c.ReadDir("folder/subfolder")
|
||||
for _, file := range files {
|
||||
//notice that [file] has os.FileInfo type
|
||||
fmt.Println(file.Name())
|
||||
}
|
||||
```
|
||||
|
||||
* [RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV](http://www.faqs.org/rfcs/rfc2518.html "RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV")
|
||||
### Download file to byte array
|
||||
```go
|
||||
webdavFilePath := "folder/subfolder/file.txt"
|
||||
localFilePath := "/tmp/webdav/file.txt"
|
||||
|
||||
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")
|
||||
* [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
|
||||
|
||||
`import "github.com/studio-b12/gowebdav"`
|
||||
@@ -65,14 +155,22 @@ PASSWORD="bar" \
|
||||
* [Subdirectories](#pkg-subdirectories)
|
||||
|
||||
### <a name="pkg-overview">Overview</a>
|
||||
Package gowebdav A golang WebDAV library
|
||||
Package gowebdav is a WebDAV client library with a command line tool
|
||||
included.
|
||||
|
||||
### <a name="pkg-index">Index</a>
|
||||
* [func FixSlash(s string) string](#FixSlash)
|
||||
* [func FixSlashes(s string) string](#FixSlashes)
|
||||
* [func Join(path0 string, path1 string) string](#Join)
|
||||
* [func PathEscape(path string) string](#PathEscape)
|
||||
* [func ReadConfig(uri, netrc string) (string, string)](#ReadConfig)
|
||||
* [func String(r io.Reader) string](#String)
|
||||
* [type Authenticator](#Authenticator)
|
||||
* [type BasicAuth](#BasicAuth)
|
||||
* [func (b *BasicAuth) Authorize(req *http.Request, method string, path string)](#BasicAuth.Authorize)
|
||||
* [func (b *BasicAuth) Pass() string](#BasicAuth.Pass)
|
||||
* [func (b *BasicAuth) Type() string](#BasicAuth.Type)
|
||||
* [func (b *BasicAuth) User() string](#BasicAuth.User)
|
||||
* [type Client](#Client)
|
||||
* [func NewClient(uri, user, pw string) *Client](#NewClient)
|
||||
* [func (c *Client) Connect() error](#Client.Connect)
|
||||
@@ -86,11 +184,17 @@ Package gowebdav A golang WebDAV library
|
||||
* [func (c *Client) RemoveAll(path string) error](#Client.RemoveAll)
|
||||
* [func (c *Client) Rename(oldpath, newpath string, overwrite bool) error](#Client.Rename)
|
||||
* [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) 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)
|
||||
* [type DigestAuth](#DigestAuth)
|
||||
* [func (d *DigestAuth) Authorize(req *http.Request, method string, path string)](#DigestAuth.Authorize)
|
||||
* [func (d *DigestAuth) Pass() string](#DigestAuth.Pass)
|
||||
* [func (d *DigestAuth) Type() string](#DigestAuth.Type)
|
||||
* [func (d *DigestAuth) User() string](#DigestAuth.User)
|
||||
* [type File](#File)
|
||||
* [func (f File) ContentType() string](#File.ContentType)
|
||||
* [func (f File) ETag() string](#File.ETag)
|
||||
@@ -98,15 +202,21 @@ Package gowebdav A golang WebDAV library
|
||||
* [func (f File) ModTime() time.Time](#File.ModTime)
|
||||
* [func (f File) Mode() os.FileMode](#File.Mode)
|
||||
* [func (f File) Name() string](#File.Name)
|
||||
* [func (f File) Path() string](#File.Path)
|
||||
* [func (f File) Size() int64](#File.Size)
|
||||
* [func (f File) String() string](#File.String)
|
||||
* [func (f File) Sys() interface{}](#File.Sys)
|
||||
* [type NoAuth](#NoAuth)
|
||||
* [func (n *NoAuth) Authorize(req *http.Request, method string, path string)](#NoAuth.Authorize)
|
||||
* [func (n *NoAuth) Pass() string](#NoAuth.Pass)
|
||||
* [func (n *NoAuth) Type() string](#NoAuth.Type)
|
||||
* [func (n *NoAuth) User() string](#NoAuth.User)
|
||||
|
||||
##### <a name="pkg-examples">Examples</a>
|
||||
* [PathEscape](#example_PathEscape)
|
||||
|
||||
##### <a name="pkg-files">Package files</a>
|
||||
[client.go](https://github.com/studio-b12/gowebdav/blob/master/client.go) [file.go](https://github.com/studio-b12/gowebdav/blob/master/file.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) [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)
|
||||
``` go
|
||||
@@ -132,13 +242,63 @@ func PathEscape(path string) string
|
||||
```
|
||||
PathEscape escapes all segemnts 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
|
||||
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)
|
||||
``` go
|
||||
func String(r io.Reader) string
|
||||
```
|
||||
String pulls a string out of our io.Reader
|
||||
|
||||
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=220:301#L18)
|
||||
### <a name="Authenticator">type</a> [Authenticator](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=381:500#L28)
|
||||
``` go
|
||||
type Authenticator interface {
|
||||
Type() string
|
||||
User() string
|
||||
Pass() string
|
||||
Authorize(*http.Request, string, string)
|
||||
}
|
||||
```
|
||||
Authenticator stub
|
||||
|
||||
### <a name="BasicAuth">type</a> [BasicAuth](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=106:157#L9)
|
||||
``` go
|
||||
type BasicAuth struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
```
|
||||
BasicAuth structure holds our credentials
|
||||
|
||||
#### <a name="BasicAuth.Authorize">func</a> (\*BasicAuth) [Authorize](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=473:549#L30)
|
||||
``` go
|
||||
func (b *BasicAuth) Authorize(req *http.Request, method string, path string)
|
||||
```
|
||||
Authorize the current request
|
||||
|
||||
#### <a name="BasicAuth.Pass">func</a> (\*BasicAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=388:421#L25)
|
||||
``` go
|
||||
func (b *BasicAuth) Pass() string
|
||||
```
|
||||
Pass holds the BasicAuth password
|
||||
|
||||
#### <a name="BasicAuth.Type">func</a> (\*BasicAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=201:234#L15)
|
||||
``` go
|
||||
func (b *BasicAuth) Type() string
|
||||
```
|
||||
Type identifies the BasicAuthenticator
|
||||
|
||||
#### <a name="BasicAuth.User">func</a> (\*BasicAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=297:330#L20)
|
||||
``` go
|
||||
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)
|
||||
``` go
|
||||
type Client struct {
|
||||
// contains filtered or unexported fields
|
||||
@@ -146,108 +306,146 @@ type Client struct {
|
||||
```
|
||||
Client defines our structure
|
||||
|
||||
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=349:393#L25)
|
||||
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1012:1056#L61)
|
||||
``` 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=1138:1170#L55)
|
||||
#### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1836:1868#L86)
|
||||
``` 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=6060:6128#L281)
|
||||
#### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6695:6763#L312)
|
||||
``` 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=5151:5207#L240)
|
||||
#### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5786:5842#L271)
|
||||
``` go
|
||||
func (c *Client) Mkdir(path string, _ os.FileMode) error
|
||||
```
|
||||
Mkdir makes a directory
|
||||
|
||||
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5386:5445#L251)
|
||||
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6021:6080#L282)
|
||||
``` go
|
||||
func (c *Client) MkdirAll(path string, _ os.FileMode) 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=6234:6284#L286)
|
||||
#### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6869:6919#L317)
|
||||
``` 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=2226:2286#L98)
|
||||
#### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2862:2922#L129)
|
||||
``` 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=6595:6658#L304)
|
||||
#### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7230:7293#L335)
|
||||
``` 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=4657:4699#L217)
|
||||
#### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5292:5334#L248)
|
||||
``` 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=4765:4810#L222)
|
||||
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5400:5445#L253)
|
||||
``` 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=5894:5964#L276)
|
||||
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6529:6599#L307)
|
||||
``` 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=721:766#L40)
|
||||
#### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1228:1273#L66)
|
||||
``` go
|
||||
func (c *Client) SetHeader(key, value string)
|
||||
```
|
||||
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=866:916#L45)
|
||||
#### <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
|
||||
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=1009:1067#L50)
|
||||
#### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1707:1765#L81)
|
||||
``` 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=3613:3668#L165)
|
||||
#### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4248:4303#L196)
|
||||
``` 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=6949:7018#L319)
|
||||
#### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7584:7653#L350)
|
||||
``` go
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) 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=7420:7500#L341)
|
||||
#### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=8009:8089#L373)
|
||||
``` go
|
||||
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error
|
||||
```
|
||||
WriteStream writes a stream
|
||||
|
||||
### <a name="DigestAuth">type</a> [DigestAuth](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=157:254#L14)
|
||||
``` go
|
||||
type DigestAuth struct {
|
||||
// contains filtered or unexported fields
|
||||
}
|
||||
```
|
||||
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:654#L36)
|
||||
``` go
|
||||
func (d *DigestAuth) Authorize(req *http.Request, method string, path string)
|
||||
```
|
||||
Authorize the current request
|
||||
|
||||
#### <a name="DigestAuth.Pass">func</a> (\*DigestAuth) [Pass](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=491:525#L31)
|
||||
``` go
|
||||
func (d *DigestAuth) Pass() string
|
||||
```
|
||||
Pass holds the DigestAuth password
|
||||
|
||||
#### <a name="DigestAuth.Type">func</a> (\*DigestAuth) [Type](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=299:333#L21)
|
||||
``` go
|
||||
func (d *DigestAuth) Type() string
|
||||
```
|
||||
Type identifies the DigestAuthenticator
|
||||
|
||||
#### <a name="DigestAuth.User">func</a> (\*DigestAuth) [User](https://github.com/studio-b12/gowebdav/blob/master/digestAuth.go?s=398:432#L26)
|
||||
``` go
|
||||
func (d *DigestAuth) User() string
|
||||
```
|
||||
User holds the DigestAuth username
|
||||
|
||||
### <a name="File">type</a> [File](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=93:253#L10)
|
||||
``` go
|
||||
type File struct {
|
||||
@@ -256,59 +454,97 @@ type File struct {
|
||||
```
|
||||
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
|
||||
func (f File) ContentType() string
|
||||
```
|
||||
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
|
||||
func (f File) ETag() string
|
||||
```
|
||||
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
|
||||
func (f File) IsDir() bool
|
||||
```
|
||||
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
|
||||
func (f File) ModTime() time.Time
|
||||
```
|
||||
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
|
||||
func (f File) Mode() os.FileMode
|
||||
```
|
||||
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
|
||||
func (f File) Name() string
|
||||
```
|
||||
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
|
||||
func (f File) Size() int64
|
||||
```
|
||||
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
|
||||
func (f File) String() string
|
||||
```
|
||||
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
|
||||
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)
|
||||
``` 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)
|
||||
``` 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)
|
||||
``` 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)
|
||||
``` 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)
|
||||
``` go
|
||||
func (n *NoAuth) User() string
|
||||
```
|
||||
User returns the current user
|
||||
|
||||
- - -
|
||||
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
|
||||
|
||||
34
basicAuth.go
Normal file
34
basicAuth.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// BasicAuth structure holds our credentials
|
||||
type BasicAuth struct {
|
||||
user string
|
||||
pw string
|
||||
}
|
||||
|
||||
// Type identifies the BasicAuthenticator
|
||||
func (b *BasicAuth) Type() string {
|
||||
return "BasicAuth"
|
||||
}
|
||||
|
||||
// User holds the BasicAuth username
|
||||
func (b *BasicAuth) User() string {
|
||||
return b.user
|
||||
}
|
||||
|
||||
// Pass holds the BasicAuth password
|
||||
func (b *BasicAuth) Pass() string {
|
||||
return b.pw
|
||||
}
|
||||
|
||||
// Authorize the current request
|
||||
func (b *BasicAuth) Authorize(req *http.Request, method string, path string) {
|
||||
a := b.user + ":" + b.pw
|
||||
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
|
||||
req.Header.Set("Authorization", auth)
|
||||
}
|
||||
116
client.go
116
client.go
@@ -1,16 +1,16 @@
|
||||
// Package gowebdav A golang WebDAV library
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
pathpkg "path"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,22 +18,49 @@ import (
|
||||
type Client struct {
|
||||
root string
|
||||
headers http.Header
|
||||
interceptor func(method string, rq *http.Request)
|
||||
c *http.Client
|
||||
|
||||
authMutex sync.Mutex
|
||||
auth Authenticator
|
||||
}
|
||||
|
||||
// Authenticator stub
|
||||
type Authenticator interface {
|
||||
Type() string
|
||||
User() string
|
||||
Pass() string
|
||||
Authorize(*http.Request, string, string)
|
||||
}
|
||||
|
||||
// NoAuth structure holds our credentials
|
||||
type NoAuth struct {
|
||||
user string
|
||||
pw string
|
||||
}
|
||||
|
||||
// Type identifies the authenticator
|
||||
func (n *NoAuth) Type() string {
|
||||
return "NoAuth"
|
||||
}
|
||||
|
||||
// User returns the current user
|
||||
func (n *NoAuth) User() string {
|
||||
return n.user
|
||||
}
|
||||
|
||||
// Pass returns the current password
|
||||
func (n *NoAuth) Pass() string {
|
||||
return n.pw
|
||||
}
|
||||
|
||||
// Authorize the current request
|
||||
func (n *NoAuth) Authorize(req *http.Request, method string, path string) {
|
||||
}
|
||||
|
||||
// NewClient creates a new instance of client
|
||||
func NewClient(uri, user, pw string) *Client {
|
||||
c := &Client{uri, make(http.Header), &http.Client{}}
|
||||
|
||||
if len(user) > 0 && len(pw) > 0 {
|
||||
a := user + ":" + pw
|
||||
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
|
||||
c.headers.Add("Authorization", auth)
|
||||
}
|
||||
|
||||
c.root = FixSlash(c.root)
|
||||
|
||||
return c
|
||||
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
|
||||
@@ -41,6 +68,11 @@ func (c *Client) SetHeader(key, value string) {
|
||||
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
|
||||
func (c *Client) SetTimeout(timeout time.Duration) {
|
||||
c.c.Timeout = timeout
|
||||
@@ -63,7 +95,7 @@ func (c *Client) Connect() error {
|
||||
return err
|
||||
}
|
||||
|
||||
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
|
||||
if rs.StatusCode != 200 {
|
||||
return newPathError("Connect", c.root, rs.StatusCode)
|
||||
}
|
||||
|
||||
@@ -113,7 +145,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
|
||||
|
||||
if p := getProps(r, "200"); p != nil {
|
||||
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)
|
||||
} else {
|
||||
f.name = p.Name
|
||||
@@ -315,6 +347,43 @@ func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
|
||||
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
|
||||
func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||
s := c.put(path, bytes.NewReader(data))
|
||||
@@ -324,23 +393,30 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
|
||||
return nil
|
||||
|
||||
case 409:
|
||||
if i := strings.LastIndex(path, "/"); i > -1 {
|
||||
if err := c.MkdirAll(path[0:i+1], 0755); err == nil {
|
||||
err := c.createParentCollection(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s = c.put(path, bytes.NewReader(data))
|
||||
if s == 200 || s == 201 || s == 204 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newPathError("Write", path, s)
|
||||
}
|
||||
|
||||
// WriteStream writes a stream
|
||||
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)
|
||||
|
||||
switch s {
|
||||
case 200, 201, 204:
|
||||
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
|
||||
```
|
||||
@@ -7,22 +7,26 @@ import (
|
||||
d "github.com/studio-b12/gowebdav"
|
||||
"io"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
|
||||
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
||||
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
||||
m := flag.String("X", "", `Method:
|
||||
user := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
||||
password := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
||||
netrc := flag.String("netrc-file", filepath.Join(getHome(), ".netrc"), "read login from netrc file")
|
||||
method := flag.String("X", "", `Method:
|
||||
LS <PATH>
|
||||
STAT <PATH>
|
||||
|
||||
MKDIR <PATH>
|
||||
MKDIRALL <PATH>
|
||||
|
||||
GET <PATH> <FILE>
|
||||
PUT <PATH> <FILE>
|
||||
GET <PATH> [<FILE>]
|
||||
PUT <PATH> [<FILE>]
|
||||
|
||||
MV <OLD> <NEW>
|
||||
CP <OLD> <NEW>
|
||||
@@ -35,24 +39,22 @@ func main() {
|
||||
fail("Set WebDAV ROOT")
|
||||
}
|
||||
|
||||
var path0, path1 string
|
||||
switch len(flag.Args()) {
|
||||
case 1:
|
||||
path0 = flag.Args()[0]
|
||||
case 2:
|
||||
path1 = flag.Args()[1]
|
||||
default:
|
||||
if argsLength := len(flag.Args()); argsLength == 0 || argsLength > 2 {
|
||||
fail("Unsupported arguments")
|
||||
}
|
||||
|
||||
c := d.NewClient(*root, *usr, *pw)
|
||||
if err := c.Connect(); err != nil {
|
||||
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
|
||||
if *password == "" {
|
||||
if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
|
||||
user = &u
|
||||
password = &p
|
||||
}
|
||||
}
|
||||
|
||||
cmd := getCmd(strings.ToUpper(*m))
|
||||
c := d.NewClient(*root, *user, *password)
|
||||
|
||||
if e := cmd(c, path0, path1); e != nil {
|
||||
cmd := getCmd(*method)
|
||||
|
||||
if e := cmd(c, flag.Arg(0), flag.Arg(1)); e != nil {
|
||||
fail(e)
|
||||
}
|
||||
}
|
||||
@@ -64,8 +66,26 @@ func fail(err interface{}) {
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func getHome() string {
|
||||
u, e := user.Current()
|
||||
if e != nil {
|
||||
return os.Getenv("HOME")
|
||||
}
|
||||
|
||||
if u != nil {
|
||||
return u.HomeDir
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
return ""
|
||||
default:
|
||||
return "~/"
|
||||
}
|
||||
}
|
||||
|
||||
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
|
||||
switch method {
|
||||
switch strings.ToUpper(method) {
|
||||
case "LS", "LIST", "PROPFIND":
|
||||
return cmdLs
|
||||
|
||||
@@ -100,7 +120,7 @@ func getCmd(method string) func(c *d.Client, p0, p1 string) error {
|
||||
}
|
||||
}
|
||||
|
||||
func cmdLs(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdLs(c *d.Client, p0, _ string) (err error) {
|
||||
files, err := c.ReadDir(p0)
|
||||
if err == nil {
|
||||
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", p0, len(files)))
|
||||
@@ -111,7 +131,7 @@ func cmdLs(c *d.Client, p0, p1 string) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func cmdStat(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdStat(c *d.Client, p0, _ string) (err error) {
|
||||
file, err := c.Stat(p0)
|
||||
if err == nil {
|
||||
fmt.Println(file)
|
||||
@@ -122,30 +142,34 @@ func cmdStat(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdGet(c *d.Client, p0, p1 string) (err error) {
|
||||
bytes, err := c.Read(p0)
|
||||
if err == nil {
|
||||
if err = writeFile(p1, bytes, 0644); err == nil {
|
||||
if p1 == "" {
|
||||
p1 = filepath.Join(".", p0)
|
||||
}
|
||||
err = writeFile(p1, bytes, 0644)
|
||||
if err == nil {
|
||||
fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), p1))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cmdRm(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdRm(c *d.Client, p0, _ string) (err error) {
|
||||
if err = c.Remove(p0); err == nil {
|
||||
fmt.Println("RM: " + p0)
|
||||
fmt.Println("Remove: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cmdMkdir(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdMkdir(c *d.Client, p0, _ string) (err error) {
|
||||
if err = c.Mkdir(p0, 0755); err == nil {
|
||||
fmt.Println("MkDir: " + p0)
|
||||
fmt.Println("Mkdir: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cmdMkdirAll(c *d.Client, p0, p1 string) (err error) {
|
||||
func cmdMkdirAll(c *d.Client, p0, _ string) (err error) {
|
||||
if err = c.MkdirAll(p0, 0755); err == nil {
|
||||
fmt.Println("MkDirAll: " + p0)
|
||||
fmt.Println("MkdirAll: " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -165,29 +189,46 @@ func cmdCp(c *d.Client, p0, p1 string) (err error) {
|
||||
}
|
||||
|
||||
func cmdPut(c *d.Client, p0, p1 string) (err error) {
|
||||
stream, err := getStream(p1)
|
||||
if err == nil {
|
||||
if err = c.WriteStream(p0, stream, 0644); err == nil {
|
||||
fmt.Println(fmt.Sprintf("Put: '%s' -> %s", p1, p0))
|
||||
if p1 == "" {
|
||||
p1 = filepath.Join(".", p0)
|
||||
}
|
||||
stream, err := getStream(p1)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
if err = c.WriteStream(p0, stream, 0644); err == nil {
|
||||
fmt.Println("Put: " + p1 + " -> " + p0)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func writeFile(path string, bytes []byte, mode os.FileMode) error {
|
||||
parent := filepath.Dir(path)
|
||||
if _, e := os.Stat(parent); os.IsNotExist(e) {
|
||||
if e := os.MkdirAll(parent, os.ModePerm); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Create(path)
|
||||
defer f.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.Write(bytes)
|
||||
return err
|
||||
}
|
||||
|
||||
func getStream(pathOrString string) (io.ReadCloser, error) {
|
||||
|
||||
fi, err := os.Stat(pathOrString)
|
||||
if err == nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if fi.IsDir() {
|
||||
return nil, &os.PathError{
|
||||
Op: "Open",
|
||||
@@ -195,23 +236,15 @@ func getStream(pathOrString string) (io.ReadCloser, error) {
|
||||
Err: errors.New("Path: '" + pathOrString + "' is a directory"),
|
||||
}
|
||||
}
|
||||
|
||||
f, err := os.Open(pathOrString)
|
||||
if err == nil {
|
||||
return f, nil
|
||||
}
|
||||
|
||||
return nil, &os.PathError{
|
||||
Op: "Open",
|
||||
Path: pathOrString,
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
return nopCloser{strings.NewReader(pathOrString)}, nil
|
||||
}
|
||||
|
||||
type nopCloser struct {
|
||||
io.Reader
|
||||
}
|
||||
|
||||
func (nopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
146
digestAuth.go
Normal file
146
digestAuth.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DigestAuth structure holds our credentials
|
||||
type DigestAuth struct {
|
||||
user string
|
||||
pw string
|
||||
digestParts map[string]string
|
||||
}
|
||||
|
||||
// Type identifies the DigestAuthenticator
|
||||
func (d *DigestAuth) Type() string {
|
||||
return "DigestAuth"
|
||||
}
|
||||
|
||||
// User holds the DigestAuth username
|
||||
func (d *DigestAuth) User() string {
|
||||
return d.user
|
||||
}
|
||||
|
||||
// Pass holds the DigestAuth password
|
||||
func (d *DigestAuth) Pass() string {
|
||||
return d.pw
|
||||
}
|
||||
|
||||
// Authorize the current request
|
||||
func (d *DigestAuth) Authorize(req *http.Request, method string, path string) {
|
||||
d.digestParts["uri"] = path
|
||||
d.digestParts["method"] = method
|
||||
d.digestParts["username"] = d.user
|
||||
d.digestParts["password"] = d.pw
|
||||
req.Header.Set("Authorization", getDigestAuthorization(d.digestParts))
|
||||
}
|
||||
|
||||
func digestParts(resp *http.Response) map[string]string {
|
||||
result := map[string]string{}
|
||||
if len(resp.Header["Www-Authenticate"]) > 0 {
|
||||
wantedHeaders := []string{"nonce", "realm", "qop", "opaque", "algorithm", "entityBody"}
|
||||
responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
|
||||
for _, r := range responseHeaders {
|
||||
for _, w := range wantedHeaders {
|
||||
if strings.Contains(r, w) {
|
||||
result[w] = strings.Trim(
|
||||
strings.SplitN(r, `=`, 2)[1],
|
||||
`"`,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func getMD5(text string) string {
|
||||
hasher := md5.New()
|
||||
hasher.Write([]byte(text))
|
||||
return hex.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
func getCnonce() string {
|
||||
b := make([]byte, 8)
|
||||
io.ReadFull(rand.Reader, b)
|
||||
return fmt.Sprintf("%x", b)[:16]
|
||||
}
|
||||
|
||||
func getDigestAuthorization(digestParts map[string]string) string {
|
||||
d := digestParts
|
||||
// These are the correct ha1 and ha2 for qop=auth. We should probably check for other types of qop.
|
||||
|
||||
var (
|
||||
ha1 string
|
||||
ha2 string
|
||||
nonceCount = 00000001
|
||||
cnonce = getCnonce()
|
||||
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
|
||||
}
|
||||
3
doc.go
Normal file
3
doc.go
Normal file
@@ -0,0 +1,3 @@
|
||||
// Package gowebdav is a WebDAV client library with a command line tool
|
||||
// included.
|
||||
package gowebdav
|
||||
5
file.go
5
file.go
@@ -17,6 +17,11 @@ type File struct {
|
||||
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
|
||||
func (f File) Name() string {
|
||||
return f.name
|
||||
|
||||
54
netrc.go
Normal file
54
netrc.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseLine(s string) (login, pass string) {
|
||||
fields := strings.Fields(s)
|
||||
for i, f := range fields {
|
||||
if f == "login" {
|
||||
login = fields[i+1]
|
||||
}
|
||||
if f == "password" {
|
||||
pass = fields[i+1]
|
||||
}
|
||||
}
|
||||
return login, pass
|
||||
}
|
||||
|
||||
// ReadConfig reads login and password configuration from ~/.netrc
|
||||
// machine foo.com login username password 123456
|
||||
func ReadConfig(uri, netrc string) (string, string) {
|
||||
u, err := url.Parse(uri)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
file, err := os.Open(netrc)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
re := fmt.Sprintf(`^.*machine %s.*$`, u.Host)
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
s := scanner.Text()
|
||||
|
||||
matched, err := regexp.MatchString(re, s)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
if matched {
|
||||
return parseLine(s)
|
||||
}
|
||||
}
|
||||
|
||||
return "", ""
|
||||
}
|
||||
105
requests.go
105
requests.go
@@ -1,36 +1,111 @@
|
||||
package gowebdav
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for k, vals := range c.headers {
|
||||
for _, v := range vals {
|
||||
r.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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 {
|
||||
rs, err := c.req("MKCOL", path, nil, nil)
|
||||
defer rs.Body.Close()
|
||||
if err != nil {
|
||||
return 400
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode == 201 || rs.StatusCode == 405 {
|
||||
return 201
|
||||
@@ -52,16 +127,16 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
|
||||
} else {
|
||||
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-Charset", "utf-8")
|
||||
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
|
||||
rq.Header.Add("Accept-Encoding", "")
|
||||
})
|
||||
defer rs.Body.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode != 207 {
|
||||
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
|
||||
@@ -87,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 {
|
||||
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
|
||||
if data != nil {
|
||||
defer data.Close()
|
||||
}
|
||||
|
||||
switch s {
|
||||
case 201, 204:
|
||||
@@ -98,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)))
|
||||
|
||||
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)
|
||||
@@ -106,10 +188,19 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
||||
|
||||
func (c *Client) put(path string, stream io.Reader) int {
|
||||
rs, err := c.req("PUT", path, stream, nil)
|
||||
defer rs.Body.Close()
|
||||
if err != nil {
|
||||
return 400
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
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 {
|
||||
s := strings.Split(path, "/")
|
||||
for i, e := range s {
|
||||
@@ -51,9 +51,10 @@ func FixSlash(s string) string {
|
||||
|
||||
// FixSlashes appends and prepends a / if they are missing
|
||||
func FixSlashes(s string) string {
|
||||
if s[0] != '/' {
|
||||
if !strings.HasPrefix(s, "/") {
|
||||
s = "/" + s
|
||||
}
|
||||
|
||||
return FixSlash(s)
|
||||
}
|
||||
|
||||
@@ -107,3 +108,39 @@ func parseXML(data io.Reader, resp interface{}, parse func(resp interface{}) err
|
||||
}
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
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