@marcel91200 / caddy Mirror · read-only / commits / 490cd02f
Fork 0

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

httpcaddyfile: Make root directive mutually exclusive

See https://caddy.community/t/caddyfile-and-v2/6766/22?u=matt

Matthew Holt committed Feb 4, 2020 at 13:04 UTC 490cd02f826b00e52b6503b61436b8c7444fc30e
1 file changed +35 -39
caddyconfig/httpcaddyfile/httptype.go
+35 -39
@@ -448,38 +448,13 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
448
449 subroute := new(caddyhttp.Subroute)
450
451 - // get a group name for rewrite directives, if needed
452 - var rewriteGroupName string
453 - var rewriteCount int
454 - for _, r := range routes {
455 - if r.directive == "rewrite" {
456 - rewriteCount++
457 - if rewriteCount > 1 {
458 - break
459 - }
460 - }
461 - }
462 - if rewriteCount > 1 {
463 - rewriteGroupName = groupCounter.nextGroup()
464 - }
465 -
466 - // get a group name for handle blocks, if needed
467 - var handleGroupName string
468 - var handleCount int
469 - for _, r := range routes {
470 - if r.directive == "handle" {
471 - handleCount++
472 - if handleCount > 1 {
473 - break
474 - }
475 - }
476 - }
477 - if handleCount > 1 {
478 - handleGroupName = groupCounter.nextGroup()
479 - }
480 -
481 - // add all the routes piled in from directives
482 - for _, r := range routes {
451 + // some directives are mutually exclusive (only first matching
452 + // instance should be evaluated); this is done by putting their
453 + // routes in the same group
454 + mutuallyExclusiveDirs := map[string]*struct {
455 + count int
456 + groupName string
457 + }{
458 // as a special case, group rewrite directives so that they are mutually exclusive;
459 // this means that only the first matching rewrite will be evaluated, and that's
460 // probably a good thing, since there should never be a need to do more than one
@@ -489,16 +464,37 @@ func buildSubroute(routes []ConfigValue, groupCounter counter) (*caddyhttp.Subro
464 // (We use this on the Caddy website, or at least we did once.) The first rewrite's
465 // result is also matched by the second rewrite, making the first rewrite pointless.
466 // See issue #2959.
492 - if r.directive == "rewrite" {
493 - route := r.Value.(caddyhttp.Route)
494 - route.Group = rewriteGroupName
495 - r.Value = route
496 - }
467 + "rewrite": {},
468
469 // handle blocks are also mutually exclusive by definition
499 - if r.directive == "handle" {
470 + "handle": {},
471 +
472 + // root just sets a variable, so if it was not mutually exclusive, intersecting
473 + // root directives would overwrite previously-matched ones; they should not cascade
474 + "root": {},
475 + }
476 + for meDir, info := range mutuallyExclusiveDirs {
477 + // see how many instances of the directive there are
478 + for _, r := range routes {
479 + if r.directive == meDir {
480 + info.count++
481 + if info.count > 1 {
482 + break
483 + }
484 + }
485 + }
486 + // if there is more than one, put them in a group
487 + if info.count > 1 {
488 + info.groupName = groupCounter.nextGroup()
489 + }
490 + }
491 +
492 + // add all the routes piled in from directives
493 + for _, r := range routes {
494 + // put this route into a group if it is mutually exclusive
495 + if info, ok := mutuallyExclusiveDirs[r.directive]; ok {
496 route := r.Value.(caddyhttp.Route)
501 - route.Group = handleGroupName
497 + route.Group = info.groupName
498 r.Value = route
499 }
500