From c3f52295bb5b388882ddee8bf72eef857cac4988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Jensen?= Date: Wed, 6 Mar 2024 22:22:05 +0100 Subject: [PATCH] Allow empty headers --- src/Http/HttpHandler.php | 4 ++-- src/Http/Message.php | 7 ------- tests/suites/http/HttpHandlerTest.php | 4 ++-- tests/suites/http/RequestTest.php | 8 ++++---- tests/suites/http/ResponseTest.php | 11 +---------- 5 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/Http/HttpHandler.php b/src/Http/HttpHandler.php index 39f7402..2717922 100644 --- a/src/Http/HttpHandler.php +++ b/src/Http/HttpHandler.php @@ -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])); @@ -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; diff --git a/src/Http/Message.php b/src/Http/Message.php index cd20e3f..67b1e9e 100644 --- a/src/Http/Message.php +++ b/src/Http/Message.php @@ -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; } } diff --git a/tests/suites/http/HttpHandlerTest.php b/tests/suites/http/HttpHandlerTest.php index 03ce93e..74e8e15 100644 --- a/tests/suites/http/HttpHandlerTest.php +++ b/tests/suites/http/HttpHandlerTest.php @@ -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); diff --git a/tests/suites/http/RequestTest.php b/tests/suites/http/RequestTest.php index 3b5c0fa..547cd28 100644 --- a/tests/suites/http/RequestTest.php +++ b/tests/suites/http/RequestTest.php @@ -240,12 +240,8 @@ public function testHeaderValueInvalidVariants(mixed $value): void public static function provideInvalidHeaderValues(): Generator { - yield ['']; - yield [' ']; - yield [['0', '']]; yield [[null]]; yield [[[0]]]; - yield [[]]; } /** @@ -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']]; diff --git a/tests/suites/http/ResponseTest.php b/tests/suites/http/ResponseTest.php index 3e1e29c..d1ae3f8 100644 --- a/tests/suites/http/ResponseTest.php +++ b/tests/suites/http/ResponseTest.php @@ -117,7 +117,7 @@ public function testWithBodyError(): void $response->withBody($factory->createStream()); } - public function testHaederNameError(): void + public function testHeaderNameError(): void { $response = new Response(); $this->expectException(InvalidArgumentException::class); @@ -125,13 +125,4 @@ public function testHaederNameError(): void $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', ''); - } }