Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cgi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,4 +760,3 @@ func FuzzSplitRawHeader(f *testing.F) {
}
})
}

28 changes: 15 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading