55 Commits
5 ... 8

Author SHA1 Message Date
Christoph Polcin
9380631c29 update README 2020-03-03 16:07:24 +01:00
Lukáš Lalinský
a93005d73c Add File.Path to return the full path (#34)
Very useful when iterating over ReadDir results and wanting to do some operation on them.
2020-03-03 15:28:06 +01:00
vitalii
321978fa73 ref: make names more descriptive 2019-12-15 01:31:08 +02:00
vitalii
c4c707907d fix method of getting user's home path 2019-12-15 01:28:41 +02:00
Justus Flerlage
9f625b1b8e Added check for root path 2019-11-14 22:08:20 +02:00
Justus Flerlage
e53b818e1b Added check of parentPath in createParentCollection 2019-11-14 22:08:20 +02:00
vitalii
ff7f737904 fix(requests.go): allow www-authenticate to be case-insensitive. close #32 2019-10-05 22:13:01 +03:00
vitalii
38f79aeaf1 fix uploading file with wrong content. close #30 2019-01-03 20:40:47 +02:00
vitalii
6c32839dbd fix import for command line tool 2018-12-30 13:28:02 +02:00
vitalii
4d70d7ea28 use 'application/xml' instead of 'text/xml'. related with (1) in #15 2018-12-29 22:04:04 +02:00
yatsen1
8bcb1b383c Fix early defer panic. (#29) 2018-11-08 09:39:42 +01:00
vitalii
cba565a9dc links was updated in readme file. Related to studio-b12/gowebdav#27
RFC 2518 was removed because it is obsolete
RFC 4918 was added because it is actual
RFC 5689 was added because it updates RFC 4918
2018-10-24 14:05:51 +03:00
vitalii
425530b55e cmd: readme was updated. closes studio-b12/gowebdav#27
Wrapper script section was updated
2018-10-22 08:29:10 +03:00
vitalii
7493d8befb readme file for command line tool was added
Close #27
2018-10-22 08:10:23 +03:00
vitalii
e29bc0f031 main readme file was updated 2018-10-22 08:09:12 +03:00
vitalii
c8fc9ca590 Merge remote-tracking branch 'upstream/master' 2018-10-21 12:52:46 +03:00
vitalii
a68e21e92b requests: nil pointer dereference panic was fixed [#25]
http.Do() method will return non-nil [error] in following cases:
1. Request is nil
2. Response missing Location header
3. Client failed to parse Location header
4. Method "request.GetBody()" returns error
5. Http.Client.Send() returns error
5. Client timeout was exceeded

Signed-off-by: Christoph Polcin <labs@polcin.de>
2018-10-21 11:15:52 +02:00
vitalii
02aa9bdaeb unused imports was removed 2018-10-20 23:08:19 +03:00
vitalii
8de8ce169b Merge remote-tracking branch 'upstream/master' 2018-07-17 14:00:43 +03:00
Christoph Polcin
3cd755d6c4 make check api 2018-07-14 01:55:58 +02:00
Vitalii
83e3d1e31e Creating parent collection method was added (#22)
* method for creating parent collection was added to Client struct

"func (c *Client) createParentCollection(itemPath string) error" was added to request.go file

* using Client's method to create parent collection

in following methods:
Client.Write()
Client.WriteStream()
Client.copymove()

deadlock is impossible in method Client.copymove() because of paragraph #6 section 9.8.5 (https://tools.ietf.org/html/rfc4918#section-9.8.5) and paragraph #6 section 9.9.4 (https://tools.ietf.org/html/rfc4918#section-9.9.4) of RFC 4918 (https://tools.ietf.org/html/rfc4918)

* install dependencies script was added to Travis-CI file

* testing was added to Travis-CI file

* error wrapping was removed from Client.put() method

* using an early return on error in case of 409 in Client.Write() method
2018-07-14 01:48:30 +02:00
Christoph Polcin
28039fda22 fmt 2018-07-13 12:12:09 +02:00
Christoph Polcin
45a56c2115 cmd: remove Connect() due to #16 2018-07-13 12:11:56 +02:00
vitalii
f821ab73e9 Merge branch 'b12-master' 2018-07-11 12:41:49 +03:00
Vitalii
ec1263db2f all cases of Digest authorization was implemented (#19)
Digest authentication was improved
2018-07-10 18:51:11 +02:00
Vitalii
95706c0747 .gitignore was expanded (#18) 2018-07-08 14:27:10 +02:00
Vitalii
f43a0a4cf8 quick bugfix for issue #20 (#21)
issue title: "Can't upload file with content"
https://github.com/studio-b12/gowebdav/issues/20
2018-07-08 14:25:00 +02:00
vitalii
68824ef55e createParentCollection() function was added 2018-06-21 16:37:02 +03:00
vitalii
6ca20e2a70 copyMove() function returns error in case of 409 status code 2018-06-21 16:35:39 +03:00
vitalii
790397514e all cases of Digest authorization was implemented 2018-06-21 16:35:26 +03:00
vitalii
4ca2f77e2b Merge remote-tracking branch 'upstream/master' 2018-06-21 16:34:17 +03:00
Vitalii
008b27eb0f bug #14 fixed (#17)
"opaque" field should not be specified if server did not provide it
2018-06-21 10:57:32 +02:00
vitalii
d1ebcbebf2 Merge branch 'develop' 2018-06-20 04:02:49 +03:00
vitalii
4f450cfd02 Merge branch 'master' into develop 2018-06-20 04:01:37 +03:00
vitalii
876ef52924 not all webdav servers returns "Dav" header 2018-06-20 03:21:30 +03:00
vitalii
21d86ab356 use import of current fork (instead of original project) 2018-06-20 03:19:23 +03:00
vitalii
97a0b83aeb bug #14 fixed
"opaque" field should not be specified if server did not provide it
2018-06-20 03:17:52 +03:00
vitalii
1fe9163c92 Merge remote-tracking branch 'studio-b12/master' 2018-06-19 11:44:16 +03:00
vitalii
8bab650703 gitignore: .idea folder and *.exe files was added 2018-06-19 11:42:02 +03:00
Christoph Polcin
c4c24955e1 update README 2018-06-19 08:43:13 +02:00
Christoph Polcin
2593a81bf0 Update issue templates 2018-06-19 08:36:40 +02:00
David
b45378c08f Check status on every request to fix #14 2018-06-18 10:02:01 -05:00
vitalii
aebc3ef9d2 .idea/ folder and *.exe files was added to .gitignore 2018-06-14 09:24:10 +03:00
misha-plus
9ff8e33634 fix authorization 2018-06-10 11:01:51 +02:00
Aaron Bieber
a33240e4ab add ability to read login / pw from ~/.netrc 2018-05-26 01:36:44 +02:00
Christoph Polcin
fbcb29d33e docs 2018-05-25 23:59:53 +02:00
David
6d8c168f72 Add Authenticator interface and Digest auth support 2018-05-25 22:40:13 +02:00
Christoph Polcin
5bedad6f1e cmd: refactor method 2018-05-25 20:31:58 +02:00
Christoph Polcin
31e0b57e53 cmd: PUT with optinal FILE argument 2018-05-25 20:27:56 +02:00
Christoph Polcin
ba3a71318b cmd: refactor output message 2018-05-25 19:58:46 +02:00
Christoph Polcin
fa51555f16 cmd: simplify arg handling 2018-05-25 12:28:37 +02:00
Christoph Polcin
e0b778960b cmd: GET with optional FILE argument 2018-05-25 12:28:37 +02:00
Christoph Polcin
a98da9745e cmd: create parent directories if writing files 2018-05-25 12:28:37 +02:00
Christoph Polcin
1786d37966 ignore second cmd arg 2018-05-25 12:28:37 +02:00
Christoph Polcin
32d5561fb6 add make check 2018-05-25 12:28:37 +02:00
14 changed files with 897 additions and 149 deletions

27
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View 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.

15
.gitignore vendored
View File

@@ -1,4 +1,19 @@
# 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

View File

@@ -2,3 +2,9 @@ language: go
go: go:
- "1.x" - "1.x"
install:
- go get ./...
script:
- go test -v --short ./...

View File

@@ -9,7 +9,7 @@ ${BIN}: ${SRC}
go build -o $@ ./cmd/gowebdav go build -o $@ ./cmd/gowebdav
test: test:
go test -v ./... go test -v --short ./...
api: api:
@sed '/^## API$$/,$$d' -i README.md @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/\/src\/target\//https:\/\/github.com\/studio-b12\/gowebdav\/blob\/master\//g' |\
sed 's/^#/##/g' >> README.md sed 's/^#/##/g' >> README.md
check:
gofmt -w -s $(SRC)
@echo
gocyclo -over 15 .
@echo
golint ./...
clean: clean:
@rm -f ${BIN} @rm -f ${BIN}
.PHONY: all cmd clean test api .PHONY: all cmd clean test api check

361
README.md
View File

@@ -1,60 +1,150 @@
# GoWebDAV # GoWebDAV
[![Build Status](https://travis-ci.org/studio-b12/gowebdav.svg?branch=master)](https://travis-ci.org/studio-b12/gowebdav) [![Build Status](https://travis-ci.org/studio-b12/gowebdav.svg?branch=master)](https://travis-ci.org/studio-b12/gowebdav)
[![GoDoc](https://godoc.org/github.com/studio-b12/gowebdav?status.svg)](https://godoc.org/github.com/studio-b12/gowebdav)
[![Go Report Card](https://goreportcard.com/badge/github.com/studio-b12/gowebdav)](https://goreportcard.com/report/github.com/studio-b12/gowebdav) [![Go Report Card](https://goreportcard.com/badge/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 ## 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>
-pw string
Password [ENV.PASSWORD]
-root string
WebDAV Endpoint [ENV.ROOT]
-user string
User [ENV.USER]
``` ```
*Example* After you can use this `Client` to perform actions, described below.
```sh **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!
ROOT="https://webdav.server/" \
USER="foo" \ ### Create path on a WebDAV server
PASSWORD="bar" \ ```go
./gowebdav -X LS / 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") * [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"`
@@ -65,14 +155,22 @@ PASSWORD="bar" \
* [Subdirectories](#pkg-subdirectories) * [Subdirectories](#pkg-subdirectories)
### <a name="pkg-overview">Overview</a> ### <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> ### <a name="pkg-index">Index</a>
* [func FixSlash(s string) string](#FixSlash) * [func FixSlash(s string) string](#FixSlash)
* [func FixSlashes(s string) string](#FixSlashes) * [func FixSlashes(s string) string](#FixSlashes)
* [func Join(path0 string, path1 string) string](#Join) * [func Join(path0 string, path1 string) string](#Join)
* [func PathEscape(path string) string](#PathEscape) * [func PathEscape(path string) string](#PathEscape)
* [func ReadConfig(uri, netrc string) (string, string)](#ReadConfig)
* [func String(r io.Reader) string](#String) * [func String(r io.Reader) string](#String)
* [type Authenticator](#Authenticator)
* [type BasicAuth](#BasicAuth)
* [func (b *BasicAuth) Authorize(c *Client, 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) * [type Client](#Client)
* [func NewClient(uri, user, pw string) *Client](#NewClient) * [func NewClient(uri, user, pw string) *Client](#NewClient)
* [func (c *Client) Connect() error](#Client.Connect) * [func (c *Client) Connect() error](#Client.Connect)
@@ -91,6 +189,11 @@ Package gowebdav A golang WebDAV library
* [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)
* [func (d *DigestAuth) Authorize(c *Client, 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) * [type File](#File)
* [func (f File) ContentType() string](#File.ContentType) * [func (f File) ContentType() string](#File.ContentType)
* [func (f File) ETag() string](#File.ETag) * [func (f File) ETag() string](#File.ETag)
@@ -98,15 +201,21 @@ Package gowebdav A golang WebDAV library
* [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)
* [func (n *NoAuth) Authorize(c *Client, 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> ##### <a name="pkg-examples">Examples</a>
* [PathEscape](#example_PathEscape) * [PathEscape](#example_PathEscape)
##### <a name="pkg-files">Package files</a> ##### <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) ### <a name="FixSlash">func</a> [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=707:737#L45)
``` go ``` go
@@ -132,13 +241,63 @@ func PathEscape(path string) string
``` ```
PathEscape escapes all segemnts of a given path 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) ### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=1150:1181#L66)
``` go ``` go
func String(r io.Reader) string 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="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=285:398#L24)
``` go
type Authenticator interface {
Type() string
User() string
Pass() string
Authorize(*Client, string, string)
}
```
Authenticator stub
### <a name="BasicAuth">type</a> [BasicAuth](https://github.com/studio-b12/gowebdav/blob/master/basicAuth.go?s=94:145#L8)
``` 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=461:529#L29)
``` go
func (b *BasicAuth) Authorize(c *Client, 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=376:409#L24)
``` 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=189:222#L14)
``` 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=285:318#L19)
``` 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=157:261#L16)
``` go ``` go
type Client struct { type Client struct {
// contains filtered or unexported fields // contains filtered or unexported fields
@@ -146,108 +305,140 @@ 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=349:393#L25) #### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=902:946#L57)
``` 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=1138:1170#L55) #### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1516:1548#L77)
``` 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=6060:6128#L281) #### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6376:6444#L303)
``` 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=5151:5207#L240) #### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5467:5523#L262)
``` 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=5386:5445#L251) #### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5702:5761#L273)
``` 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=6234:6284#L286) #### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6550:6600#L308)
``` 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=2226:2286#L98) #### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2542:2602#L120)
``` 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=6595:6658#L304) #### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6911:6974#L326)
``` 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=4657:4699#L217) #### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4973:5015#L239)
``` 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=4765:4810#L222) #### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5081:5126#L244)
``` 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=5894:5964#L276) #### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6210:6280#L298)
``` 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=721:766#L40) #### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1099:1144#L62)
``` 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=866:916#L45) #### <a name="Client.SetTimeout">func</a> (\*Client) [SetTimeout](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1244:1294#L67)
``` 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=1009:1067#L50) #### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1387:1445#L72)
``` 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=3613:3668#L165) #### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=3929:3984#L187)
``` 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=6949:7018#L319) #### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7265:7334#L341)
``` 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=7420:7500#L341) #### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7690:7770#L364)
``` 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
``` ```
WriteStream writes a stream 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:646#L36)
``` go
func (d *DigestAuth) Authorize(c *Client, 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) ### <a name="File">type</a> [File](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=93:253#L10)
``` go ``` go
type File struct { type File struct {
@@ -256,59 +447,97 @@ 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)
``` 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=785:850#L53)
``` go
func (n *NoAuth) Authorize(c *Client, 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=703:733#L48)
``` 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=529:559#L38)
``` 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=615:645#L43)
``` go
func (n *NoAuth) User() string
```
User returns the current user
- - - - - -
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md) Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)

33
basicAuth.go Normal file
View File

@@ -0,0 +1,33 @@
package gowebdav
import (
"encoding/base64"
)
// 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(c *Client, method string, path string) {
a := b.user + ":" + b.pw
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
c.headers.Set("Authorization", auth)
}

View File

@@ -1,9 +1,7 @@
// Package gowebdav A golang WebDAV library
package gowebdav package gowebdav
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/xml" "encoding/xml"
"io" "io"
"net/http" "net/http"
@@ -19,21 +17,45 @@ type Client struct {
root string root string
headers http.Header headers http.Header
c *http.Client c *http.Client
auth Authenticator
}
// Authenticator stub
type Authenticator interface {
Type() string
User() string
Pass() string
Authorize(*Client, 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(c *Client, 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 {
c := &Client{uri, make(http.Header), &http.Client{}} return &Client{FixSlash(uri), make(http.Header), &http.Client{}, &NoAuth{user, pw}}
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
} }
// SetHeader lets us set arbitrary headers for a given client // SetHeader lets us set arbitrary headers for a given client
@@ -63,7 +85,7 @@ func (c *Client) Connect() error {
return err 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) return newPathError("Connect", c.root, rs.StatusCode)
} }
@@ -324,23 +346,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
View 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
```

View File

@@ -7,22 +7,26 @@ import (
d "github.com/studio-b12/gowebdav" d "github.com/studio-b12/gowebdav"
"io" "io"
"os" "os"
"os/user"
"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]")
m := flag.String("X", "", `Method: netrc := flag.String("netrc-file", filepath.Join(getHome(), ".netrc"), "read login from netrc file")
method := flag.String("X", "", `Method:
LS <PATH> LS <PATH>
STAT <PATH> STAT <PATH>
MKDIR <PATH> MKDIR <PATH>
MKDIRALL <PATH> MKDIRALL <PATH>
GET <PATH> <FILE> GET <PATH> [<FILE>]
PUT <PATH> <FILE> PUT <PATH> [<FILE>]
MV <OLD> <NEW> MV <OLD> <NEW>
CP <OLD> <NEW> CP <OLD> <NEW>
@@ -35,24 +39,22 @@ func main() {
fail("Set WebDAV ROOT") fail("Set WebDAV ROOT")
} }
var path0, path1 string if argsLength := len(flag.Args()); argsLength == 0 || argsLength > 2 {
switch len(flag.Args()) {
case 1:
path0 = flag.Args()[0]
case 2:
path1 = flag.Args()[1]
default:
fail("Unsupported arguments") fail("Unsupported arguments")
} }
c := d.NewClient(*root, *usr, *pw) if *password == "" {
if err := c.Connect(); err != nil { if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error())) 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) fail(e)
} }
} }
@@ -64,8 +66,26 @@ func fail(err interface{}) {
os.Exit(-1) 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 { func getCmd(method string) func(c *d.Client, p0, p1 string) error {
switch method { switch strings.ToUpper(method) {
case "LS", "LIST", "PROPFIND": case "LS", "LIST", "PROPFIND":
return cmdLs 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) files, err := c.ReadDir(p0)
if err == nil { if err == nil {
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", p0, len(files))) 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 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) file, err := c.Stat(p0)
if err == nil { if err == nil {
fmt.Println(file) 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) { func cmdGet(c *d.Client, p0, p1 string) (err error) {
bytes, err := c.Read(p0) bytes, err := c.Read(p0)
if err == nil { 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)) fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), p1))
} }
} }
return 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 { if err = c.Remove(p0); err == nil {
fmt.Println("RM: " + p0) fmt.Println("Remove: " + p0)
} }
return 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 { if err = c.Mkdir(p0, 0755); err == nil {
fmt.Println("MkDir: " + p0) fmt.Println("Mkdir: " + p0)
} }
return 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 { if err = c.MkdirAll(p0, 0755); err == nil {
fmt.Println("MkDirAll: " + p0) fmt.Println("MkdirAll: " + p0)
} }
return 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) { func cmdPut(c *d.Client, p0, p1 string) (err error) {
stream, err := getStream(p1) if p1 == "" {
if err == nil { p1 = filepath.Join(".", p0)
if err = c.WriteStream(p0, stream, 0644); err == nil {
fmt.Println(fmt.Sprintf("Put: '%s' -> %s", p1, 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 return
} }
func writeFile(path string, bytes []byte, mode os.FileMode) error { 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) f, err := os.Create(path)
defer f.Close()
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
_, err = f.Write(bytes) _, err = f.Write(bytes)
return err return err
} }
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",
@@ -195,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
} }

146
digestAuth.go Normal file
View 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(c *Client, method string, path string) {
d.digestParts["uri"] = path
d.digestParts["method"] = method
d.digestParts["username"] = d.user
d.digestParts["password"] = d.pw
c.headers.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
View File

@@ -0,0 +1,3 @@
// Package gowebdav is a WebDAV client library with a command line tool
// included.
package gowebdav

View File

@@ -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

54
netrc.go Normal file
View 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 "", ""
}

View File

@@ -1,17 +1,32 @@
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) // Tee the body, because if authorization fails we will need to read from it again.
var r *http.Request
var ba bytes.Buffer
bb := io.TeeReader(body, &ba)
if body == nil {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), nil)
} else {
r, err = http.NewRequest(method, PathEscape(Join(c.root, path)), bb)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
c.auth.Authorize(c, method, path)
for k, vals := range c.headers { for k, vals := range c.headers {
for _, v := range vals { for _, v := range vals {
r.Header.Add(k, v) r.Header.Add(k, v)
@@ -22,15 +37,44 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
intercept(r) intercept(r)
} }
return c.c.Do(r) rs, err := c.c.Do(r)
if err != nil {
return nil, err
}
if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
wwwAuthenticateHeader := strings.ToLower(rs.Header.Get("Www-Authenticate"))
if strings.Index(wwwAuthenticateHeader, "digest") > -1 {
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
} else if strings.Index(wwwAuthenticateHeader, "basic") > -1 {
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
} else {
return rs, newPathError("Authorize", c.root, rs.StatusCode)
}
if body == nil {
return c.req(method, path, nil, intercept)
} else {
return c.req(method, path, &ba, 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
@@ -52,16 +96,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)
@@ -98,7 +142,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)
@@ -106,10 +155,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)
}