Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 18:00 UTC
reverseproxy: Fix Caddyfile support for `replace_status` (#4754)
Francis Lavoie committed
May 2, 2022 at 13:44 UTC
4a223f52038cb77bbf97ca3f3345550dea4e12d8
2 files changed
+37
-19
caddytest/integration/caddyfile_adapt/reverse_proxy_handle_response.txt
+21
-2
@@ -1,8 +1,13 @@
1
:8884
2
3
reverse_proxy 127.0.0.1:65535 {
4
- @changeStatus status 500
5
- replace_status @changeStatus 400
4
+ @500 status 500
5
+ replace_status @500 400
6
+
7
+ @all status 2xx 3xx 4xx 5xx
8
+ replace_status @all {http.error.status_code}
9
+
10
+ replace_status {http.error.status_code}
11
12
@accel header X-Accel-Redirect *
13
handle_response @accel {
@@ -78,6 +83,17 @@ reverse_proxy 127.0.0.1:65535 {
83
},
84
"status_code": 400
85
},
86
+ {
87
+ "match": {
88
+ "status_code": [
89
+ 2,
90
+ 3,
91
+ 4,
92
+ 5
93
+ ]
94
+ },
95
+ "status_code": "{http.error.status_code}"
96
+ },
97
{
98
"match": {
99
"headers": {
@@ -228,6 +244,9 @@ reverse_proxy 127.0.0.1:65535 {
244
}
245
]
246
},
247
+ {
248
+ "status_code": "{http.error.status_code}"
249
+ },
250
{
251
"routes": [
252
{
modules/caddyhttp/reverseproxy/caddyfile.go
+16
-17
@@ -627,22 +627,24 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
627
628
case "replace_status":
629
args := d.RemainingArgs()
630
- if len(args) != 2 {
631
- return d.Errf("must have two arguments: a response matcher and a status code")
630
+ if len(args) != 1 && len(args) != 2 {
631
+ return d.Errf("must have one or two arguments: an optional response matcher, and a status code")
632
}
633
634
- if !strings.HasPrefix(args[0], matcherPrefix) {
635
- return d.Errf("must use a named response matcher, starting with '@'")
636
- }
637
-
638
- foundMatcher, ok := h.responseMatchers[args[0]]
639
- if !ok {
640
- return d.Errf("no named response matcher defined with name '%s'", args[0][1:])
641
- }
634
+ responseHandler := caddyhttp.ResponseHandler{}
635
643
- _, err := strconv.Atoi(args[1])
644
- if err != nil {
645
- return d.Errf("bad integer value '%s': %v", args[1], err)
636
+ if len(args) == 2 {
637
+ if !strings.HasPrefix(args[0], matcherPrefix) {
638
+ return d.Errf("must use a named response matcher, starting with '@'")
639
+ }
640
+ foundMatcher, ok := h.responseMatchers[args[0]]
641
+ if !ok {
642
+ return d.Errf("no named response matcher defined with name '%s'", args[0][1:])
643
+ }
644
+ responseHandler.Match = &foundMatcher
645
+ responseHandler.StatusCode = caddyhttp.WeakString(args[1])
646
+ } else if len(args) == 1 {
647
+ responseHandler.StatusCode = caddyhttp.WeakString(args[0])
648
}
649
650
// make sure there's no block, cause it doesn't make sense
@@ -652,10 +654,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
654
655
h.HandleResponse = append(
656
h.HandleResponse,
655
- caddyhttp.ResponseHandler{
656
- Match: &foundMatcher,
657
- StatusCode: caddyhttp.WeakString(args[1]),
658
- },
657
+ responseHandler,
658
)
659
660
default: