Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Implement some shorthand placeholders for Caddyfile
Matthew Holt committed
Aug 21, 2019 at 11:03 UTC
fa334c4bdf1c5e6c0014a36460d329c8d22cafb0
1 file changed
+23
-21
caddyconfig/httpcaddyfile/httptype.go
+23
-21
@@ -17,7 +17,6 @@ package httpcaddyfile
17
import (
18
"encoding/json"
19
"fmt"
20
- "log"
20
"reflect"
21
"sort"
22
"strconv"
@@ -39,8 +38,6 @@ func init() {
38
type ServerType struct {
39
}
40
42
-// TODO: error on unrecognized directives
43
-
41
// Setup makes a config from the tokens.
42
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
43
options map[string]string) (*caddy.Config, []caddyconfig.Warning, error) {
@@ -55,6 +52,27 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
52
}
53
54
for _, sb := range serverBlocks {
55
+ // replace shorthand placeholders (which are
56
+ // convenient when writing a Caddyfile) with
57
+ // their actual placeholder identifiers or
58
+ // variable names
59
+ replacer := strings.NewReplacer(
60
+ "{uri}", "{http.request.uri}",
61
+ "{path}", "{http.request.uri.path}",
62
+ "{host}", "{http.request.host}",
63
+ "{hostport}", "{http.request.hostport}",
64
+ "{method}", "{http.request.method}",
65
+ "{scheme}", "{http.request.scheme}",
66
+ "{file}", "{http.request.uri.path.file}",
67
+ "{dir}", "{http.request.uri.path.dir}",
68
+ "{query}", "{http.request.uri.query}",
69
+ )
70
+ for _, segment := range sb.block.Segments {
71
+ for i := 0; i < len(segment); i++ {
72
+ segment[i].Text = replacer.Replace(segment[i].Text)
73
+ }
74
+ }
75
+
76
// extract matcher definitions
77
d := sb.block.DispenseDirective("matcher")
78
matcherDefs, err := st.parseMatcherDefinitions(d)
@@ -82,8 +100,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
100
sb.pile[result.Class] = append(sb.pile[result.Class], result)
101
}
102
} else {
85
- // TODO: this should be an error
86
- log.Printf("%s not registered", dir)
103
+ tkn := segment[0]
104
+ return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
105
}
106
}
107
}
@@ -97,22 +115,6 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
115
// reduce
116
pairings := st.consolidateAddrMappings(sbmap)
117
100
- // TODO: shorthand placeholders
101
- // for _, p := range pairings {
102
- // for _, sblock := range p.serverBlocks {
103
- // for _, tokens := range sblock.Tokens {
104
- // for i := 0; i < len(tokens); i++ {
105
- // switch tokens[i].Text {
106
- // case "{uri}":
107
- // tokens[i].Text = "{http.request.uri}"
108
- // case "{path}":
109
- // tokens[i].Text = "{http.request.uri.path}"
110
- // }
111
- // }
112
- // }
113
- // }
114
- // }
115
-
118
// each pairing of listener addresses to list of server
119
// blocks is basically a server definition
120
servers, err := st.serversFromPairings(pairings, &warnings)