Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a33240e4ab | ||
|
|
fbcb29d33e | ||
|
|
6d8c168f72 | ||
|
|
5bedad6f1e | ||
|
|
31e0b57e53 | ||
|
|
ba3a71318b | ||
|
|
fa51555f16 | ||
|
|
e0b778960b | ||
|
|
a98da9745e | ||
|
|
1786d37966 | ||
|
|
32d5561fb6 |
11
Makefile
11
Makefile
@@ -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
|
||||||
|
|||||||
217
README.md
217
README.md
@@ -1,11 +1,12 @@
|
|||||||
# GoWebDAV
|
# GoWebDAV
|
||||||
|
|
||||||
[](https://travis-ci.org/studio-b12/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)
|
[](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
|
||||||
|
|
||||||
A WebDAV client and library for golang.
|
A golang WebDAV client library and command line tool.
|
||||||
|
|
||||||
## Install
|
## Install command line tool
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
|
||||||
@@ -24,29 +25,55 @@ Usage of gowebdav
|
|||||||
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>
|
||||||
|
|
||||||
DEL <PATH>
|
DEL <PATH>
|
||||||
|
|
||||||
|
-netrc-file string
|
||||||
|
read login from netrc file (default "~/.netrc")
|
||||||
-pw string
|
-pw string
|
||||||
Password [ENV.PASSWORD]
|
Password [ENV.PASSWORD]
|
||||||
-root string
|
-root string
|
||||||
WebDAV Endpoint [ENV.ROOT]
|
WebDAV Endpoint [ENV.ROOT]
|
||||||
-user string
|
-user string
|
||||||
User [ENV.USER]
|
User [ENV.USER] (default "$USER")
|
||||||
```
|
```
|
||||||
|
|
||||||
*Example*
|
*gowebdav wrapper script*
|
||||||
|
|
||||||
|
Create a wrapper script for example `$EDITOR ./dav && chmod a+x ./dav` for your
|
||||||
|
server and use [pass](https://www.passwordstore.org/ "the standard unix password manager")
|
||||||
|
or similar tools to retrieve the password.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
ROOT="https://webdav.server/" \
|
#!/bin/sh
|
||||||
|
|
||||||
|
ROOT="https://my.dav.server/" \
|
||||||
USER="foo" \
|
USER="foo" \
|
||||||
PASSWORD="bar" \
|
PASSWORD="$(pass dav/foo@my.dav.server)" \
|
||||||
./gowebdav -X LS /
|
gowebdav $@
|
||||||
|
```
|
||||||
|
|
||||||
|
*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
|
||||||
```
|
```
|
||||||
|
|
||||||
## LINKS
|
## LINKS
|
||||||
@@ -65,14 +92,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 +126,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)
|
||||||
@@ -101,12 +141,17 @@ Package gowebdav A golang WebDAV library
|
|||||||
* [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 +177,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 +241,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=6960:7028#L314)
|
||||||
``` 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=6051:6107#L273)
|
||||||
``` 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=6286:6345#L284)
|
||||||
``` 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=7134:7184#L319)
|
||||||
``` 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=3126:3186#L131)
|
||||||
``` 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=7495:7558#L337)
|
||||||
``` 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=5557:5599#L250)
|
||||||
``` 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=5665:5710#L255)
|
||||||
``` 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=6794:6864#L309)
|
||||||
``` 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=4513:4568#L198)
|
||||||
``` 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=7849:7918#L352)
|
||||||
``` 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=8320:8400#L374)
|
||||||
``` 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 {
|
||||||
@@ -310,5 +437,37 @@ 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
33
basicAuth.go
Normal 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)
|
||||||
|
}
|
||||||
61
client.go
61
client.go
@@ -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,18 @@ func (c *Client) Connect() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
|
if rs.StatusCode == 401 && c.auth.Type() == "NoAuth" {
|
||||||
|
if strings.Index(rs.Header.Get("Www-Authenticate"), "Digest") > -1 {
|
||||||
|
c.auth = &DigestAuth{c.auth.User(), c.auth.Pass(), digestParts(rs)}
|
||||||
|
} else if strings.Index(rs.Header.Get("Www-Authenticate"), "Basic") > -1 {
|
||||||
|
c.auth = &BasicAuth{c.auth.User(), c.auth.Pass()}
|
||||||
|
} else {
|
||||||
|
return newPathError("Authorize", c.root, rs.StatusCode)
|
||||||
|
}
|
||||||
|
return c.Connect()
|
||||||
|
} else if rs.StatusCode == 401 {
|
||||||
|
return newPathError("Authorize", c.root, rs.StatusCode)
|
||||||
|
} else if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
|
||||||
return newPathError("Connect", c.root, rs.StatusCode)
|
return newPathError("Connect", c.root, rs.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import (
|
|||||||
d "github.com/studio-b12/gowebdav"
|
d "github.com/studio-b12/gowebdav"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,15 +16,16 @@ 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]")
|
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
|
||||||
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
|
pw := 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 +38,25 @@ func main() {
|
|||||||
fail("Set WebDAV ROOT")
|
fail("Set WebDAV ROOT")
|
||||||
}
|
}
|
||||||
|
|
||||||
var path0, path1 string
|
if l := len(flag.Args()); l == 0 || l > 2 {
|
||||||
switch len(flag.Args()) {
|
|
||||||
case 1:
|
|
||||||
path0 = flag.Args()[0]
|
|
||||||
case 2:
|
|
||||||
path1 = flag.Args()[1]
|
|
||||||
default:
|
|
||||||
fail("Unsupported arguments")
|
fail("Unsupported arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *pw == "" {
|
||||||
|
if u, p := d.ReadConfig(*root, *netrc); u != "" && p != "" {
|
||||||
|
usr = &u
|
||||||
|
pw = &p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c := d.NewClient(*root, *usr, *pw)
|
c := d.NewClient(*root, *usr, *pw)
|
||||||
if err := c.Connect(); err != nil {
|
if err := c.Connect(); err != nil {
|
||||||
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
|
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := getCmd(strings.ToUpper(*m))
|
cmd := getCmd(*method)
|
||||||
|
|
||||||
if e := cmd(c, path0, path1); e != nil {
|
if e := cmd(c, flag.Arg(0), flag.Arg(1)); e != nil {
|
||||||
fail(e)
|
fail(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,8 +68,15 @@ func fail(err interface{}) {
|
|||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getHome() string {
|
||||||
|
if u, e := user.Current(); e != nil {
|
||||||
|
return u.HomeDir
|
||||||
|
}
|
||||||
|
return os.Getenv("HOME")
|
||||||
|
}
|
||||||
|
|
||||||
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 +111,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 +122,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 +133,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,21 +180,34 @@ 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
|
||||||
|
|||||||
83
digestAuth.go
Normal file
83
digestAuth.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
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"}
|
||||||
|
responseHeaders := strings.Split(resp.Header["Www-Authenticate"][0], ",")
|
||||||
|
for _, r := range responseHeaders {
|
||||||
|
for _, w := range wantedHeaders {
|
||||||
|
if strings.Contains(r, w) {
|
||||||
|
result[w] = strings.Split(r, `"`)[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.
|
||||||
|
ha1 := getMD5(d["username"] + ":" + d["realm"] + ":" + d["password"])
|
||||||
|
ha2 := getMD5(d["method"] + ":" + d["uri"])
|
||||||
|
nonceCount := 00000001
|
||||||
|
cnonce := getCnonce()
|
||||||
|
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", cnonce="%s", nc="%v", qop="%s", response="%s", opaque="%s"`,
|
||||||
|
d["username"], d["realm"], d["nonce"], d["uri"], cnonce, nonceCount, d["qop"], response, 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
|
||||||
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 "", ""
|
||||||
|
}
|
||||||
@@ -12,12 +12,15 @@ func (c *Client) req(method, path string, body io.Reader, intercept func(*http.R
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.auth.Authorize(c, method, path)
|
||||||
|
|
||||||
if intercept != nil {
|
if intercept != nil {
|
||||||
intercept(r)
|
intercept(r)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user