From ee73939f120ec05b6b70a55e23503a50fbc0d8a8 Mon Sep 17 00:00:00 2001 From: Alliballibaba Date: Sun, 6 Jul 2025 19:34:56 +0200 Subject: [PATCH 1/5] Adds 'root' to try_files. --- caddy/module.go | 2 ++ caddy/module_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 caddy/module_test.go diff --git a/caddy/module.go b/caddy/module.go index 1aae408859..50056ead4d 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 0000000000..1e749299b4 --- /dev/null +++ b/caddy/module_test.go @@ -0,0 +1,40 @@ +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") + tester.InitServer(` + { + skip_install_trust + admin localhost:2999 + } + + http://localhost:`+testPort+` { + root ../testdata/files + php_server + } + + http://localhost:`+testPortTwo+` { + php_server { + root ../testdata/files + } + } + `, "caddyfile") + + // serve the file with root outside of php_server + tester.AssertGetResponse("http://localhost:"+testPort+"/static.txt", http.StatusOK, string(expectedFileResponse)) + + // serve the file with root within php_server + tester.AssertGetResponse("http://localhost:"+testPortTwo+"/static.txt", http.StatusOK, string(expectedFileResponse)) +} From 11d06c3977dfdbd9c97fcad70ff815535a1431bd Mon Sep 17 00:00:00 2001 From: Alliballibaba Date: Sun, 6 Jul 2025 19:41:32 +0200 Subject: [PATCH 2/5] Formatting. --- caddy/module_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/caddy/module_test.go b/caddy/module_test.go index 1e749299b4..dc8231f36f 100644 --- a/caddy/module_test.go +++ b/caddy/module_test.go @@ -26,10 +26,10 @@ func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) { } http://localhost:`+testPortTwo+` { - php_server { - root ../testdata/files - } - } + php_server { + root ../testdata/files + } + } `, "caddyfile") // serve the file with root outside of php_server From 5b111ef6a0e3f7fd958e9ada5cbd8fd93ca8f2c4 Mon Sep 17 00:00:00 2001 From: Alliballibaba Date: Sun, 6 Jul 2025 19:54:50 +0200 Subject: [PATCH 3/5] Fixes test with wrong assumption. --- caddy/caddy_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/caddy/caddy_test.go b/caddy/caddy_test.go index d02cd86c57..47854c3557 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) { From 0751ccfa0f2d572bba8e2990bd9a62652835f43d Mon Sep 17 00:00:00 2001 From: Alliballibaba Date: Mon, 7 Jul 2025 21:22:40 +0200 Subject: [PATCH 4/5] Adds more test cases. --- caddy/module_test.go | 29 +++++++++++++++++++++-------- testdata/files/index.php | 3 +++ 2 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 testdata/files/index.php diff --git a/caddy/module_test.go b/caddy/module_test.go index dc8231f36f..5b5713d5f4 100644 --- a/caddy/module_test.go +++ b/caddy/module_test.go @@ -14,27 +14,40 @@ func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.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 + debug } - http://localhost:`+testPort+` { - root ../testdata/files + `+hostWithRootOutside+` { + root ../testdata php_server } - http://localhost:`+testPortTwo+` { + `+hostWithRootInside+` { php_server { - root ../testdata/files + root ../testdata } } `, "caddyfile") - // serve the file with root outside of php_server - tester.AssertGetResponse("http://localhost:"+testPort+"/static.txt", http.StatusOK, string(expectedFileResponse)) + // 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 the file with root within php_server - tester.AssertGetResponse("http://localhost:"+testPortTwo+"/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+"/files/", http.StatusOK, "Hello from directory index.php") + tester.AssertGetResponse(hostWithRootInside+"/files/", http.StatusOK, "Hello from directory index.php") } diff --git a/testdata/files/index.php b/testdata/files/index.php new file mode 100644 index 0000000000..c530e8094a --- /dev/null +++ b/testdata/files/index.php @@ -0,0 +1,3 @@ + Date: Mon, 7 Jul 2025 22:16:02 +0200 Subject: [PATCH 5/5] Prevents conflicts with other tests. --- caddy/module_test.go | 5 ++--- testdata/{files => dirindex}/index.php | 0 2 files changed, 2 insertions(+), 3 deletions(-) rename testdata/{files => dirindex}/index.php (100%) diff --git a/caddy/module_test.go b/caddy/module_test.go index 5b5713d5f4..2934cf2b9a 100644 --- a/caddy/module_test.go +++ b/caddy/module_test.go @@ -20,7 +20,6 @@ func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) { { skip_install_trust admin localhost:2999 - debug } `+hostWithRootOutside+` { @@ -48,6 +47,6 @@ func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) { 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+"/files/", http.StatusOK, "Hello from directory index.php") - tester.AssertGetResponse(hostWithRootInside+"/files/", http.StatusOK, "Hello from directory index.php") + 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/files/index.php b/testdata/dirindex/index.php similarity index 100% rename from testdata/files/index.php rename to testdata/dirindex/index.php