php / php/php-src

Headers functions do nothing in CLI SAPI

Open
#12,304 5 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Feature SAPI: cli Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

It seems an stupid issue, but please read.
Related to #12303
The following code:

<?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 using the fallback. Only the headers_sent() need to be always false.
https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c#L446-L448

Related to #12303
https://3v4l.org/Wub2V

Actual behavior
https://3v4l.org/vZtmZ
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
header_remove();
http_response_code(200);

ob_start();

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($body);

send_to_socket($headers, $body);

So we can use any PHP file from CLI and send it to any socket.
Actually we need to create custom functions to manage the headers, and change the normal php files.
But with the headers_list() and http_response_code() we can create all the http message.

BC: Nothing
That functions now do nothing from CLI.
If accepted it'll be good to be added to all php supported versions.

PHP Version

All

Operating System

All

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in sapi/cli/php_cli.c, especially sapi_cli_header_handler(), sapi_cli_send_headers(), and the headers_sent() implementation described in the issue. Check how CLI currently handles header(), header_remove(), headers_list(), http_response_code(), and setcookie(). Done means CLI retains header state for these functions while still not sending headers and keeping headers_sent() false; verify the behavior with the PHP examples in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.