diff --git a/cgi_test.go b/cgi_test.go index 03b3c1020f..d0dff8aef4 100644 --- a/cgi_test.go +++ b/cgi_test.go @@ -760,4 +760,3 @@ func FuzzSplitRawHeader(f *testing.F) { } }) } - diff --git a/context.go b/context.go index 07f58ef599..2ea2c3f216 100644 --- a/context.go +++ b/context.go @@ -96,15 +96,25 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } // assign a worker directly if it has a request matcher + matched := false if fc.worker == nil { for _, w := range s.workersWithRequestMatcher { if w.matchRequest(request) { fc.worker = w + matched = true + break } } } + // if no originalRequest was passed, use the URI from the actual request + // when using Caddy's http module, the original unchanged uri will be used here + // request.URL is often already rewritten to match a PHP script path + if fc.requestURI == "" { + fc.requestURI = fc.request.URL.RequestURI() + } + if fc.documentRoot == "" { if EmbeddedAppPath != "" { fc.documentRoot = EmbeddedAppPath @@ -116,11 +126,15 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } - // if no originalRequest was passed, use the URI from the actual request - // when using Caddy's http module, the original unchanged uri will be used here - // request.URL is often already rewritten to match a PHP script path - if fc.requestURI == "" { - fc.requestURI = fc.request.URL.RequestURI() + if matched { + fc.scriptFilename = fc.worker.fileName + + // frameworks derive their base URL from SCRIPT_NAME, so keep it for a worker inside the document root + if rest, ok := strings.CutPrefix(fc.worker.fileName, fc.documentRoot); ok && rest != "" && rest[0] == filepath.Separator { + fc.scriptName = filepath.ToSlash(rest) + } + + return fc, nil } if err := splitCgiPath(fc); err != nil { diff --git a/docs/config.md b/docs/config.md index 49b2683212..2190e90812 100644 --- a/docs/config.md +++ b/docs/config.md @@ -265,18 +265,16 @@ The following example will always serve a file in the public directory if presen and otherwise forward the request to the worker matching the path pattern. ```caddyfile -{ - frankenphp { - php_server { - worker { - file /path/to/worker.php # file can be outside of public path - match /api/* # all requests starting with /api/ will be handled by this worker - } - } +php_server { + worker { + file /path/to/worker.php # file can be outside of public path + match /api/* # all requests starting with /api/ will be handled by this worker } } ``` +When `match` routes a request to a worker, `PATH_INFO` and `DOCUMENT_URI` are always empty. `SCRIPT_NAME` and `PHP_SELF` name the worker script when it is inside the document root, and are empty otherwise. + ## Restarting threads after a number of requests (experimental) FrankenPHP can automatically restart PHP threads after they have handled a given number of requests. diff --git a/server_test.go b/server_test.go index 89fcfbd575..88e7c00979 100644 --- a/server_test.go +++ b/server_test.go @@ -144,6 +144,38 @@ func TestServer(t *testing.T) { assert.ErrorIs(t, err6, frankenphp.ErrInvalidScriptPath, "the matcher worker is scoped to server 2, so server 1 has no script for this path") }) + t.Run("request_matcher_clears_script_vars", func(t *testing.T) { + // worker filenames are resolved, so a symlinked checkout path would leave SCRIPT_NAME empty + root, err := filepath.EvalSymlinks(testDataDir) + require.NoError(t, err) + + server, _ := frankenphp.NewServer(root) + initServers( + t, + frankenphp.WithServer(server), + frankenphp.WithWorkers("match", filepath.Join(root, "server-globals.php"), 1, + frankenphp.WithWorkerServerScope(server), + frankenphp.WithWorkerMatcher(func(r *http.Request) bool { + return strings.HasPrefix(r.URL.Path, "/match/") + }), + ), + ) + + // without the matcher, "/server-globals.php/extra" is split into SCRIPT_NAME and PATH_INFO (control) + unmatched := serverGet(t, server, "http://example.com/server-globals.php/extra") + assert.Contains(t, unmatched, "SCRIPT_NAME: /server-globals.php\n") + assert.Contains(t, unmatched, "PATH_INFO: /extra\n") + + // the matched worker bypasses path splitting: SCRIPT_NAME names the worker, PATH_INFO stays empty + matched := serverGet(t, server, "http://example.com/match/server-globals.php/extra") + assert.Contains(t, matched, "SCRIPT_NAME: /server-globals.php\n") + assert.Contains(t, matched, "PHP_SELF: /server-globals.php\n") + assert.Contains(t, matched, "PATH_INFO:\n") + assert.Contains(t, matched, "DOCUMENT_URI:\n") + assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(root, "server-globals.php")+"\n") + assert.Contains(t, matched, "REQUEST_URI: /match/server-globals.php/extra\n") + }) + t.Run("worker_env_inheritance", func(t *testing.T) { server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{ "FROM_SERVER_ENV": "original",