Skip to content

ext/standard/head.c: Small refactoring of headers_sent() #16109

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 13 additions & 18 deletions ext/standard/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,39 +294,34 @@ PHP_FUNCTION(setrawcookie)
/* {{{ Returns true if headers have already been sent, false otherwise */
PHP_FUNCTION(headers_sent)
{
zval *arg1 = NULL, *arg2 = NULL;
const char *file="";
int line=0;
zval *by_ref_filename = NULL;
zval *by_ref_line = NULL;
const char *file = "";
int line = 0;

ZEND_PARSE_PARAMETERS_START(0, 2)
Z_PARAM_OPTIONAL
Z_PARAM_ZVAL(arg1)
Z_PARAM_ZVAL(arg2)
Z_PARAM_ZVAL(by_ref_filename)
Z_PARAM_ZVAL(by_ref_line)
ZEND_PARSE_PARAMETERS_END();

if (SG(headers_sent)) {
line = php_output_get_start_lineno();
file = php_output_get_start_filename();
}

switch(ZEND_NUM_ARGS()) {
case 2:
ZEND_TRY_ASSIGN_REF_LONG(arg2, line);
ZEND_FALLTHROUGH;
case 1:
if (by_ref_filename) {
if (file) {
ZEND_TRY_ASSIGN_REF_STRING(arg1, file);
ZEND_TRY_ASSIGN_REF_STRING(by_ref_filename, file);
} else {
ZEND_TRY_ASSIGN_REF_EMPTY_STRING(arg1);
ZEND_TRY_ASSIGN_REF_EMPTY_STRING(by_ref_filename);
}
break;
}

if (SG(headers_sent)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
if (by_ref_line) {
ZEND_TRY_ASSIGN_REF_LONG(by_ref_line, line);
}

RETURN_BOOL(SG(headers_sent));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick question: is there a reason why headers_sent global is not just a bool ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it hasn't been refactored since the move to C99, but it should yes.

}
/* }}} */

Expand Down
110 changes: 110 additions & 0 deletions ext/standard/tests/general_functions/headers_sent_by_ref_args.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
--TEST--
headers_sent() by-ref argument with named arguments
--FILE--
<?php

ob_start();
$file = null;
$line = null;

$v1 = headers_sent(line: $line);
$v2 = headers_list();

echo 'headers_sent():', PHP_EOL;
var_dump($v1);
echo 'headers_list():', PHP_EOL;
var_dump($v2);
echo '$file:', PHP_EOL;
var_dump($file);
echo '$line:', PHP_EOL;
var_dump($line);

$file = null;
$line = null;
$v1 = headers_sent(filename: $file);
$v2 = headers_list();

echo 'headers_sent():', PHP_EOL;
var_dump($v1);
echo 'headers_list():', PHP_EOL;
var_dump($v2);
echo '$file:', PHP_EOL;
var_dump($file);
echo '$line:', PHP_EOL;
var_dump($line);

echo 'header():', PHP_EOL;
var_dump(header("HTTP 1.0", true, 200));

ob_end_flush();

$file = null;
$line = null;
$v1 = headers_sent(line: $line);
$v2 = headers_list();

echo 'headers_sent():', PHP_EOL;
var_dump($v1);
echo 'headers_list():', PHP_EOL;
var_dump($v2);
echo '$file:', PHP_EOL;
var_dump($file);
echo '$line:', PHP_EOL;
var_dump($line);

$file = null;
$line = null;
$v1 = headers_sent(filename: $file);
$v2 = headers_list();

echo 'headers_sent():', PHP_EOL;
var_dump($v1);
echo 'headers_list():', PHP_EOL;
var_dump($v2);
echo '$file:', PHP_EOL;
var_dump($file);
echo '$line:', PHP_EOL;
var_dump($line);

echo "Done\n";
?>
--EXPECTF--
headers_sent():
bool(false)
headers_list():
array(0) {
}
$file:
NULL
$line:
int(0)
headers_sent():
bool(false)
headers_list():
array(0) {
}
$file:
string(0) ""
$line:
NULL
header():
NULL
headers_sent():
bool(true)
headers_list():
array(0) {
}
$file:
NULL
$line:
int(36)
headers_sent():
bool(true)
headers_list():
array(0) {
}
$file:
string(%d) "%s"
$line:
NULL
Done