Skip to content

Commit 960780f

Browse files
committed
- headers normalization improvement
1 parent 3e15a8a commit 960780f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

HeaderTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,15 @@ public function getCanonicalizedHeaders(array $names = []): string
156156
*/
157157
protected function normalizeHeader(string $name, $value, bool $skipKey): void
158158
{
159+
$name = trim($name);
160+
159161
if (false === $skipKey) {
160162
$name = ucwords(str_replace('_', '-', strtolower($name)), '-');
161163
}
162164

163165
$this->headersMap[strtolower($name)] = $name;
164166

165-
$this->headers[$name] = str_replace(["\r", "\n"], '', array_map('trim', (array)$value));
167+
$this->headers[$name] = array_map('trim', (array)$value);
166168
}
167169

168170
/**

Tests/HeaderTraitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ public function test_canonicalized_headers_with_nonexistent_headers()
170170
]), 'The last element is without a newline');
171171
}
172172

173+
public function test_normalizing_headers_key_and_value()
174+
{
175+
$this->SUT = $this->SUT->withHeaders([
176+
"HTTP/1.1 401 Authorization Required\r\n" => "\r\n",
177+
"cache-control\r\n" => " no-cache, no-store, must-revalidate, pre-check=0, post-check=0\r\n",
178+
"x-xss-protection\r\n" => "0 \r\n"
179+
]);
180+
181+
$this->assertSame([
182+
'Http/1.1 401 authorization required' => [''],
183+
'Cache-Control' => ['no-cache, no-store, must-revalidate, pre-check=0, post-check=0'],
184+
'X-Xss-Protection' => ['0']
185+
], $this->SUT->getHeaders());
186+
}
187+
173188
protected function setUp()
174189
{
175190
$this->SUT = new MockHttpHeader;

0 commit comments

Comments
 (0)