summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2019-09-30 09:09:57 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2019-09-30 09:09:57 -0600
commit7b4aa108c7d67416bf68574da6c5a43899715b3d (patch)
tree420f10c0b9506832ea60466638bc68b1333d1db8
parent8b11ed347b00f323b6333c5df078a3e427a1ca71 (diff)
caddyhttp: 'not' matcher: Support Caddyfile unmarshaling
-rw-r--r--modules/caddyhttp/matchers.go37
1 files changed, 36 insertions, 1 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go
index 3eaf48f..7c35a39 100644
--- a/modules/caddyhttp/matchers.go
+++ b/modules/caddyhttp/matchers.go
@@ -414,7 +414,42 @@ func (m MatchNegate) MarshalJSON() ([]byte, error) {
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *MatchNegate) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
- // TODO: figure out how this will work
+ // first, unmarshal each matcher in the set from its tokens
+
+ matcherMap := make(map[string]RequestMatcher)
+ for d.Next() {
+ for d.NextBlock(0) {
+ matcherName := d.Val()
+ mod, err := caddy.GetModule("http.matchers." + matcherName)
+ if err != nil {
+ return d.Errf("getting matcher module '%s': %v", matcherName, err)
+ }
+ unm, ok := mod.New().(caddyfile.Unmarshaler)
+ if !ok {
+ return d.Errf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
+ }
+ err = unm.UnmarshalCaddyfile(d.NewFromNextTokens())
+ if err != nil {
+ return err
+ }
+ rm := unm.(RequestMatcher)
+ m.Matchers = append(m.Matchers, rm)
+ matcherMap[matcherName] = rm
+ }
+ }
+
+ // we should now be functional, but we also need
+ // to be able to marshal as JSON, otherwise config
+ // adaptation won't work properly
+ m.MatchersRaw = make(map[string]json.RawMessage)
+ for name, matchers := range matcherMap {
+ jsonBytes, err := json.Marshal(matchers)
+ if err != nil {
+ return fmt.Errorf("marshaling matcher %s: %v", name, err)
+ }
+ m.MatchersRaw[name] = jsonBytes
+ }
+
return nil
}