From d532772355a51ee9171de7a9f56c3abf3936e205 Mon Sep 17 00:00:00 2001 From: Rob Landers Date: Sun, 11 Aug 2024 22:34:50 +0200 Subject: [PATCH] fix: reset sapi response code (#964) * 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 * update response header test * fix assertion * appears to affect workers too --- frankenphp.c | 3 +++ frankenphp_test.go | 6 ++++++ testdata/response-headers.php | 6 ++++-- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/frankenphp.c b/frankenphp.c index f3d085e..d09c1a8 100644 --- a/frankenphp.c +++ b/frankenphp.c @@ -455,6 +455,9 @@ int frankenphp_update_server_context( 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; diff --git a/frankenphp_test.go b/frankenphp_test.go index 2c1e5c6..eee4e09 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+100, 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 c994bf0..3444a65 100644 --- a/testdata/response-headers.php +++ b/testdata/response-headers.php @@ -7,7 +7,9 @@ return function () { 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()); };