summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/vars.go
diff options
context:
space:
mode:
authorMohammed Al Sahaf <msaa1990@gmail.com>2020-09-26 02:50:26 +0300
committerGitHub <noreply@github.com>2020-09-25 17:50:26 -0600
commita33e4b542689cbe7ac711e3cfde1d2a30c0156ae (patch)
tree7b582fc1e8f25e02475d7865143768a85f0ab04f /modules/caddyhttp/vars.go
parentf197cec7f3a599ca18807e7b7719ef7666cfdb70 (diff)
caddyfile: Add support for `vars` and `vars_regexp` matchers (#3730)
* caddyfile: support vars and vars_regexp matchers in the caddyfile * caddyfile: matchers: Brian Kernighan said printf is good debugging tool but didn't say keep them around
Diffstat (limited to 'modules/caddyhttp/vars.go')
-rw-r--r--modules/caddyhttp/vars.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/modules/caddyhttp/vars.go b/modules/caddyhttp/vars.go
index 9561d46..479ef0a 100644
--- a/modules/caddyhttp/vars.go
+++ b/modules/caddyhttp/vars.go
@@ -20,6 +20,7 @@ import (
"net/http"
"github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
)
func init() {
@@ -64,6 +65,24 @@ func (VarsMatcher) CaddyModule() caddy.ModuleInfo {
}
}
+// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
+func (m *VarsMatcher) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
+ if *m == nil {
+ *m = make(map[string]string)
+ }
+ for d.Next() {
+ var field, val string
+ if !d.Args(&field, &val) {
+ return d.Errf("malformed vars matcher: expected both field and value")
+ }
+ (*m)[field] = val
+ if d.NextBlock(0) {
+ return d.Err("malformed vars matcher: blocks are not supported")
+ }
+ }
+ return nil
+}
+
// Match matches a request based on variables in the context.
func (m VarsMatcher) Match(r *http.Request) bool {
vars := r.Context().Value(VarsCtxKey).(map[string]interface{})
@@ -106,6 +125,35 @@ func (MatchVarsRE) CaddyModule() caddy.ModuleInfo {
}
}
+// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
+func (m *MatchVarsRE) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
+ if *m == nil {
+ *m = make(map[string]*MatchRegexp)
+ }
+ for d.Next() {
+ var first, second, third string
+ if !d.Args(&first, &second) {
+ return d.ArgErr()
+ }
+
+ var name, field, val string
+ if d.Args(&third) {
+ name = first
+ field = second
+ val = third
+ } else {
+ field = first
+ val = second
+ }
+
+ (*m)[field] = &MatchRegexp{Pattern: val, Name: name}
+ if d.NextBlock(0) {
+ return d.Err("malformed vars_regexp matcher: blocks are not supported")
+ }
+ }
+ return nil
+}
+
// Provision compiles m's regular expressions.
func (m MatchVarsRE) Provision(ctx caddy.Context) error {
for _, rm := range m {