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

caddyhttp: Set Content-Type for static response #4999

Merged
merged 2 commits into from
Aug 31, 2022
Merged

Conversation

mholt
Copy link
Member

@mholt mholt commented Aug 31, 2022

Since the Body field is a string, and it is generally configured via command-line or text config file, it follows that a reasonable default Content-Type is text/plain. This allows templates to render templates included in the response, and makes it clear to clients that they do not need to download a binary file, without extra configuration of this header.

We also detect valid JSON (JSON can be a number or quoted string which is easily ambiguous as to Content-Type, but the most common form of JSON is an object or array, so we only do it for those structures) and set application/json. But those are the only 2 special cases where we set the header automatically. I think those are probably the most common use cases for respond: plain text and hard-coded JSON.

This should make using the static_response handler less cumbersome and more convenient, especially now that it has a CLI shortcut with caddy respond.

@mholt mholt added this to the v2.6.0-beta.1 milestone Aug 31, 2022
@francislavoie
Copy link
Member

Worth adding, in addition to enabling templates support, it'll also enable encode support since it also matches on the response content type header to determine if it should do its work.

@mholt
Copy link
Member Author

mholt commented Aug 31, 2022

True, as long as the body is the minimum length for encoding (I think 512 bytes currently), this should enable compression if that's configured!

@mholt mholt merged commit 2d5a30b into master Aug 31, 2022
@mholt mholt deleted the response-default-ct branch August 31, 2022 15:43
@mholt mholt modified the milestones: v2.6.0-beta.1, v2.6.0 Sep 13, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants