Skip to content

Commit a19fcdb

Browse files
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.
1 parent a161af2 commit a19fcdb

4 files changed

Lines changed: 59 additions & 4 deletions

File tree

caddy/caddy_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,14 +1383,12 @@ func TestWorkerMatchDirectiveWithMultipleWorkers(t *testing.T) {
13831383
require.NoError(t, err, "static.txt file must be readable for this test")
13841384
tester.AssertGetResponse("http://localhost:"+testPort+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
13851385

1386-
// 404 if the request falls through
1387-
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusNotFound, "")
1388-
13891386
// serve php file directly as fallback
13901387
tester.AssertGetResponse("http://localhost:"+testPort+"/hello.php", http.StatusOK, "Hello from PHP")
13911388

1392-
// serve worker file directly as fallback
1389+
// serve index.php file directly as fallback
13931390
tester.AssertGetResponse("http://localhost:"+testPort+"/index.php", http.StatusOK, "I am by birth a Genevese (i not set)")
1391+
tester.AssertGetResponse("http://localhost:"+testPort+"/not-matched", http.StatusOK, "I am by birth a Genevese (i not set)")
13941392
}
13951393

13961394
func TestWorkerMatchDirectiveWithoutFileServer(t *testing.T) {

caddy/module.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
464464
redirMatcherSet := caddy.ModuleMap{
465465
"file": h.JSON(fileserver.MatchFile{
466466
TryFiles: []string{dirIndex},
467+
Root: phpsrv.Root,
467468
}),
468469
"not": h.JSON(caddyhttp.MatchNot{
469470
MatcherSetsRaw: []caddy.ModuleMap{
@@ -491,6 +492,7 @@ func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error)
491492
TryFiles: tryFiles,
492493
TryPolicy: tryPolicy,
493494
SplitPath: extensions,
495+
Root: phpsrv.Root,
494496
}),
495497
}
496498
rewriteHandler := rewrite.Rewrite{

caddy/module_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package caddy_test
2+
3+
import (
4+
"net/http"
5+
"os"
6+
"strconv"
7+
"testing"
8+
9+
"github.com/caddyserver/caddy/v2/caddytest"
10+
)
11+
12+
func TestRootBehavesTheSameOutsideAndInsidePhpServer(t *testing.T) {
13+
tester := caddytest.NewTester(t)
14+
testPortNum, _ := strconv.Atoi(testPort)
15+
testPortTwo := strconv.Itoa(testPortNum + 1)
16+
expectedFileResponse, _ := os.ReadFile("../testdata/files/static.txt")
17+
hostWithRootOutside := "http://localhost:" + testPort
18+
hostWithRootInside := "http://localhost:" + testPortTwo
19+
tester.InitServer(`
20+
{
21+
skip_install_trust
22+
admin localhost:2999
23+
}
24+
25+
`+hostWithRootOutside+` {
26+
root ../testdata
27+
php_server
28+
}
29+
30+
`+hostWithRootInside+` {
31+
php_server {
32+
root ../testdata
33+
}
34+
}
35+
`, "caddyfile")
36+
37+
// serve a static file
38+
tester.AssertGetResponse(hostWithRootOutside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
39+
tester.AssertGetResponse(hostWithRootInside+"/files/static.txt", http.StatusOK, string(expectedFileResponse))
40+
41+
// serve a php file
42+
tester.AssertGetResponse(hostWithRootOutside+"/hello.php", http.StatusOK, "Hello from PHP")
43+
tester.AssertGetResponse(hostWithRootInside+"/hello.php", http.StatusOK, "Hello from PHP")
44+
45+
// fallback to index.php
46+
tester.AssertGetResponse(hostWithRootOutside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")
47+
tester.AssertGetResponse(hostWithRootInside+"/some-path", http.StatusOK, "I am by birth a Genevese (i not set)")
48+
49+
// fallback to directory index ('dirIndex' in module.go)
50+
tester.AssertGetResponse(hostWithRootOutside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
51+
tester.AssertGetResponse(hostWithRootInside+"/dirindex/", http.StatusOK, "Hello from directory index.php")
52+
}

testdata/dirindex/index.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "Hello from directory index.php";

0 commit comments

Comments
 (0)