From b86b6102a053c80fb8ed90139ef2e18cc2f4cfc6 Mon Sep 17 00:00:00 2001 From: maciekpaprocki Date: Wed, 1 Feb 2023 14:36:40 +0000 Subject: [PATCH] Added support for index.php in nested directories --- local/php/envs.go | 36 ++++++-- local/php/envs_test.go | 91 ++++++++++++++++++- .../testdata/public/subdirectory/index.php | 0 .../subdirectory/subdirectory/index.php | 0 4 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 local/php/testdata/public/subdirectory/index.php create mode 100644 local/php/testdata/public/subdirectory/subdirectory/index.php diff --git a/local/php/envs.go b/local/php/envs.go index a16b4e76..d0bf19b5 100644 --- a/local/php/envs.go +++ b/local/php/envs.go @@ -29,22 +29,38 @@ import ( "github.com/symfony-cli/symfony-cli/envs" ) -func (p *Server) generateEnv(req *http.Request) map[string]string { - scriptName := p.passthru - https := "" - if req.TLS != nil { - https = "On" - } - - pathInfo := req.URL.Path +func (p *Server) resolveScriptName(pathInfo string) (string, string) { if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 { file := pathInfo[:pos+4] if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil { - scriptName = file - pathInfo = pathInfo[pos+4:] + return file, pathInfo[pos+4:] + } + } + + paths := strings.Split(strings.Trim(pathInfo, "/"), "/") + for n := len(paths); n > 0; n-- { + pathPart := paths[n-1] + if pathPart == "" { + continue + } + + file := filepath.Join(append(paths[:n], p.passthru)...) + if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil { + return "/" + file, pathInfo[strings.LastIndex(pathInfo, pathPart)+len(pathPart):] } } + 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 == "" { diff --git a/local/php/envs_test.go b/local/php/envs_test.go index ac9ad28f..add48f2f 100644 --- a/local/php/envs_test.go +++ b/local/php/envs_test.go @@ -33,6 +33,7 @@ type PHPFPMSuite struct{} var _ = Suite(&PHPFPMSuite{}) func (s *PHPFPMSuite) TestGenerateEnv(c *C) { + testdataDir := "testdata" tests := []struct { uri string @@ -182,6 +183,94 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) { "SCRIPT_NAME": "/index.php", }, }, + { + passthru: "/index.php", + uri: "/subdirectory", + expected: map[string]string{ + "PATH_INFO": "", + "REQUEST_URI": "/subdirectory", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory/", + expected: map[string]string{ + "PATH_INFO": "/", + "REQUEST_URI": "/subdirectory/", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory/unknown.php", + expected: map[string]string{ + "PATH_INFO": "/unknown.php", + "REQUEST_URI": "/subdirectory/unknown.php", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory/unknown.php/", + expected: map[string]string{ + "PATH_INFO": "/unknown.php/", + "REQUEST_URI": "/subdirectory/unknown.php/", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory/index.php/foo", + expected: map[string]string{ + "PATH_INFO": "/foo", + "REQUEST_URI": "/subdirectory/index.php/foo", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory/subdirectory/", + expected: map[string]string{ + "PATH_INFO": "/", + "REQUEST_URI": "/subdirectory/subdirectory/", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "///subdirectory", + expected: map[string]string{ + "PATH_INFO": "", + "REQUEST_URI": "///subdirectory", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/index.php", + }, + }, + { + passthru: "/index.php", + uri: "/subdirectory///subdirectory//foo/", + expected: map[string]string{ + "PATH_INFO": "//foo/", + "REQUEST_URI": "/subdirectory///subdirectory//foo/", + "QUERY_STRING": "", + "SCRIPT_FILENAME": testdataDir + "/public/subdirectory/subdirectory/index.php", + "SCRIPT_NAME": "/subdirectory/subdirectory/index.php", + }, + }, } for _, test := range tests { process := &Server{ @@ -197,7 +286,7 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) { for k, v := range test.expected { vv, ok := env[k] c.Assert(ok, Equals, true) - c.Assert(vv, DeepEquals, v) + c.Assert(vv, DeepEquals, v, Commentf("#test uri:\"%s\" varName:\"%s\"", test.uri, k)) } } } diff --git a/local/php/testdata/public/subdirectory/index.php b/local/php/testdata/public/subdirectory/index.php new file mode 100644 index 00000000..e69de29b diff --git a/local/php/testdata/public/subdirectory/subdirectory/index.php b/local/php/testdata/public/subdirectory/subdirectory/index.php new file mode 100644 index 00000000..e69de29b