8000 Added support for index.php in nested directories · symfony-cli/symfony-cli@2a2401d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a2401d

Browse files
Added support for index.php in nested directories
1 parent 75f91e3 commit 2a2401d

File tree

4 files changed

+117
-11
lines changed

4 files changed

+117
-11
lines changed

local/php/envs.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,39 @@ import (
2929
"github.com/symfony-cli/symfony-cli/envs"
3030
)
3131

32-
func (p *Server) generateEnv(req *http.Request) map[string]string {
33-
scriptName := p.passthru
34-
https := ""
35-
if req.TLS != nil {
36-
https = "On"
37-
}
38-
39-
pathInfo := req.URL.Path
32+
func (p *Server) resolveScriptName(pathInfo string) (string, string) {
4033
if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 {
4134
file := pathInfo[:pos+4]
4235
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
43-
scriptName = file
44-
pathInfo = pathInfo[pos+4:]
36+
return file, pathInfo[pos+4:]
37+
}
38+
}
39+
40+
if len(pathInfo) > 1 {
41+
paths := strings.Split(strings.Trim(pathInfo, "/"), "/")
42+
for n := len(paths); n > 0; n-- {
43+
if paths[n-1] == "" {
44+
continue
45+
}
46+
47+
file := filepath.Join(append(paths[:n], p.passthru)...)
48+
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
49+
return "/" + file, pathInfo[strings.LastIndex(pathInfo, paths[n-1])+len(paths[n-1]):]
50+
}
4551
}
4652
}
4753

54+
return p.passthru, pathInfo
55+
}
56+
57+
func (p *Server) generateEnv(req *http.Request) map[string]string {
58+
scriptName, pathInfo := p.resolveScriptName(req.URL.Path)
59+
60+
https := ""
61+
if req.TLS != nil {
62+
https = "On"
63+
}
64+
4865
remoteAddr := req.Header.Get("X-Client-IP")
4966
remotePort := ""
5067
if remoteAddr == "" {

local/php/envs_test.go

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type PHPFPMSuite struct{}
3333
var _ = Suite(&PHPFPMSuite{})
3434

3535
func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
36+
3637
testdataDir := "testdata"
3738
tests := []struct {
3839
uri string
@@ -182,6 +183,94 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
182183
"SCRIPT_NAME": "/index.php",
183184
},
184185
},
186+
{
187+
passthru: "/index.php",
188+
uri: "/subdirectory",
189+
expected: map[string]string{
190+
"PATH_INFO": "",
191+
"REQUEST_URI": "/subdirectory",
192+
"QUERY_STRING": "",
193+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
194+
"SCRIPT_NAME": "/subdirectory/index.php",
195+
},
196+
},
197+
{
198+
passthru: "/index.php",
199+
uri: "/subdirectory/",
200+
expected: map[string]string{
201+
"PATH_INFO": "/",
202+
"REQUEST_URI": "/subdirectory/",
203+
"QUERY_STRING": "",
204+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
205+
"SCRIPT_NAME": "/subdirectory/index.php",
206+
},
207+
},
208+
{
209+
passthru: "/index.php",
210+
uri: "/subdirectory/unknown.php",
211+
expected: map[string]string{
212+
"PATH_INFO": "/unknown.php",
213+
"REQUEST_URI": "/subdirectory/unknown.php",
214+
"QUERY_STRING": "",
215+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
216+
"SCRIPT_NAME": "/subdirectory/index.php",
217+
},
218+
},
219+
{
220+
passthru: "/index.php",
221+
uri: "/subdirectory/unknown.php/",
222+
expected: map[string]string{
223+
"PATH_INFO": "/unknown.php/",
224+
"REQUEST_URI": "/subdirectory/unknown.php/",
225+
"QUERY_STRING": "",
226+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
227+
"SCRIPT_NAME": "/subdirectory/index.php",
228+
},
229+
},
230+
{
231+
passthru: "/index.php",
< 9E81 /code>232+
uri: "/subdirectory/index.php/foo",
233+
expected: map[string]string{
234+
"PATH_INFO": "/foo",
235+
"REQUEST_URI": "/subdirectory/index.php/foo",
236+
"QUERY_STRING": "",
237+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
238+
"SCRIPT_NAME": "/subdirectory/index.php",
239+
},
240+
},
241+
{
242+
passthru: "/index.php",
243+
uri: "/subdirectory/subdirectory/",
244+
expected: map[string]string{
245+
"PATH_INFO": "/",
246+
"REQUEST_URI": "/subdirectory/subdirectory/",
247+
"QUERY_STRING": "",
248+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/subdirectory/index.php",
249+
"SCRIPT_NAME": "/subdirectory/subdirectory/index.php",
250+
},
251+
},
252+
{
253+
passthru: "/index.php",
254+
uri: "///subdirectory",
255+
expected: map[string]string{
256+
"PATH_INFO": "",
257+
"REQUEST_URI": "///subdirectory",
258+
"QUERY_STRING": "",
259+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
260+
"SCRIPT_NAME": "/subdirectory/index.php",
261+
},
262+
},
263+
{
264+
passthru: "/index.php",
265+
uri: "/subdirectory///subdirectory//foo/",
266+
expected: map[string]string{
267+
"PATH_INFO": "//foo/",
268+
"REQUEST_URI": "/subdirectory///subdirectory//foo/",
269+
"QUERY_STRING": "",
270+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/subdirectory/index.php",
271+
"SCRIPT_NAME": "/subdirectory/subdirectory/index.php",
272+
},
273+
},
185274
}
186275
for _, test := range tests {
187276
process := &Server{
@@ -197,7 +286,7 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
197286
for k, v := range test.expected {
198287
vv, ok := env[k]
199288
c.Assert(ok, Equals, true)
200-
c.Assert(vv, DeepEquals, v)
289+
c.Assert(vv, DeepEquals, v, Commentf("#test uri:\"%s\" varName:\"%s\"", test.uri, k))
201290
}
202291
}
203292
}

local/php/testdata/public/subdirectory/index.php

Whitespace-only changes.

local/php/testdata/public/subdirectory/subdirectory/index.php

Whitespace-only changes.

0 commit comments

Comments
 (0)
0