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 |
mholt
left a comment
There was a problem hiding this comment.
Alright, I'm convinced this could be useful. Thanks!
After reading a question about the
handle_responsefeature 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).