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..21c87a46d7 100644 --- a/context.go +++ b/context.go @@ -95,14 +95,11 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } - // assign a worker directly if it has a request matcher - if fc.worker == nil { - for _, w := range s.workersWithRequestMatcher { - if w.matchRequest(request) { - fc.worker = w - 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 == "" { @@ -116,11 +113,16 @@ 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() + // assign a worker directly if it has a request matcher + if fc.worker == nil { + for _, w := range s.workersWithRequestMatcher { + if w.matchRequest(request) { + fc.worker = w + fc.scriptFilename = w.fileName + + return fc, nil + } + } } if err := splitCgiPath(fc); err != nil { diff --git a/docs/config.md b/docs/config.md index 49b2683212..171bd14ae9 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 } } ``` +The following CGI superglobals will always be empty if a request is routed to a woker through `match`: `SCRIPT_NAME`, `PATH_INFO`, `PHP_SELF`, `DOCUMENT_URI`. + ## 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..f4469165ed 100644 --- a/server_test.go +++ b/server_test.go @@ -144,6 +144,34 @@ 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) { + server, _ := frankenphp.NewServer(testDataDir) + initServers( + t, + frankenphp.WithServer(server), + frankenphp.WithWorkers("match", testDataDir+"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, so the script name variables must be empty + matched := serverGet(t, server, "http://example.com/match/server-globals.php/extra") + assert.Contains(t, matched, "SCRIPT_NAME:\n") + assert.Contains(t, matched, "PHP_SELF:\n") + assert.Contains(t, matched, "PATH_INFO:\n") + assert.Contains(t, matched, "DOCUMENT_URI:\n") + assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(testDataDir, "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",