Skip to content

Commit

Permalink
Do not send headers by SAPI (#3571)
Browse files Browse the repository at this point in the history
* Do not send headers by SAPI

* Add tests

* Group headers_sent tests (#3574)

* Better way

Co-authored-by: Sergii Shymko <[email protected]>
Co-authored-by: Sergii Shymko <[email protected]>
  • Loading branch information
3 people authored Aug 19, 2020
1 parent 1048afc commit a547e0e
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
4 changes: 4 additions & 0 deletions swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,15 @@ void PHPCoroutine::on_close(void *arg) {
}

if (OG(handlers).elements) {
zend_bool no_headers = SG(request_info).no_headers;
/* Do not send headers by SAPI */
SG(request_info).no_headers = 1;
if (OG(active)) {
php_output_end_all();
}
php_output_deactivate();
php_output_activate();
SG(request_info).no_headers = no_headers;
}
if (task->array_walk_fci) {
efree(task->array_walk_fci);
Expand Down
49 changes: 49 additions & 0 deletions tests/swoole_http_server/headers_sent.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
swoole_http_server: headers sent (coroutine disabled)
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$pm = new ProcessManager;

$pm->parentFunc = function () use ($pm) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:{$pm->getFreePort()}/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
echo curl_exec($ch);
curl_close($ch);
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$http = new swoole_http_server('127.0.0.1', $pm->getFreePort());
$http->set([
'worker_num' => 1,
'enable_coroutine' => false,
'log_file' => '/dev/null'
]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$http->on('request', function (swoole_http_request $request, swoole_http_response $response) {
ob_start();
echo 'Test';
$output = ob_get_clean();
$response->write('buffered_output=' . $output . "\n");
$response->write('headers_sent=' . (int)headers_sent() . "\n");
$response->end();
});
$http->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
buffered_output=Test
headers_sent=0
buffered_output=Test
headers_sent=0
49 changes: 49 additions & 0 deletions tests/swoole_http_server/headers_sent_coroutine.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
swoole_http_server: headers sent (coroutine enabled)
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

$pm = new ProcessManager;

$pm->parentFunc = function () use ($pm) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:{$pm->getFreePort()}/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($ch);
echo curl_exec($ch);
curl_close($ch);
$pm->kill();
};

$pm->childFunc = function () use ($pm) {
$http = new swoole_http_server('127.0.0.1', $pm->getFreePort());
$http->set([
'worker_num' => 1,
'enable_coroutine' => true,
'log_file' => '/dev/null'
]);
$http->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$http->on('request', function (swoole_http_request $request, swoole_http_response $response) {
ob_start();
echo 'Test';
$output = ob_get_clean();
$response->write('buffered_output=' . $output . "\n");
$response->write('headers_sent=' . (int)headers_sent() . "\n");
$response->end();
});
$http->start();
};

$pm->childFirst();
$pm->run();
?>
--EXPECT--
buffered_output=Test
headers_sent=0
buffered_output=Test
headers_sent=0

0 comments on commit a547e0e

Please sign in to comment.