Skip to content

Commit dc7f716

Browse files
author
epriestley
committed
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
1 parent f2938ba commit dc7f716

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/aphront/AphrontRequest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,14 +466,44 @@ public static function flattenData(array $data) {
466466
}
467467

468468

469-
public static function getHTTPHeader($name, $default = null) {
469+
/**
470+
* Read the value of an HTTP header from `$_SERVER`, or a similar datasource.
471+
*
472+
* This function accepts a canonical header name, like `"Accept-Encoding"`,
473+
* and looks up the appropriate value in `$_SERVER` (in this case,
474+
* `"HTTP_ACCEPT_ENCODING"`).
475+
*
476+
* @param string Canonical header name, like `"Accept-Encoding"`.
477+
* @param wild Default value to return if header is not present.
478+
* @param array? Read this instead of `$_SERVER`.
479+
* @return string|wild Header value if present, or `$default` if not.
480+
*/
481+
public static function getHTTPHeader($name, $default = null, $data = null) {
470482
// PHP mangles HTTP headers by uppercasing them and replacing hyphens with
471483
// underscores, then prepending 'HTTP_'.
472484
$php_index = strtoupper($name);
473485
$php_index = str_replace('-', '_', $php_index);
474-
$php_index = 'HTTP_'.$php_index;
475486

476-
return idx($_SERVER, $php_index, $default);
487+
$try_names = array();
488+
489+
$try_names[] = 'HTTP_'.$php_index;
490+
if ($php_index == 'CONTENT_TYPE' || $php_index == 'CONTENT_LENGTH') {
491+
// These headers may be available under alternate names. See
492+
// http://www.php.net/manual/en/reserved.variables.server.php#110763
493+
$try_names[] = $php_index;
494+
}
495+
496+
if ($data === null) {
497+
$data = $_SERVER;
498+
}
499+
500+
foreach ($try_names as $try_name) {
501+
if (array_key_exists($try_name, $data)) {
502+
return $data[$try_name];
503+
}
504+
}
505+
506+
return $default;
477507
}
478508

479509
}

src/aphront/__tests__/AphrontRequestTestCase.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,21 @@ public function testFlattenRequestData() {
131131
}
132132
}
133133

134+
public function testGetHTTPHeader() {
135+
$server_data = array(
136+
'HTTP_ACCEPT_ENCODING' => 'duck/quack',
137+
'CONTENT_TYPE' => 'cow/moo',
138+
);
139+
140+
$this->assertEqual(
141+
'duck/quack',
142+
AphrontRequest::getHTTPHeader('AcCePt-EncOdING', null, $server_data));
143+
$this->assertEqual(
144+
'cow/moo',
145+
AphrontRequest::getHTTPHeader('cONTent-TyPE', null, $server_data));
146+
$this->assertEqual(
147+
null,
148+
AphrontRequest::getHTTPHeader('Pie-Flavor', null, $server_data));
149+
}
150+
134151
}

0 commit comments

Comments
 (0)