Skip to content

Commit 93cb65a

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

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed

local/php/envs.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,37 @@ 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-
}
32+
func (p *Server) resolveIndexFile(pathInfo string) (string, string) {
3833

39-
pathInfo := req.URL.Path
4034
if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 {
4135
file := pathInfo[:pos+4]
4236
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
43-
scriptName = file
44-
pathInfo = pathInfo[pos+4:]
37+
return file, pathInfo[pos+4:]
4538
}
39+
40+
}
41+
42+
if len(pathInfo) > 1 {
43+
paths := strings.Split(strings.Trim(pathInfo, "/"), "/")
44+
for n := len(paths); n > 0; n-- {
45+
indexDir := filepath.Join(paths[:n]...)
46+
file := filepath.Join(indexDir, p.passthru)
47+
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
48+
return "/" + file, pathInfo[strings.Index(pathInfo, indexDir)+len(indexDir):]
49+
}
50+
}
51+
}
52+
53+
return p.passthru, pathInfo
54+
}
55+
56+
func (p *Server) generateEnv(req *http.Request) map[string]string {
57+
58+
scriptName, pathInfo := p.resolveIndexFile(req.URL.Path)
59+
60+
https := ""
61+
if req.TLS != nil {
62+
https = "On"
4663
}
4764

4865
remoteAddr := req.Header.Get("X-Client-IP")

local/php/envs_test.go

Lines changed: 79 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,83 @@ 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",
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+
},
185263
}
186264
for _, test := range tests {
187265
process := &Server{
@@ -197,7 +275,7 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
197275
for k, v := range test.expected {
198276
vv, ok := env[k]
199277
c.Assert(ok, Equals, true)
200-
c.Assert(vv, DeepEquals, v)
278+
c.Assert(vv, DeepEquals, v, Commentf("#test uri:\"%s\" varName:\"%s\"", test.uri, k))
201279
}
202280
}
203281
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy