-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdav.go
82 lines (77 loc) · 1.66 KB
/
dav.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
//req = requests.Request(
//"https://elearning.hslf.Url.ch/ilias/webdav.php/hslu/ref_1669268/",method="PROPFIND",
//auth=('user','pw'))
import (
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
"time"
)
func Dav(f File, ch chan File) {
if f.Url.Scheme == "davs" {
f.Url.Scheme = "https"
} else if f.Url.Scheme == "dav" {
f.Url.Scheme = "http"
}
req, err := http.NewRequest("PROPFIND", f.Url.String(), nil)
if err != nil {
f.SendErr(ch, &err)
return
}
user, pw, err := Keychain(f.Url)
if err != nil {
f.SendErr(ch, &err)
return
}
req.SetBasicAuth(user, pw)
resp, err := HClient.Do(req)
if err != nil {
f.SendErr(ch, &err)
return
}
defer resp.Body.Close()
msg, err := ioutil.ReadAll(resp.Body)
if err != nil {
f.SendErr(ch, &err)
return
}
var result Multistatus
err = xml.Unmarshal(msg, &result)
if err != nil {
f.SendErr(ch, &err)
return
}
for _, resp := range result.Responses {
fileurl, err := url.Parse(f.Url.String() + resp.Name[len(f.Url.Path):])
if err != nil {
f.SendErr(ch, &err)
continue
}
mtime, err := time.Parse(time.RFC1123, resp.Mtime)
if err != nil {
f.SendErr(ch, &err)
continue
}
filepath := resp.Name[len(f.Url.Path):]
filepath, err = url.QueryUnescape(filepath)
if err != nil {
f.SendErr(ch, &err)
continue
}
if resp.Mimetype == "httpd/unix-directory" {
continue
}
ch <- File{Path: filepath, Url: *fileurl, Mtime: mtime}
}
return
}
type Multistatus struct {
Responses []Response `xml:"response"`
}
type Response struct {
Name string `xml:"href"`
Mtime string `xml:"propstat>prop>getlastmodified"`
Mimetype string `xml:"propstat>prop>getcontenttype"`
}