Double Content-Type headers added to request if context->http->header is a multiline string
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.1k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、file_get_contents、stream_context_create、および issue に示されている複数行のヘッダーを使ってリクエストを再現し、netcat で結果を取得します。文字列ヘッダーに対する PHP の HTTP ストリーム処理を追跡し、配列ヘッダーの場合と比較します。指定された Content-Type が追加の application/x-www-form-urlencoded ヘッダーなしで保持されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- php
- 領域
- networking
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100