This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup.go
224 lines (207 loc) · 6.04 KB
/
setup.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
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
/*
* Copyright (c) 2017-2018 Kurt Jung (Gmail: kurt.w.jung)
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package cgi
import (
"path/filepath"
"strings"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
func init() {
caddy.RegisterPlugin("cgi", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// configureServer processes the tokens collected from the Caddy configuration
// file for the "cgi" directives and, if successful, inserts the cgi handler
// into the middleware chain.
func configureServer(ctrl *caddy.Controller, cfg *httpserver.SiteConfig) (err error) {
var root string
root, err = filepath.Abs(cfg.Root)
if err == nil {
var rules []ruleType
rules, err = cgiParse(ctrl)
if err == nil {
cfg.AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return handlerType{
next: next,
rules: rules,
root: root,
}
})
}
}
return
}
// setup configures a new CGI middleware instance with the specified filesystem
// root
func setup(c *caddy.Controller) (err error) {
return configureServer(c, httpserver.GetConfig(c))
}
// parseDir parses an "dir" line
func parseDir(rule *ruleType, args []string) (err error) {
if len(args) == 1 {
if rule.dir == "" {
rule.dir = args[0]
} else {
err = errorf("\"dir\" may only be specified once per block")
}
} else {
err = errorf("expecting exactly one argument to follow \"dir\"")
}
return
}
// parseExec parses an "exec" line
func parseExec(rule *ruleType, args []string) (err error) {
if len(args) > 0 {
rule.exe = args[0]
rule.args = append(rule.args, args[1:]...)
} else {
err = errorf("expecting at least one argument to follow \"exec\"")
}
return
}
// parseMatch parses a match line
func parseMatch(rule *ruleType, args []string) (err error) {
if len(args) > 0 {
rule.matches = append(rule.matches, args...)
} else {
err = errorf("expecting at least one argument to follow \"match\"")
}
return
}
// parseExcept parses a match line
func parseExcept(rule *ruleType, args []string) (err error) {
if len(args) > 0 {
rule.exceptions = append(rule.exceptions, args...)
} else {
err = errorf("expecting at least one argument to follow \"except\"")
}
return
}
// parseInspect parses a line beginning with the "inspect" subdirective
func parseInspect(rule *ruleType, args []string) (err error) {
if len(args) == 0 {
rule.inspect = true
} else {
err = errorf("not expecting any arguments to follow \"inspect\"")
}
return
}
// parseAllEnv parses a line beginning with the "pass_all_env" subdirective
func parseAllEnv(rule *ruleType, args []string) (err error) {
if len(args) == 0 {
rule.passAll = true
} else {
err = errorf("not expecting any arguments to follow \"pass_all_env\"")
}
return
}
// parseEnv parses a list of "key = value" pairs on a line
func parseEnv(envs *[][2]string, args []string) (err error) {
count := len(args)
for j := 0; j < count && err == nil; j++ {
pair := strings.SplitN(args[j], "=", 2)
if len(pair) == 2 {
var kv [2]string
kv[0] = trim(pair[0])
kv[1] = trim(pair[1])
*envs = append(*envs, kv)
} else {
err = errorf("expecting key=value format, got \"%s\"", args[j])
}
}
return
}
func parseToken(val string, rule *ruleType, args []string, loop *bool) (err error) {
switch val {
case "match": // [1..n]
err = parseMatch(rule, args)
case "except": // [0..n]
err = parseExcept(rule, args)
case "exec": // [1]
err = parseExec(rule, args)
case "env": // [0..n]
err = parseEnv(&rule.envs, args)
case "pass_env": // [0..n]
rule.passEnvs = append(rule.passEnvs, args...)
case "empty_env": // [0..n]
rule.emptyEnvs = append(rule.emptyEnvs, args...)
case "pass_all_env": // [0]
err = parseAllEnv(rule, args)
case "dir": // [1]
err = parseDir(rule, args)
case "inspect": // [0]
err = parseInspect(rule, args)
case "}":
*loop = false
}
return
}
// parseBlock parses the advance brace-block form of a "cgi" configuration
// directive
func parseBlock(c *caddy.Controller) (rule ruleType, err error) {
if c.Next() {
if c.Val() == "{" {
loop := true
for err == nil && loop && c.Next() {
val := c.Val()
args := c.RemainingArgs()
err = parseToken(val, &rule, args, &loop)
}
if len(rule.matches) == 0 {
err = errorf("block must contain at least one \"match\" subdirective")
} else if rule.exe == "" {
err = errorf("block must contain an \"exec\" subdirective")
}
} else {
err = errorf("expecting \"{\", got \"%s\"", c.Val())
}
} else {
err = errorf("expecting brace block directive")
}
return
}
// cgiParse parses one or more "cgi" configuration directives
func cgiParse(c *caddy.Controller) (rules []ruleType, err error) {
for err == nil && c.Next() {
val := c.Val()
args := c.RemainingArgs()
// printf("Line %2d: [%s] [%s]\n", c.Line(), val, join(args, ", "))
if val == "cgi" {
switch {
case len(args) == 0: // advanced brace-block syntax
var rule ruleType
rule, err = parseBlock(c)
if err == nil {
rules = append(rules, rule)
}
case len(args) >= 2: // simple one-line syntax: one match, exe, args
rules = append(rules, ruleType{
matches: []string{args[0]},
exe: args[1],
args: args[2:],
})
default:
err = errorf("expecting at least 2 arguments for simple directive, got %d", len(args))
}
} else {
err = errorf("expecting \"cgi\", got \"%s\"", val)
}
}
return
}