-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
envs.go
117 lines (98 loc) · 3.23 KB
/
envs.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com>
*
* This file is part of Symfony CLI project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package php
import (
"net"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/symfony-cli/symfony-cli/envs"
)
func (p *Server) resolveScriptName(pathInfo string) (string, string) {
pathInfo = path.Clean(pathInfo)
if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 {
file := path.Clean(pathInfo[:pos+4])
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
return file, pathInfo[pos+4:]
}
}
if len(pathInfo) <= 1 {
return p.passthru, pathInfo
}
paths := strings.Split(pathInfo, "/")
for n := len(paths); n > 0; n-- {
file := filepath.Clean(filepath.Join(paths[:n]...))
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
serverPath := path.Join(paths[n:]...)
if serverPath == "" && pathInfo[len(pathInfo)-1:] != "/" {
return file, ""
}
return file, "/" + serverPath
}
}
return p.passthru, pathInfo
}
func (p *Server) generateEnv(req *http.Request) map[string]string {
scriptName, pathInfo := p.resolveScriptName(req.URL.Path)
https := ""
if req.TLS != nil {
https = "On"
}
remoteAddr := req.Header.Get("X-Client-IP")
remotePort := ""
if remoteAddr == "" {
remoteAddr, remotePort, _ = net.SplitHostPort(req.RemoteAddr)
}
env := map[string]string{
"CONTENT_LENGTH": req.Header.Get("Content-Length"),
"CONTENT_TYPE": req.Header.Get("Content-Type"),
"DOCUMENT_URI": scriptName,
"DOCUMENT_ROOT": p.documentRoot,
"GATEWAY_INTERFACE": "CGI/1.1",
"HTTP_HOST": req.Host,
"HTTP_MOD_REWRITE": "On", // because Pagekit relies on it
"HTTPS": https,
"PATH_INFO": pathInfo,
"QUERY_STRING": req.URL.RawQuery,
"REDIRECT_STATUS": "200", // required if PHP was built with --enable-force-cgi-redirect
"REMOTE_ADDR": remoteAddr,
"REMOTE_PORT": remotePort,
"REQUEST_METHOD": req.Method,
"REQUEST_URI": req.RequestURI,
"SCRIPT_FILENAME": filepath.Join(p.documentRoot, scriptName),
"SCRIPT_NAME": scriptName,
}
if local, err := envs.NewLocal(p.projectDir, false); err == nil {
for k, v := range envs.AsMap(local) {
env[k] = v
}
}
// iterate over request headers and append them to the environment variables in the valid format
for k, v := range req.Header {
key := strings.Replace(strings.ToUpper(k), "-", "_", -1)
// ignore HTTP_HOST -- see https://httpoxy.org/
if key == "HOST" {
continue
}
env["HTTP_"+key] = strings.Join(v, ";")
}
return env
}