-
Notifications
You must be signed in to change notification settings - Fork 23
/
if.go
164 lines (158 loc) · 5.41 KB
/
if.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
package nginxconf
import (
"encoding/json"
"fmt"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/encode"
caddygzip "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode/gzip"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
)
func (ss *setupState) ifContext(dirs []Directive) ([]json.RawMessage, []caddyconfig.Warning) {
var warnings []caddyconfig.Warning
var handlers []json.RawMessage
for _, dir := range dirs {
var warns []caddyconfig.Warning
switch dir.Name() {
case "break":
h := caddyhttp.StaticResponse{
Close: true,
}
encodedHandler := caddyconfig.JSONModuleObject(h, "handler", "static_response", &warns)
handlers = append(handlers, encodedHandler)
case "return":
h, w := processReturn(dir)
warns = append(warns, w...)
encodedHandler := caddyconfig.JSONModuleObject(h, "handler", "static_response", &warns)
handlers = append(handlers, encodedHandler)
case "rewrite":
h, w := processRewrite(dir)
warns = append(warns, w...)
encodedHandler := caddyconfig.JSONModuleObject(h, "handler", "subroute", &warns)
handlers = append(handlers, encodedHandler)
case "set":
h := caddyhttp.VarsMiddleware{
dir.Param(1): dir.Param(2),
}
handlers = append(handlers, caddyconfig.JSONModuleObject(h, "handler", "vars", &warns))
default:
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: ErrUnrecognized,
})
}
warnings = append(warnings, warns...)
}
return handlers, warnings
}
func (ss *setupState) ifInLocationContext(dirs []Directive) ([]json.RawMessage, []caddyconfig.Warning) {
var warnings []caddyconfig.Warning
var handlers []json.RawMessage
for _, dir := range dirs {
var warns []caddyconfig.Warning
switch dir.Name() {
case "root":
fileServer := fileserver.FileServer{
Root: dir.Param(1),
// TODO: all remaining fields...
}
handlers = append(handlers, caddyconfig.JSONModuleObject(fileServer, "handler", "file_server", &warns))
case "fastcgi_pass":
h, w := processFastCGIPass([]Directive{dir})
warns = append(warns, w...)
handlers = append(handlers, caddyconfig.JSONModuleObject(h, "handler", "subroute", &warns))
case "gzip":
h := encode.Encode{
EncodingsRaw: caddy.ModuleMap{
"gzip": caddyconfig.JSON(caddygzip.Gzip{}, nil),
},
}
handlers = append(handlers, caddyconfig.JSONModuleObject(h, "handler", "encode", &warns))
case "add_header":
hdr, w := processAddHeader(dir)
warns = append(warns, w...)
handlers = append(handlers, caddyconfig.JSONModuleObject(hdr, "handler", "headers", &warns))
case "expires":
hdr, w := processExpires(dir)
warns = append(warns, w...)
handlers = append(handlers, caddyconfig.JSONModuleObject(hdr, "handler", "headers", &warns))
case "proxy_pass":
h, w := processProxyPass(dir, ss.upstreams)
warns = append(warns, w...)
handlers = append(handlers, caddyconfig.JSONModuleObject(h, "handler", "reverse_proxy", &warns))
default:
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: ErrUnrecognized,
})
}
warnings = append(warnings, warns...)
}
return handlers, warnings
}
func calculateIfMatcher(dir Directive) (caddy.ModuleMap, []caddyconfig.Warning) {
var warns []caddyconfig.Warning
var routeMatcher caddy.ModuleMap
switch len(dir.Params) {
case 2: // something like this: if ($invalid_referer)
arg := strings.Trim(dir.Param(1), "()")
routeMatcher = caddy.ModuleMap{
"vars": caddyconfig.JSON(caddyhttp.VarsMatcher{getCaddyVar(arg): []string{"true"}}, &warns),
}
case 4: // something like this: if ($http_cookie ~* "id=([^;]+)(?:;|$)")
loperand, op, roperand := strings.TrimLeft(dir.Param(1), "("), dir.Param(2), strings.TrimRight(dir.Param(3), ")")
switch op {
case "=":
// Caddy sets a collection of HTTP variables to the request context, so the VarMatcher
// as wildcard matcher.
// https://github.com/caddyserver/caddy/blob/271b5af14894a8cca5fc6aa6f1c17823a1fb5ff3/modules/caddyhttp/server.go#L139
routeMatcher = caddy.ModuleMap{
"vars": caddyconfig.JSON(caddyhttp.VarsMatcher{getCaddyVar(loperand): []string{roperand}}, &warns),
}
case "~", "!~", "~*", "!~*": // regexps
pattern := roperand
if strings.HasSuffix(pattern, "*") {
pattern = "(?i)" + pattern // case-insensitive matching
}
routeMatcher = caddy.ModuleMap{
"vars_regexp": caddyconfig.JSON(caddyhttp.MatchVarsRE{
getCaddyVar(loperand): &caddyhttp.MatchRegexp{
Pattern: pattern,
},
}, &warns),
}
if op == "!~" || op == "!~*" {
routeMatcher = caddy.ModuleMap{
"not": caddyconfig.JSON(caddyhttp.MatchNot{
MatcherSetsRaw: []caddy.ModuleMap{
routeMatcher,
},
}, &warns),
}
}
default:
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: fmt.Sprintf("unsupported `if` operator: %s", dir.Param(2)),
})
return nil, warns
}
default:
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: fmt.Sprintf("unsupported count of `if` arguments: %v", dir.Params),
})
return nil, warns
}
return routeMatcher, warns
}