summaryrefslogtreecommitdiff
path: root/modules/caddyhttp
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp')
-rw-r--r--modules/caddyhttp/matchers.go2
-rw-r--r--modules/caddyhttp/vars.go48
2 files changed, 50 insertions, 0 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index 164f443..a832024 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -927,6 +927,8 @@ var (
_ caddyfile.Unmarshaler = (*MatchHeaderRE)(nil)
_ caddyfile.Unmarshaler = (*MatchProtocol)(nil)
_ caddyfile.Unmarshaler = (*MatchRemoteIP)(nil)
+ _ caddyfile.Unmarshaler = (*VarsMatcher)(nil)
+ _ caddyfile.Unmarshaler = (*MatchVarsRE)(nil)
_ json.Marshaler = (*MatchNot)(nil)
_ json.Unmarshaler = (*MatchNot)(nil)
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 {