Skip to content

Commit

Permalink
Additional header checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jan 9, 2024
1 parent 78f61b2 commit e171655
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
### `2.1.1`

* Fix for HTTP headers (@zgrguric, @sirn-se)
* Fix issue with falsy but valid HTTP headers (@zgrguric)
* Additional check for HTTP headers (@sirn-se)

### `2.1.0`

Expand Down
13 changes: 7 additions & 6 deletions src/Http/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function getHeaderLine(string $name): string
* @return static
* @throws \InvalidArgumentException for invalid header names or values.
*/
public function withHeader(string $name, $value): self
public function withHeader(string $name, mixed $value): self
{
$new = clone $this;
if ($this->hasHeader($name)) {
Expand All @@ -116,7 +116,7 @@ public function withHeader(string $name, $value): self
* @throws \InvalidArgumentException for invalid header names.
* @throws \InvalidArgumentException for invalid header values.
*/
public function withAddedHeader(string $name, $value): self
public function withAddedHeader(string $name, mixed $value): self
{
$new = clone $this;
$new->handleHeader($name, $value);
Expand Down Expand Up @@ -164,19 +164,20 @@ public function getAsArray(): array
return $lines;
}

private function handleHeader(string $name, string|array $value): void
private function handleHeader(string $name, mixed $value): void
{
// @todo: Add all available characters, these are just some of them.
if (!preg_match('|^[0-9a-zA-Z#_-]+$|', $name)) {
throw new InvalidArgumentException("'{$name}' is not a valid header field name.");
}
$value = array_map(function (string|array $item) {
return trim($item);
}, is_array($value) ? $value : [$value]);
$value = is_array($value) ? $value : [$value];
if (empty($value)) {
throw new InvalidArgumentException("Invalid header value(s) provided.");
}
foreach ($value as $content) {
if (!is_string($content) && !is_numeric($content)) {
throw new InvalidArgumentException("Invalid header value(s) provided.");
}
$content = trim($content);
if ('' === $content) {
throw new InvalidArgumentException("Invalid header value(s) provided.");
Expand Down
3 changes: 3 additions & 0 deletions tests/suites/http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ public static function provideInvalidHeaderValues(): Generator
yield [''];
yield [' '];
yield [['0', '']];
yield [[null]];
yield [[[0]]];
yield [[]];
}

Expand All @@ -262,5 +264,6 @@ public static function provideValidHeaderValues(): Generator
yield ['0 ', ['name' => ['0']]];
yield [' 0', ['name' => ['0']]];
yield [['0', '1'], ['name' => ['0', '1']]];
yield [0, ['name' => ['0']]];
}
}

0 comments on commit e171655

Please sign in to comment.