Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 22:00 UTC
reverseproxy: Replace health header placeholders (#5861)
Francis Lavoie committed
Oct 11, 2023 at 11:50 UTC
05dbe1c171846b0b683dedbe2c4c20683e867ba0
3 files changed
+21
-5
caddytest/integration/caddyfile_adapt/reverse_proxy_health_headers.txt
+10
@@ -6,6 +6,9 @@ reverse_proxy 127.0.0.1:65535 {
6
X-Header-Key 95ca39e3cbe7
7
X-Header-Keys VbG4NZwWnipo 335Q9/MhqcNU3s2TO
8
X-Empty-Value
9
+ Same-Key 1
10
+ Same-Key 2
11
+ X-System-Hostname {system.hostname}
12
}
13
health_uri /health
14
}
@@ -29,6 +32,10 @@ reverse_proxy 127.0.0.1:65535 {
32
"Host": [
33
"example.com"
34
],
35
+ "Same-Key": [
36
+ "1",
37
+ "2"
38
+ ],
39
"X-Empty-Value": [
40
""
41
],
@@ -38,6 +45,9 @@ reverse_proxy 127.0.0.1:65535 {
45
"X-Header-Keys": [
46
"VbG4NZwWnipo",
47
"335Q9/MhqcNU3s2TO"
48
+ ],
49
+ "X-System-Hostname": [
50
+ "{system.hostname}"
51
]
52
},
53
"uri": "/health"
modules/caddyhttp/reverseproxy/caddyfile.go
+1
-1
@@ -372,7 +372,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
372
if len(values) == 0 {
373
values = append(values, "")
374
}
375
- healthHeaders[key] = values
375
+ healthHeaders[key] = append(healthHeaders[key], values...)
376
}
377
if h.HealthChecks == nil {
378
h.HealthChecks = new(HealthChecks)
modules/caddyhttp/reverseproxy/healthchecks.go
+10
-4
@@ -358,11 +358,17 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
358
}
359
ctx = context.WithValue(ctx, caddyhttp.OriginalRequestCtxKey, *req)
360
req = req.WithContext(ctx)
361
- for key, hdrs := range h.HealthChecks.Active.Headers {
361
+
362
+ // set headers, using a replacer with only globals (env vars, system info, etc.)
363
+ repl := caddy.NewReplacer()
364
+ for key, vals := range h.HealthChecks.Active.Headers {
365
+ key = repl.ReplaceAll(key, "")
366
if key == "Host" {
363
- req.Host = h.HealthChecks.Active.Headers.Get(key)
364
- } else {
365
- req.Header[key] = hdrs
367
+ req.Host = repl.ReplaceAll(h.HealthChecks.Active.Headers.Get(key), "")
368
+ continue
369
+ }
370
+ for _, val := range vals {
371
+ req.Header.Add(key, repl.ReplaceKnown(val, ""))
372
}
373
}
374