@marcel91200 / caddy Mirror · read-only / commits / 7527c017
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

v2: Implement Caddyfile enhancements (breaking changes) (#2960)

* http: path matcher: exact match by default; substring matches (#2959) This is a breaking change. * caddyfile: Change "matcher" directive to "@matcher" syntax (#2959) * cmd: Assume caddyfile adapter for config files named Caddyfile * Sub-sort handlers by path matcher length (#2959) Caddyfile-generated subroutes have handlers, which are sorted first by directive order (this is unchanged), but within directives we now sort by specificity of path matcher in descending order (longest path first, assuming that longest path is most specific). This only applies if there is only one matcher set, and the path matcher in that set has only one path in it. Path matchers with two or more paths are not sorted like this; and routes with more than one matcher set are not sorted like this either, since specificity is difficult or impossible to infer correctly. This is a special case, but definitely a very common one, as a lot of routing decisions are based on paths. * caddyfile: New 'route' directive for appearance-order handling (#2959) * caddyfile: Make rewrite directives mutually exclusive (#2959) This applies only to rewrites in the top-level subroute created by the HTTP caddyfile.

Matt Holt committed Jan 9, 2020 at 14:00 UTC 7527c0170558ce7e31b79b7bc31dbf2f25afd983
7 files changed +205 -55
caddyconfig/httpcaddyfile/builtins.go
+33
@@ -33,6 +33,7 @@ func init() {
33 RegisterDirective("tls", parseTLS)
34 RegisterHandlerDirective("redir", parseRedir)
35 RegisterHandlerDirective("respond", parseRespond)
36 + RegisterHandlerDirective("route", parseRoute)
37 }
38
39 func parseBind(h Helper) ([]ConfigValue, error) {
@@ -297,3 +298,35 @@ func parseRespond(h Helper) (caddyhttp.MiddlewareHandler, error) {
298 }
299 return sr, nil
300 }
301 +
302 +func parseRoute(h Helper) (caddyhttp.MiddlewareHandler, error) {
303 + sr := new(caddyhttp.Subroute)
304 +
305 + for h.Next() {
306 + for nesting := h.Nesting(); h.NextBlock(nesting); {
307 + dir := h.Val()
308 +
309 + dirFunc, ok := registeredDirectives[dir]
310 + if !ok {
311 + return nil, h.Errf("unrecognized directive: %s", dir)
312 + }
313 +
314 + subHelper := h
315 + subHelper.Dispenser = h.NewFromNextTokens()
316 +
317 + results, err := dirFunc(subHelper)
318 + if err != nil {
319 + return nil, h.Errf("parsing caddyfile tokens for '%s': %v", dir, err)
320 + }
321 + for _, result := range results {
322 + handler, ok := result.Value.(caddyhttp.Route)
323 + if !ok {
324 + return nil, h.Errf("%s directive returned something other than an HTTP route: %#v (only handler directives can be used in routes)", dir, result.Value)
325 + }
326 + sr.Routes = append(sr.Routes, handler)
327 + }
328 + }
329 + }
330 +
331 + return sr, nil
332 +}
caddyconfig/httpcaddyfile/directives.go
+1
@@ -36,6 +36,7 @@ var defaultDirectiveOrder = []string{
36 "request_header",
37 "encode",
38 "templates",
39 + "route",
40 "redir",
41 "respond",
42 "reverse_proxy",
caddyconfig/httpcaddyfile/httptype.go
+101 -37
@@ -114,37 +114,45 @@ func (st ServerType) Setup(originalServerBlocks []caddyfile.ServerBlock,
114 }
115
116 // extract matcher definitions
117 - d := sb.block.DispenseDirective("matcher")
118 - matcherDefs, err := parseMatcherDefinitions(d)
119 - if err != nil {
120 - return nil, warnings, err
117 + matcherDefs := make(map[string]caddy.ModuleMap)
118 + for _, segment := range sb.block.Segments {
119 + if dir := segment.Directive(); strings.HasPrefix(dir, matcherPrefix) {
120 + d := sb.block.DispenseDirective(dir)
121 + err := parseMatcherDefinitions(d, matcherDefs)
122 + if err != nil {
123 + return nil, warnings, err
124 + }
125 + }
126 }
127
128 for _, segment := range sb.block.Segments {
129 dir := segment.Directive()
125 - if dir == "matcher" {
126 - // TODO: This is a special case because we pre-processed it; handle this better
130 +
131 + if strings.HasPrefix(dir, matcherPrefix) {
132 + // matcher definitions were pre-processed
133 continue
134 }
129 - if dirFunc, ok := registeredDirectives[dir]; ok {
130 - results, err := dirFunc(Helper{
131 - Dispenser: caddyfile.NewDispenser(segment),
132 - options: options,
133 - warnings: &warnings,
134 - matcherDefs: matcherDefs,
135 - parentBlock: sb.block,
136 - })
137 - if err != nil {
138 - return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
139 - }
140 - for _, result := range results {
141 - result.directive = dir
142 - sb.pile[result.Class] = append(sb.pile[result.Class], result)
143 - }
144 - } else {
135 +
136 + dirFunc, ok := registeredDirectives[dir]
137 + if !ok {
138 tkn := segment[0]
139 return nil, warnings, fmt.Errorf("%s:%d: unrecognized directive: %s", tkn.File, tkn.Line, dir)
140 }
141 +
142 + results, err := dirFunc(Helper{
143 + Dispenser: caddyfile.NewDispenser(segment),
144 + options: options,
145 + warnings: &warnings,
146 + matcherDefs: matcherDefs,
147 + parentBlock: sb.block,
148 + })
149 + if err != nil {
150 + return nil, warnings, fmt.Errorf("parsing caddyfile tokens for '%s': %v", dir, err)
151 + }
152 + for _, result := range results {
153 + result.directive = dir
154 + sb.pile[result.Class] = append(sb.pile[result.Class], result)
155 + }
156 }
157 }
158
@@ -372,10 +380,63 @@ func (st *ServerType) serversFromPairings(
380 }
381 sort.SliceStable(dirRoutes, func(i, j int) bool {
382 iDir, jDir := dirRoutes[i].directive, dirRoutes[j].directive
383 + if iDir == jDir {
384 + // TODO: we really need to refactor this into a separate function or method...
385 + // sub-sort by path matcher length, if there's only one
386 + iRoute := dirRoutes[i].Value.(caddyhttp.Route)
387 + jRoute := dirRoutes[j].Value.(caddyhttp.Route)
388 + if len(iRoute.MatcherSetsRaw) == 1 && len(jRoute.MatcherSetsRaw) == 1 {
389 + // for slightly better efficiency, only decode the path matchers once,
390 + // then just store them arbitrarily in the decoded MatcherSets field,
391 + // ours should be the only thing in there
392 + var iPM, jPM caddyhttp.MatchPath
393 + if len(iRoute.MatcherSets) == 1 {
394 + iPM = iRoute.MatcherSets[0][0].(caddyhttp.MatchPath)
395 + }
396 + if len(jRoute.MatcherSets) == 1 {
397 + jPM = jRoute.MatcherSets[0][0].(caddyhttp.MatchPath)
398 + }
399 + // if it's our first time seeing this route's path matcher, decode it
400 + if iPM == nil {
401 + var pathMatcher caddyhttp.MatchPath
402 + _ = json.Unmarshal(iRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
403 + iRoute.MatcherSets = caddyhttp.MatcherSets{{pathMatcher}}
404 + iPM = pathMatcher
405 + }
406 + if jPM == nil {
407 + var pathMatcher caddyhttp.MatchPath
408 + _ = json.Unmarshal(jRoute.MatcherSetsRaw[0]["path"], &pathMatcher)
409 + jRoute.MatcherSets = caddyhttp.MatcherSets{{pathMatcher}}
410 + jPM = pathMatcher
411 + }
412 + // finally, if there is only one path in the
413 + // matcher, sort by longer path first
414 + if len(iPM) == 1 && len(jPM) == 1 {
415 + return len(iPM[0]) > len(jPM[0])
416 + }
417 + }
418 + }
419 return dirPositions[iDir] < dirPositions[jDir]
420 })
421 }
422 +
423 + // add all the routes piled in from directives
424 for _, r := range dirRoutes {
425 + // as a special case, group rewrite directives so that they are mutually exclusive;
426 + // this means that only the first matching rewrite will be evaluated, and that's
427 + // probably a good thing, since there should never be a need to do more than one
428 + // rewrite (I think?), and cascading rewrites smell bad... imagine these rewrites:
429 + // rewrite /docs/json/* /docs/json/index.html
430 + // rewrite /docs/* /docs/index.html
431 + // (We use this on the Caddy website, or at least we did once.) The first rewrite's
432 + // result is also matched by the second rewrite, making the first rewrite pointless.
433 + // See issue #2959.
434 + if r.directive == "rewrite" {
435 + route := r.Value.(caddyhttp.Route)
436 + route.Group = "rewriting"
437 + r.Value = route
438 + }
439 +
440 handlerSubroute.Routes = append(handlerSubroute.Routes, r.Value.(caddyhttp.Route))
441 }
442
@@ -480,17 +541,16 @@ func matcherSetFromMatcherToken(
541 if tkn.Text == "*" {
542 // match all requests == no matchers, so nothing to do
543 return nil, true, nil
483 - } else if strings.HasPrefix(tkn.Text, "/") || strings.HasPrefix(tkn.Text, "=/") {
544 + } else if strings.HasPrefix(tkn.Text, "/") {
545 // convenient way to specify a single path match
546 return caddy.ModuleMap{
547 "path": caddyconfig.JSON(caddyhttp.MatchPath{tkn.Text}, warnings),
548 }, true, nil
488 - } else if strings.HasPrefix(tkn.Text, "match:") {
549 + } else if strings.HasPrefix(tkn.Text, matcherPrefix) {
550 // pre-defined matcher
490 - matcherName := strings.TrimPrefix(tkn.Text, "match:")
491 - m, ok := matcherDefs[matcherName]
551 + m, ok := matcherDefs[tkn.Text]
552 if !ok {
493 - return nil, false, fmt.Errorf("unrecognized matcher name: %+v", matcherName)
553 + return nil, false, fmt.Errorf("unrecognized matcher name: %+v", tkn.Text)
554 }
555 return m, true, nil
556 }
@@ -577,35 +637,37 @@ func (st *ServerType) compileEncodedMatcherSets(sblock caddyfile.ServerBlock) ([
637 return matcherSetsEnc, nil
638 }
639
580 -func parseMatcherDefinitions(d *caddyfile.Dispenser) (map[string]caddy.ModuleMap, error) {
581 - matchers := make(map[string]caddy.ModuleMap)
640 +func parseMatcherDefinitions(d *caddyfile.Dispenser, matchers map[string]caddy.ModuleMap) error {
641 for d.Next() {
642 definitionName := d.Val()
643 +
644 + if _, ok := matchers[definitionName]; ok {
645 + return fmt.Errorf("matcher is defined more than once: %s", definitionName)
646 + }
647 + matchers[definitionName] = make(caddy.ModuleMap)
648 +
649 for nesting := d.Nesting(); d.NextBlock(nesting); {
650 matcherName := d.Val()
651 mod, err := caddy.GetModule("http.matchers." + matcherName)
652 if err != nil {
588 - return nil, fmt.Errorf("getting matcher module '%s': %v", matcherName, err)
653 + return fmt.Errorf("getting matcher module '%s': %v", matcherName, err)
654 }
655 unm, ok := mod.New().(caddyfile.Unmarshaler)
656 if !ok {
592 - return nil, fmt.Errorf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
657 + return fmt.Errorf("matcher module '%s' is not a Caddyfile unmarshaler", matcherName)
658 }
659 err = unm.UnmarshalCaddyfile(d.NewFromNextTokens())
660 if err != nil {
596 - return nil, err
661 + return err
662 }
663 rm, ok := unm.(caddyhttp.RequestMatcher)
664 if !ok {
600 - return nil, fmt.Errorf("matcher module '%s' is not a request matcher", matcherName)
601 - }
602 - if _, ok := matchers[definitionName]; !ok {
603 - matchers[definitionName] = make(caddy.ModuleMap)
665 + return fmt.Errorf("matcher module '%s' is not a request matcher", matcherName)
666 }
667 matchers[definitionName][matcherName] = caddyconfig.JSON(rm, nil)
668 }
669 }
608 - return matchers, nil
670 + return nil
671 }
672
673 func encodeMatcherSet(matchers map[string]caddyhttp.RequestMatcher) (caddy.ModuleMap, error) {
@@ -643,5 +705,7 @@ type sbAddrAssociation struct {
705 serverBlocks []serverBlock
706 }
707
708 +const matcherPrefix = "@"
709 +
710 // Interface guard
711 var _ caddyfile.ServerType = (*ServerType)(nil)
caddyconfig/httpcaddyfile/parser_test.go
+2 -2
@@ -29,7 +29,7 @@ func TestParse(t *testing.T) {
29 }{
30 {
31 input: `http://localhost
32 - matcher debug {
32 + @debug {
33 query showdebug=1
34 }
35 `,
@@ -38,7 +38,7 @@ func TestParse(t *testing.T) {
38 },
39 {
40 input: `http://localhost
41 - matcher debug {
41 + @debug {
42 query bad format
43 }
44 `,
cmd/main.go
+7
@@ -134,6 +134,13 @@ func loadConfig(configFile, adapterName string) ([]byte, error) {
134 }
135 }
136
137 + // as a special case, if a config file called "Caddyfile" was
138 + // specified, and no adapter is specified, assume caddyfile adapter
139 + // for convenience
140 + if filepath.Base(configFile) == "Caddyfile" && adapterName == "" {
141 + adapterName = "caddyfile"
142 + }
143 +
144 // load config adapter
145 if adapterName != "" {
146 cfgAdapter = caddyconfig.GetAdapter(adapterName)
modules/caddyhttp/matchers.go
+30 -9
@@ -46,7 +46,17 @@ type (
46 // [customized or disabled](/docs/json/apps/http/servers/automatic_https/).
47 MatchHost []string
48
49 - // MatchPath matches requests by the URI's path (case-insensitive).
49 + // MatchPath matches requests by the URI's path (case-insensitive). Path
50 + // matches are exact, but wildcards may be used:
51 + //
52 + // - At the end, for a prefix match (`/prefix/*`)
53 + // - At the beginning, for a suffix match (`*.suffix`)
54 + // - On both sides, for a substring match (`*/contains/*`)
55 + // - In the middle, for a globular match (`/accounts/*/info`)
56 + //
57 + // This matcher is fast, so it does not support regular expressions or
58 + // capture groups. For slower but more capable matching, use the path_regexp
59 + // matcher.
60 MatchPath []string
61
62 // MatchPathRE matches requests by a regular expression on the URI's path.
@@ -197,11 +207,15 @@ func (m MatchPath) Match(r *http.Request) bool {
207 // being matched by *.php to be treated as PHP scripts
208 lowerPath = strings.TrimRight(lowerPath, ". ")
209
210 + repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
211 +
212 for _, matchPath := range m {
201 - // special case: first character is equals sign,
202 - // treat it as an exact match
203 - if strings.HasPrefix(matchPath, "=") {
204 - if lowerPath == matchPath[1:] {
213 + matchPath = repl.ReplaceAll(matchPath, "")
214 +
215 + // special case: first and last characters are wildcard,
216 + // treat it as a fast substring match
217 + if strings.HasPrefix(matchPath, "*") && strings.HasSuffix(matchPath, "*") {
218 + if strings.Contains(lowerPath, matchPath[1:len(matchPath)-1]) {
219 return true
220 }
221 continue
@@ -216,15 +230,22 @@ func (m MatchPath) Match(r *http.Request) bool {
230 continue
231 }
232
233 + // special case: last character is a wildcard,
234 + // treat it as a fast prefix match
235 + if strings.HasSuffix(matchPath, "*") {
236 + if strings.HasPrefix(lowerPath, matchPath[:len(matchPath)-1]) {
237 + return true
238 + }
239 + continue
240 + }
241 +
242 + // for everything else, try globular matching, which also
243 + // is exact matching if there are no glob/wildcard chars;
244 // can ignore error here because we can't handle it anyway
245 matches, _ := filepath.Match(matchPath, lowerPath)
246 if matches {
247 return true
248 }
224 -
225 - if strings.HasPrefix(lowerPath, matchPath) {
226 - return true
227 - }
249 }
250 return false
251 }
modules/caddyhttp/matchers_test.go
+31 -7
@@ -182,9 +182,19 @@ func TestPathMatcher(t *testing.T) {
182 input: "/foo/bar",
183 expect: false,
184 },
185 + {
186 + match: MatchPath{"/foo/bar/"},
187 + input: "/foo/bar/",
188 + expect: true,
189 + },
190 {
191 match: MatchPath{"/foo/bar/", "/other"},
192 input: "/other/",
193 + expect: false,
194 + },
195 + {
196 + match: MatchPath{"/foo/bar/", "/other"},
197 + input: "/other",
198 expect: true,
199 },
200 {
@@ -213,25 +223,30 @@ func TestPathMatcher(t *testing.T) {
223 expect: false,
224 },
225 {
216 - match: MatchPath{"=/foo"},
217 - input: "/foo",
226 + match: MatchPath{"*substring*"},
227 + input: "/foo/substring/bar.txt",
228 expect: true,
229 },
230 {
221 - match: MatchPath{"=/foo"},
231 + match: MatchPath{"/foo"},
232 input: "/foo/bar",
233 expect: false,
234 },
235 {
226 - match: MatchPath{"=/foo"},
227 - input: "/FOO",
228 - expect: true,
236 + match: MatchPath{"/foo"},
237 + input: "/foo/bar",
238 + expect: false,
239 },
240 {
241 match: MatchPath{"/foo"},
242 input: "/FOO",
243 expect: true,
244 },
245 + {
246 + match: MatchPath{"/foo*"},
247 + input: "/FOOOO",
248 + expect: true,
249 + },
250 {
251 match: MatchPath{"/foo/bar.txt"},
252 input: "/foo/BAR.txt",
@@ -239,6 +254,10 @@ func TestPathMatcher(t *testing.T) {
254 },
255 } {
256 req := &http.Request{URL: &url.URL{Path: tc.input}}
257 + repl := caddy.NewReplacer()
258 + ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
259 + req = req.WithContext(ctx)
260 +
261 actual := tc.match.Match(req)
262 if actual != tc.expect {
263 t.Errorf("Test %d %v: Expected %t, got %t for '%s'", i, tc.match, tc.expect, actual, tc.input)
@@ -251,8 +270,13 @@ func TestPathMatcherWindows(t *testing.T) {
270 // only Windows has this bug where it will ignore
271 // trailing dots and spaces in a filename, but we
272 // test for it on all platforms to be more consistent
254 - match := MatchPath{"*.php"}
273 +
274 req := &http.Request{URL: &url.URL{Path: "/index.php . . .."}}
275 + repl := caddy.NewReplacer()
276 + ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
277 + req = req.WithContext(ctx)
278 +
279 + match := MatchPath{"*.php"}
280 matched := match.Match(req)
281 if !matched {
282 t.Errorf("Expected to match; should ignore trailing dots and spaces")