Skip to content

Commit 2c652b6

Browse files
authored
Merge pull request #195 from utopia-php/fix-array-header-size-calculation
2 parents 334b99d + 2b500d0 commit 2c652b6

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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)