@marcel91200 / caddy Mirror · read-only / commits / 51f35ba0
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

reverseproxy: Fix upstreams with placeholders with no port (#4046)

Francis Lavoie committed Mar 3, 2021 at 12:12 UTC 51f35ba03f78e691d0e141b24ff19f82bfb34b67
2 files changed +109
caddytest/integration/caddyfile_adapt/reverse_proxy_upstream_placeholder.txt new
+102
@@ -0,0 +1,102 @@
1 +:8884 {
2 + map {host} {upstream} {
3 + foo.example.com 1.2.3.4
4 + default 2.3.4.5
5 + }
6 +
7 + # Upstream placeholder with a port should retain the port
8 + reverse_proxy {upstream}:80
9 +}
10 +
11 +:8885 {
12 + map {host} {upstream} {
13 + foo.example.com 1.2.3.4:8080
14 + default 2.3.4.5:8080
15 + }
16 +
17 + # Upstream placeholder with no port should not have a port joined
18 + reverse_proxy {upstream}
19 +}
20 +----------
21 +{
22 + "apps": {
23 + "http": {
24 + "servers": {
25 + "srv0": {
26 + "listen": [
27 + ":8884"
28 + ],
29 + "routes": [
30 + {
31 + "handle": [
32 + {
33 + "defaults": [
34 + "2.3.4.5"
35 + ],
36 + "destinations": [
37 + "{upstream}"
38 + ],
39 + "handler": "map",
40 + "mappings": [
41 + {
42 + "input": "foo.example.com",
43 + "outputs": [
44 + "1.2.3.4"
45 + ]
46 + }
47 + ],
48 + "source": "{http.request.host}"
49 + },
50 + {
51 + "handler": "reverse_proxy",
52 + "upstreams": [
53 + {
54 + "dial": "{upstream}:80"
55 + }
56 + ]
57 + }
58 + ]
59 + }
60 + ]
61 + },
62 + "srv1": {
63 + "listen": [
64 + ":8885"
65 + ],
66 + "routes": [
67 + {
68 + "handle": [
69 + {
70 + "defaults": [
71 + "2.3.4.5:8080"
72 + ],
73 + "destinations": [
74 + "{upstream}"
75 + ],
76 + "handler": "map",
77 + "mappings": [
78 + {
79 + "input": "foo.example.com",
80 + "outputs": [
81 + "1.2.3.4:8080"
82 + ]
83 + }
84 + ],
85 + "source": "{http.request.host}"
86 + },
87 + {
88 + "handler": "reverse_proxy",
89 + "upstreams": [
90 + {
91 + "dial": "{upstream}"
92 + }
93 + ]
94 + }
95 + ]
96 + }
97 + ]
98 + }
99 + }
100 + }
101 + }
102 +}
modules/caddyhttp/reverseproxy/caddyfile.go
+7
@@ -184,6 +184,13 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
184 if network != "" {
185 return caddy.JoinNetworkAddress(network, host, port), nil
186 }
187 +
188 + // if the host is a placeholder, then we don't want to join with an empty port,
189 + // because that would just append an extra ':' at the end of the address.
190 + if port == "" && strings.Contains(host, "{") {
191 + return host, nil
192 + }
193 +
194 return net.JoinHostPort(host, port), nil
195 }
196