This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathinit.go
198 lines (177 loc) · 5.52 KB
/
init.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
187
188
189
190
191
192
193
194
195
196
197
198
package filter
import (
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"io/ioutil"
"os"
"regexp"
"strconv"
)
func init() {
caddy.RegisterPlugin("filter", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func setup(controller *caddy.Controller) error {
handler, err := parseConfiguration(controller)
if err != nil {
return err
}
config := httpserver.GetConfig(controller)
config.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
handler.next = next
return handler
})
return nil
}
func parseConfiguration(controller *caddy.Controller) (*filterHandler, error) {
handler := new(filterHandler)
handler.rules = []*rule{}
handler.maximumBufferSize = defaultMaxBufferSize
for controller.Next() {
err := evalFilterBlock(controller, handler)
if err != nil {
return nil, err
}
}
if len(handler.rules) <= 0 {
return nil, controller.Err("No rule block provided.")
}
return handler, nil
}
func evalFilterBlock(controller *caddy.Controller, target *filterHandler) error {
args := controller.RemainingArgs()
if len(args) == 0 {
return evalDefaultFilterBlock(controller, target)
}
return evalNamedBlock(controller, args, target)
}
func evalDefaultFilterBlock(controller *caddy.Controller, target *filterHandler) error {
for controller.NextBlock() {
args := []string{controller.Val()}
args = append(args, controller.RemainingArgs()...)
err := evalNamedBlock(controller, args, target)
if err != nil {
return err
}
}
return nil
}
func evalNamedBlock(controller *caddy.Controller, args []string, target *filterHandler) error {
switch args[0] {
case "rule":
return evalRule(controller, args[1:], target)
case "max_buffer_size":
return evalMaximumBufferSize(controller, args[1:], target)
}
return controller.Errf("Unknown directive: %v", args[0])
}
func evalRule(controller *caddy.Controller, args []string, target *filterHandler) (err error) {
if len(args) > 0 {
return controller.Errf("No more arguments for filter block 'rule' supported.")
}
targetRule := new(rule)
targetRule.pathAndContentTypeCombination = pathAndContentTypeAndCombination
for controller.NextBlock() {
optionName := controller.Val()
switch optionName {
case "path":
err = evalPath(controller, targetRule)
case "content_type":
err = evalContentType(controller, targetRule)
case "path_content_type_combination":
err = evalPathAndContentTypeCombination(controller, targetRule)
case "search_pattern":
err = evalSearchPattern(controller, targetRule)
case "replacement":
err = evalReplacement(controller, targetRule)
default:
err = controller.Errf("Unknown option: %v", optionName)
}
if err != nil {
return err
}
}
if targetRule.path == nil && targetRule.contentType == nil {
return controller.Errf("Neither 'path' nor 'content_type' definition was provided for filter rule block.")
}
if targetRule.searchPattern == nil {
return controller.Errf("No 'search_pattern' definition was provided for filter rule block.")
}
target.rules = append(target.rules, targetRule)
return nil
}
func evalPath(controller *caddy.Controller, target *rule) error {
return evalRegexpOption(controller, func(value *regexp.Regexp) error {
target.path = value
return nil
})
}
func evalContentType(controller *caddy.Controller, target *rule) error {
return evalRegexpOption(controller, func(value *regexp.Regexp) error {
target.contentType = value
return nil
})
}
func evalPathAndContentTypeCombination(controller *caddy.Controller, target *rule) error {
return evalSimpleOption(controller, func(plainValue string) error {
for _, candidate := range possiblePathAndContentTypeCombination {
if string(candidate) == plainValue {
target.pathAndContentTypeCombination = candidate
return nil
}
}
return controller.Errf("Illegal value for 'path_content_type_combination': %v", plainValue)
})
}
func evalSearchPattern(controller *caddy.Controller, target *rule) error {
return evalRegexpOption(controller, func(value *regexp.Regexp) error {
target.searchPattern = value
return nil
})
}
func evalReplacement(controller *caddy.Controller, target *rule) error {
return evalSimpleOption(controller, func(value string) error {
target.replacement = []byte(value)
if len(target.replacement) > 1 && target.replacement[0] == '@' {
targetFilename := string(target.replacement[1:])
content, err := ioutil.ReadFile(targetFilename)
if err != nil {
if !os.IsNotExist(err) {
return controller.Errf("Could not read file provided in 'replacement' definition. Got: %v", err)
}
} else {
target.replacement = content
}
}
return nil
})
}
func evalSimpleOption(controller *caddy.Controller, setter func(string) error) error {
args := controller.RemainingArgs()
if len(args) != 1 {
return controller.ArgErr()
}
return setter(args[0])
}
func evalRegexpOption(controller *caddy.Controller, setter func(*regexp.Regexp) error) error {
return evalSimpleOption(controller, func(plainValue string) error {
value, err := regexp.Compile(plainValue)
if err != nil {
return err
}
return setter(value)
})
}
func evalMaximumBufferSize(controller *caddy.Controller, args []string, target *filterHandler) (err error) {
if len(args) != 1 {
return controller.Errf("There are exact one argument for filter directive 'max_buffer_size' expected.")
}
value, err := strconv.Atoi(args[0])
if err != nil {
return controller.Errf("There is no valid value for filter directive 'max_buffer_size' provided. Got: %v", err)
}
target.maximumBufferSize = value
return nil
}