Skip to content

Commit

Permalink
Added support for index.php in nested directories
Browse files Browse the repository at this point in the history
  • Loading branch information
maciekpaprocki committed Feb 14, 2023
1 parent 75f91e3 commit b86b610
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
36 changes: 26 additions & 10 deletions local/php/envs.go
Expand Up @@ -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 == "" {
Expand Down
91 changes: 90 additions & 1 deletion local/php/envs_test.go
Expand Up @@ -33,6 +33,7 @@ type PHPFPMSuite struct{}
var _ = Suite(&PHPFPMSuite{})

func (s *PHPFPMSuite) TestGenerateEnv(c *C) {

testdataDir := "testdata"
tests := []struct {
uri string
Expand Down Expand Up @@ -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{
Expand All @@ -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))
}
}
}
Empty file.
Empty file.

0 comments on commit b86b610

Please sign in to comment.