Skip to content

Commit c777a4c

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

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

local/php/envs.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,36 @@ import (
3131

3232
func (p *Server) generateEnv(req *http.Request) map[string]string {
3333
scriptName := p.passthru
34+
3435
https := ""
3536
if req.TLS != nil {
3637
https = "On"
3738
}
3839

3940
pathInfo := req.URL.Path
41+
filenameMatch := false
42+
4043
if pos := strings.Index(strings.ToLower(pathInfo), ".php"); pos != -1 {
4144
file := pathInfo[:pos+4]
4245
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
4346
scriptName = file
4447
pathInfo = pathInfo[pos+4:]
48+
filenameMatch = true
49+
}
50+
51+
}
52+
53+
if filenameMatch == false && pathInfo != "/" && len(pathInfo) > 0 {
54+
paths := strings.Split(strings.Trim(pathInfo, "/"), "/")
55+
for i, n := 0, len(paths); i < n; i++ {
56+
poppedPath := paths[:len(paths)-i]
57+
indexDir := filepath.Join(poppedPath...)
58+
file := filepath.Join(indexDir, p.passthru)
59+
if _, err := os.Stat(filepath.Join(p.documentRoot, file)); err == nil {
60+
scriptName = string(os.PathSeparator) + file
61+
pathInfo = pathInfo[strings.Index(pathInfo, indexDir)+len(indexDir):]
62+
break
63+
}
4564
}
4665
}
4766

local/php/envs_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package php
2121

2222
import (
23+
"fmt"
2324
"net/http"
2425
"testing"
2526

@@ -33,6 +34,7 @@ type PHPFPMSuite struct{}
3334
var _ = Suite(&PHPFPMSuite{})
3435

3536
func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
37+
3638
testdataDir := "testdata"
3739
tests := []struct {
3840
uri string
@@ -182,6 +184,83 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
182184
"SCRIPT_NAME": "/index.php",
183185
},
184186
},
187+
{
188+
passthru: "/index.php",
189+
uri: "/subdirectory",
190+
expected: map[string]string{
191+
"PATH_INFO": "",
192+
"REQUEST_URI": "/subdirectory",
193+
"QUERY_STRING": "",
194+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
195+
"SCRIPT_NAME": "/subdirectory/index.php",
196+
},
197+
},
198+
{
199+
passthru: "/index.php",
200+
uri: "/subdirectory/",
201+
expected: map[string]string{
202+
"PATH_INFO": "/",
203+
"REQUEST_URI": "/subdirectory/",
204+
"QUERY_STRING": "",
205+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
206+
"SCRIPT_NAME": "/subdirectory/index.php",
207+
},
208+
},
209+
{
210+
passthru: "/index.php",
211+
uri: "/subdirectory/unknown.php",
212+
expected: map[string]string{
213+
"PATH_INFO": "/unknown.php",
214+
"REQUEST_URI": "/subdirectory/unknown.php",
215+
"QUERY_STRING": "",
216+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
217+
"SCRIPT_NAME": "/subdirectory/index.php",
218+
},
219+
},
220+
{
221+
passthru: "/index.php",
222+
uri: "/subdirectory/unknown.php/",
223+
expected: map[string]string{
224+
"PATH_INFO": "/unknown.php/",
225+
"REQUEST_URI": "/subdirectory/unknown.php/",
226+
"QUERY_STRING": "",
227+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
228+
"SCRIPT_NAME": "/subdirectory/index.php",
229+
},
230+
},
231+
{
232+
passthru: "/index.php",
233+
uri: "/subdirectory/index.php/foo",
234+
expected: map[string]string{
235+
"PATH_INFO": "/foo",
236+
"REQUEST_URI": "/subdirectory/index.php/foo",
237+
"QUERY_STRING": "",
238+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
239+
"SCRIPT_NAME": "/subdirectory/index.php",
240+
},
241+
},
242+
{
243+
passthru: "/index.php",
244+
uri: "/subdirectory/subdirectory/",
245+
expected: map[string]string{
246+
"PATH_INFO": "/",
247+
"REQUEST_URI": "/subdirectory/subdirectory/",
248+
"QUERY_STRING": "",
249+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/subdirectory/index.php",
250+
"SCRIPT_NAME": "/subdirectory/subdirectory/index.php",
251+
},
252+
},
253+
{
254+
passthru: "/index.php",
255+
uri: "///subdirectory",
256+
expected: map[string]string{
257+
"PATH_INFO": "",
258+
"REQUEST_URI": "///subdirectory",
259+
"QUERY_STRING": "",
260+
"SCRIPT_FILENAME": testdataDir + "/public/subdirectory/index.php",
261+
"SCRIPT_NAME": "/subdirectory/index.php",
262+
},
263+
},
185264
}
186265
for _, test := range tests {
187266
process := &Server{
@@ -190,14 +269,15 @@ func (s *PHPFPMSuite) TestGenerateEnv(c *C) {
190269
passthru: test.passthru,
191270
}
192271
req, err := http.NewRequest("GET", test.uri, nil)
272+
fmt.Println("test", req.URL.Path, "test", test.uri)
193273
c.Assert(err, IsNil)
194274

195275
req.RequestURI = test.uri
196276
env := process.generateEnv(req)
197277
for k, v := range test.expected {
198278
vv, ok := env[k]
199279
c.Assert(ok, Equals, true)
200-
c.Assert(vv, DeepEquals, v)
280+
c.Assert(vv, DeepEquals, v, Commentf("#test uri:\"%s\" varName:\"%s\"", test.uri, k))
201281
}
202282
}
203283
}

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