Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
caddyhttp: 'not' matcher: Support Caddyfile unmarshaling
Matthew Holt committed
Sep 30, 2019 at 09:09 UTC
7b4aa108c7d67416bf68574da6c5a43899715b3d
1 file changed
+36
-1
modules/caddyhttp/matchers.go
+36
-1
@@ -414,7 +414,42 @@ func (m MatchNegate) MarshalJSON() ([]byte, error) {
414
415
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
416
func (m *MatchNegate) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
417
- // TODO: figure out how this will work
417
+ // first, unmarshal each matcher in the set from its tokens
418
+
419
+ matcherMap := make(map[string]RequestMatcher)
420
+ for d.Next() {
421
+ for d.NextBlock(0) {
422
+ matcherName := d.Val()
423
+ mod, err := caddy.GetModule("http.matchers." + matcherName)
424
+ if err != nil {
425
+ return d.Errf("getting matcher module '%s': %v", matcherName, err)
426
+ }
427
+ unm, ok := mod.New().(caddyfile.Unmarshaler)
428
+ if !ok {
429
+ return d.Errf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
430
+ }
431
+ err = unm.UnmarshalCaddyfile(d.NewFromNextTokens())
432
+ if err != nil {
433
+ return err
434
+ }
435
+ rm := unm.(RequestMatcher)
436
+ m.Matchers = append(m.Matchers, rm)
437
+ matcherMap[matcherName] = rm
438
+ }
439
+ }
440
+
441
+ // we should now be functional, but we also need
442
+ // to be able to marshal as JSON, otherwise config
443
+ // adaptation won't work properly
444
+ m.MatchersRaw = make(map[string]json.RawMessage)
445
+ for name, matchers := range matcherMap {
446
+ jsonBytes, err := json.Marshal(matchers)
447
+ if err != nil {
448
+ return fmt.Errorf("marshaling matcher %s: %v", name, err)
449
+ }
450
+ m.MatchersRaw[name] = jsonBytes
451
+ }
452
+
453
return nil
454
}
455