diff options
author | Matthew Holt <[email protected]> | 2019-09-30 09:09:57 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-09-30 09:09:57 -0600 |
commit | 7b4aa108c7d67416bf68574da6c5a43899715b3d (patch) | |
tree | 420f10c0b9506832ea60466638bc68b1333d1db8 | |
parent | 8b11ed347b00f323b6333c5df078a3e427a1ca71 (diff) | |
download | caddy-7b4aa108c7d67416bf68574da6c5a43899715b3d.tar.gz caddy-7b4aa108c7d67416bf68574da6c5a43899715b3d.zip |
caddyhttp: 'not' matcher: Support Caddyfile unmarshaling
-rw-r--r-- | modules/caddyhttp/matchers.go | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 3eaf48f5c..7c35a3963 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 } |