This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
webdav.go
186 lines (149 loc) · 3.57 KB
/
webdav.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package webdav
import (
"net/http"
"regexp"
"strconv"
"strings"
wd "golang.org/x/net/webdav"
"github.com/hacdias/webdav/v3/lib"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
func init() {
caddy.RegisterPlugin("webdav", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// WebDav is the middleware that contains the configuration for each instance.
type WebDav struct {
Next httpserver.Handler
Configs []*config
}
type config struct {
*lib.Config
baseURL string
}
// ServeHTTP determines if the request is for this plugin, and if all prerequisites are met.
func (d WebDav) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
for i := range d.Configs {
// Checks if the current request is for the current configuration.
if !httpserver.Path(r.URL.Path).Matches(d.Configs[i].baseURL) {
continue
}
d.Configs[i].ServeHTTP(w, r)
return 0, nil
}
return d.Next.ServeHTTP(w, r)
}
// setup configures a new FileManager middleware instance.
func setup(c *caddy.Controller) error {
configs, err := parse(c)
if err != nil {
return err
}
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return WebDav{Configs: configs, Next: next}
})
return nil
}
func parse(c *caddy.Controller) ([]*config, error) {
configs := []*config{}
for c.Next() {
conf := &config{
baseURL: "/",
Config: &lib.Config{
Auth: false, // Must use basicauth directive for this.
Users: map[string]*lib.User{},
User: &lib.User{
Scope: ".",
Rules: []*lib.Rule{},
Modify: true,
},
},
}
args := c.RemainingArgs()
if len(args) > 0 {
conf.baseURL = args[0]
}
if len(args) > 1 {
return nil, c.ArgErr()
}
conf.baseURL = strings.TrimSuffix(conf.baseURL, "/")
conf.baseURL = strings.TrimPrefix(conf.baseURL, "/")
conf.baseURL = "/" + conf.baseURL
if conf.baseURL == "/" {
conf.baseURL = ""
}
u := conf.User
for c.NextBlock() {
switch c.Val() {
case "scope":
if !c.NextArg() {
return nil, c.ArgErr()
}
u.Scope = c.Val()
case "allow", "allow_r", "block", "block_r":
ruleType := c.Val()
if !c.NextArg() {
return configs, c.ArgErr()
}
if c.Val() == "dotfiles" && !strings.HasSuffix(ruleType, "_r") {
ruleType += "_r"
}
rule := &lib.Rule{
Allow: ruleType == "allow" || ruleType == "allow_r",
Regex: ruleType == "allow_r" || ruleType == "block_r",
}
if rule.Regex {
if c.Val() == "dotfiles" {
rule.Regexp = regexp.MustCompile(`\/\..+`)
} else {
rule.Regexp = regexp.MustCompile(c.Val())
}
} else {
rule.Path = c.Val()
}
u.Rules = append(u.Rules, rule)
case "modify":
if !c.NextArg() {
u.Modify = true
continue
}
val, err := strconv.ParseBool(c.Val())
if err != nil {
return nil, err
}
u.Modify = val
default:
if c.NextArg() {
return nil, c.ArgErr()
}
val := c.Val()
if !strings.HasSuffix(val, ":") {
return nil, c.ArgErr()
}
val = strings.TrimSuffix(val, ":")
u.Handler = &wd.Handler{
Prefix: conf.baseURL,
FileSystem: wd.Dir(u.Scope),
LockSystem: wd.NewMemLS(),
}
conf.Users[val] = &lib.User{
Rules: conf.Rules,
Scope: conf.Scope,
Modify: conf.Modify,
Handler: conf.Handler,
}
u = conf.Users[val]
}
}
u.Handler = &wd.Handler{
Prefix: conf.baseURL,
FileSystem: wd.Dir(u.Scope),
LockSystem: wd.NewMemLS(),
}
configs = append(configs, conf)
}
return configs, nil
}