github.com/symfony-cli/symfony-cli@v0.0.0-20240514161054-ece2df437dfa/local/php/envs.go (about) 1 /* 2 * Copyright (c) 2021-present Fabien Potencier <fabien@symfony.com> 3 * 4 * This file is part of Symfony CLI project 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Affero General Public License as 8 * published by the Free Software Foundation, either version 3 of the 9 * License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Affero General Public License for more details. 15 * 16 * You should have received a copy of the GNU Affero General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package php 21 22 import ( 23 "net" 24 "net/http" 25 "os" 26 "path" 27 "path/filepath" 28 "strings" 29 30 "github.com/symfony-cli/symfony-cli/envs" 31 ) 32 33 func (p *Server) generateEnv(req *http.Request) map[string]string { 34 scriptName := p.passthru 35 https := "" 36 if req.TLS != nil { 37 https = "On" 38 } 39 40 pathInfo := req.URL.Path 41 if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 { 42 file := path.Clean(pathInfo[:pos+4]) 43 if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil { 44 scriptName = file 45 pathInfo = pathInfo[pos+4:] 46 } 47 } 48 49 remoteAddr := req.Header.Get("X-Client-IP") 50 remotePort := "" 51 if remoteAddr == "" { 52 remoteAddr, remotePort, _ = net.SplitHostPort(req.RemoteAddr) 53 } 54 55 env := map[string]string{ 56 "CONTENT_LENGTH": req.Header.Get("Content-Length"), 57 "CONTENT_TYPE": req.Header.Get("Content-Type"), 58 "DOCUMENT_URI": scriptName, 59 "DOCUMENT_ROOT": p.documentRoot, 60 "GATEWAY_INTERFACE": "CGI/1.1", 61 "HTTP_HOST": req.Host, 62 "HTTP_MOD_REWRITE": "On", // because Pagekit relies on it 63 "HTTPS": https, 64 "PATH_INFO": pathInfo, 65 "QUERY_STRING": req.URL.RawQuery, 66 "REDIRECT_STATUS": "200", // required if PHP was built with --enable-force-cgi-redirect 67 "REMOTE_ADDR": remoteAddr, 68 "REMOTE_PORT": remotePort, 69 "REQUEST_METHOD": req.Method, 70 "REQUEST_URI": req.RequestURI, 71 "SCRIPT_FILENAME": filepath.Join(p.documentRoot, scriptName), 72 "SCRIPT_NAME": scriptName, 73 } 74 75 if local, err := envs.NewLocal(p.projectDir, false); err == nil { 76 for k, v := range envs.AsMap(local) { 77 env[k] = v 78 } 79 } 80 81 // iterate over request headers and append them to the environment variables in the valid format 82 for k, v := range req.Header { 83 key := strings.Replace(strings.ToUpper(k), "-", "_", -1) 84 // ignore HTTP_HOST -- see https://httpoxy.org/ 85 if key == "HOST" { 86 continue 87 } 88 env["HTTP_"+key] = strings.Join(v, ";") 89 } 90 return env 91 }