[php-src] Issue #12304: Headers functions do nothing in CLI SAPI
| From: | joanhey | Date: | Tue, 26 Sep 2023 21:45:17 +0000 |
| Subject: | [php-src] Issue #12304: Headers functions do nothing in CLI SAPI | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245440@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12304
Author: joanhey
### Description
It seems an stupid issue, but please read.
Related to #12303
The following code:
``
```php
<?php
header('Custom: xxx');
print_r(headers_list());`
````
Resulted in this output:
```
Array
(
)
```
But I expected this output instead:
```
Array
(
[0] => Custon: xxx
)
```
We know that the CLI SAPI don't send headers, and for that it's OK that the
sapi_cli_send_headerS() do nothing.
https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c#L395-L405
But all the other header(), header_remove(), headers_list()
that depend from sapi_cli_header_handler()
https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c#L389-L392
can work normally from CLI. Only the headers_sent() need to be always
false.
Actual behavior:
https://3v4l.org/XcuBg
Yes, it's curious that the setcookie() return true.
And the http_response_code() also work from CLI, we only need to set the initial value,
but not the headers.
First because other php functions use it internally, like setcookie, session, ...
But it's more important to use php files without changes from CLI.
Basic example:
```php
<?php
ob_start();
http_response_code(200);
include 'file.php';
$body = ob_get_clean();
$headers = create_status( http_response_code());
$headers .= implode("\r\n", headers_list()) . "\r\n";
$headers .= 'Content-Length: ' . strlen($content);
send_to_socket($headers, $body);
```
So we can use any PHP file from CLI and send it to any socket.
**BC**: Nothing
That functions now do nothing from CLI.
If acepted it'll be good to ve added to all php suported versions.
### PHP Version
All
### Operating System
All