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

Use Caddy's request matchers, directive ordering #2

Open
francislavoie opened this issue Jun 15, 2020 · 1 comment
Open

Use Caddy's request matchers, directive ordering #2

francislavoie opened this issue Jun 15, 2020 · 1 comment

Comments

@francislavoie
Copy link

francislavoie commented Jun 15, 2020

I saw you're using path as a matcher inside of the filter plugin. This isn't necessary, because you can already use Caddy's built-in request matchers for this.

When you register your Caddyfile directive with RegisterHandlerDirective, it allows a matcher token to be the first argument to your directive, like most other Caddyfile directives. This means you can do this:

filter /foo/* {
	...
}

If you want to use regexp patterns, then you'd do this:

@pattern {
	path_regexp ^/(foo|bar).*
}
filter @pattern {
	...
}

I would've suggested also getting rid of content_type but I think that looks at the response headers which isn't something you can do with request matching (obviously). So that one's fine to leave as-is.

Also, you should mention that users should set an order for the directive in their global options to use it, because otherwise they'll encounter an error when trying to use it (at least when outside of a route block). See https://caddyserver.com/docs/caddyfile/options, the directive ordering is here: https://caddyserver.com/docs/caddyfile/directives#directive-order

I'd recommend something like:

{
	order filter after encode
}

This will make your directive get handled just before the response is encoded (gzipped, etc) and after the response is templated.

@francislavoie francislavoie changed the title Use Caddy's request matchers Use Caddy's request matchers, directive ordering Jun 15, 2020
@htfy96
Copy link
Contributor

htfy96 commented Jun 15, 2020

Thanks for suggestions! I've updated the doc to include the order directive (actually they exist in test file, but order filter after encode is definitely better than my order filter first.

For the Caddy's request matcher, I'll look into that this weekend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants