Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63a228c

Browse files
committed
Map normalized header names to original capitalization
1 parent 2be4720 commit 63a228c

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public function sendHeaders()
259259
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText));
260260

261261
// headers
262-
foreach ($this->headers->all() as $name => $values) {
262+
foreach ($this->headers->allPreserveCase() as $name => $values) {
263263
foreach ($values as $value) {
264264
header($name.': '.$value, false);
265265
}

src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class ResponseHeaderBag extends HeaderBag
3636
*/
3737
protected $cookies = array();
3838

39+
/**
40+
* @var array
41+
*/
42+
protected $headerNames = array();
43+
3944
/**
4045
* Constructor.
4146
*
@@ -48,7 +53,7 @@ public function __construct(array $headers = array())
4853
parent::__construct($headers);
4954

5055
if (!isset($this->headers['cache-control'])) {
51-
$this->set('cache-control', '');
56+
$this->set('Cache-Control', '');
5257
}
5358
}
5459

@@ -65,17 +70,29 @@ public function __toString()
6570
return parent::__toString().$cookies;
6671
}
6772

73+
/**
74+
* Returns the headers, with original capitalizations.
75+
*
76+
* @return array An array of headers
77+
*/
78+
public function allPreserveCase()
79+
{
80+
return array_combine($this->headerNames, $this->headers);
81+
}
82+
6883
/**
6984
* {@inheritdoc}
7085
*
7186
* @api
7287
*/
7388
public function replace(array $headers = array())
7489
{
90+
$this->headerNames = array();
91+
7592
parent::replace($headers);
7693

7794
if (!isset($this->headers['cache-control'])) {
78-
$this->set('cache-control', '');
95+
$this->set('Cache-Control', '');
7996
}
8097
}
8198

@@ -88,10 +105,14 @@ public function set($key, $values, $replace = true)
88105
{
89106
parent::set($key, $values, $replace);
90107

108+
$uniqueKey = strtr(strtolower($key), '_', '-');
109+
$this->headerNames[$uniqueKey] = $key;
110+
91111
// ensure the cache-control header has sensible defaults
92-
if (in_array(strtr(strtolower($key), '_', '-'), array('cache-control', 'etag', 'last-modified', 'expires'))) {
112+
if (in_array($uniqueKey, array('cache-control', 'etag', 'last-modified', 'expires'))) {
93113
$computed = $this->computeCacheControlValue();
94114
$this->headers['cache-control'] = array($computed);
115+
$this->headerNames['cache-control'] = 'Cache-Control';
95116
$this->computedCacheControl = $this->parseCacheControl($computed);
96117
}
97118
}
@@ -105,7 +126,10 @@ public function remove($key)
105126
{
106127
parent::remove($key);
107128

108-
if ('cache-control' === strtr(strtolower($key), '_', '-')) {
129+
$uniqueKey = strtr(strtolower($key), '_', '-');
130+
unset($this->headerNames[$uniqueKey]);
131+
132+
if ('cache-control' === $uniqueKey) {
109133
$this->computedCacheControl = array();
110134
}
111135
}

src/Symfony/Component/HttpFoundation/Tests/ResponseHeaderBagTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,51 @@
1616

1717
class ResponseHeaderBagTest extends \PHPUnit_Framework_TestCase
1818
{
19+
/**
20+
* @covers Symfony\Component\HttpFoundation\ResponseHeaderBag::allPreserveCase
21+
* @dataProvider provideAllPreserveCase
22+
*/
23+
public function testAllPreserveCase($headers, $expected)
24+
{
25+
$bag = new ResponseHeaderBag($headers);
26+
27+
$this->assertEquals($expected, $bag->allPreserveCase(), '->allPreserveCase() gets all input keys in original case');
28+
}
29+
30+
public function provideAllPreserveCase()
31+
{
32+
return array(
33+
array(
34+
array('fOo' => 'BAR'),
35+
array('fOo' => array('BAR'), 'Cache-Control' => array('no-cache'))
36+
),
37+
array(
38+
array('ETag' => 'xyzzy'),
39+
array('ETag' => array('xyzzy'), 'Cache-Control' => array('private, must-revalidate'))
40+
),
41+
array(
42+
array('Content-MD5' => 'Q2hlY2sgSW50ZWdyaXR5IQ=='),
43+
array('Content-MD5' => array('Q2hlY2sgSW50ZWdyaXR5IQ=='), 'Cache-Control' => array('no-cache'))
44+
),
45+
array(
46+
array('P3P' => 'CP="CAO PSA OUR"'),
47+
array('P3P' => array('CP="CAO PSA OUR"'), 'Cache-Control' => array('no-cache'))
48+
),
49+
array(
50+
array('WWW-Authenticate' => 'Basic realm="WallyWorld"'),
51+
array('WWW-Authenticate' => array('Basic realm="WallyWorld"'), 'Cache-Control' => array('no-cache'))
52+
),
53+
array(
54+
array('X-UA-Compatible' => 'IE=edge,chrome=1'),
55+
array('X-UA-Compatible' => array('IE=edge,chrome=1'), 'Cache-Control' => array('no-cache'))
56+
),
57+
array(
58+
array('X-XSS-Protection' => '1; mode=block'),
59+
array('X-XSS-Protection' => array('1; mode=block'), 'Cache-Control' => array('no-cache'))
60+
),
61+
);
62+
}
63+
1964
public function testCacheControlHeader()
2065
{
2166
$bag = new ResponseHeaderBag(array());

0 commit comments

Comments
 (0)