-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
fileserver: Add status code override #4076
Conversation
dcffec6
to
4c87164
Compare
Wouldn't it be better to use the error routes instead? ala https://github.com/caddyserver/caddy/pull/4076/files#diff-08835e4955ebc248e067238c368ac226eb8b44742ee4b04400ddb5f1e550cb47R268-R284 |
Unfortunately I don't think so, because I don't think error routes will let you keep the same error code if you're serving a file with But either way, that would be pretty roundabout... This is still useful in other situations though, e.g. if you want to serve a "maintenance mode" page with a specific status, without emitting |
Why did we add the |
I'm saying those are separate but valid usecases. The times where you use Here's an example of how it would look when paired with
But if it had to be done with
And that assumes that |
After reading a question about the `handle_response` feature of `reverse_proxy`, I realized that we didn't have a way of serving an arbitrary file with a status code other than 200. This is an issue in situations where you want to serve a custom error page in routes that are not errors, like the aforementioned `handle_response`, where you may want to retain the status code returned by the proxy but write a response with content from a file. This feature is super simple, basically if a status code is configured (can be a status code number, or a placeholder string) then that status will be written out before serving the file - if we write the status code first, then the stdlib won't write its own (only the first HTTP status header wins).
79af89f
to
12a8199
Compare
Another example usecase for this https://caddy.community/t/serving-a-single-static-file-that-matches-multiple-paths-with-a-http-status-code-of-410/12072 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I'm convinced this could be useful. Thanks!
After reading a question about the
handle_response
feature ofreverse_proxy
, I realized that we didn't have a way of serving an arbitrary file with a status code other than 200. This is an issue in situations where you want to serve a custom error page in routes that are not errors, like the aforementionedhandle_response
, where you may want to retain the status code returned by the proxy but write a response with content from a file.This feature is super simple, basically if a status code is configured (can be a status code number, or a placeholder string) then that status will be written out before serving the file - if we write the status code first, then the stdlib won't write its own (only the first HTTP status header wins).