PrepareRequest fills the request r for use in a Caddy HTTP handler chain. w and s can be nil, but the handlers will lose response placeholders and access to the server.
(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter, s *Server)
| 980 | // PrepareRequest fills the request r for use in a Caddy HTTP handler chain. w and s can |
| 981 | // be nil, but the handlers will lose response placeholders and access to the server. |
| 982 | func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter, s *Server) *http.Request { |
| 983 | // set up the context for the request |
| 984 | ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl) |
| 985 | ctx = context.WithValue(ctx, ServerCtxKey, s) |
| 986 | |
| 987 | trusted, clientIP := determineTrustedProxy(r, s) |
| 988 | ctx = context.WithValue(ctx, VarsCtxKey, map[string]any{ |
| 989 | TrustedProxyVarKey: trusted, |
| 990 | ClientIPVarKey: clientIP, |
| 991 | }) |
| 992 | |
| 993 | ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{})) |
| 994 | |
| 995 | var url2 url.URL // avoid letting this escape to the heap |
| 996 | ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2)) |
| 997 | |
| 998 | ctx = context.WithValue(ctx, ExtraLogFieldsCtxKey, new(ExtraLogFields)) |
| 999 | r = r.WithContext(ctx) |
| 1000 | |
| 1001 | // once the pointer to the request won't change |
| 1002 | // anymore, finish setting up the replacer |
| 1003 | addHTTPVarsToReplacer(repl, r, w) |
| 1004 | |
| 1005 | return r |
| 1006 | } |
| 1007 | |
| 1008 | // originalRequest returns a partial, shallow copy of |
| 1009 | // req, including: req.Method, deep copy of req.URL |