aboutsummaryrefslogtreecommitdiffhomepage
path: root/caddyconfig
diff options
context:
space:
mode:
authorFrancis Lavoie <[email protected]>2024-04-17 14:19:14 -0400
committerGitHub <[email protected]>2024-04-17 12:19:14 -0600
commit9cd472c0313b01e71d1f142769c3653058d75c86 (patch)
tree2393ff4a9a2280e35acfacf4109ca99075c26a43 /caddyconfig
parente0daa39cd3373d26dcf78ae0d30ac24d1df5dd57 (diff)
downloadcaddy-9cd472c0313b01e71d1f142769c3653058d75c86.tar.gz
caddy-9cd472c0313b01e71d1f142769c3653058d75c86.zip
caddyfile: Populate regexp matcher names by default (#6145)
* caddyfile: Populate regexp matcher names by default * Some lint cleanup that my VSCode complained about * Pass down matcher name through expression matcher * Compat with #6113: fix adapt test, set both styles in replacer
Diffstat (limited to 'caddyconfig')
-rw-r--r--caddyconfig/caddyfile/dispenser.go34
-rw-r--r--caddyconfig/httpcaddyfile/httptype.go10
-rw-r--r--caddyconfig/httpcaddyfile/serveroptions.go2
-rw-r--r--caddyconfig/httpcaddyfile/tlsapp.go6
4 files changed, 49 insertions, 3 deletions
diff --git a/caddyconfig/caddyfile/dispenser.go b/caddyconfig/caddyfile/dispenser.go
index 215a1641f..e36275a1c 100644
--- a/caddyconfig/caddyfile/dispenser.go
+++ b/caddyconfig/caddyfile/dispenser.go
@@ -30,6 +30,10 @@ type Dispenser struct {
tokens []Token
cursor int
nesting int
+
+ // A map of arbitrary context data that can be used
+ // to pass through some information to unmarshalers.
+ context map[string]any
}
// NewDispenser returns a Dispenser filled with the given tokens.
@@ -454,6 +458,34 @@ func (d *Dispenser) DeleteN(amount int) []Token {
return d.tokens
}
+// SetContext sets a key-value pair in the context map.
+func (d *Dispenser) SetContext(key string, value any) {
+ if d.context == nil {
+ d.context = make(map[string]any)
+ }
+ d.context[key] = value
+}
+
+// GetContext gets the value of a key in the context map.
+func (d *Dispenser) GetContext(key string) any {
+ if d.context == nil {
+ return nil
+ }
+ return d.context[key]
+}
+
+// GetContextString gets the value of a key in the context map
+// as a string, or an empty string if the key does not exist.
+func (d *Dispenser) GetContextString(key string) string {
+ if d.context == nil {
+ return ""
+ }
+ if val, ok := d.context[key].(string); ok {
+ return val
+ }
+ return ""
+}
+
// isNewLine determines whether the current token is on a different
// line (higher line number) than the previous token. It handles imported
// tokens correctly. If there isn't a previous token, it returns true.
@@ -485,3 +517,5 @@ func (d *Dispenser) isNextOnNewLine() bool {
next := d.tokens[d.cursor+1]
return isNextOnNewLine(curr, next)
}
+
+const MatcherNameCtxKey = "matcher_name"
diff --git a/caddyconfig/httpcaddyfile/httptype.go b/caddyconfig/httpcaddyfile/httptype.go
index ab1012939..0d831403b 100644
--- a/caddyconfig/httpcaddyfile/httptype.go
+++ b/caddyconfig/httpcaddyfile/httptype.go
@@ -1397,6 +1397,14 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
// given a matcher name and the tokens following it, parse
// the tokens as a matcher module and record it
makeMatcher := func(matcherName string, tokens []caddyfile.Token) error {
+ // create a new dispenser from the tokens
+ dispenser := caddyfile.NewDispenser(tokens)
+
+ // set the matcher name (without @) in the dispenser context so
+ // that matcher modules can access it to use it as their name
+ // (e.g. regexp matchers which use the name for capture groups)
+ dispenser.SetContext(caddyfile.MatcherNameCtxKey, definitionName[1:])
+
mod, err := caddy.GetModule("http.matchers." + matcherName)
if err != nil {
return fmt.Errorf("getting matcher module '%s': %v", matcherName, err)
@@ -1405,7 +1413,7 @@ func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.M
if !ok {
return fmt.Errorf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
}
- err = unm.UnmarshalCaddyfile(caddyfile.NewDispenser(tokens))
+ err = unm.UnmarshalCaddyfile(dispenser)
if err != nil {
return err
}
diff --git a/caddyconfig/httpcaddyfile/serveroptions.go b/caddyconfig/httpcaddyfile/serveroptions.go
index 62902b964..18f14996c 100644
--- a/caddyconfig/httpcaddyfile/serveroptions.go
+++ b/caddyconfig/httpcaddyfile/serveroptions.go
@@ -291,7 +291,7 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) {
func applyServerOptions(
servers map[string]*caddyhttp.Server,
options map[string]any,
- warnings *[]caddyconfig.Warning,
+ _ *[]caddyconfig.Warning,
) error {
serverOpts, ok := options["servers"].([]serverOptions)
if !ok {
diff --git a/caddyconfig/httpcaddyfile/tlsapp.go b/caddyconfig/httpcaddyfile/tlsapp.go
index 08da3a5c7..e5d1603ed 100644
--- a/caddyconfig/httpcaddyfile/tlsapp.go
+++ b/caddyconfig/httpcaddyfile/tlsapp.go
@@ -487,7 +487,11 @@ func fillInGlobalACMEDefaults(issuer certmagic.Issuer, options map[string]any) e
// for any other automation policies. A nil policy (and no error) will be
// returned if there are no default/global options. However, if always is
// true, a non-nil value will always be returned (unless there is an error).
-func newBaseAutomationPolicy(options map[string]any, warnings []caddyconfig.Warning, always bool) (*caddytls.AutomationPolicy, error) {
+func newBaseAutomationPolicy(
+ options map[string]any,
+ _ []caddyconfig.Warning,
+ always bool,
+) (*caddytls.AutomationPolicy, error) {
issuers, hasIssuers := options["cert_issuer"]
_, hasLocalCerts := options["local_certs"]
keyType, hasKeyType := options["key_type"]