69 Commits

Author SHA1 Message Date
Kévin Dunglas
03c26eca02 refactor: hoist LoaderFunc to package-level var in phpheaders (#2053)
## Summary

Hoists the `otter.LoaderFunc` closure in `GetUnCommonHeader` to a
package-level `loader` var, so it is allocated once at init time instead
of being re-created on every call.

This is a minor cleanup — the previous code created a new `LoaderFunc`
closure each time `GetUnCommonHeader` was called. While otter's
cache-hit path is fast enough that this doesn't show a measurable
difference in end-to-end benchmarks, avoiding the repeated allocation is
strictly better.

## What changed

**Before** (closure created per call):
```go
func GetUnCommonHeader(ctx context.Context, key string) string {
    phpHeaderKey, err := headerKeyCache.Get(
        ctx,
        key,
        otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) {
            return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil
        }),
    )
    ...
}
```

**After** (closure allocated once):
```go
var loader = otter.LoaderFunc[string, string](func(_ context.Context, key string) (string, error) {
    return "HTTP_" + headerNameReplacer.Replace(strings.ToUpper(key)) + "\x00", nil
})

func GetUnCommonHeader(ctx context.Context, key string) string {
    phpHeaderKey, err := headerKeyCache.Get(ctx, key, loader)
    ...
}
```

## Benchmarks

Apple M1 Pro, 8 runs, `benchstat` comparison — no regressions, no extra
allocations:

| Benchmark | main | PR | vs base |
|---|---|---|---|
| HelloWorld | 41.81µ ± 2% | 42.75µ ± 5% | ~ (p=0.065) |
| ServerSuperGlobal | 73.36µ ± 2% | 74.20µ ± 3% | ~ (p=0.105) |
| UncommonHeaders | 69.03µ ± 3% | 68.71µ ± 1% | ~ (p=0.382) |

All results within noise. Zero change in allocations.
2026-03-10 19:07:32 +01:00
Kévin Dunglas
25ed020036 feat: Windows support (#2119)
Closes #83 #880 #1286.

Working patch for Windows support.

Supports linking to the [official PHP release (TS
version)](https://www.php.net/downloads.php).
Includes some work from #1286 (thanks @TenHian!!)

This patch allows using Visual Studio to compile the cgo code. To do so,
it must be compiled with Go 1.26 (RC) with the following setup:

```powershell
winget install -e --id Microsoft.VisualStudio.2022.Community --override "--passive --wait --add Microsoft.VisualStudio.Workload.NativeDesktop --add Microsoft.VisualStudio.Component.VC.Llvm.Clang --includeRecommended"
winget install -e --id GoLang.Go

$env:PATH += ';C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\Llvm\bin'

cd c:\
gh repo clone microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat
.\vcpkg\vcpkg install pthreads brotli

# build watcher
Invoke-WebRequest -Uri "https://github.com/e-dant/watcher/releases/download/0.14.3/x86_64-pc-windows-msvc.tar" -OutFile "$env:TEMP\watcher.tar"
tar -xf "$env:TEMP\watcher.tar" -C C:\
Rename-Item -Path "C:\x86_64-pc-windows-msvc" -NewName "watcher-x86_64-pc-windows-msvc"
Remove-Item "$env:TEMP\watcher.tar"

# download php
Invoke-WebRequest -Uri "https://downloads.php.net/~windows/releases/archives/php-8.5.1-Win32-vs17-x64.zip" -OutFile "$env:TEMP\php.zip"
Expand-Archive -Path "$env:TEMP\php.zip" -DestinationPath "C:\"
Remove-Item "$env:TEMP\php.zip"

# download php development package
Invoke-WebRequest -Uri "https://downloads.php.net/~windows/releases/archives/php-devel-pack-8.5.1-Win32-vs17-x64.zip" -OutFile "$env:TEMP\php-devel.zip"
Expand-Archive -Path "$env:TEMP\php-devel.zip" -DestinationPath "C:\"
Remove-Item "$env:TEMP\php-devel.zip"

$env:GOTOOLCHAIN = 'go1.26rc1'
$env:CC = 'clang'
$env:CXX = 'clang++'
$env:CGO_CFLAGS = "-I$env:C:\vcpkg\installed\x64-windows\include -IC:\watcher-x86_64-pc-windows-msvc -IC:\php-8.5.1-devel-vs17-x64\include -IC:\php-8.5.1-devel-vs17-x64\include\main -IC:\php-8.5.1-devel-vs17-x64\include\TSRM -IC:\php-8.5.1-devel-vs17-x64\include\Zend -IC:\php-8.5.1-devel-vs17-x64\include\ext"
$env:CGO_LDFLAGS = '-LC:\vcpkg\installed\x64-windows\lib -lbrotlienc -LC:\watcher-x86_64-pc-windows-msvc -llibwatcher-c -LC:\php-8.5.1-Win32-vs17-x64 -LC:\php-8.5.1-devel-vs17-x64\lib -lphp8ts -lphp8embed'

# clone frankenphp and build
git clone -b windows https://github.com/php/frankenphp.git
cd frankenphp\caddy\frankenphp
go build -ldflags '-extldflags="-fuse-ld=lld"' -tags nowatcher,nobadger,nomysql,nopgx

# Tests

$env:PATH += ";$env:VCPKG_ROOT\installed\x64-windows\bin;C:\watcher-x86_64-pc-windows-msvc";C:\php-8.5.1-Win32-vs17-x64"
"opcache.enable=0`r`nopcache.enable_cli=0" | Out-File -Encoding ascii php.ini
$env:PHPRC = Get-Location
go test -ldflags '-extldflags="-fuse-ld=lld"' -tags nowatcher,nobadger,nomysql,nopgx .
```

TODO:

- [x] Fix remaining skipped tests (scaling and watcher)
- [x] Test if the watcher mode works as expected
- [x] Automate the build with GitHub Actions

---------

Signed-off-by: Marc <m@pyc.ac>
Co-authored-by: Kévin Dunglas <kevin@dunglas.dev>
Co-authored-by: DubbleClick <m@pyc.ac>
2026-02-26 12:38:14 +01:00
Rob Landers
6d86ea84bc chore: run go fmt (#2211)
A few files were not formatted correctly.

Signed-off-by: Robert Landers <landers.robert@gmail.com>
Co-authored-by: Marc <m@pyc.ac>
2026-02-21 17:38:51 +01:00
Kévin Dunglas
abba64b0ff chore: fix golangci-lint 2026-02-18 14:27:45 +01:00
Kévin Dunglas
e2062af083 chore: cs improvements 2026-02-11 15:49:39 +01:00
Kévin Dunglas
040ce55e17 perf: various optimizations (#2175) 2026-02-11 15:21:55 +01:00
Alexandre Daubois
d704e60bb0 chore: bump to Go 1.26 (#2178)
Bump and free perf boost with Green tea GC 🍵
2026-02-11 14:55:57 +01:00
Alexandre Daubois
fba79a6ac8 feat(extgen): make the generator idempotent and avoid touching the original source (#2011) 2026-02-05 12:50:28 +01:00
Kévin Dunglas
8e1641b81c ci: disable codespell linter (#2153) 2026-01-29 17:23:50 +01:00
Alexandre Daubois
1bfa3db096 fix(extgen): use fast ZPP (#2088)
Fast ZPP is being more and more widely used in php-src with PR such as
https://github.com/php/php-src/pull/20441 or
https://github.com/php/php-src/pull/20644, let's use it here as well.
2026-01-14 15:20:54 +01:00
Alexandre Daubois
c6b2b02277 fix(extgen): correctly handle const blocks to declare iota constants (#2086)
While continuing the work on #2011, I realized that constant
declarations have a problem when using `iota`. I mean, it technically
works, but const *blocks* we not supported which means that setting all
constants to `iota` as shown in the documentation was non-sensical, as
`iota` resets every time outside of const blocks.

So, this is between the bug fix and the feature. To me, it's a bug fix
as the behavior wasn't the one intended when creating extgen.
2026-01-12 15:44:46 +01:00
Rob Landers
e0f01d12d6 Handle symlinking edge cases (#1660)
This one is interesting — though I’m not sure the best way to provide a
test. I will have to look into maybe an integration test because it is a
careful dance between how we resolve paths in the Caddy module vs.
workers. I looked into making a proper change (literally using the same
logic everywhere), but I think it is best to wait until #1646 is merged.

But anyway, this change deals with some interesting edge cases. I will
use gherkin to describe them:

```gherkin
Feature: FrankenPHP symlinked edge cases
  Background: 
    Given a `test` folder
    And a `public` folder linked to `test`
    And a worker script located at `test/index.php`
    And a `test/nested` folder
    And a worker script located at `test/nested/index.php`
  Scenario: neighboring worker script
    Given frankenphp located in the test folder
    When I execute `frankenphp php-server --listen localhost:8080 -w index.php` from `public`
    Then I expect to see the worker script executed successfully
  Scenario: nested worker script
    Given frankenphp located in the test folder
    When I execute `frankenphp --listen localhost:8080 -w nested/index.php` from `public`
    Then I expect to see the worker script executed successfully
  Scenario: outside the symlinked folder
    Given frankenphp located in the root folder
    When I execute `frankenphp --listen localhost:8080 -w public/index.php` from the root folder
    Then I expect to see the worker script executed successfully
  Scenario: specified root directory
    Given frankenphp located in the root folder
    When I execute `frankenphp --listen localhost:8080 -w public/index.php -r public` from the root folder
    Then I expect to see the worker script executed successfully    
```

Trying to write that out in regular English would be more complex IMHO.

These scenarios should all pass now with this PR.

---------

Signed-off-by: Marc <m@pyc.ac>
Co-authored-by: henderkes <m@pyc.ac>
Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
2026-01-02 10:23:16 +01:00
Alexander Stecher
175e644d10 feat: multiple curly braces for watcher (#2068)
Allows doing something like this:

```caddyfile
watch "/app/{config,src}/*.{php,js}"
```

In the long term it would be nice to have pattern matching in the
watcher repo itself
2025-12-17 00:22:28 +01:00
Alexandre Daubois
bb1c3678dc feat(extgen): add support for callable in parameters (#1731) 2025-12-15 12:50:50 +01:00
Alexander Stecher
11213fd1de fix: returns a zend_array directly in types.go (#1894) 2025-12-12 22:55:58 +01:00
Alexandre Daubois
599c92b15d tests(extgen): add integration tests (#1984)
Fix #1975
2025-12-12 14:32:00 +01:00
Kévin Dunglas
225ca409d3 feat: hot reload (#2031)
This patch brings hot reloading capabilities to PHP apps: in
development, the browser will automatically refresh the page when any
source file changes!
It's similar to HMR in JavaScript.

It is built on top of [the watcher
mechanism](https://frankenphp.dev/docs/config/#watching-for-file-changes)
and of the [Mercure](https://frankenphp.dev/docs/mercure/) integration.

Each time a watched file is modified, a Mercure update is sent, giving
the ability to the client to reload the page, or part of the page
(assets, images...).

Here is an example implementation:

```caddyfile
root ./public


mercure {
      subscriber_jwt {env.MERCURE_SUBSCRIBER_JWT_KEY}
      anonymous
}

php_server {
      hot_reload
}
```

```php
<?php
header('Content-Type: text/html');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script>
    const es = new EventSource('<?=$_SERVER['FRANKENPHP_HOT_RELOAD']?>');
    es.onmessage = () => location.reload();
</script>
</head>
<body>
Hello
```

I plan to create a helper JS library to handle more advanced cases
(reloading CSS, JS, etc), similar to [HotWire
Spark](https://github.com/hotwired/spark). Be sure to attend my
SymfonyCon to learn more!

There is still room for improvement:

- Provide an option to only trigger the update without reloading the
worker for some files (ex, images, JS, CSS...)
- Support classic mode (currently, only the worker mode is supported)
- Don't reload all workers when only the files used by one change

However, this PR is working as-is and can be merged as a first step.

This patch heavily refactors the watcher module. Maybe it will be
possible to extract it as a standalone library at some point (would be
useful to add a similar feature but not tight to PHP as a Caddy module).

---------

Signed-off-by: Kévin Dunglas <kevin@dunglas.fr>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-12 14:29:18 +01:00
Alexander Stecher
98573ed7c0 refactor: extract the state module and make the backoff error instead of panic
This PR:
- moves state.go to its own module
- moves the phpheaders test the phpheaders module
- simplifies backoff.go
- makes the backoff error instead of panic (so it can be tested)
- removes some unused C structs
2025-12-02 23:10:12 +01:00
Alexandre Daubois
1fbabf91c9 fix(extgen): use RETURN_EMPTY_STRING() when returning empty string (#2049)
One last bug spotted by #1984, empty strings should be returned using
`RETURN_EMPTY_STRING()` or it may segfault.
2025-12-01 15:43:45 +01:00
Alexandre Daubois
2fa7663d3b fix(extgen): use REGISTER(_NS)_BOOL_CONSTANT (#2047)
Spotted in #1984, this is the right macros to declare boolean constants
2025-12-01 15:35:34 +01:00
Max
c9ad9fc55a headerKeyCache: use otter v2 (#2040)
Benchmarks show that version 1, while extremely fast with hot keys,
becomes several times slower than no‑cache under frequent misses.
Version 2 delivers consistently better performance across all scenarios,
with no allocations and stable latency.

```
BenchmarkGetUnCommonHeaderNoCacheSequential-12                           7545640               169.4 ns/op            72 B/op          4 allocs/op
BenchmarkGetUnCommonHeaderV2Sequential-12                               14471982                85.98 ns/op            0 B/op          0 allocs/op
BenchmarkGetUnCommonHeaderV1Sequential-12                               19748048                59.63 ns/op            0 B/op          0 allocs/op

BenchmarkGetUnCommonHeaderNoCacheParallelOneKey-12                      24352088                44.47 ns/op           72 B/op          4 allocs/op
BenchmarkGetUnCommonHeaderV2ParallelOneKey-12                           91024160                11.76 ns/op            0 B/op          0 allocs/op
BenchmarkGetUnCommonHeaderV1ParallelOneKey-12                           192048842                6.186 ns/op           0 B/op          0 allocs/op

BenchmarkGetUnCommonHeaderNoCacheParallelRandomMaximumSize-12           26261611                43.07 ns/op           62 B/op          3 allocs/op
BenchmarkGetUnCommonHeaderV2ParallelRandomMaximumSize-12                100000000               14.43 ns/op            0 B/op          0 allocs/op
BenchmarkGetUnCommonHeaderV1ParallelRandomMaximumSize-12                137813384                8.965 ns/op           0 B/op          0 allocs/op

BenchmarkGetUnCommonHeaderNoCacheParallelRandomLenKeys-12               24224664                46.57 ns/op           71 B/op          3 allocs/op
BenchmarkGetUnCommonHeaderV2ParallelRandomLenKeys-12                    69002575                17.42 ns/op            0 B/op          0 allocs/op
BenchmarkGetUnCommonHeaderV1ParallelRandomLenKeys-12                     8498404               253.1 ns/op            42 B/op          1 allocs/op
```

---------

Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
2025-12-01 11:37:40 +01:00
Kévin Dunglas
c93729e136 chore: use sync.WaitGroup.Go when possible (#1996)
* chore: use sync.WaitGroup.Go when possible

* Update internal/watcher/watcher.go

Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>

---------

Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
2025-11-20 11:48:18 +01:00
Alexandre Daubois
bd943f49de feat(extgen): print gen_stub.php in case of failure 2025-11-18 11:10:03 +01:00
Alexandre Daubois
8f298ab060 fix(extgen): constant should be declared under the namespace provided by export_php:namespace 2025-11-18 10:40:59 +01:00
Alexandre Daubois
eeb7d1a0c4 fix(extgen): only register ext_functions if functions are declared 2025-11-17 17:40:30 +01:00
Kévin Dunglas
8341cc98c6 refactor: rely on context.Context for log/slog and others (#1969)
* refactor: rely on context.Context for log/slog and others

* optimize

* refactor

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix watcher-skip

* better globals handling

* fix

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-11-17 16:32:23 +01:00
Alexandre Daubois
4e6d67e0b4 fix(extgen): don't remove everything in the build directory now that there's no build subdir 2025-11-14 15:13:54 +01:00
Alexandre Daubois
f7298557aa feat(extgen): automatically add "runtime/cgo" to the imports if necessary 2025-11-14 15:12:28 +01:00
Alexandre Daubois
861b345b05 fix(extgen): replace any by interface{} in the generated go file when dealing with handles 2025-11-14 14:54:40 +01:00
Kévin Dunglas
b749f52ae5 chore: simplify string using backticks
# Conflicts:
#	internal/extgen/classparser.go
#	internal/extgen/gofile_test.go
2025-10-14 14:09:17 +02:00
Kévin Dunglas
5514491a18 feat(extgen): support for mixed type (#1913)
* feat(extgent): support for mixed type

* refactor: use unsafe.Pointer

* Revert "refactor: use unsafe.Pointer"

This reverts commit 8a0b9c1beb.

* fix docs

* fix docs

* cleanup template

* fix template

* fix tests
2025-10-09 14:10:45 +02:00
Alexander Stecher
952754db27 fix: don't flush env between requests (#1814) 2025-08-27 08:30:40 +02:00
Alexander Stecher
d540727369 feat:(extgen) make Go arrays more consistent with PHP arrays (#1800)
* Makes go arrays more consistent with PHP arrays.

* NewAssociativeArray.

* linting

* go linting

* Exposes all primitive types.

* Removes pointer alias

* linting

* Optimizes hash update.

* Fixes extgen tests.

* Moves file to tests.

* Fixes suggested by @dunglas.

* Replaces 'interface{}' with 'any'.

* Panics on wrong zval.

* interface improvements as suggested by @dunglas.

* Adjusts docs.

* Adjusts docs.

* Removes PackedArray alias and adjusts docs.

* Updates docs.
2025-08-25 16:24:15 +02:00
Alexandre Daubois
4dd6b5ea16 fix: support filename other than ext.go and keep local vars on generation 2025-08-25 16:17:01 +02:00
Alexander Stecher
2b78ffe15c tests: make caddy_tests faster (#1823) 2025-08-25 16:16:32 +02:00
Kévin Dunglas
a1ae2692e1 chore: modernize Go code 2025-08-15 00:22:44 +02:00
Kévin Dunglas
1da2ba1f28 fix(ci): Docker builds 2025-07-17 10:14:18 +02:00
Alexandre Daubois
34fbfd467b chore(extgen): remove useless constructors 2025-07-16 12:06:23 +02:00
Alexandre Daubois
8df41236d9 feat(extgen): add support for arrays as parameters and return types (#1724)
* feat(extgen): add support for arrays as parameters and return types

* cs

---------

Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
2025-07-16 12:05:29 +02:00
Alexandre Daubois
1804e36b93 feat(extgen): add support for //export_php:namespace (#1721) 2025-07-16 12:01:39 +02:00
Alexandre Daubois
6ce99f251a chore(extgen): unexport more symbols (#1719) 2025-07-07 05:55:09 +02:00
Luffy
ac900e0df4 docs: update repository links and sync cn readme (#1711) 2025-07-01 10:29:55 +02:00
Alexandre Daubois
94ac4b4935 chore: use modern ZPP macros in the extension generator (#1703) 2025-06-30 14:50:36 +02:00
Alexandre Daubois
58fde42654 fix: improve generated C extension code (#1698) 2025-06-30 09:23:21 +02:00
Alexandre Daubois
30ef5f6657 chore: use filepath.Separator instead of hardcoded separator (#1685) 2025-06-27 14:36:31 +02:00
Alexandre Daubois
9e3b47c52f fix(extgen): capitalize cgo handle function call (#1696) 2025-06-27 14:26:09 +02:00
Kévin Dunglas
abfd893d88 feat: FrankenPHP extensions (#1651)
* feat: add helpers to create PHP extensions (#1644)

* feat: add helpers to create PHP extensions

* cs

* feat: GoString

* test

* add test for RegisterExtension

* cs

* optimize includes

* fix

* feat(extensions): add the PHP extension generator (#1649)

* feat(extensions): add the PHP extension generator

* unexport many types

* unexport more symbols

* cleanup some tests

* unexport more symbols

* fix

* revert types files

* revert

* add better validation and fix templates

* remove GoStringCopy

* small fixes

---------

Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>

* try to fix tests

* fix CS

* try some workarounds

* try some workarounds

* ingore TestRegisterExtension

* exclude cgo tests in Docker images

* fix

* workaround...

* race detector

* simplify tests and code

* make linter happy

* feat(gofile): use templates to generate the Go file (#1666)

---------

Co-authored-by: Alexandre Daubois <2144837+alexandre-daubois@users.noreply.github.com>
2025-06-25 10:18:22 +02:00
Max
71aebbe0e7 perf: add popular proxy headers (#1661)
* perf: add popular proxy headers

* X-Real-IP => X-Real-Ip
2025-06-19 14:05:26 +02:00
Alexander Stecher
3741782330 feat: '-r' option for php-cli (#1482) 2025-05-01 02:06:31 +02:00
Kévin Dunglas
8583afd83e chore: add context to logs to make the linter happy (#1533) 2025-04-29 01:08:15 +02:00