From a19fcdb38d673a3fcd57b410d50dace587e521c8 Mon Sep 17 00:00:00 2001 From: Alexander Stecher <45872305+AlliBalliBaba@users.noreply.github.com> Date: Wed, 16 Jul 2025 11:58:36 +0200 Subject: [PATCH] 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. --- caddy/caddy_test.go | 6 ++--- caddy/module.go | 2 ++ caddy/module_test.go | 52 +++++++++++++++++++++++++++++++++++++ testdata/dirindex/index.php | 3 +++ 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 caddy/module_test.go create mode 100644 testdata/dirindex/index.php diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index d02cd86c..47854c35 100644 --- a/caddy/caddy_test.go +++ b/caddy/caddy_test.go @@ -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) { diff --git a/caddy/module.go b/caddy/module.go index 1aae4088..50056ead 100644 --- a/caddy/module.go +++ b/caddy/module.go @@ -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{ diff --git a/caddy/module_test.go b/caddy/module_test.go new file mode 100644 index 00000000..2934cf2b --- /dev/null +++ b/caddy/module_test.go @@ -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") +} diff --git a/testdata/dirindex/index.php b/testdata/dirindex/index.php new file mode 100644 index 00000000..c530e809 --- /dev/null +++ b/testdata/dirindex/index.php @@ -0,0 +1,3 @@ +