解析多个 headers 同名

Parse multiple headers with same name

我想在我的应用程序中正确实施 header 解析。但是,例如,让我们有 headers,例如:

HTTP/1.1 200 OK
...
Foo: Bar
Foo: Baz

在这种情况下,仅使用 getallheaders() returns Foo => Bar 并丢弃 Baz 值。有没有其他方法可以获取所有 header 值?

根据 this bug report,这似乎取决于您为 PHP 使用的 SAPI。基本的 question/statement 是:

If I make request:

GET / HTTP/1.1
Forwarded: for=10.0.0.1,for=20.30.40.50;host=php.net,host=awesome.proxy.com;proto=https,proto=http
Forwarded: for=10.30.20.10;host=second.awesome.proxy.com;proto=http

I get 3 different responses based on SAPI

  • FPM只保留最后一个header
  • php -s 只保留第一个 header
  • apache 用 ,
  • 连接它们

对工单的回复是:

The ability to get headers is highly dependent on the SAPI. Apache's works nicely, but php -s is just a quick development server that is not going to be suitable for all purposes.

So really, the issue here is that php-fpm doesn't properly handle multiple headers. The HTTP spec requires that multiple headers only be allowed when their values can be combined into comma-separated lists, which means $_SERVER and getallheaders() are still sufficient.