forked from hacdias/caddy-v1-webdav
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebdav.go
More file actions
228 lines (191 loc) · 4.54 KB
/
webdav.go
File metadata and controls
228 lines (191 loc) · 4.54 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package webdav
import (
"net/http"
"regexp"
"strconv"
"strings"
wd "golang.org/x/net/webdav"
"github.com/admpub/caddy"
"github.com/admpub/caddy/caddyhttp/httpserver"
"github.com/admpub/webdav/v4/lib"
)
func init() {
caddy.RegisterPlugin("webdav", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func DirFS(u *lib.User, conf *lib.Config, options map[string]string) wd.FileSystem {
return lib.WebDavDir{
Dir: wd.Dir(u.Scope),
User: u,
NoSniff: conf.NoSniff,
}
}
func LazyloadFS(fs lib.FSGenerator) func(u *lib.User, conf *lib.Config, options map[string]string) wd.FileSystem {
return func(u *lib.User, conf *lib.Config, options map[string]string) wd.FileSystem {
return lib.WebDavFS{
FS: lib.FS{
Scope: u.Scope,
FS: fs,
Options: options,
},
User: u,
NoSniff: conf.NoSniff,
}
}
}
var FSGenerator = DirFS
// 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
options map[string]string
}
func (c *config) SetBaseURL(baseURL string) *config {
c.baseURL = baseURL
if !strings.HasPrefix(c.baseURL, "/") {
c.baseURL = "/" + c.baseURL
}
c.baseURL = strings.TrimSuffix(c.baseURL, "/")
if c.baseURL == "/" {
c.baseURL = ""
}
return c
}
// 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: "",
options: map[string]string{},
Config: &lib.Config{
Auth: false, // Must use basicauth directive for this.
NoSniff: true,
Users: map[string]*lib.User{},
User: &lib.User{
Scope: ".",
Rules: []*lib.Rule{},
Modify: true,
},
},
}
args := c.RemainingArgs()
if len(args) > 0 {
conf.SetBaseURL(args[0])
}
if len(args) > 1 {
for i, v := range args[1:] {
k := `arg` + strconv.Itoa(i)
conf.options[k] = v
}
}
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",
Modify: u.Modify,
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()
}
if c.NextArg() {
switch c.Val() {
case `+w`:
rule.Modify = true
case `-w`:
rule.Modify = false
}
}
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: FSGenerator(u, conf.Config, conf.options),
LockSystem: wd.NewMemLS(),
}
conf.Users[val] = &lib.User{
Global: conf.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: FSGenerator(u, conf.Config, conf.options),
LockSystem: wd.NewMemLS(),
}
configs = append(configs, conf)
}
return configs, nil
}