Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not send headers by SAPI #3571

Merged
merged 4 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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