| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddyhttpscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/ |
| 16 | |
| 17 | import ( |
| 18 | "contextscip-go gomod github.com/golang/go/src go1.25.1 context/" |
| 19 | "crypto/tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/" |
| 20 | "crypto/x509scip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/" |
| 21 | "encoding/pemscip-go gomod github.com/golang/go/src go1.25.1 `encoding/pem`/" |
| 22 | "netscip-go gomod github.com/golang/go/src go1.25.1 net/" |
| 23 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 24 | "net/http/httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/" |
| 25 | "testingscip-go gomod github.com/golang/go/src go1.25.1 testing/" |
| 26 | |
| 27 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 28 | ) |
| 29 | |
| 30 | func TestHTTPVarReplacementscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/TestHTTPVarReplacement().(t *testingscip-go gomod github.com/golang/go/src go1.25.1 testing/.Tscip-go gomod github.com/golang/go/src go1.25.1 testing/T#) { |
| 31 | req, _ := httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.NewRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/NewRequest().(httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet., "/foo/bar.tar.gz?a=1&b=2", nil) |
| 32 | repl := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.NewReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/NewReplacer().() |
| 33 | localAddr, _ := netscip-go gomod github.com/golang/go/src go1.25.1 net/.ResolveTCPAddrscip-go gomod github.com/golang/go/src go1.25.1 net/ResolveTCPAddr().("tcp", "192.168.159.1:80") |
| 34 | ctx := contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(req.Contextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Context().(), caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ReplacerCtxKeyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ReplacerCtxKey., repl) |
| 35 | ctx = contextscip-go gomod github.com/golang/go/src go1.25.1 context/.WithValuescip-go gomod github.com/golang/go/src go1.25.1 context/WithValue().(ctx, httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.LocalAddrContextKeyscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/LocalAddrContextKey., localAddr) |
| 36 | req = req.WithContextscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#WithContext().(ctx) |
| 37 | req.Hostscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Host. = "example.com:80" |
| 38 | req.RemoteAddrscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#RemoteAddr. = "192.168.159.32:1234" |
| 39 | |
| 40 | clientCert := []byte(`-----BEGIN CERTIFICATE----- |
| 41 | MIIB9jCCAV+gAwIBAgIBAjANBgkqhkiG9w0BAQsFADAYMRYwFAYDVQQDDA1DYWRk |
| 42 | eSBUZXN0IENBMB4XDTE4MDcyNDIxMzUwNVoXDTI4MDcyMTIxMzUwNVowHTEbMBkG |
| 43 | A1UEAwwSY2xpZW50LmxvY2FsZG9tYWluMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB |
| 44 | iQKBgQDFDEpzF0ew68teT3xDzcUxVFaTII+jXH1ftHXxxP4BEYBU4q90qzeKFneF |
| 45 | z83I0nC0WAQ45ZwHfhLMYHFzHPdxr6+jkvKPASf0J2v2HDJuTM1bHBbik5Ls5eq+ |
| 46 | fVZDP8o/VHKSBKxNs8Goc2NTsr5b07QTIpkRStQK+RJALk4x9QIDAQABo0swSTAJ |
| 47 | BgNVHRMEAjAAMAsGA1UdDwQEAwIHgDAaBgNVHREEEzARgglsb2NhbGhvc3SHBH8A |
| 48 | AAEwEwYDVR0lBAwwCgYIKwYBBQUHAwIwDQYJKoZIhvcNAQELBQADgYEANSjz2Sk+ |
| 49 | eqp31wM9il1n+guTNyxJd+FzVAH+hCZE5K+tCgVDdVFUlDEHHbS/wqb2PSIoouLV |
| 50 | 3Q9fgDkiUod+uIK0IynzIKvw+Cjg+3nx6NQ0IM0zo8c7v398RzB4apbXKZyeeqUH |
| 51 | 9fNwfEi+OoXR6s+upSKobCmLGLGi9Na5s5g= |
| 52 | -----END CERTIFICATE-----`) |
| 53 | |
| 54 | block, _ := pemscip-go gomod github.com/golang/go/src go1.25.1 `encoding/pem`/.Decodescip-go gomod github.com/golang/go/src go1.25.1 `encoding/pem`/Decode().(clientCert) |
| 55 | if block == nil { |
| 56 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("failed to decode PEM certificate") |
| 57 | } |
| 58 | |
| 59 | cert, err := x509scip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/.ParseCertificatescip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/ParseCertificate().(block.Bytesscip-go gomod github.com/golang/go/src go1.25.1 `encoding/pem`/Block#Bytes.) |
| 60 | if err != nil { |
| 61 | t.Fatalfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Fatalf().("failed to decode PEM certificate: %v", err) |
| 62 | } |
| 63 | |
| 64 | req.TLSscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#TLS. = &tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.ConnectionStatescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#{ |
| 65 | Versionscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#Version.: tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.VersionTLS13scip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/VersionTLS13., |
| 66 | HandshakeCompletescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#HandshakeComplete.: true, |
| 67 | ServerNamescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#ServerName.: "example.com", |
| 68 | CipherSuitescip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#CipherSuite.: tlsscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/.TLS_AES_256_GCM_SHA384scip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/TLS_AES_256_GCM_SHA384., |
| 69 | PeerCertificatesscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#PeerCertificates.: []*x509scip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/Certificate#{cert}, |
| 70 | NegotiatedProtocolscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#NegotiatedProtocol.: "h2", |
| 71 | NegotiatedProtocolIsMutualscip-go gomod github.com/golang/go/src go1.25.1 `crypto/tls`/ConnectionState#NegotiatedProtocolIsMutual.: true, |
| 72 | } |
| 73 | |
| 74 | res := httptestscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/.NewRecorderscip-go gomod github.com/golang/go/src go1.25.1 `net/http/httptest`/NewRecorder().() |
| 75 | addHTTPVarsToReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddyhttp`/addHTTPVarsToReplacer().(repl, req, res) |
| 76 | |
| 77 | for i, tc := range []struct { |
| 78 | get string |
| 79 | expect string |
| 80 | }{ |
| 81 | { |
| 82 | get: "http.request.scheme", |
| 83 | expect: "https", |
| 84 | }, |
| 85 | { |
| 86 | get: "http.request.method", |
| 87 | expect: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet., |
| 88 | }, |
| 89 | { |
| 90 | get: "http.request.host", |
| 91 | expect: "example.com", |
| 92 | }, |
| 93 | { |
| 94 | get: "http.request.port", |
| 95 | expect: "80", |
| 96 | }, |
| 97 | { |
| 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", |
| 116 | }, |
| 117 | { |
| 118 | get: "http.request.remote.host/24", |
| 119 | expect: "192.168.159.0/24", |
| 120 | }, |
| 121 | { |
| 122 | get: "http.request.remote.host/24,32", |
| 123 | expect: "192.168.159.0/24", |
| 124 | }, |
| 125 | { |
| 126 | get: "http.request.remote.host/999", |
| 127 | expect: "", |
| 128 | }, |
| 129 | { |
| 130 | get: "http.request.remote.port", |
| 131 | expect: "1234", |
| 132 | }, |
| 133 | { |
| 134 | get: "http.request.host.labels.0", |
| 135 | expect: "com", |
| 136 | }, |
| 137 | { |
| 138 | get: "http.request.host.labels.1", |
| 139 | expect: "example", |
| 140 | }, |
| 141 | { |
| 142 | get: "http.request.host.labels.2", |
| 143 | expect: "", |
| 144 | }, |
| 145 | { |
| 146 | get: "http.request.uri", |
| 147 | expect: "/foo/bar.tar.gz?a=1&b=2", |
| 148 | }, |
| 149 | { |
| 150 | get: "http.request.uri_escaped", |
| 151 | expect: "%2Ffoo%2Fbar.tar.gz%3Fa%3D1%26b%3D2", |
| 152 | }, |
| 153 | { |
| 154 | get: "http.request.uri.path", |
| 155 | expect: "/foo/bar.tar.gz", |
| 156 | }, |
| 157 | { |
| 158 | get: "http.request.uri.path_escaped", |
| 159 | expect: "%2Ffoo%2Fbar.tar.gz", |
| 160 | }, |
| 161 | { |
| 162 | get: "http.request.uri.path.file", |
| 163 | expect: "bar.tar.gz", |
| 164 | }, |
| 165 | { |
| 166 | get: "http.request.uri.path.file.base", |
| 167 | expect: "bar.tar", |
| 168 | }, |
| 169 | { |
| 170 | // not ideal, but also most correct, given that files can have dots (example: index.<SHA>.html) TODO: maybe this isn't right.. |
| 171 | get: "http.request.uri.path.file.ext", |
| 172 | expect: ".gz", |
| 173 | }, |
| 174 | { |
| 175 | get: "http.request.uri.query", |
| 176 | expect: "a=1&b=2", |
| 177 | }, |
| 178 | { |
| 179 | get: "http.request.uri.query_escaped", |
| 180 | expect: "a%3D1%26b%3D2", |
| 181 | }, |
| 182 | { |
| 183 | get: "http.request.uri.query.a", |
| 184 | expect: "1", |
| 185 | }, |
| 186 | { |
| 187 | get: "http.request.uri.query.b", |
| 188 | expect: "2", |
| 189 | }, |
| 190 | { |
| 191 | get: "http.request.uri.prefixed_query", |
| 192 | expect: "?a=1&b=2", |
| 193 | }, |
| 194 | { |
| 195 | get: "http.request.tls.cipher_suite", |
| 196 | expect: "TLS_AES_256_GCM_SHA384", |
| 197 | }, |
| 198 | { |
| 199 | get: "http.request.tls.proto", |
| 200 | expect: "h2", |
| 201 | }, |
| 202 | { |
| 203 | get: "http.request.tls.proto_mutual", |
| 204 | expect: "true", |
| 205 | }, |
| 206 | { |
| 207 | get: "http.request.tls.resumed", |
| 208 | expect: "false", |
| 209 | }, |
| 210 | { |
| 211 | get: "http.request.tls.server_name", |
| 212 | expect: "example.com", |
| 213 | }, |
| 214 | { |
| 215 | get: "http.request.tls.version", |
| 216 | expect: "tls1.3", |
| 217 | }, |
| 218 | { |
| 219 | get: "http.request.tls.client.fingerprint", |
| 220 | expect: "9f57b7b497cceacc5459b76ac1c3afedbc12b300e728071f55f84168ff0f7702", |
| 221 | }, |
| 222 | { |
| 223 | get: "http.request.tls.client.issuer", |
| 224 | expect: "CN=Caddy Test CA", |
| 225 | }, |
| 226 | { |
| 227 | get: "http.request.tls.client.serial", |
| 228 | expect: "2", |
| 229 | }, |
| 230 | { |
| 231 | get: "http.request.tls.client.subject", |
| 232 | expect: "CN=client.localdomain", |
| 233 | }, |
| 234 | { |
| 235 | get: "http.request.tls.client.san.dns_names", |
| 236 | expect: "[localhost]", |
| 237 | }, |
| 238 | { |
| 239 | get: "http.request.tls.client.san.dns_names.0", |
| 240 | expect: "localhost", |
| 241 | }, |
| 242 | { |
| 243 | get: "http.request.tls.client.san.dns_names.1", |
| 244 | expect: "", |
| 245 | }, |
| 246 | { |
| 247 | get: "http.request.tls.client.san.ips", |
| 248 | expect: "[127.0.0.1]", |
| 249 | }, |
| 250 | { |
| 251 | get: "http.request.tls.client.san.ips.0", |
| 252 | expect: "127.0.0.1", |
| 253 | }, |
| 254 | { |
| 255 | get: "http.request.tls.client.certificate_pem", |
| 256 | expect: string(clientCert) + "\n", // returned value comes with a newline appended to it |
| 257 | }, |
| 258 | } { |
| 259 | actual, got := repl.GetStringscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#GetString().(tc.get) |
| 260 | if !got { |
| 261 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected to recognize the placeholder name, but didn't", i) |
| 262 | } |
| 263 | if actual != tc.expect { |
| 264 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.1 testing/common#Errorf().("Test %d: Expected %s to be '%s' but got '%s'", |
| 265 | i, tc.get, tc.expect, actual) |
| 266 | } |
| 267 | } |
| 268 | } |