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
247
248
249
250
251
252
253
254
255
256
257
|
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httpcaddyfile
import (
"encoding/json"
"fmt"
"html"
"net/http"
"github.com/caddyserver/caddy/v2/caddyconfig"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/v2/modules/caddytls"
)
func (st *ServerType) parseRoot(
tkns []caddyfile.Token,
matcherDefs map[string]map[string]json.RawMessage,
warnings *[]caddyconfig.Warning,
) ([]caddyhttp.Route, error) {
var routes []caddyhttp.Route
matchersAndTokens, err := st.tokensToMatcherSets(tkns, matcherDefs, warnings)
if err != nil {
return nil, err
}
for _, mst := range matchersAndTokens {
d := caddyfile.NewDispenser("Caddyfile", mst.tokens)
var root string
for d.Next() {
if !d.NextArg() {
return nil, d.ArgErr()
}
root = d.Val()
if d.NextArg() {
return nil, d.ArgErr()
}
}
varsHandler := caddyhttp.VarsMiddleware{"root": root}
route := caddyhttp.Route{
Handle: []json.RawMessage{
caddyconfig.JSONModuleObject(varsHandler, "handler", "vars", warnings),
},
}
if mst.matcherSet != nil {
route.MatcherSets = []map[string]json.RawMessage{mst.matcherSet}
}
routes = append(routes, route)
}
return routes, nil
}
func (st *ServerType) parseRedir(
tkns []caddyfile.Token,
matcherDefs map[string]map[string]json.RawMessage,
warnings *[]caddyconfig.Warning,
) ([]caddyhttp.Route, error) {
var routes []caddyhttp.Route
matchersAndTokens, err := st.tokensToMatcherSets(tkns, matcherDefs, warnings)
if err != nil {
return nil, err
}
for _, mst := range matchersAndTokens {
var route caddyhttp.Route
d := caddyfile.NewDispenser("Caddyfile", mst.tokens)
for d.Next() {
if !d.NextArg() {
return nil, d.ArgErr()
}
to := d.Val()
var code string
if d.NextArg() {
code = d.Val()
}
if code == "permanent" {
code = "301"
}
if code == "temporary" || code == "" {
code = "307"
}
var body string
if code == "meta" {
// Script tag comes first since that will better imitate a redirect in the browser's
// history, but the meta tag is a fallback for most non-JS clients.
const metaRedir = `<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<script>window.location.replace("%s");</script>
<meta http-equiv="refresh" content="0; URL='%s'">
</head>
<body>Redirecting to <a href="%s">%s</a>...</body>
</html>
`
safeTo := html.EscapeString(to)
body = fmt.Sprintf(metaRedir, safeTo, safeTo, safeTo, safeTo)
}
handler := caddyhttp.StaticResponse{
StatusCode: caddyhttp.WeakString(code),
Headers: http.Header{"Location": []string{to}},
Body: body,
}
route.Handle = append(route.Handle,
caddyconfig.JSONModuleObject(handler, "handler", "static_response", warnings))
}
if mst.matcherSet != nil {
route.MatcherSets = []map[string]json.RawMessage{mst.matcherSet}
}
routes = append(routes, route)
}
return routes, nil
}
func (st *ServerType) parseTLSAutomationManager(d *caddyfile.Dispenser) (caddytls.ACMEManagerMaker, error) {
var m caddytls.ACMEManagerMaker
for d.Next() {
firstLine := d.RemainingArgs()
if len(firstLine) == 1 && firstLine[0] != "off" {
m.Email = firstLine[0]
}
var hasBlock bool
for d.NextBlock() {
hasBlock = true
switch d.Val() {
case "ca":
arg := d.RemainingArgs()
if len(arg) != 1 {
return m, d.ArgErr()
}
m.CA = arg[0]
// TODO: other properties
}
}
// a naked tls directive is not allowed
if len(firstLine) == 0 && !hasBlock {
return m, d.ArgErr()
}
}
return m, nil
}
func (st *ServerType) parseTLSCerts(d *caddyfile.Dispenser) (map[string]caddytls.CertificateLoader, error) {
var fileLoader caddytls.FileLoader
var folderLoader caddytls.FolderLoader
for d.Next() {
// file loader
firstLine := d.RemainingArgs()
if len(firstLine) == 2 {
fileLoader = append(fileLoader, caddytls.CertKeyFilePair{
Certificate: firstLine[0],
Key: firstLine[1],
// TODO: tags, for enterprise module's certificate selection
})
}
// folder loader
for d.NextBlock() {
if d.Val() == "load" {
folderLoader = append(folderLoader, d.RemainingArgs()...)
}
}
}
// put configured loaders into the map
loaders := make(map[string]caddytls.CertificateLoader)
if len(fileLoader) > 0 {
loaders["load_files"] = fileLoader
}
if len(folderLoader) > 0 {
loaders["load_folders"] = folderLoader
}
return loaders, nil
}
func (st *ServerType) parseTLSConnPolicy(d *caddyfile.Dispenser) (*caddytls.ConnectionPolicy, error) {
cp := new(caddytls.ConnectionPolicy)
for d.Next() {
for d.NextBlock() {
switch d.Val() {
case "protocols":
args := d.RemainingArgs()
if len(args) == 0 {
return nil, d.SyntaxErr("one or two protocols")
}
if len(args) > 0 {
if _, ok := caddytls.SupportedProtocols[args[0]]; !ok {
return nil, d.Errf("Wrong protocol name or protocol not supported: '%s'", args[0])
}
cp.ProtocolMin = args[0]
}
if len(args) > 1 {
if _, ok := caddytls.SupportedProtocols[args[1]]; !ok {
return nil, d.Errf("Wrong protocol name or protocol not supported: '%s'", args[1])
}
cp.ProtocolMax = args[1]
}
case "ciphers":
for d.NextArg() {
if _, ok := caddytls.SupportedCipherSuites[d.Val()]; !ok {
return nil, d.Errf("Wrong cipher suite name or cipher suite not supported: '%s'", d.Val())
}
cp.CipherSuites = append(cp.CipherSuites, d.Val())
}
case "curves":
for d.NextArg() {
if _, ok := caddytls.SupportedCurves[d.Val()]; !ok {
return nil, d.Errf("Wrong curve name or curve not supported: '%s'", d.Val())
}
cp.Curves = append(cp.Curves, d.Val())
}
case "alpn":
args := d.RemainingArgs()
if len(args) == 0 {
return nil, d.ArgErr()
}
cp.ALPN = args
}
}
}
return cp, nil
}
|