| 56 | } |
| 57 | |
| 58 | func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.ResponseWriter) { |
| 59 | SetVar(req.Context(), "start_time", time.Now()) |
| 60 | SetVar(req.Context(), "uuid", new(requestID)) |
| 61 | |
| 62 | httpVars := func(key string) (any, bool) { |
| 63 | if req != nil { |
| 64 | // query string parameters |
| 65 | if strings.HasPrefix(key, reqURIQueryReplPrefix) { |
| 66 | vals := req.URL.Query()[key[len(reqURIQueryReplPrefix):]] |
| 67 | // always return true, since the query param might |
| 68 | // be present only in some requests |
| 69 | return strings.Join(vals, ","), true |
| 70 | } |
| 71 | |
| 72 | // request header fields |
| 73 | if strings.HasPrefix(key, reqHeaderReplPrefix) { |
| 74 | field := key[len(reqHeaderReplPrefix):] |
| 75 | vals := req.Header[textproto.CanonicalMIMEHeaderKey(field)] |
| 76 | // always return true, since the header field might |
| 77 | // be present only in some requests |
| 78 | return strings.Join(vals, ","), true |
| 79 | } |
| 80 | |
| 81 | // cookies |
| 82 | if strings.HasPrefix(key, reqCookieReplPrefix) { |
| 83 | name := key[len(reqCookieReplPrefix):] |
| 84 | for _, cookie := range req.Cookies() { |
| 85 | if strings.EqualFold(name, cookie.Name) { |
| 86 | // always return true, since the cookie might |
| 87 | // be present only in some requests |
| 88 | return cookie.Value, true |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // http.request.tls.* |
| 94 | if strings.HasPrefix(key, reqTLSReplPrefix) { |
| 95 | return getReqTLSReplacement(req, key) |
| 96 | } |
| 97 | |
| 98 | switch key { |
| 99 | case "http.request.method": |
| 100 | return req.Method, true |
| 101 | case "http.request.scheme": |
| 102 | if req.TLS != nil { |
| 103 | return "https", true |
| 104 | } |
| 105 | return "http", true |
| 106 | case "http.request.proto": |
| 107 | return req.Proto, true |
| 108 | case "http.request.host": |
| 109 | host, _, err := net.SplitHostPort(req.Host) |
| 110 | if err != nil { |
| 111 | return req.Host, true // OK; there probably was no port |
| 112 | } |
| 113 | return host, true |
| 114 | case "http.request.port": |
| 115 | _, port, _ := net.SplitHostPort(req.Host) |