Skip to content

Commit a59b649

Browse files
fix: headers before flushing (#1622)
* add tests * fix test * attempt to send headers when flushing * Update testdata/only-headers.php Co-authored-by: Kévin Dunglas <kevin@dunglas.fr> --------- Co-authored-by: Kévin Dunglas <kevin@dunglas.fr>
1 parent 68a4548 commit a59b649

4 files changed

Lines changed: 32 additions & 0 deletions

File tree

frankenphp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ static int frankenphp_send_headers(sapi_headers_struct *sapi_headers) {
588588
}
589589

590590
static void frankenphp_sapi_flush(void *server_context) {
591+
sapi_send_headers();
591592
if (go_sapi_flush(thread_index)) {
592593
php_handle_aborted_connection();
593594
}

frankenphp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ func addHeader(fc *frankenPHPContext, cString *C.char, length C.int) {
492492
func go_write_headers(threadIndex C.uintptr_t, status C.int, headers *C.zend_llist) C.bool {
493493
fc := phpThreads[threadIndex].getRequestContext()
494494

495+
if fc == nil {
496+
return C.bool(false)
497+
}
498+
495499
if fc.isDone {
496500
return C.bool(false)
497501
}

frankenphp_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,22 @@ func testRejectInvalidHeaders(t *testing.T, opts *testOptions) {
10231023
}
10241024
}
10251025

1026+
func TestFlushEmptyResponse_module(t *testing.T) { testFlushEmptyResponse(t, &testOptions{}) }
1027+
func TestFlushEmptyRespnse_worker(t *testing.T) {
1028+
testFlushEmptyResponse(t, &testOptions{workerScript: "only-headers.php"})
1029+
}
1030+
1031+
func testFlushEmptyResponse(t *testing.T, opts *testOptions) {
1032+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, _ int) {
1033+
req := httptest.NewRequest("GET", "http://example.com/only-headers.php", nil)
1034+
w := httptest.NewRecorder()
1035+
handler(w, req)
1036+
1037+
resp := w.Result()
1038+
assert.Equal(t, 204, resp.StatusCode)
1039+
}, opts)
1040+
}
1041+
10261042
// Worker mode will clean up unreferenced streams between requests
10271043
// Make sure referenced streams are not cleaned up
10281044
func TestFileStreamInWorkerMode(t *testing.T) {

testdata/only-headers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
require_once __DIR__.'/_executor.php';
4+
5+
return function () {
6+
header('Content-Type: application/json');
7+
header('HTTP/1.1 204 No Content', true, 204);
8+
9+
echo '{"status": "test"}';
10+
flush();
11+
};

0 commit comments

Comments
 (0)