Gomi
Gomi

Reputation: 662

Parse multiple headers with same name

I want to have properly implemented header parsing in my app. However, for example, let's have headers such as:

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

In this case, using getallheaders() returns only Foo => Bar and Baz value is dropped. Is there any other way to get all header values?

Upvotes: 1

Views: 265

Answers (1)

Chris Haas
Chris Haas

Reputation: 55427

According to this bug report, this appears to be dependent upon which SAPI you are using for PHP. The basic question/statement was:

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 only keeps last header
  • php -s only keeps first header
  • apache concatenates them with ,

And the response to the ticket was:

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.

Upvotes: 3

Related Questions