[PATCH] Allow unsetting headers previously set using header()
| From: | Christian Schmidt | Date: | Sun, 19 Oct 2008 21:41:27 +0000 |
| Subject: | [PATCH] Allow unsetting headers previously set using header() | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-41281@lists.php.net to get a copy of this message | ||
(This is a repost of a proposal originally sent to the list 6 months
ago. It didn't gather much attention back then, but I talked to several
people off-list who found it useful and suggested that I should try to
bring it up again at a later date - so here goes)
When a header has been set using header('Foo: bar') it can be replaced
with another value, but it cannot be removed.
This ability to remove headers is relevant e.g. if the auto_prepend_file
calls header('Vary: Accept-Language'), but later on in the request it is
determined that a particular file does not do language-negotiation (e.g.
a PHP script outputting an image file). Setting it to the empty string
using header('Vary:') is not equivalent to unsetting the header.
It is possible to work around this limitation by maintaining a custom
header buffer, but since PHP allows replacing and appending to
previously set headers, it seems reasonable to also allow unsetting headers.
I suggest extending the behaviour of header() so that when the first
argument does not contain a colon (and does not begin with "HTTP/"),
e.g. header('Vary'), it unsets the header with the specified name. I
decided to extend the existing header() function rather than adding a
new function, because the existing function already supports multiple
actions (add, append and replace).
AFAICT this change is backwards compatible. According to the HTTP spec,
RFC 2616, section 4.2, all header lines must include a colon, so calling
e.g. header('Vary') does currently not generate a valid header:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
The attached patch contains an implementation for the cgi and
apache2handler SAPIs for PHP_5_3. The 6 months old patch still applies,
though I haven't tested it again except from running "make test".
AFAICT these SAPIs don't need to be changed:
cli, embed, isapi, milter, pi3web, thttpd, tux, webjames.
These SAPIs need additional patching:
apache, apache_hooks, apache2filter, continuity/capi, caudium, nsapi,
phttpd, roxen
I don't have an easy way to test these SAPIs, so any help with adding
support for them is appreciated. AFAICT some of these don't have full
header() support (in particular for the second $replace parameter), so
it may not be a big issue if they aren't patched right away.
For some reason I couldn't build apache2filter on my machine.
The attached phpt file is intended to be put in
ext/standard/tests/general_functions.
Christian