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

Headers can't be added by matching against response matchers #6763

Closed
lilnasy opened this issue Jan 4, 2025 · 4 comments
Closed

Headers can't be added by matching against response matchers #6763

lilnasy opened this issue Jan 4, 2025 · 4 comments

Comments

@lilnasy
Copy link
Contributor

lilnasy commented Jan 4, 2025

Caddyfile:

example.com:443 {
	route {
		@success status 2xx
		header @success Cache-Control "public, max-age=3600"
		file_server {
			root /www
		}
	}
}

Error:

Error: adapting config using caddyfile: getting matcher module 'status': module not registered: http.matchers.status

Since Chrome caches 404 responses, I want to add the Cache-Control response header only when the response status is ok (2xx).

I looked at Response Matchers (Caddyfile) -- Caddy documentation, which documents the response matcher above, and combined that with the header directive as documented in header (Caddyfile directive) -- Caddy documentation, but counterintuitively, header can only use request matchers.

encode (Caddyfile directive) -- Caddy documentation leads to believe that response matchers can only be used in specific places, not in a named matcher (is the response matcher example out of date?). However, since both the header directive and the encode directive act on pre-existing responses, it's natural that both can match using response matchers, not just encode.

A caddy community help post (Header only on successful response) suggests a workaround using php_fastcgi. However, that makes the Caddyfile unreadable and opaque since my application really has nothing to do with PHP.

@francislavoie
Copy link
Member

You can use intercept for this

@lilnasy
Copy link
Contributor Author

lilnasy commented Jan 4, 2025

Thanks, that helps a lot!

Is there a limitation preventing header from working similarly to encode?

@francislavoie
Copy link
Member

I'm not sure what you mean. They do completely different things.

The header directive's job is to manipulate response headers. It takes a request matcher, just like every other directive. That's it.

The encode directive enables compression, and it takes a request matcher as well. But because only some content types should be compressed, and we only know the content type after the handler that writes the response is done its job, it also has a response matcher option (with a good default).

They have completely different responsibilities.

@lilnasy
Copy link
Contributor Author

lilnasy commented Jan 4, 2025

Thanks for the reply. I should clarify, what I meant to ask was, could header use a response matcher in the same that encode does. After looking at the source code, I learned that not only it can, but it already does!

For example, the following json configuration matches 2xx responses:

{
    "handler": "headers",
    "response": {
        "require": {
            "status_code": [
                2
            ]
        },
        "set": {
            "Cache-Control": [
                "public, max-age=3600"
            ]
        }
    }
},

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