-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
95 lines (78 loc) · 2.67 KB
/
env.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package dotnet
import (
"net"
"net/http"
"path/filepath"
"strings"
"github.com/caddyserver/caddy/v2"
)
var headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
// buildEnv returns a set of environment variables for the request.
func (d DotNet) buildEnv(r *http.Request) (map[string]string, error) {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
env := make(map[string]string)
// Separate remote IP and port; more lenient than net.SplitHostPort
var ip, port string
if idx := strings.LastIndex(r.RemoteAddr, ":"); idx > -1 {
ip = r.RemoteAddr[:idx]
port = r.RemoteAddr[idx+1:]
} else {
ip = r.RemoteAddr
}
// Remove [] from IPv6 addresses
ip = strings.Replace(ip, "[", "", 1)
ip = strings.Replace(ip, "]", "", 1)
// make sure file root is absolute
root, err := filepath.Abs(repl.ReplaceAll(d.WorkingDir, "."))
if err != nil {
return nil, err
}
docURI := r.URL.Path
scriptName := docURI
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
scriptFilename := filepath.Join(root, scriptName)
requestScheme := "http"
if r.TLS != nil {
requestScheme = "https"
}
reqHost, reqPort, err := net.SplitHostPort(r.Host)
if err != nil {
// whatever, just assume there was no port
reqHost = r.Host
}
authUser, _ := repl.GetString("http.auth.user.id")
env["AUTH_TYPE"] = "" // Not used
env["CONTENT_LENGTH"] = r.Header.Get("Content-Length")
env["CONTENT_TYPE"] = r.Header.Get("Content-Type")
env["GATEWAY_INTERFACE"] = "CGI/1.1"
env["PATH_INFO"] = ""
env["QUERY_STRING"] = r.URL.RawQuery
env["REMOTE_ADDR"] = ip
env["REMOTE_HOST"] = ip // For speed, remote host lookups disabled
env["REMOTE_PORT"] = port
env["REMOTE_IDENT"] = "" // Not used
env["REMOTE_USER"] = authUser
env["REQUEST_METHOD"] = r.Method
env["REQUEST_SCHEME"] = requestScheme
env["SERVER_NAME"] = reqHost
env["SERVER_PROTOCOL"] = r.Proto
env["SERVER_SOFTWARE"] = d.logger.Name()
// Other variables
env["DOCUMENT_ROOT"] = root
env["DOCUMENT_URI"] = docURI
env["HTTP_HOST"] = r.Host // added here, since not always part of headers
env["REQUEST_URI"] = r.URL.RequestURI()
env["SCRIPT_FILENAME"] = scriptFilename
env["SCRIPT_NAME"] = scriptName
if reqPort != "" {
env["SERVER_PORT"] = reqPort
} else if requestScheme == "http" {
env["SERVER_PORT"] = "80"
} else if requestScheme == "https" {
env["SERVER_PORT"] = "443"
}
addTLSEnv(r, env)
addUserDefinedEnv(d, repl, env)
addHTTPHeadersEnv(r, env)
return env, nil
}