fix: forwards php_server root to try_files (#1729)

* Adds 'root' to try_files.

* Formatting.

* Fixes test with wrong assumption.

* Adds more test cases.

* Prevents conflicts with other tests.
This commit is contained in:
Alexander Stecher
2025-07-16 11:58:36 +02:00
committed by GitHub
parent a161af26ae
commit a19fcdb38d
4 changed files with 59 additions and 4 deletions

View File

@@ -1383,14 +1383,12 @@ func TestWorkerMatchDirectiveWithMultipleWorkers(t *testing.T) {
require.NoError(t, err, "static.txt file must be readable for this test")
tester.AssertGetResponse("http://localhost:"+testPort+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
// 404 if the request falls through
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusNotFound, "")
// serve php file directly as fallback
tester.AssertGetResponse("http://localhost:"+testPort+"/hello.php", http.StatusOK, "Hello from PHP")
// serve worker file directly as fallback
// serve index.php file directly as fallback
tester.AssertGetResponse("http://localhost:"+testPort+"/index.php", http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusOK, "I am by birth a Genevese (i not set)")
}
func TestWorkerMatchDirectiveWithoutFileServer(t *testing.T) {

View File

@@ -464,6 +464,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
redirMatcherSet := caddy.ModuleMap{
"file": h.JSON(fileserver.MatchFile{
TryFiles: []string{dirIndex},
Root: phpsrv.Root,
}),
"not": h.JSON(caddyhttp.MatchNot{
MatcherSetsRaw: []caddy.ModuleMap{
@@ -491,6 +492,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
TryFiles: tryFiles,
TryPolicy: tryPolicy,
SplitPath: extensions,
Root: phpsrv.Root,
}),
}
rewriteHandler := rewrite.Rewrite{

52
caddy/module_test.go Normal file
View File

@@ -0,0 +1,52 @@
package caddy_test
import (
"net/http"
"os"
"strconv"
"testing"
"github.com/caddyserver/caddy/v2/caddytest"
)
func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) {
tester := caddytest.NewTester(t)
testPortNum, _ := strconv.Atoi(testPort)
testPortTwo := strconv.Itoa(testPortNum + 1)
expectedFileResponse, _ := os.ReadFile("../testdata/files/static.txt")
hostWithRootOutside := "http://localhost:" + testPort
hostWithRootInside := "http://localhost:" + testPortTwo
tester.InitServer(`
{
skip_install_trust
admin localhost:2999
}
`+hostWithRootOutside+` {
root ../testdata
php_server
}
`+hostWithRootInside+` {
php_server {
root ../testdata
}
}
`, "caddyfile")
// serve a static file
tester.AssertGetResponse(hostWithRootOutside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
tester.AssertGetResponse(hostWithRootInside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
// serve a php file
tester.AssertGetResponse(hostWithRootOutside+"/hello.php", http.StatusOK, "Hello from PHP")
tester.AssertGetResponse(hostWithRootInside+"/hello.php", http.StatusOK, "Hello from PHP")
// fallback to index.php
tester.AssertGetResponse(hostWithRootOutside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")
tester.AssertGetResponse(hostWithRootInside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")
// fallback to directory index ('dirIndex' in module.go)
tester.AssertGetResponse(hostWithRootOutside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
tester.AssertGetResponse(hostWithRootInside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
}

3
testdata/dirindex/index.php vendored Normal file
View File

@@ -0,0 +1,3 @@
<?php
echo "Hello from directory index.php";