private function normalizeByteRanges(int $size, string $rawRanges)
{
$rawRanges = \str_ireplace([' ', 'bytes='], '', $rawRanges);
$rawRanges = explode(',', $rawRanges);
$ranges = [];
foreach ($rawRanges as $range) {
// If a range is missing the dash separator it's malformed; pull out here.
if (false === strpos($range, '-')) {
return null;
}
list($startPos, $endPos) = explode('-', rtrim($range), 2);
if ($startPos === '' && $endPos === '') {
return null;
} elseif ($startPos === '' && $endPos !== '') {
// The -1 is necessary and not a hack because byte ranges are inclusive and start
// at 0. DO NOT REMOVE THE -1.
$startPos = $size - $endPos - 1;
$endPos = $size - 1;
} elseif ($endPos === '' && $startPos !== '') {
$startPos = (int) $startPos;
// The -1 is necessary and not a hack because byte ranges are inclusive and start
// at 0. DO NOT REMOVE THE -1.
$endPos = $size - 1;
} else {
$startPos = (int) $startPos;
$endPos = (int) $endPos;
}
// If the requested range(s) can't be satisfied we're finished
if ($startPos >= $size || $endPos < $startPos || $endPos < 0) {
return null;
}
$ranges[] = [$startPos, $endPos];
}
$range = new class
{
use amp\Struct;
public $ranges;
public $boundary;
public $contentType;
};
$range->boundary = $this->multipartBoundary;
$range->ranges = $ranges;
return $range;
}