Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpcaddyfile: Improve directive sorting logic #3658

Merged
merged 6 commits into from
Aug 17, 2020

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Aug 14, 2020

See discussion in https://caddy.community/t/v2-help-with-path-regexp-and-rewrite/9442/11

I'm pretty sure that it's almost always incorrect to sort root handlers from the Caddyfile in the same order as other directives, because it's essentially setting a variable, so only the last one run will apply - so you want the last one run to be the most-specific one.

Also, see the discussion in https://caddy.community/t/presenting-different-content-based-on-origin-of-request/9467/7

If a directive has any kind of matcher, it should still sort before a directive of the same name with no matcher at all.

@francislavoie francislavoie added the bug 🐞 Something isn't working label Aug 14, 2020
@francislavoie francislavoie added this to the v2.2.0 milestone Aug 14, 2020
@francislavoie francislavoie requested a review from mholt August 14, 2020 22:52
@francislavoie francislavoie changed the title httpcaddyfile: Flip root directive sort order httpcaddyfile: Improve directive sorting logic Aug 16, 2020
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change and the tests!

Some logic needs to be corrected and we should probably include other directives that need to be sorted differently...

Actually... root, along with the rewrite and handle directives are already mutually-exclusive of other instances of the same directive. I wonder if all of those would need to be added here, along with header...

caddyconfig/httpcaddyfile/directives.go Outdated Show resolved Hide resolved
caddyconfig/httpcaddyfile/directives.go Outdated Show resolved Hide resolved
@francislavoie francislavoie requested a review from mholt August 17, 2020 20:52
@francislavoie
Copy link
Member Author

francislavoie commented Aug 17, 2020

Hmm. Looks like we have a flaky test with the Caddyfile adapter. For some reason on some environments, the first group is 0, and sometimes it's 1. 🤔

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now, thanks for looking into this!

@mholt
Copy link
Member

mholt commented Aug 17, 2020

Note that the issue at https://caddy.community/t/v2-help-with-path-regexp-and-rewrite/9442 is not resolved by this because we don't (can't?) sort path_regexp matcher by length; only path.

@francislavoie
Copy link
Member Author

francislavoie commented Aug 17, 2020

Note that the issue at caddy.community/t/v2-help-with-path-regexp-and-rewrite/9442 is not resolved by this because we don't (can't?) sort path_regexp matcher by length; only path.

Actually it is resolved, because having a named matcher will now sort ahead of *, but mutual exclusivity will prevent the * one from taking effect altogether.

:8881

root * /foo

@regexp path_regexp bar ^/z(.*)$
root @regexp /bar

respond "Root: {http.vars.root}"
{
	"apps": {
		"http": {
			"servers": {
				"srv0": {
					"listen": [
						":8881"
					],
					"routes": [
						{
							"group": "group0",
							"match": [
								{
									"path_regexp": {
										"name": "bar",
										"pattern": "^/z(.*)$"
									}
								}
							],
							"handle": [
								{
									"handler": "vars",
									"root": "/bar"
								}
							]
						},
						{
							"group": "group0",
							"handle": [
								{
									"handler": "vars",
									"root": "/foo"
								}
							]
						},
						{
							"handle": [
								{
									"body": "Root: {http.vars.root}",
									"handler": "static_response"
								}
							]
						}
					]
				}
			}
		}
	}
}
$ curl localhost:8881/
Root: /foo

$ curl localhost:8881/zabc
Root: /bar

@mholt
Copy link
Member

mholt commented Aug 17, 2020

Ah, indeed, I was thinking more generally I guess, that this won't sort path_regexp matchers relative to path matchers.

Will merge this in a sec.

@mholt mholt merged commit 0afbab8 into caddyserver:master Aug 17, 2020
@francislavoie francislavoie deleted the caddyfile-root-sort branch August 17, 2020 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants