- md5
- f494625c94547f268f4844933946b1ca
- sha1
- b0190c999354c146255c0c435aef21ef026438d8
- sha256
- 28dc4c26cf9a320ba3c2e269737ec1604898b9a62a3953eec20720956af2aa8d
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Internal;
use Symfony\Component\HttpClient\Exception\TransportException;
/**
* A pure PHP alternative to the native "dechunk" stream filter.
*
* @author Nicolas Grekas <[email protected]>
*
* @internal
*/
final class Dechunker
{
private const STATE_SIZE = 0;
private const STATE_SIZE_EXT = 1;
private const STATE_SIZE_LF = 2;
private const STATE_DATA = 3;
private const STATE_DATA_CR = 4;
private const STATE_DATA_LF = 5;
private const STATE_TRAILER = 6;
private int $state = self::STATE_SIZE;
private string $size = '';
private int $remaining = 0;
public function isFinished(): bool
{
return self::STATE_TRAILER === $this->state;
}
/**
* @throws TransportException When the chunked encoding is malformed
*/
public function dechunk(string $data): string
{
$out = '';
$offset = 0;
$len = \strlen($data);
while ($offset < $len) {
switch ($this->state) {
case self::STATE_SIZE:
if (0 < $spn = strspn($data, '0123456789ABCDEFabcdef', $offset)) {
$this->size = ltrim($this->size.substr($data, $offset, $spn), '0') ?: '0';
$offset += $spn;
if (2 * \PHP_INT_SIZE - 1 < \strlen($this->size)) {
throw new TransportException('Malformed chunked body: chunk size is too big.');
}
break;
}
if ('' === $this->size) {
throw new TransportException('Malformed chunked body: invalid chunk size.');
}
$this->state = self::STATE_SIZE_EXT;
// no break
case self::STATE_SIZE_EXT:
if ($len === $offset += strcspn($data, "\r\n", $offset)) {
break;
}
if ("\r" === $data[$offset]) {
++$offset;
}
$this->state = self::STATE_SIZE_LF;
break;
case self::STATE_SIZE_LF:
if ("\n" !== $data[$offset]) {
throw new TransportException('Malformed chunked body: invalid line ending after chunk size.');
}
++$offset;
$this->state = ($this->remaining = (int) hexdec($this->size)) ? self::STATE_DATA : self::STATE_TRAILER;
$this->size = '';
break;
case self::STATE_DATA:
$out .= $chunk = substr($data, $offset, $this->remaining);
$offset += \strlen($chunk);
if (0 === $this->remaining -= \strlen($chunk)) {
$this->state = self::STATE_DATA_CR;
}
break;
case self::STATE_DATA_CR:
if ("\r" === $data[$offset]) {
++$offset;
}
$this->state = self::STATE_DATA_LF;
break;
case self::STATE_DATA_LF:
if ("\n" !== $data[$offset]) {
throw new TransportException('Malformed chunked body: invalid line ending after chunk data.');
}
++$offset;
$this->state = self::STATE_SIZE;
break;
case self::STATE_TRAILER:
// Trailer fields and anything else after the terminal chunk are ignored
return $out;
}
}
return $out;
}
}
The same content is in 4 releases, Joomla 5.4.11 to 6.3.0-alpha1. See them all, or every version of this file.