Double Content-Type headers added to request if context->http->header is a multiline string
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- C
- Star
- 40.4k
- Fork
- 8.2k
- Merge trung bình
- 2 ngày 13 giờ
- Pull request đã merge (30 ngày)
- 96
Mô tả
Description
When using file_get_contents to post an HTTP/HTTPS request, context->http->header can either be a string or an array of strings. If a string, it may ignore the header containing a Content-Type line and add an additional one.
The following code:
<?php
$header = "Authentication: Bearer XYZ" . PHP_EOL;
$header .= 'Content-Type: application/json' . PHP_EOL;
$httpoptions = [
'method' => 'POST',
'ignore_errors' => true,
'content' => json_encode(["message" => "Hello world"]),
'header' => $header,
];
$context = stream_context_create(['http' => $httpoptions]);
$result = file_get_contents("http://some-test-url/", false, $context);
Resulted in this request (captured using netcat):
POST / HTTP/1.1
Host: some-test-url
Connection: close
Content-Length: 25
Authentication: Bearer XYZ
Content-Type: application/json
Content-Type: application/x-www-form-urlencoded
{"message":"Hello world"}
But I expected this output instead:
POST / HTTP/1.1
Host: some-test-url
Connection: close
Content-Length: 25
Authentication: Bearer XYZ
Content-Type: application/json
{"message":"Hello world"}
Commentary:
There is a warning, "file_get_contents(): Content-type not specified assuming application/x-www-form-urlencoded", that is on some occasions issued (although not, oddly enough, for the code we tracked down this issue as applying to.)
The issue goes away if you build context->http->header as an array.
While arguably building the header as a multiline string seems (always seemed) odd to me, it's frequently quoted in examples across the Internet (which is probably how we ended up doing it) - several examples here: https://www.php.net/manual/en/function.stream-context-create.php
I doubt there are any backward compatibility issues that would be caused by a straight fix to this.
PHP Version
PHP 8.1.2
Operating System
Ubuntu 22.04
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách tái hiện request với file_get_contents, stream_context_create và header nhiều dòng được hiển thị trong issue, đồng thời ghi lại kết quả bằng netcat. Theo dõi cách PHP xử lý HTTP stream đối với các header dạng string và so sánh với các header dạng array. Hoàn tất khi Content-Type được cung cấp được giữ nguyên mà không có thêm header application/x-www-form-urlencoded.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- php
- Lĩnh vực
- networking
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 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
- 45/100