Skip to content

Commit

Permalink
Allow empty headers
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Mar 6, 2024
1 parent 4c837d6 commit c3f5229
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Http/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function pull(): MessageInterface
foreach ($headers as $header) {
$parts = explode(':', $header, 2);
if (count($parts) == 2) {
if (empty($message->getheaderLine($parts[0]))) {
if ($message->getheaderLine($parts[0]) === '') {
$message = $message->withHeader($parts[0], trim($parts[1]));
} else {
$message = $message->withAddedHeader($parts[0], trim($parts[1]));
Expand All @@ -92,7 +92,7 @@ public function pull(): MessageInterface
}
if ($message instanceof Request) {
$uri = new Uri("//{$message->getHeaderLine('Host')}{$path}");
$message = $message->withUri($uri);
$message = $message->withUri($uri, true);
}

return $message;
Expand Down
7 changes: 0 additions & 7 deletions src/Http/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,15 @@ public function getAsArray(): array

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 = 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.");
}
$this->headers[strtolower($name)][$name][] = $content;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/suites/http/HttpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,14 @@ public function testPullServerRequest(): void
$this->assertInstanceOf(HttpHandler::class, $handler);

$this->expectSocketStreamReadLine()->setReturn(function () {
return "GET /a/path?a=b HTTP/1.1\r\nHost: test.com:123\r\n\r\n";
return "GET /a/path?a=b HTTP/1.1\r\nA: \r\nA: 0\r\nA: B\r\nHost: test.com:123\r\n\r\n";
});
$request = $handler->pull();
$this->assertInstanceOf(ServerRequest::class, $request);
$this->assertEquals('/a/path?a=b', $request->getRequestTarget());
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('1.1', $request->getProtocolVersion());
$this->assertEquals(['Host' => ['test.com:123']], $request->getHeaders());
$this->assertEquals(['Host' => ['test.com:123'], 'A' => ['0', 'B']], $request->getHeaders());
$this->assertTrue($request->hasHeader('Host'));
$uri = $request->getUri();
$this->assertInstanceOf(UriInterface::class, $uri);
Expand Down
8 changes: 4 additions & 4 deletions tests/suites/http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,8 @@ public function testHeaderValueInvalidVariants(mixed $value): void

public static function provideInvalidHeaderValues(): Generator
{
yield [''];
yield [' '];
yield [['0', '']];
yield [[null]];
yield [[[0]]];
yield [[]];
}

/**
Expand All @@ -262,6 +258,10 @@ public function testHeaderValueValidVariants(mixed $value, array $expected): voi

public static function provideValidHeaderValues(): Generator
{
yield ['', ['']];
yield [' ', ['']];
yield [['0', ''], ['0', '']];
yield [[], []];
yield ['null', ['null']];
yield ['0 ', ['0']];
yield [' 0', ['0']];
Expand Down
11 changes: 1 addition & 10 deletions tests/suites/http/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,12 @@ public function testWithBodyError(): void
$response->withBody($factory->createStream());
}

public function testHaederNameError(): void
public function testHeaderNameError(): void
{
$response = new Response();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage("'.' is not a valid header field name.");
$response->withHeader('.', 'invaid name');
}

public function testHaederValueError(): void
{
$response = new Response();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage("Invalid header value(s) provided.");
$response->withHeader('name', '');
}
}

0 comments on commit c3f5229

Please sign in to comment.