Skip to content
Merged
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) {
}
})
}

24 changes: 19 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
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
}
}
```

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.
Expand Down
32 changes: 32 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading