Skip to content

Commit 2b500d0

Browse files
committed
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.
1 parent 334b99d commit 2b500d0

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Http/Request.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,16 @@ abstract public function removeHeader(string $key): static;
332332
*/
333333
public function getSize(): int
334334
{
335-
return \mb_strlen(\implode("\n", $this->generateHeaders()), '8bit') + \mb_strlen(\file_get_contents('php://input'), '8bit');
335+
$headers = $this->generateHeaders();
336+
$headerStrings = [];
337+
foreach ($headers as $key => $value) {
338+
if (\is_array($value)) {
339+
$headerStrings[] = $key . ': ' . \implode(', ', $value);
340+
} else {
341+
$headerStrings[] = $key . ': ' . $value;
342+
}
343+
}
344+
return \mb_strlen(\implode("\n", $headerStrings), '8bit') + \mb_strlen(\file_get_contents('php://input'), '8bit');
336345
}
337346

338347
/**

tests/RequestTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,23 @@ public function testCanGetRange()
313313
$this->assertEquals(null, $this->request->getRangeStart());
314314
$this->assertEquals(null, $this->request->getRangeEnd());
315315
}
316+
317+
public function testCanGetSizeWithArrayHeaders()
318+
{
319+
$this->request->addHeader('content-type', 'application/json');
320+
321+
$reflection = new \ReflectionClass($this->request);
322+
$headersProperty = $reflection->getProperty('headers');
323+
$headersProperty->setAccessible(true);
324+
325+
$headers = $headersProperty->getValue($this->request) ?? [];
326+
$headers['accept'] = ['application/json', 'text/html'];
327+
$headers['x-custom'] = ['value1', 'value2', 'value3'];
328+
$headersProperty->setValue($this->request, $headers);
329+
330+
$size = $this->request->getSize();
331+
332+
$this->assertIsInt($size);
333+
$this->assertGreaterThan(0, $size);
334+
}
316335
}

0 commit comments

Comments
 (0)