m6w6 / ext-http

Extended HTTP Support
BSD 2-Clause "Simplified" License
79 stars 22 forks source link

Allow _ (Underscore) in Header Field Names #61

Open rcanavan opened 7 years ago

rcanavan commented 7 years ago

RFC 7320 permits a number of "special" characters in the field-name of header fields, including both minus (-) and underscore (_):

header-field = field-name ":" OWS field-value OWS
field-name = token
token = 1*tchar
tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA

ext-http however forces a conversion from _ to - by setting xhyphen to true in php_http_pretty_key() whenever new headers are merged. I believe the following test should work, but currently does not due to the aforementioned conversion:

--TEST--
header string with hyphens and underscores
--SKIPIF--
<?php
include "skipif.inc";
?>
--FILE--
<?php

echo "Test\n";

$h = new http\Header("foo_bar-baz", "bar");
var_dump("Foo_Bar-Baz: bar" === (string) $h);

?>
Done
--EXPECT--
Test
bool(true)
Done
m6w6 commented 7 years ago

Hm, yeah, that origins from importing headers from the (CGI) environment where header names look like HTTP_ACCEPT_ENCODING.

m6w6 commented 7 years ago

Looks like Apache as well as nginx drop headers with underscores.

afflerbach commented 7 years ago

At least for nginx, you can explicitly allow underscores by setting underscores_in_headers on.

filex commented 7 years ago

for apache, afaik, it's only a limitation in mod_proxy that applies to fcgi/fpm setups. mod_php should get underscore headers.

m6w6 commented 7 years ago

For Apache+mod_php: only with apache_request_headers(), but not through the environment, i.e. $_SERVER.