27 Commits
3 ... 5

Author SHA1 Message Date
Christoph Polcin
d6b82df09a update cmd usage 2018-05-25 12:28:34 +02:00
Christoph Polcin
c16034adb0 refactor getCmd 2018-05-25 10:15:26 +02:00
Christoph Polcin
1e4b7a5554 refactor client 2018-05-24 01:24:27 +02:00
Christoph Polcin
3993494db0 refactor cmd 2018-05-24 01:24:25 +02:00
Christoph Polcin
34368960d0 refactor 2018-05-23 14:15:46 +02:00
Christoph Polcin
375f391c8a Escape URL 2018-05-23 13:41:06 +02:00
Shishkin Artem
c49c91989e % and # in the URL 2018-05-22 12:34:34 +02:00
Christoph Polcin
7ea52a8e4f fix close 2018-05-17 01:24:14 +02:00
Abdurrahman
0e7fe90d28 Fetch ContentType and ETag 2018-05-17 01:22:58 +02:00
Christoph Polcin
0120c3c3c4 hello travis-ci 2018-05-17 00:27:52 +02:00
Christoph Polcin
410e447c49 simplify 409 write error handler 2018-05-16 23:39:37 +02:00
Christoph Polcin
65a34986d4 update Readme 2018-05-16 14:35:38 +02:00
Keith Ball
73b90bf27e Do not do a read on the root on connect 2018-05-16 14:17:35 +02:00
Keith Ball
9c695fe0df Return codes 2018-05-16 14:15:47 +02:00
Christoph Polcin
732590873e docs 2018-05-16 14:05:18 +02:00
Christoph Polcin
5a1a85f622 update README 2018-05-16 14:05:18 +02:00
Christoph Polcin
056ad2c4d5 refactor main 2018-05-16 12:23:42 +02:00
Aaron Bieber
8a7d0f4982 cleanup 2018-05-16 10:43:04 +02:00
Aaron Bieber
beeefa572c more docs / cleanup 2018-05-16 10:42:53 +02:00
Aaron Bieber
b290410ef9 add a bit of doc, cleanup some warnings 2018-05-16 10:42:38 +02:00
Aaron Bieber
af17afdfdc reference full path for gowebdav 2018-05-16 10:34:52 +02:00
Aaron Bieber
76d61cc2bc use defaults for GOPATH 2018-05-16 10:34:42 +02:00
Christoph Polcin
06583fe483 update rfc links 2018-05-15 12:14:08 +02:00
Christoph Polcin
49e0e45d8f fmt 2018-05-15 11:51:59 +02:00
Christoph Polcin
a3c4dbab4a add SetTimeout 2018-05-15 11:51:53 +02:00
Christoph Polcin
f60c73fbb4 Merge pull request #8 from Ferada/fix-read-status-code
Handle error response when reading a file.
2017-12-20 19:29:10 +01:00
Olof-Joachim Frahm
a11466bd13 Handle error response when reading a file. 2017-12-20 10:53:49 +01:00
11 changed files with 738 additions and 269 deletions

6
.gitignore vendored
View File

@@ -1,2 +1,4 @@
src
bin
/src
/bin
/pkg
/gowebdav

4
.travis.yml Normal file
View File

@@ -0,0 +1,4 @@
language: go
go:
- "1.x"

View File

@@ -1,21 +1,26 @@
SRC := $(wildcard *.go) main/client.go
BIN := bin
CLIENT := ${BIN}/client
GOPATH ?= ${PWD}
BIN := gowebdav
SRC := $(wildcard *.go) cmd/gowebdav/main.go
all: test client
all: test cmd
client: ${CLIENT}
cmd: ${BIN}
${CLIENT}: ${SRC}
@echo build $@
@GOPATH=${GOPATH} go build -o $@ -- main/client.go
${BIN}: ${SRC}
go build -o $@ ./cmd/gowebdav
test:
@GOPATH=${GOPATH} go test
go test -v ./...
api:
@sed '/^## API$$/,$$d' -i README.md
@echo '## API' >> README.md
@godoc2md github.com/studio-b12/gowebdav | sed '/^$$/N;/^\n$$/D' |\
sed '2d' |\
sed 's/\/src\/github.com\/studio-b12\/gowebdav\//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
clean:
@echo clean ${BIN}
@rm -f ${BIN}/*
@rm -f ${BIN}
.PHONY: all client clean
.PHONY: all cmd clean test api

314
README.md
View File

@@ -1,10 +1,314 @@
# GOWEBDAV - WebDAV Client for golang
# GoWebDAV
[![Build Status](https://travis-ci.org/studio-b12/gowebdav.svg?branch=master)](https://travis-ci.org/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.
## Install
```sh
go get -u github.com/studio-b12/gowebdav/cmd/gowebdav
```
## Usage
```sh
$ gowebdav --help
Usage of gowebdav
-X string
Method:
LS <PATH>
STAT <PATH>
MKDIR <PATH>
MKDIRALL <PATH>
GET <PATH> <FILE>
PUT <PATH> <FILE>
MV <OLD> <NEW>
CP <OLD> <NEW>
DEL <PATH>
-pw string
Password [ENV.PASSWORD]
-root string
WebDAV Endpoint [ENV.ROOT]
-user string
User [ENV.USER]
```
*Example*
```sh
ROOT="https://webdav.server/" \
USER="foo" \
PASSWORD="bar" \
./gowebdav -X LS /
```
## LINKS
* [rfc2518](http://asg.andrew.cmu.edu/rfc/rfc2518.html)
* [rfc2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
* [RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV](http://www.faqs.org/rfcs/rfc2518.html "RFC 2518 - HTTP Extensions for Distributed Authoring -- WEBDAV")
* [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")
## Books
## API
* [WebDav: Next Gen](https://books.google.de/books?isbn=0130652083 "WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseault")
`import "github.com/studio-b12/gowebdav"`
* [Overview](#pkg-overview)
* [Index](#pkg-index)
* [Examples](#pkg-examples)
* [Subdirectories](#pkg-subdirectories)
### <a name="pkg-overview">Overview</a>
Package gowebdav A golang WebDAV library
### <a name="pkg-index">Index</a>
* [func FixSlash(s string) string](#FixSlash)
* [func FixSlashes(s string) string](#FixSlashes)
* [func Join(path0 string, path1 string) string](#Join)
* [func PathEscape(path string) string](#PathEscape)
* [func String(r io.Reader) string](#String)
* [type Client](#Client)
* [func NewClient(uri, user, pw string) *Client](#NewClient)
* [func (c *Client) Connect() error](#Client.Connect)
* [func (c *Client) Copy(oldpath, newpath string, overwrite bool) error](#Client.Copy)
* [func (c *Client) Mkdir(path string, _ os.FileMode) error](#Client.Mkdir)
* [func (c *Client) MkdirAll(path string, _ os.FileMode) error](#Client.MkdirAll)
* [func (c *Client) Read(path string) ([]byte, error)](#Client.Read)
* [func (c *Client) ReadDir(path string) ([]os.FileInfo, error)](#Client.ReadDir)
* [func (c *Client) ReadStream(path string) (io.ReadCloser, error)](#Client.ReadStream)
* [func (c *Client) Remove(path string) error](#Client.Remove)
* [func (c *Client) RemoveAll(path string) error](#Client.RemoveAll)
* [func (c *Client) Rename(oldpath, newpath string, overwrite bool) error](#Client.Rename)
* [func (c *Client) SetHeader(key, value string)](#Client.SetHeader)
* [func (c *Client) SetTimeout(timeout time.Duration)](#Client.SetTimeout)
* [func (c *Client) SetTransport(transport http.RoundTripper)](#Client.SetTransport)
* [func (c *Client) Stat(path string) (os.FileInfo, error)](#Client.Stat)
* [func (c *Client) Write(path string, data []byte, _ os.FileMode) error](#Client.Write)
* [func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error](#Client.WriteStream)
* [type File](#File)
* [func (f File) ContentType() string](#File.ContentType)
* [func (f File) ETag() string](#File.ETag)
* [func (f File) IsDir() bool](#File.IsDir)
* [func (f File) ModTime() time.Time](#File.ModTime)
* [func (f File) Mode() os.FileMode](#File.Mode)
* [func (f File) Name() string](#File.Name)
* [func (f File) Size() int64](#File.Size)
* [func (f File) String() string](#File.String)
* [func (f File) Sys() interface{}](#File.Sys)
##### <a name="pkg-examples">Examples</a>
* [PathEscape](#example_PathEscape)
##### <a name="pkg-files">Package files</a>
[client.go](https://github.com/studio-b12/gowebdav/blob/master/client.go) [file.go](https://github.com/studio-b12/gowebdav/blob/master/file.go) [requests.go](https://github.com/studio-b12/gowebdav/blob/master/requests.go) [utils.go](https://github.com/studio-b12/gowebdav/blob/master/utils.go)
### <a name="FixSlash">func</a> [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=707:737#L45)
``` go
func FixSlash(s string) string
```
FixSlash appends a trailing / to our string
### <a name="FixSlashes">func</a> [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=859:891#L53)
``` go
func FixSlashes(s string) string
```
FixSlashes appends and prepends a / if they are missing
### <a name="Join">func</a> [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=976:1020#L61)
``` go
func Join(path0 string, path1 string) string
```
Join joins two paths
### <a name="PathEscape">func</a> [PathEscape](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=506:541#L36)
``` go
func PathEscape(path string) string
```
PathEscape escapes all segemnts of a given path
### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=1150:1181#L66)
``` go
func String(r io.Reader) string
```
String pulls a string out of our io.Reader
### <a name="Client">type</a> [Client](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=220:301#L18)
``` go
type Client struct {
// contains filtered or unexported fields
}
```
Client defines our structure
#### <a name="NewClient">func</a> [NewClient](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=349:393#L25)
``` go
func NewClient(uri, user, pw string) *Client
```
NewClient creates a new instance of client
#### <a name="Client.Connect">func</a> (\*Client) [Connect](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1138:1170#L55)
``` go
func (c *Client) Connect() error
```
Connect connects to our dav server
#### <a name="Client.Copy">func</a> (\*Client) [Copy](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6060:6128#L281)
``` go
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error
```
Copy copies a file from A to B
#### <a name="Client.Mkdir">func</a> (\*Client) [Mkdir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5151:5207#L240)
``` go
func (c *Client) Mkdir(path string, _ os.FileMode) error
```
Mkdir makes a directory
#### <a name="Client.MkdirAll">func</a> (\*Client) [MkdirAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5386:5445#L251)
``` go
func (c *Client) MkdirAll(path string, _ os.FileMode) error
```
MkdirAll like mkdir -p, but for webdav
#### <a name="Client.Read">func</a> (\*Client) [Read](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6234:6284#L286)
``` go
func (c *Client) Read(path string) ([]byte, error)
```
Read reads the contents of a remote file
#### <a name="Client.ReadDir">func</a> (\*Client) [ReadDir](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=2226:2286#L98)
``` go
func (c *Client) ReadDir(path string) ([]os.FileInfo, error)
```
ReadDir reads the contents of a remote directory
#### <a name="Client.ReadStream">func</a> (\*Client) [ReadStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6595:6658#L304)
``` go
func (c *Client) ReadStream(path string) (io.ReadCloser, error)
```
ReadStream reads the stream for a given path
#### <a name="Client.Remove">func</a> (\*Client) [Remove](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4657:4699#L217)
``` go
func (c *Client) Remove(path string) error
```
Remove removes a remote file
#### <a name="Client.RemoveAll">func</a> (\*Client) [RemoveAll](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=4765:4810#L222)
``` go
func (c *Client) RemoveAll(path string) error
```
RemoveAll removes remote files
#### <a name="Client.Rename">func</a> (\*Client) [Rename](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=5894:5964#L276)
``` go
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error
```
Rename moves a file from A to B
#### <a name="Client.SetHeader">func</a> (\*Client) [SetHeader](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=721:766#L40)
``` go
func (c *Client) SetHeader(key, value string)
```
SetHeader lets us set arbitrary headers for a given client
#### <a name="Client.SetTimeout">func</a> (\*Client) [SetTimeout](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=866:916#L45)
``` go
func (c *Client) SetTimeout(timeout time.Duration)
```
SetTimeout exposes the ability to set a time limit for requests
#### <a name="Client.SetTransport">func</a> (\*Client) [SetTransport](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=1009:1067#L50)
``` go
func (c *Client) SetTransport(transport http.RoundTripper)
```
SetTransport exposes the ability to define custom transports
#### <a name="Client.Stat">func</a> (\*Client) [Stat](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=3613:3668#L165)
``` go
func (c *Client) Stat(path string) (os.FileInfo, error)
```
Stat returns the file stats for a specified path
#### <a name="Client.Write">func</a> (\*Client) [Write](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=6949:7018#L319)
``` go
func (c *Client) Write(path string, data []byte, _ os.FileMode) error
```
Write writes data to a given path
#### <a name="Client.WriteStream">func</a> (\*Client) [WriteStream](https://github.com/studio-b12/gowebdav/blob/master/client.go?s=7420:7500#L341)
``` go
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error
```
WriteStream writes a stream
### <a name="File">type</a> [File](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=93:253#L10)
``` go
type File struct {
// contains filtered or unexported fields
}
```
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)
``` go
func (f File) ContentType() string
```
ContentType returns the content type of a file
#### <a name="File.ETag">func</a> (File) [ETag](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=841:868#L51)
``` go
func (f File) ETag() string
```
ETag returns the ETag of a file
#### <a name="File.IsDir">func</a> (File) [IsDir](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=947:973#L56)
``` go
func (f File) IsDir() bool
```
IsDir let us see if a given file is a directory or not
#### <a name="File.ModTime">func</a> (File) [ModTime](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=748:781#L46)
``` go
func (f File) ModTime() time.Time
```
ModTime returns the modified time of a file
#### <a name="File.Mode">func</a> (File) [Mode](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=577:609#L36)
``` go
func (f File) Mode() os.FileMode
```
Mode will return the mode of a given file
#### <a name="File.Name">func</a> (File) [Name](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=290:317#L21)
``` go
func (f File) Name() string
```
Name returns the name of a file
#### <a name="File.Size">func</a> (File) [Size](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=485:511#L31)
``` go
func (f File) Size() int64
```
Size returns the size of a file
#### <a name="File.String">func</a> (File) [String](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1095:1124#L66)
``` go
func (f File) String() string
```
String lets us see file information
#### <a name="File.Sys">func</a> (File) [Sys](https://github.com/studio-b12/gowebdav/blob/master/file.go?s=1007:1038#L61)
``` go
func (f File) Sys() interface{}
```
Sys ????
- - -
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)

117
client.go
View File

@@ -1,3 +1,4 @@
// Package gowebdav A golang WebDAV library
package gowebdav
import (
@@ -13,13 +14,15 @@ import (
"time"
)
// Client defines our structure
type Client struct {
root string
headers http.Header
c *http.Client
}
func NewClient(uri string, user string, pw string) *Client {
// NewClient creates a new instance of client
func NewClient(uri, user, pw string) *Client {
c := &Client{uri, make(http.Header), &http.Client{}}
if len(user) > 0 && len(pw) > 0 {
@@ -33,26 +36,38 @@ func NewClient(uri string, user string, pw string) *Client {
return c
}
// SetHeader lets us set arbitrary headers for a given client
func (c *Client) SetHeader(key, value string) {
c.headers.Add(key, value)
}
// SetTimeout exposes the ability to set a time limit for requests
func (c *Client) SetTimeout(timeout time.Duration) {
c.c.Timeout = timeout
}
// SetTransport exposes the ability to define custom transports
func (c *Client) SetTransport(transport http.RoundTripper) {
c.c.Transport = transport
}
// Connect connects to our dav server
func (c *Client) Connect() error {
rs, err := c.options("/")
if err == nil {
rs.Body.Close()
if err != nil {
return err
}
err = rs.Body.Close()
if err != nil {
return err
}
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
return newPathError("Connect", c.root, rs.StatusCode)
}
_, err = c.ReadDir("/")
}
return err
return nil
}
type props struct {
@@ -60,8 +75,11 @@ type props struct {
Name string `xml:"DAV: prop>displayname,omitempty"`
Type xml.Name `xml:"DAV: prop>resourcetype>collection,omitempty"`
Size string `xml:"DAV: prop>getcontentlength,omitempty"`
ContentType string `xml:"DAV: prop>getcontenttype,omitempty"`
ETag string `xml:"DAV: prop>getetag,omitempty"`
Modified string `xml:"DAV: prop>getlastmodified,omitempty"`
}
type response struct {
Href string `xml:"DAV: href"`
Props []props `xml:"DAV: propstat"`
@@ -69,13 +87,14 @@ type response struct {
func getProps(r *response, status string) *props {
for _, prop := range r.Props {
if strings.Index(prop.Status, status) != -1 {
if strings.Contains(prop.Status, status) {
return &prop
}
}
return nil
}
// ReadDir reads the contents of a remote directory
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
path = FixSlashes(path)
files := make([]os.FileInfo, 0)
@@ -101,6 +120,8 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
}
f.path = path + f.name
f.modified = parseModified(&p.Modified)
f.etag = p.ETag
f.contentType = p.ContentType
if p.Type.Local == "collection" {
f.path += "/"
@@ -124,6 +145,8 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
<d:displayname/>
<d:resourcetype/>
<d:getcontentlength/>
<d:getcontenttype/>
<d:getetag/>
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
@@ -132,20 +155,23 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
if err != nil {
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
err = newPathErrorErr("ReadDir", path, err)
}
}
return files, err
}
// Stat returns the file stats for a specified path
func (c *Client) Stat(path string) (os.FileInfo, error) {
var f *File = nil
var f *File
parse := func(resp interface{}) error {
r := resp.(*response)
if p := getProps(r, "200"); p != nil && f == nil {
f = new(File)
f.name = p.Name
f.path = path
f.etag = p.ETag
f.contentType = p.ContentType
if p.Type.Local == "collection" {
if !strings.HasSuffix(f.path, "/") {
@@ -171,6 +197,8 @@ func (c *Client) Stat(path string) (os.FileInfo, error) {
<d:displayname/>
<d:resourcetype/>
<d:getcontentlength/>
<d:getcontenttype/>
<d:getetag/>
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
@@ -179,40 +207,47 @@ func (c *Client) Stat(path string) (os.FileInfo, error) {
if err != nil {
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
err = newPathErrorErr("ReadDir", path, err)
}
}
return f, err
}
// Remove removes a remote file
func (c *Client) Remove(path string) error {
return c.RemoveAll(path)
}
// RemoveAll removes remote files
func (c *Client) RemoveAll(path string) error {
rs, err := c.req("DELETE", path, nil, nil)
if err != nil {
return newPathError("Remove", path, 400)
}
rs.Body.Close()
err = rs.Body.Close()
if err != nil {
return err
}
if rs.StatusCode == 200 || rs.StatusCode == 204 || rs.StatusCode == 404 {
return nil
} else {
return newPathError("Remove", path, rs.StatusCode)
}
}
return newPathError("Remove", path, rs.StatusCode)
}
// Mkdir makes a directory
func (c *Client) Mkdir(path string, _ os.FileMode) error {
path = FixSlashes(path)
status := c.mkcol(path)
if status == 201 {
return nil
} else {
return newPathError("Mkdir", path, status)
}
}
return newPathError("Mkdir", path, status)
}
// MkdirAll like mkdir -p, but for webdav
func (c *Client) MkdirAll(path string, _ os.FileMode) error {
path = FixSlashes(path)
status := c.mkcol(path)
@@ -237,61 +272,77 @@ func (c *Client) MkdirAll(path string, _ os.FileMode) error {
return newPathError("MkdirAll", path, status)
}
func (c *Client) Rename(oldpath string, newpath string, overwrite bool) error {
// Rename moves a file from A to B
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error {
return c.copymove("MOVE", oldpath, newpath, overwrite)
}
func (c *Client) Copy(oldpath string, newpath string, overwrite bool) error {
// Copy copies a file from A to B
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error {
return c.copymove("COPY", oldpath, newpath, overwrite)
}
// Read reads the contents of a remote file
func (c *Client) Read(path string) ([]byte, error) {
if stream, err := c.ReadStream(path); err == nil {
defer stream.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes(), nil
} else {
var stream io.ReadCloser
var err error
if stream, err = c.ReadStream(path); err != nil {
return nil, err
}
defer stream.Close()
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(stream)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// ReadStream reads the stream for a given path
func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
rs, err := c.req("GET", path, nil, nil)
if err != nil {
return nil, newPathErrorErr("ReadStream", path, err)
}
if rs.StatusCode == 200 {
return rs.Body, nil
}
rs.Body.Close()
return nil, newPathError("ReadStream", path, rs.StatusCode)
}
// Write writes data to a given path
func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
s := c.put(path, bytes.NewReader(data))
switch s {
case 200, 201:
case 200, 201, 204:
return nil
case 409:
if idx := strings.LastIndex(path, "/"); idx == -1 {
// faulty root
return newPathError("Write", path, 500)
} else {
if err := c.MkdirAll(path[0:idx+1], 0755); err == nil {
if i := strings.LastIndex(path, "/"); i > -1 {
if err := c.MkdirAll(path[0:i+1], 0755); err == nil {
s = c.put(path, bytes.NewReader(data))
if s == 200 || s == 201 {
if s == 200 || s == 201 || s == 204 {
return nil
}
}
}
}
return newPathError("Write", path, s)
}
// WriteStream writes a stream
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error {
// TODO check if parent collection exists
s := c.put(path, stream)
switch s {
case 200, 201:
case 200, 201, 204:
return nil
default:

217
cmd/gowebdav/main.go Normal file
View File

@@ -0,0 +1,217 @@
package main
import (
"errors"
"flag"
"fmt"
d "github.com/studio-b12/gowebdav"
"io"
"os"
"strings"
)
func main() {
root := flag.String("root", os.Getenv("ROOT"), "WebDAV Endpoint [ENV.ROOT]")
usr := flag.String("user", os.Getenv("USER"), "User [ENV.USER]")
pw := flag.String("pw", os.Getenv("PASSWORD"), "Password [ENV.PASSWORD]")
m := flag.String("X", "", `Method:
LS <PATH>
STAT <PATH>
MKDIR <PATH>
MKDIRALL <PATH>
GET <PATH> <FILE>
PUT <PATH> <FILE>
MV <OLD> <NEW>
CP <OLD> <NEW>
DEL <PATH>
`)
flag.Parse()
if *root == "" {
fail("Set WebDAV ROOT")
}
var path0, path1 string
switch len(flag.Args()) {
case 1:
path0 = flag.Args()[0]
case 2:
path1 = flag.Args()[1]
default:
fail("Unsupported arguments")
}
c := d.NewClient(*root, *usr, *pw)
if err := c.Connect(); err != nil {
fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
}
cmd := getCmd(strings.ToUpper(*m))
if e := cmd(c, path0, path1); e != nil {
fail(e)
}
}
func fail(err interface{}) {
if err != nil {
fmt.Println(err)
}
os.Exit(-1)
}
func getCmd(method string) func(c *d.Client, p0, p1 string) error {
switch method {
case "LS", "LIST", "PROPFIND":
return cmdLs
case "STAT":
return cmdStat
case "GET", "PULL", "READ":
return cmdGet
case "DELETE", "RM", "DEL":
return cmdRm
case "MKCOL", "MKDIR":
return cmdMkdir
case "MKCOLALL", "MKDIRALL", "MKDIRP":
return cmdMkdirAll
case "RENAME", "MV", "MOVE":
return cmdMv
case "COPY", "CP":
return cmdCp
case "PUT", "PUSH", "WRITE":
return cmdPut
default:
return func(c *d.Client, p0, p1 string) (err error) {
return errors.New("Unsupported method: " + method)
}
}
}
func cmdLs(c *d.Client, p0, p1 string) (err error) {
files, err := c.ReadDir(p0)
if err == nil {
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", p0, len(files)))
for _, f := range files {
fmt.Println(f)
}
}
return
}
func cmdStat(c *d.Client, p0, p1 string) (err error) {
file, err := c.Stat(p0)
if err == nil {
fmt.Println(file)
}
return
}
func cmdGet(c *d.Client, p0, p1 string) (err error) {
bytes, err := c.Read(p0)
if err == nil {
if err = writeFile(p1, bytes, 0644); err == nil {
fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), p1))
}
}
return
}
func cmdRm(c *d.Client, p0, p1 string) (err error) {
if err = c.Remove(p0); err == nil {
fmt.Println("RM: " + p0)
}
return
}
func cmdMkdir(c *d.Client, p0, p1 string) (err error) {
if err = c.Mkdir(p0, 0755); err == nil {
fmt.Println("MkDir: " + p0)
}
return
}
func cmdMkdirAll(c *d.Client, p0, p1 string) (err error) {
if err = c.MkdirAll(p0, 0755); err == nil {
fmt.Println("MkDirAll: " + p0)
}
return
}
func cmdMv(c *d.Client, p0, p1 string) (err error) {
if err = c.Rename(p0, p1, true); err == nil {
fmt.Println("Rename: " + p0 + " -> " + p1)
}
return
}
func cmdCp(c *d.Client, p0, p1 string) (err error) {
if err = c.Copy(p0, p1, true); err == nil {
fmt.Println("Copy: " + p0 + " -> " + p1)
}
return
}
func cmdPut(c *d.Client, p0, p1 string) (err error) {
stream, err := getStream(p1)
if err == nil {
if err = c.WriteStream(p0, stream, 0644); err == nil {
fmt.Println(fmt.Sprintf("Put: '%s' -> %s", p1, p0))
}
}
return
}
func writeFile(path string, bytes []byte, mode os.FileMode) error {
f, err := os.Create(path)
defer f.Close()
if err != nil {
return err
}
_, err = f.Write(bytes)
return err
}
func getStream(pathOrString string) (io.ReadCloser, error) {
fi, err := os.Stat(pathOrString)
if err == nil {
if fi.IsDir() {
return nil, &os.PathError{
Op: "Open",
Path: pathOrString,
Err: errors.New("Path: '" + pathOrString + "' is a directory"),
}
}
f, err := os.Open(pathOrString)
if err == nil {
return f, nil
}
return nil, &os.PathError{
Op: "Open",
Path: pathOrString,
Err: err,
}
}
return nopCloser{strings.NewReader(pathOrString)}, nil
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}

30
file.go
View File

@@ -6,47 +6,67 @@ import (
"time"
)
// File is our structure for a given file
type File struct {
path string
name string
contentType string
size int64
modified time.Time
etag string
isdir bool
}
// Name returns the name of a file
func (f File) Name() string {
return f.name
}
// ContentType returns the content type of a file
func (f File) ContentType() string {
return f.contentType
}
// Size returns the size of a file
func (f File) Size() int64 {
return f.size
}
// Mode will return the mode of a given file
func (f File) Mode() os.FileMode {
// TODO check webdav perms
if f.isdir {
return 0775 | os.ModeDir
} else {
return 0664
}
}
return 0664
}
// ModTime returns the modified time of a file
func (f File) ModTime() time.Time {
return f.modified
}
// ETag returns the ETag of a file
func (f File) ETag() string {
return f.etag
}
// IsDir let us see if a given file is a directory or not
func (f File) IsDir() bool {
return f.isdir
}
// Sys ????
func (f File) Sys() interface{} {
return nil
}
// String lets us see file information
func (f File) String() string {
if f.isdir {
return fmt.Sprintf("Dir : '%s' - '%s'", f.path, f.name)
} else {
return fmt.Sprintf("File: '%s' SIZE: %d MODIFIED: %s", f.path, f.size, f.modified.String())
}
return fmt.Sprintf("File: '%s' SIZE: %d MODIFIED: %s ETAG: %s CTYPE: %s", f.path, f.size, f.modified.String(), f.etag, f.contentType)
}

View File

@@ -1,181 +0,0 @@
package main
import (
"errors"
"flag"
"fmt"
d "gowebdav"
"io"
"os"
"strings"
)
func Fail(err interface{}) {
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Usage: client FLAGS ARGS")
fmt.Println("Flags:")
flag.PrintDefaults()
fmt.Println("Method <ARGS>")
fmt.Println(" LS | LIST | PROPFIND <PATH>")
fmt.Println(" RM | DELETE | DEL <PATH>")
fmt.Println(" MKDIR | MKCOL <PATH>")
fmt.Println(" MKDIRALL | MKCOLALL <PATH>")
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
fmt.Println(" GET | PULL | READ <PATH>")
fmt.Println(" PUT | PUSH | WRITE <PATH> <FILE>")
fmt.Println(" STAT <PATH>")
}
os.Exit(-1)
}
func writeFile(path string, bytes []byte, mode os.FileMode) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(bytes)
return err
}
func main() {
root := flag.String("root", "URL", "WebDAV Endpoint")
usr := flag.String("user", "", "user")
pw := flag.String("pw", "", "password")
m := flag.String("X", "GET", "Method ...")
flag.Parse()
if *root == "URL" {
Fail(nil)
}
M := strings.ToUpper(*m)
m = &M
c := d.NewClient(*root, *usr, *pw)
if err := c.Connect(); err != nil {
Fail(fmt.Sprintf("Failed to connect due to: %s", err.Error()))
}
alen := len(flag.Args())
if alen == 1 {
path := flag.Args()[0]
switch *m {
case "LS", "LIST", "PROPFIND":
if files, err := c.ReadDir(path); err == nil {
fmt.Println(fmt.Sprintf("ReadDir: '%s' entries: %d ", path, len(files)))
for _, f := range files {
fmt.Println(f)
}
} else {
fmt.Println(err)
}
case "STAT":
if file, err := c.Stat(path); err == nil {
fmt.Println(file)
} else {
fmt.Println(err)
}
case "GET", "PULL", "READ":
if bytes, err := c.Read(path); err == nil {
if lidx := strings.LastIndex(path, "/"); lidx != -1 {
path = path[lidx+1:]
}
if err := writeFile(path, bytes, 0644); err != nil {
fmt.Println(err)
} else {
fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), path))
}
} else {
fmt.Println(err)
}
case "DELETE", "RM", "DEL":
if err := c.Remove(path); err != nil {
fmt.Println(err)
} else {
fmt.Println("Remove: " + path)
}
case "MKCOL", "MKDIR":
if err := c.Mkdir(path, 0); err != nil {
fmt.Println(err)
} else {
fmt.Println("MkDir: " + path)
}
case "MKCOLALL", "MKDIRALL":
if err := c.MkdirAll(path, 0); err != nil {
fmt.Println(err)
} else {
fmt.Println("MkDirAll: " + path)
}
default:
Fail(nil)
}
} else if alen == 2 {
a0 := flag.Args()[0]
a1 := flag.Args()[1]
switch *m {
case "RENAME", "MV", "MOVE":
if err := c.Rename(a0, a1, true); err != nil {
fmt.Println(err)
} else {
fmt.Println("Rename: " + a0 + " -> " + a1)
}
case "COPY", "CP":
if err := c.Copy(a0, a1, true); err != nil {
fmt.Println(err)
} else {
fmt.Println("Copy: " + a0 + " -> " + a1)
}
case "PUT", "PUSH", "WRITE":
stream, err := getStream(a1)
if err != nil {
Fail(err)
}
if err := c.WriteStream(a0, stream, 0644); err != nil {
fmt.Println(err)
} else {
fmt.Println(fmt.Sprintf("Written: '%s' -> %s", a1, a0))
}
default:
Fail(nil)
}
} else {
Fail(nil)
}
}
func getStream(pathOrString string) (io.ReadCloser, error) {
fi, err := os.Stat(pathOrString)
if err == nil {
if fi.IsDir() {
return nil, &os.PathError{"Open", pathOrString, errors.New("Path: '" + pathOrString + "' is a directory")}
}
f, err := os.Open(pathOrString)
if err == nil {
return f, nil
}
return nil, &os.PathError{"Open", pathOrString, err}
} else {
return nopCloser{strings.NewReader(pathOrString)}, nil
}
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}

View File

@@ -1,15 +1,14 @@
package gowebdav
import (
"errors"
"fmt"
"io"
"net/http"
"strings"
)
func (c *Client) req(method string, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
r, err := http.NewRequest(method, Join(c.root, path), body)
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)
if err != nil {
return nil, err
}
@@ -28,10 +27,10 @@ func (c *Client) req(method string, path string, body io.Reader, intercept func(
func (c *Client) mkcol(path string) int {
rs, err := c.req("MKCOL", path, nil, nil)
defer rs.Body.Close()
if err != nil {
return 400
}
rs.Body.Close()
if rs.StatusCode == 201 || rs.StatusCode == 405 {
return 201
@@ -59,13 +58,13 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
rq.Header.Add("Accept-Encoding", "")
})
defer rs.Body.Close()
if err != nil {
return err
}
defer rs.Body.Close()
if rs.StatusCode != 207 {
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, "PROPFIND", path))
return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
}
return parseXML(rs.Body, resp, parse)
@@ -88,9 +87,7 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
if data != nil {
defer data.Close()
}
switch s {
case 201, 204:
@@ -109,9 +106,10 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
func (c *Client) put(path string, stream io.Reader) int {
rs, err := c.req("PUT", path, stream, nil)
defer rs.Body.Close()
if err != nil {
return 400
}
rs.Body.Close()
return rs.StatusCode
}

View File

@@ -3,9 +3,9 @@ package gowebdav
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"net/url"
"os"
"strconv"
"strings"
@@ -17,13 +17,31 @@ func log(msg interface{}) {
}
func newPathError(op string, path string, statusCode int) error {
return &os.PathError{op, path, errors.New(fmt.Sprintf("%d", statusCode))}
return &os.PathError{
Op: op,
Path: path,
Err: fmt.Errorf("%d", statusCode),
}
}
func newPathErrorErr(op string, path string, err error) error {
return &os.PathError{op, path, err}
return &os.PathError{
Op: op,
Path: path,
Err: err,
}
}
// PathEscape escapes all segemnts of a given path
func PathEscape(path string) string {
s := strings.Split(path, "/")
for i, e := range s {
s[i] = url.PathEscape(e)
}
return strings.Join(s, "/")
}
// FixSlash appends a trailing / to our string
func FixSlash(s string) string {
if !strings.HasSuffix(s, "/") {
s += "/"
@@ -31,6 +49,7 @@ func FixSlash(s string) string {
return s
}
// FixSlashes appends and prepends a / if they are missing
func FixSlashes(s string) string {
if s[0] != '/' {
s = "/" + s
@@ -38,13 +57,16 @@ func FixSlashes(s string) string {
return FixSlash(s)
}
// Join joins two paths
func Join(path0 string, path1 string) string {
return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/")
}
// String pulls a string out of our io.Reader
func String(r io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
// TODO - make String return an error as well
_, _ = buf.ReadFrom(r)
return buf.String()
}

View File

@@ -1,6 +1,10 @@
package gowebdav
import "testing"
import (
"fmt"
"net/url"
"testing"
)
func TestJoin(t *testing.T) {
eq(t, "/", "", "")
@@ -16,3 +20,26 @@ func eq(t *testing.T, expected string, s0 string, s1 string) {
t.Error("For", "'"+s0+"','"+s1+"'", "expeted", "'"+expected+"'", "got", "'"+s+"'")
}
}
func ExamplePathEscape() {
fmt.Println(PathEscape(""))
fmt.Println(PathEscape("/"))
fmt.Println(PathEscape("/web"))
fmt.Println(PathEscape("/web/"))
fmt.Println(PathEscape("/w e b/d a v/s%u&c#k:s/"))
// Output:
//
// /
// /web
// /web/
// /w%20e%20b/d%20a%20v/s%25u&c%23k:s/
}
func TestEscapeURL(t *testing.T) {
ex := "https://foo.com/w%20e%20b/d%20a%20v/s%25u&c%23k:s/"
u, _ := url.Parse("https://foo.com" + PathEscape("/w e b/d a v/s%u&c#k:s/"))
if ex != u.String() {
t.Error("expected: " + ex + " got: " + u.String())
}
}