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

Method buildEnv

modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go:257–417  ·  view source on GitHub ↗

buildEnv returns a set of CGI environment variables for the request.

(r *http.Request)

Source from the content-addressed store, hash-verified

255
256// buildEnv returns a set of CGI environment variables for the request.
257func (t Transport) buildEnv(r *http.Request) (envVars, error) {
258 repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
259
260 var env envVars
261
262 // Separate remote IP and port; more lenient than net.SplitHostPort
263 var ip, port string
264 if idx := strings.LastIndex(r.RemoteAddr, ":"); idx > -1 {
265 ip = r.RemoteAddr[:idx]
266 port = r.RemoteAddr[idx+1:]
267 } else {
268 ip = r.RemoteAddr
269 }
270
271 // Remove [] from IPv6 addresses
272 ip = strings.Replace(ip, "[", "", 1)
273 ip = strings.Replace(ip, "]", "", 1)
274
275 // make sure file root is absolute
276 root, err := caddy.FastAbs(repl.ReplaceAll(t.Root, "."))
277 if err != nil {
278 return nil, err
279 }
280
281 if t.ResolveRootSymlink {
282 root, err = filepath.EvalSymlinks(root)
283 if err != nil {
284 return nil, err
285 }
286 }
287
288 fpath := r.URL.Path
289 scriptName := fpath
290
291 docURI := fpath
292 // split "actual path" from "path info" if configured
293 var pathInfo string
294 if splitPos := t.splitPos(fpath); splitPos > -1 {
295 docURI = fpath[:splitPos]
296 pathInfo = fpath[splitPos:]
297
298 // Strip PATH_INFO from SCRIPT_NAME
299 scriptName = strings.TrimSuffix(scriptName, pathInfo)
300 }
301
302 // Try to grab the path remainder from a file matcher
303 // if we didn't get a split result here.
304 // See https://github.com/caddyserver/caddy/issues/3718
305 if pathInfo == "" {
306 pathInfo, _ = repl.GetString("http.matchers.file.remainder")
307 }
308
309 // SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
310 scriptFilename := caddyhttp.SanitizedPathJoin(root, scriptName)
311
312 // Ensure the SCRIPT_NAME has a leading slash for compliance with RFC3875
313 // Info: https://tools.ietf.org/html/rfc3875#section-4.1.13
314 if scriptName != "" && !strings.HasPrefix(scriptName, "/") {

Callers 1

RoundTripMethod · 0.95

Calls 7

splitPosMethod · 0.95
SanitizedPathJoinFunction · 0.92
SupportedCipherSuitesFunction · 0.92
ReplaceAllMethod · 0.80
GetStringMethod · 0.80
GetMethod · 0.65
ValueMethod · 0.45

Tested by

no test coverage detected