summaryrefslogtreecommitdiff
path: root/caddytest
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2021-04-08 13:09:12 -0400
committerGitHub <noreply@github.com>2021-04-08 11:09:12 -0600
commit3f6283b385642c56f34b479d1275095379b062d3 (patch)
tree06a2102829e53d936e731d4a11d12543dba65272 /caddytest
parent45fb7202ac0e606ccb7b4fe95f169424f0a6cabc (diff)
fileserver: Add status code override (#4076)
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).
Diffstat (limited to 'caddytest')
-rw-r--r--caddytest/integration/caddyfile_adapt/file_server_status.txt112
1 files changed, 112 insertions, 0 deletions
diff --git a/caddytest/integration/caddyfile_adapt/file_server_status.txt b/caddytest/integration/caddyfile_adapt/file_server_status.txt
new file mode 100644
index 0000000..ede1f4a
--- /dev/null
+++ b/caddytest/integration/caddyfile_adapt/file_server_status.txt
@@ -0,0 +1,112 @@
+localhost
+
+root * /srv
+
+handle /nope* {
+ file_server {
+ status 403
+ }
+}
+
+handle /custom-status* {
+ file_server {
+ status {env.CUSTOM_STATUS}
+ }
+}
+----------
+{
+ "apps": {
+ "http": {
+ "servers": {
+ "srv0": {
+ "listen": [
+ ":443"
+ ],
+ "routes": [
+ {
+ "match": [
+ {
+ "host": [
+ "localhost"
+ ]
+ }
+ ],
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "handler": "vars",
+ "root": "/srv"
+ }
+ ]
+ },
+ {
+ "group": "group2",
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "handler": "file_server",
+ "hide": [
+ "./Caddyfile"
+ ],
+ "status_code": "{env.CUSTOM_STATUS}"
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/custom-status*"
+ ]
+ }
+ ]
+ },
+ {
+ "group": "group2",
+ "handle": [
+ {
+ "handler": "subroute",
+ "routes": [
+ {
+ "handle": [
+ {
+ "handler": "file_server",
+ "hide": [
+ "./Caddyfile"
+ ],
+ "status_code": 403
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "match": [
+ {
+ "path": [
+ "/nope*"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ],
+ "terminal": true
+ }
+ ]
+ }
+ }
+ }
+ }
+} \ No newline at end of file