From 2b500d0798806e0fad62cd9a27dda42fa183f2b7 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 11 Dec 2025 18:30:55 +0530 Subject: [PATCH] Fix array to string conversion warning in Request::getSize() Headers can now be arrays (after recent changes allowing array headers). The getSize() method was attempting to directly implode headers, causing a warning when a header value was an array. This fix properly handles both string and array header values by joining array values with commas (standard HTTP header format) before calculating the request size. Added test case to verify the fix works correctly with array headers. --- src/Http/Request.php | 11 ++++++++++- tests/RequestTest.php | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Http/Request.php b/src/Http/Request.php index 8332d99d..c9762123 100755 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -332,7 +332,16 @@ abstract public function removeHeader(string $key): static; */ public function getSize(): int { - return \mb_strlen(\implode("\n", $this->generateHeaders()), '8bit') + \mb_strlen(\file_get_contents('php://input'), '8bit'); + $headers = $this->generateHeaders(); + $headerStrings = []; + foreach ($headers as $key => $value) { + if (\is_array($value)) { + $headerStrings[] = $key . ': ' . \implode(', ', $value); + } else { + $headerStrings[] = $key . ': ' . $value; + } + } + return \mb_strlen(\implode("\n", $headerStrings), '8bit') + \mb_strlen(\file_get_contents('php://input'), '8bit'); } /** diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 55d49ee9..02d8930a 100755 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -313,4 +313,23 @@ public function testCanGetRange() $this->assertEquals(null, $this->request->getRangeStart()); $this->assertEquals(null, $this->request->getRangeEnd()); } + + public function testCanGetSizeWithArrayHeaders() + { + $this->request->addHeader('content-type', 'application/json'); + + $reflection = new \ReflectionClass($this->request); + $headersProperty = $reflection->getProperty('headers'); + $headersProperty->setAccessible(true); + + $headers = $headersProperty->getValue($this->request) ?? []; + $headers['accept'] = ['application/json', 'text/html']; + $headers['x-custom'] = ['value1', 'value2', 'value3']; + $headersProperty->setValue($this->request, $headers); + + $size = $this->request->getSize(); + + $this->assertIsInt($size); + $this->assertGreaterThan(0, $size); + } }