summaryrefslogtreecommitdiff
path: root/caddyconfig
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-08-21 11:03:50 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-08-21 11:03:50 -0600
commitfa334c4bdf1c5e6c0014a36460d329c8d22cafb0 (patch)
tree27f006fc9c072cda0143d8e2c12356422fdce7b6 /caddyconfig
parentd73b650c266a1fe1c6ae4ecc9a51b2ef3ed30b14 (diff)
Implement some shorthand placeholders for Caddyfile
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go44
1 files changed, 23 insertions, 21 deletions
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index 1c12ccd..42c1be5 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -17,7 +17,6 @@ package httpcaddyfile
import (
"encoding/json"
"fmt"
- "log"
"reflect"
"sort"
"strconv"
@@ -39,8 +38,6 @@ func init() {
type ServerType struct {
}
-// TODO: error on unrecognized directives
-
// Setup makes a config from the tokens.
func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
options map[string]string) (*caddy.Config, []caddyconfig.Warning, error) {
@@ -55,6 +52,27 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
}
for _, sb := range serverBlocks {
+ // replace shorthand placeholders (which are
+ // convenient when writing a Caddyfile) with
+ // their actual placeholder identifiers or
+ // variable names
+ replacer := strings.NewReplacer(
+ "{uri}", "{http.request.uri}",
+ "{path}", "{http.request.uri.path}",
+ "{host}", "{http.request.host}",
+ "{hostport}", "{http.request.hostport}",
+ "{method}", "{http.request.method}",
+ "{scheme}", "{http.request.scheme}",
+ "{file}", "{http.request.uri.path.file}",
+ "{dir}", "{http.request.uri.path.dir}",
+ "{query}", "{http.request.uri.query}",
+ )
+ for _, segment := range sb.block.Segments {
+ for i := 0; i < len(segment); i++ {
+ segment[i].Text = replacer.Replace(segment[i].Text)
+ }
+ }
+
// extract matcher definitions
d := sb.block.DispenseDirective("matcher")
matcherDefs, err := st.parseMatcherDefinitions(d)
@@ -82,8 +100,8 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
sb.pile[result.Class] = append(sb.pile[result.Class], result)
}
} else {
- // TODO: this should be an error
- log.Printf("%s not registered", dir)
+ tkn := segment[0]
+ return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
}
}
}
@@ -97,22 +115,6 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
// reduce
pairings := st.consolidateAddrMappings(sbmap)
- // TODO: shorthand placeholders
- // for _, p := range pairings {
- // for _, sblock := range p.serverBlocks {
- // for _, tokens := range sblock.Tokens {
- // for i := 0; i < len(tokens); i++ {
- // switch tokens[i].Text {
- // case "{uri}":
- // tokens[i].Text = "{http.request.uri}"
- // case "{path}":
- // tokens[i].Text = "{http.request.uri.path}"
- // }
- // }
- // }
- // }
- // }
-
// each pairing of listener addresses to list of server
// blocks is basically a server definition
servers, err := st.serversFromPairings(pairings, &warnings)