1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-30 01:10:58 +01:00

Fix an issue where PHP puts the content type in CONTENT_TYPE instead of HTTP_CONTENT_TYPE

Summary: Fixes T4084. See that task for discussion.

Test Plan: Did `git clone`. My setup doesn't precisely reproduce the original issue, but hopefully @enko can confirm this is a fix.

Reviewers: btrahan, enko

Reviewed By: enko

CC: enko, aran

Maniphest Tasks: T4084

Differential Revision: https://secure.phabricator.com/D7561
This commit is contained in:
epriestley 2013-11-11 12:17:34 -08:00
parent f2938bacd9
commit dc7f716156
2 changed files with 50 additions and 3 deletions

View file

@ -466,14 +466,44 @@ final class AphrontRequest {
}
public static function getHTTPHeader($name, $default = null) {
/**
* Read the value of an HTTP header from `$_SERVER`, or a similar datasource.
*
* This function accepts a canonical header name, like `"Accept-Encoding"`,
* and looks up the appropriate value in `$_SERVER` (in this case,
* `"HTTP_ACCEPT_ENCODING"`).
*
* @param string Canonical header name, like `"Accept-Encoding"`.
* @param wild Default value to return if header is not present.
* @param array? Read this instead of `$_SERVER`.
* @return string|wild Header value if present, or `$default` if not.
*/
public static function getHTTPHeader($name, $default = null, $data = null) {
// PHP mangles HTTP headers by uppercasing them and replacing hyphens with
// underscores, then prepending 'HTTP_'.
$php_index = strtoupper($name);
$php_index = str_replace('-', '_', $php_index);
$php_index = 'HTTP_'.$php_index;
return idx($_SERVER, $php_index, $default);
$try_names = array();
$try_names[] = 'HTTP_'.$php_index;
if ($php_index == 'CONTENT_TYPE' || $php_index == 'CONTENT_LENGTH') {
// These headers may be available under alternate names. See
// http://www.php.net/manual/en/reserved.variables.server.php#110763
$try_names[] = $php_index;
}
if ($data === null) {
$data = $_SERVER;
}
foreach ($try_names as $try_name) {
if (array_key_exists($try_name, $data)) {
return $data[$try_name];
}
}
return $default;
}
}

View file

@ -131,4 +131,21 @@ final class AphrontRequestTestCase extends PhabricatorTestCase {
}
}
public function testGetHTTPHeader() {
$server_data = array(
'HTTP_ACCEPT_ENCODING' => 'duck/quack',
'CONTENT_TYPE' => 'cow/moo',
);
$this->assertEqual(
'duck/quack',
AphrontRequest::getHTTPHeader('AcCePt-EncOdING', null, $server_data));
$this->assertEqual(
'cow/moo',
AphrontRequest::getHTTPHeader('cONTent-TyPE', null, $server_data));
$this->assertEqual(
null,
AphrontRequest::getHTTPHeader('Pie-Flavor', null, $server_data));
}
}