8000 [HttpFoundation] optimize normalization of headers · symfony/symfony@9c676d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c676d3

Browse files
[HttpFoundation] optimize normalization of headers
1 parent b56a4b4 commit 9c676d3

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

src/Symfony/Component/HttpFoundation/HeaderBag.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
class HeaderBag implements \IteratorAggregate, \Countable
2020
{
21+
protected const UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ';
22+
protected const LOWER = '-abcdefghijklmnopqrstuvwxyz';
23+
2124
protected $headers = [];
2225
protected $cacheControl = [];
2326

@@ -62,9 +65,7 @@ public function __toString()
6265
public function all(/*string $key = null*/)
6366
{
6467
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
65-
$key = str_replace('_', '-', strtolower($key));
66-
67-
return $this->headers[$key] ?? [];
68+
return $this->headers[strtr($key, self::UPPER, self::LOWER)] ?? [];
6869
}
6970

7071
return $this->headers;
@@ -138,7 +139,7 @@ public function get($key, $default = null)
138139
*/
139140
public function set($key, $values, $replace = true)
140141
{
141-
$key = str_replace('_', '-', strtolower($key));
142+
$key = strtr($key, self::UPPER, self::LOWER);
142143

143144
if (\is_array($values)) {
144145
$values = array_values($values);
@@ -170,7 +171,7 @@ public function set($key, $values, $replace = true)
170171
*/
171172
public function has($key)
172173
{
173-
return \array_key_exists(str_replace('_', '-', strtolower($key)), $this->all());
174+
return \array_key_exists(strtr($key, self::UPPER, self::LOWER), $this->all());
174175
}
175176

176177
/**
@@ -193,7 +194,7 @@ public function contains($key, $value)
193194
*/
194195
public function remove($key)
195196
{
196-
$key = str_replace('_', '-', strtolower($key));
197+
$key = strtr($key, self::UPPER, self::LOWER);
197198

198199
unset($this->headers[$key]);
199200

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public function overrideGlobals()
541541

542542
foreach ($this->headers->all() as $key => $value) {
543543
$key = strtoupper(str_replace('-', '_', $key));
544-
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'])) {
544+
if (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
545545
$_SERVER[$key] = implode(', ', $value);
546546
} else {
547547
$_SERVER['HTTP_'.$key] = implode(', ', $value);

src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function allPreserveCase()
5151
{
5252
$headers = [];
5353
foreach ($this->all() as $name => $value) {
54-
$headers[isset($this->headerNames[$name]) ? $this->headerNames[$name] : $name] = $value;
54+
$headers[$this->headerNames[$name] ?? $name] = $value;
5555
}
5656

5757
return $headers;
@@ -95,7 +95,7 @@ public function all(/*string $key = null*/)
9595
$headers = parent::all();
9696

9797
if (1 <= \func_num_args() && null !== $key = func_get_arg(0)) {
98-
$key = str_replace('_', '-', strtolower($key));
98+
$key = strtr($key, self::UPPER, self::LOWER);
9999

100100
return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies());
101101
}
@@ -112,7 +112,7 @@ public function all(/*string $key = null*/)
112112
*/
113113
public function set($key, $values, $replace = true)
114114
{
115-
$uniqueKey = str_replace('_', '-', strtolower($key));
115+
$uniqueKey = strtr($key, self::UPPER, self::LOWER);
116116

117117
if ('set-cookie' === $uniqueKey) {
118118
if ($replace) {
@@ -143,7 +143,7 @@ public function set($key, $values, $replace = true)
143143
*/
144144
public function remove($key)
145145
{
146-
$uniqueKey = str_replace('_', '-', strtolower($key));
146+
$uniqueKey = strtr($key, self::UPPER, self::LOWER);
147147
unset($this->headerNames[$uniqueKey]);
148148

149149
if ('set-cookie' === $uniqueKey) {

src/Symfony/Component/HttpFoundation/ServerBag.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ class ServerBag extends ParameterBag
2828
public function getHeaders()
2929
{
3030
$headers = [];
31-
$contentHeaders = ['CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true];
3231
foreach ($this->parameters as $key => $value) {
3332
if (0 === strpos($key, 'HTTP_')) {
3433
$headers[substr($key, 5)] = $value;
35-
}
36-
// CONTENT_* are not prefixed with HTTP_
37-
elseif (isset($contentHeaders[$key])) {
34+
} elseif (\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH', 'CONTENT_MD5'], true)) {
3835
$headers[$key] = $value;
3936
}
4037
}

0 commit comments

Comments
 (0)
0