php / php/php-src

Headers functions do nothing in CLI SAPI

オープン
#12,304 コメント 5 件 リアクション 13 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Feature SAPI: cli Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 13時間
マージ済み PR(30日)
96

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

sapi/cli/php_cli.c から始め、特に sapi_cli_header_handler()、sapi_cli_send_headers()、および issue で説明されている headers_sent() の実装を確認してください。CLI が現在 header()、header_remove()、headers_list()、http_response_code()、setcookie() をどのように処理しているかを確認します。CLI がこれらの関数についてヘッダーの状態を保持しつつ、引き続きヘッダーを送信せず、headers_sent() が false のままであれば完了です。issue にある PHP の例で動作を検証してください。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, php
領域
cli
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。