php / php/php-src

Headers functions do nothing in CLI SAPI

Đang mở
#12,304 5 bình luận 13 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Feature SAPI: cli Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu trong sapi/cli/php_cli.c, đặc biệt là sapi_cli_header_handler(), sapi_cli_send_headers() và phần triển khai headers_sent() được mô tả trong issue. Kiểm tra cách CLI hiện đang xử lý header(), header_remove(), headers_list(), http_response_code() và setcookie(). Được xem là hoàn tất khi CLI giữ lại trạng thái header cho các hàm này, vẫn không gửi header và headers_sent() vẫn là false; xác minh hành vi bằng các ví dụ PHP trong issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
c, php
Lĩnh vực
cli
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.