From 43d09c26a9e033cc2afb45a25d3908d07957b251 Mon Sep 17 00:00:00 2001 From: Robert Landers Date: Sun, 11 Aug 2024 11:02:11 +0200 Subject: [PATCH 1/4] fix: reset sapi response code It turns out that the sapi response code is NOT reset between requests by the zend engine. This resets the code for cgi-based requests. fixes: #960 --- frankenphp.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/frankenphp.c b/frankenphp.c index f3d085e6d..53e4ba55e 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -451,6 +451,9 @@ int frankenphp_update_server_context( ctx->finished = false; SG(server_context) = ctx; + + // It is not reset by zend engine, set it to 200. + SG(sapi_headers).http_response_code = 200; } else { ctx = (frankenphp_server_context *)SG(server_context); } From 5bdcaa090bc5dad79b2ef9f8b8bba2448f9b082e Mon Sep 17 00:00:00 2001 From: Robert Landers Date: Sun, 11 Aug 2024 11:24:42 +0200 Subject: [PATCH 2/4] update response header test --- frankenphp_test.go | 6 ++++++ testdata/response-headers.php | 6 ++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/frankenphp_test.go b/frankenphp_test.go index 2c1e5c681..01d21f337 100644 --- a/frankenphp_test.go +++ b/frankenphp_test.go @@ -246,6 +246,12 @@ func testResponseHeaders(t *testing.T, opts *testOptions) { resp := w.Result() body, _ := io.ReadAll(resp.Body) + if i%3 != 0 { + assert.Equal(t, i, resp.StatusCode) + } else { + assert.Equal(t, 200, resp.StatusCode) + } + assert.Contains(t, string(body), "'X-Powered-By' => 'PH") assert.Contains(t, string(body), "'Foo' => 'bar',") assert.Contains(t, string(body), "'Foo2' => 'bar2',") diff --git a/testdata/response-headers.php b/testdata/response-headers.php index c994bf0f3..3444a651a 100644 --- a/testdata/response-headers.php +++ b/testdata/response-headers.php @@ -7,7 +7,9 @@ header('Foo2: bar2'); header('Invalid'); header('I: ' . ($_GET['i'] ?? 'i not set')); - http_response_code(201); - + if ($_GET['i'] % 3) { + http_response_code($_GET['i'] + 100); + } + var_export(apache_response_headers()); }; From 57011556067c50c1e98055df570d0d3f5005e0bf Mon Sep 17 00:00:00 2001 From: Robert Landers Date: Sun, 11 Aug 2024 11:27:16 +0200 Subject: [PATCH 3/4] fix assertion --- frankenphp_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frankenphp_test.go b/frankenphp_test.go index 01d21f337..eee4e0970 100644 --- a/frankenphp_test.go +++ b/frankenphp_test.go @@ -247,7 +247,7 @@ func testResponseHeaders(t *testing.T, opts *testOptions) { body, _ := io.ReadAll(resp.Body) if i%3 != 0 { - assert.Equal(t, i, resp.StatusCode) + assert.Equal(t, i+100, resp.StatusCode) } else { assert.Equal(t, 200, resp.StatusCode) } From 7c5c282c01c285c8956d32109d993c5cecd85160 Mon Sep 17 00:00:00 2001 From: Robert Landers Date: Sun, 11 Aug 2024 11:31:29 +0200 Subject: [PATCH 4/4] appears to affect workers too --- frankenphp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index 53e4ba55e..d09c1a8e5 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -451,13 +451,13 @@ int frankenphp_update_server_context( ctx->finished = false; SG(server_context) = ctx; - - // It is not reset by zend engine, set it to 200. - SG(sapi_headers).http_response_code = 200; } else { ctx = (frankenphp_server_context *)SG(server_context); } + // It is not reset by zend engine, set it to 200. + SG(sapi_headers).http_response_code = 200; + ctx->main_request = main_request; ctx->current_request = current_request;