MCPcopy
hub / github.com/caddyserver/caddy / addHTTPVarsToReplacer

Function addHTTPVarsToReplacer

modules/caddyhttp/replacer.go:58–404  ·  view source on GitHub ↗
(repl *caddy.Replacer, req *http.Request, w http.ResponseWriter)

Source from the content-addressed store, hash-verified

56}
57
58func 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)

Callers 10

newVarsTestRequestFunction · 0.85
PrepareRequestFunction · 0.85
TestPathREMatcherFunction · 0.85
TestHeaderREMatcherFunction · 0.85
BenchmarkHeaderREMatcherFunction · 0.85
TestVarREMatcherFunction · 0.85
TestHTTPVarReplacementFunction · 0.85
NewTestReplacerFunction · 0.85
TestMatchExpressionMatchFunction · 0.85

Calls 10

SetVarFunction · 0.85
getReqTLSReplacementFunction · 0.85
GetVarFunction · 0.85
LoadMethod · 0.80
MapMethod · 0.80
ValueMethod · 0.45
StringMethod · 0.45
SetMethod · 0.45
CloseMethod · 0.45
HeaderMethod · 0.45

Tested by 8

newVarsTestRequestFunction · 0.68
TestPathREMatcherFunction · 0.68
TestHeaderREMatcherFunction · 0.68
BenchmarkHeaderREMatcherFunction · 0.68
TestVarREMatcherFunction · 0.68
TestHTTPVarReplacementFunction · 0.68
TestMatchExpressionMatchFunction · 0.68