-
Notifications
You must be signed in to change notification settings - Fork 23
/
server.go
246 lines (223 loc) · 6.87 KB
/
server.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package nginxconf
import (
"encoding/json"
"strconv"
"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/fileserver"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
)
func (ss *setupState) serverContext(dirs []Directive) ([]caddyconfig.Warning, error) {
var warnings []caddyconfig.Warning
srv := new(caddyhttp.Server)
srvName := "server_" + strconv.Itoa(len(ss.servers))
route := caddyhttp.Route{}
// slice of maps
var matcherSets []map[string]caddyhttp.RequestMatcher
var hostMatcher map[string]caddyhttp.RequestMatcher
var logName string
var hosts []string
nextDirective:
for _, dir := range dirs {
var warns []caddyconfig.Warning
switch dir.Name() {
case "listen":
addr := dir.Param(1)
if strings.HasPrefix(addr, "unix:") {
// unix socket
addr = "unix/" + addr[5:]
} else if isNumeric(addr) {
// port only
addr = ":" + addr
}
// see if existing server has this address, and if so, use
// it; Caddy does not allow servers to have overlapping
// listener addresses
for otherSrvName, otherSrv := range ss.servers {
for _, otherAddr := range otherSrv.Listen {
if addr == otherAddr {
srv = otherSrv
srvName = otherSrvName
continue nextDirective
}
}
}
srv.Listen = append(srv.Listen, addr)
case "server_name":
hostMatcher = map[string]caddyhttp.RequestMatcher{
"host": caddyhttp.MatchHost(dir.Params[1:]),
}
matcherSets = append(matcherSets, hostMatcher)
hosts = append(hosts, dir.Params[1:]...)
case "location":
var matcher caddyhttp.RequestMatcher
matchConfMap := make(map[string]caddyhttp.RequestMatcher)
if len(dir.Params) > 2 {
switch dir.Param(1) {
case "=":
matchConfMap["path"] = caddyhttp.MatchPath(dir.Params[2:])
case "~", "~*":
pattern := dir.Param(2)
if strings.HasSuffix(pattern, "*") {
pattern = "(?i)" + pattern // case-insensitive matching
}
matchConfMap["path_regexp"] = caddyhttp.MatchPathRE{
MatchRegexp: caddyhttp.MatchRegexp{
Pattern: pattern,
},
}
case "^~":
/*
What it does is... if it is matched, then no regular expression locations will try to be matched.
It basically terminates location block matching.
https://www.keycdn.com/support/nginx-location-directive
*/
matchConfMap["path"] = caddyhttp.MatchPath([]string{dir.Param(2) + "*"})
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: "the adapter treats the ^~ location modifier as prefix match only, with no prioritization",
})
}
} else if len(dir.Params) == 2 { // only path
if strings.HasPrefix(dir.Param(1), "@") {
warnings = append(warnings, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: ErrNamedLocation,
})
continue nextDirective
}
// append wild character because nginx treat naked path matchers as prefix matchers
matcher = caddyhttp.MatchPath([]string{dir.Param(1) + "*"})
matchConfMap["path"] = matcher
}
locationMatcherSet := append(matcherSets[:], matchConfMap)
subroutes, warns, err := ss.locationContext(matchConfMap, dir.Block)
warnings = append(warnings, warns...)
if err != nil || len(subroutes) == 0 {
return warnings, err
}
var matcherSetsEnc caddyhttp.RawMatcherSets
// encode the matchers then set the result as raw matcher config
matcherSetsEnc, err = encodeMatcherSets(locationMatcherSet)
if err != nil {
return warnings, err
}
// set the matcher to route
route.MatcherSetsRaw = matcherSetsEnc
h := caddyhttp.Subroute{
Routes: subroutes,
}
route.HandlersRaw = []json.RawMessage{
caddyconfig.JSONModuleObject(h, "handler", "subroute", &warns),
}
// append the route
srv.Routes = append(srv.Routes, route)
// empty the route for next iteration
route = caddyhttp.Route{}
case "root":
route.MatcherSetsRaw = []caddy.ModuleMap{
{
"host": caddyconfig.JSON(hostMatcher["host"], &warns),
},
}
fileServer := fileserver.FileServer{
Root: dir.Param(1),
// TODO: all remaining fields...
}
// inject the argument of the index directive if exists
if indexDir, found := getDirective(dirs, "index"); found {
fileServer.IndexNames = indexDir.Params[1:]
}
route.HandlersRaw = append(route.HandlersRaw,
caddyconfig.JSONModuleObject(fileServer, "handler", "file_server", &warns),
)
// append the route
srv.Routes = append(srv.Routes, route)
case "access_log":
if dir.Param(1) == "off" {
continue nextDirective
}
// just mark the variable
logName = dir.Param(1)
case "rewrite":
reqMatcher := caddyhttp.MatchPathRE{
MatchRegexp: caddyhttp.MatchRegexp{
Pattern: dir.Param(1),
},
}
rewriteHandler := rewrite.Rewrite{
URI: dir.Param(2),
}
route.MatcherSetsRaw = []caddy.ModuleMap{
{
"path_regexp": caddyconfig.JSON(reqMatcher, &warns),
},
}
route.HandlersRaw = []json.RawMessage{
caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", &warns),
}
// append the route
srv.Routes = append(srv.Routes, route)
// empty the route for next iteration
route = caddyhttp.Route{}
case "if":
matcher, w := calculateIfMatcher(dir)
warns = append(warns, w...)
if matcher == nil { // warning of failures already appended
continue nextDirective
}
route.MatcherSetsRaw = []caddy.ModuleMap{matcher}
hs, w := ss.ifContext(dir.Block)
route.HandlersRaw = hs
// append the route
srv.Routes = append(srv.Routes, route)
// empty the route for next iteration
route = caddyhttp.Route{}
warns = append(warns, w...)
default:
warns = append(warns, caddyconfig.Warning{
File: dir.File,
Line: dir.Line,
Directive: dir.Name(),
Message: ErrUnrecognized,
})
}
warnings = append(warnings, warns...)
}
if !(route).Empty() {
srv.Routes = append(srv.Routes, route)
}
if logName != "" {
loggerName := strings.Join(hosts, "-") + "_log"
fileWriter := map[string]interface{}{
"filename": logName,
}
loggerJSON := caddyconfig.JSONModuleObject(fileWriter, "output", "file", &warnings)
if ss.mainConfig.Logging == nil {
ss.mainConfig.Logging = &caddy.Logging{
Logs: make(map[string]*caddy.CustomLog),
}
}
ss.mainConfig.Logging.Logs[loggerName] = &caddy.CustomLog{
BaseLog: caddy.BaseLog{
WriterRaw: loggerJSON,
},
}
if srv.Logs == nil {
srv.Logs = &caddyhttp.ServerLogConfig{
LoggerNames: make(map[string]caddyhttp.StringArray),
}
}
for _, v := range hosts {
srv.Logs.LoggerNames[v] = caddyhttp.StringArray{loggerName}
}
}
ss.servers[srvName] = srv
return warnings, nil
}