@marcel91200 / caddy Mirror · read-only / commits / ddb1d2c2
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 12, 14:30 UTC

caddyhttp: add http.request.local{,.host,.port} placeholder (#6182)

* caddyhttp: add `http.request.local{,.host,.port}` placeholder This is the counterpart of `http.request.remote{,.host,.port}`. `http.request.remote` operates on the remote client's address, while `http.request.local` operates on the address the connection arrived on. Take the following example: - Caddy serving on `203.0.113.1:80` - Client on `203.0.113.2` `http.request.remote.host` would return `203.0.113.2` (client IP) `http.request.local.host` would return `203.0.113.1` (server IP) `http.request.local.port` would return `80` (server port) I find this helpful for debugging setups with multiple servers and/or multiple network paths (multiple IPs, AnyIP, Anycast). Co-authored-by: networkException <git@nwex.de> * caddyhttp: add unit test for `http.request.local{,.host,.port}` * caddyhttp: add integration test for `http.request.local.port` * caddyhttp: fix `http.request.local.host` placeholder handling with unix sockets The implementation matches the one of `http.request.remote.host` now and returns the unix socket path (just like `http.request.local` already did) instead of an empty string. --------- Co-authored-by: networkException <git@nwex.de>

Emily committed Mar 27, 2024 at 22:36 UTC ddb1d2c2b11b860f1e91b43d830d283d1e1363b2
4 files changed +64
caddytest/integration/caddyfile_test.go
+19
@@ -517,6 +517,25 @@ func TestUriOps(t *testing.T) {
517 tester.AssertGetResponse("http://localhost:9080/endpoint?foo=bar0&baz=buz&taz=nottest&changethis=val", 200, "changed=val&foo=bar0&foo=bar&key%3Dvalue=example&taz=test")
518 }
519
520 +// Tests the `http.request.local.port` placeholder.
521 +// We don't test the very similar `http.request.local.host` placeholder,
522 +// because depending on the host the test is running on, localhost might
523 +// refer to 127.0.0.1 or ::1.
524 +// TODO: Test each http version separately (especially http/3)
525 +func TestHttpRequestLocalPortPlaceholder(t *testing.T) {
526 + tester := caddytest.NewTester(t)
527 +
528 + tester.InitServer(`
529 + {
530 + admin localhost:2999
531 + http_port 9080
532 + }
533 + :9080
534 + respond "{http.request.local.port}"`, "caddyfile")
535 +
536 + tester.AssertGetResponse("http://localhost:9080/", 200, "9080")
537 +}
538 +
539 func TestSetThenAddQueryParams(t *testing.T) {
540 tester := caddytest.NewTester(t)
541
modules/caddyhttp/app.go
+3
@@ -68,6 +68,9 @@ func init() {
68 // `{http.request.orig_uri.query}` | The request's original query string (without `?`)
69 // `{http.request.port}` | The port part of the request's Host header
70 // `{http.request.proto}` | The protocol of the request
71 +// `{http.request.local.host}` | The host (IP) part of the local address the connection arrived on
72 +// `{http.request.local.port}` | The port part of the local address the connection arrived on
73 +// `{http.request.local}` | The local address the connection arrived on
74 // `{http.request.remote.host}` | The host (IP) part of the remote client's address
75 // `{http.request.remote.port}` | The port part of the remote client's address
76 // `{http.request.remote}` | The address of the remote client
modules/caddyhttp/replacer.go
+27
@@ -119,11 +119,38 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
119 return port, true
120 case "http.request.hostport":
121 return req.Host, true
122 + case "http.request.local":
123 + localAddr, _ := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
124 + return localAddr.String(), true
125 + case "http.request.local.host":
126 + localAddr, _ := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
127 + host, _, err := net.SplitHostPort(localAddr.String())
128 + if err != nil {
129 + // localAddr is host:port for tcp and udp sockets and /unix/socket.path
130 + // for unix sockets. net.SplitHostPort only operates on tcp and udp sockets,
131 + // not unix sockets and will fail with the latter.
132 + // We assume when net.SplitHostPort fails, localAddr is a unix socket and thus
133 + // already "split" and save to return.
134 + return localAddr, true
135 + }
136 + return host, true
137 + case "http.request.local.port":
138 + localAddr, _ := req.Context().Value(http.LocalAddrContextKey).(net.Addr)
139 + _, port, _ := net.SplitHostPort(localAddr.String())
140 + if portNum, err := strconv.Atoi(port); err == nil {
141 + return portNum, true
142 + }
143 + return port, true
144 case "http.request.remote":
145 return req.RemoteAddr, true
146 case "http.request.remote.host":
147 host, _, err := net.SplitHostPort(req.RemoteAddr)
148 if err != nil {
149 + // req.RemoteAddr is host:port for tcp and udp sockets and /unix/socket.path
150 + // for unix sockets. net.SplitHostPort only operates on tcp and udp sockets,
151 + // not unix sockets and will fail with the latter.
152 + // We assume when net.SplitHostPort fails, req.RemoteAddr is a unix socket
153 + // and thus already "split" and save to return.
154 return req.RemoteAddr, true
155 }
156 return host, true
modules/caddyhttp/replacer_test.go
+15
@@ -19,6 +19,7 @@ import (
19 "crypto/tls"
20 "crypto/x509"
21 "encoding/pem"
22 + "net"
23 "net/http"
24 "net/http/httptest"
25 "testing"
@@ -29,7 +30,9 @@ import (
30 func TestHTTPVarReplacement(t *testing.T) {
31 req, _ := http.NewRequest(http.MethodGet, "/foo/bar.tar.gz", nil)
32 repl := caddy.NewReplacer()
33 + localAddr, _ := net.ResolveTCPAddr("tcp", "192.168.159.1:80")
34 ctx := context.WithValue(req.Context(), caddy.ReplacerCtxKey, repl)
35 + ctx = context.WithValue(ctx, http.LocalAddrContextKey, localAddr)
36 req = req.WithContext(ctx)
37 req.Host = "example.com:80"
38 req.RemoteAddr = "192.168.159.32:1234"
@@ -95,6 +98,18 @@ eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV
98 get: "http.request.hostport",
99 expect: "example.com:80",
100 },
101 + {
102 + get: "http.request.local.host",
103 + expect: "192.168.159.1",
104 + },
105 + {
106 + get: "http.request.local.port",
107 + expect: "80",
108 + },
109 + {
110 + get: "http.request.local",
111 + expect: "192.168.159.1:80",
112 + },
113 {
114 get: "http.request.remote.host",
115 expect: "192.168.159.32",