2022-09-13 13:13:11 +00:00
|
|
|
<?php
|
|
|
|
// RangeHeader.php
|
|
|
|
// Created: 2022-02-14
|
2023-01-01 19:00:00 +00:00
|
|
|
// Updated: 2023-01-01
|
2022-09-13 13:13:11 +00:00
|
|
|
|
|
|
|
namespace Index\Http\Headers;
|
|
|
|
|
|
|
|
use stdClass;
|
|
|
|
use Index\Http\HttpHeader;
|
|
|
|
|
|
|
|
class RangeHeader {
|
|
|
|
private string $unit;
|
|
|
|
private array $ranges;
|
|
|
|
|
|
|
|
public function __construct(string $unit, array $ranges) {
|
|
|
|
$this->unit = $unit;
|
|
|
|
$this->ranges = $ranges;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUnit(): string {
|
|
|
|
return $this->unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isBytes(): bool {
|
|
|
|
return $this->unit === 'bytes';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRanges(): array {
|
|
|
|
return $this->ranges;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function parse(HttpHeader $header): RangeHeader {
|
|
|
|
$parts = explode('=', trim($header->getFirstLine()), 2);
|
|
|
|
$unit = trim($parts[0]);
|
|
|
|
|
|
|
|
$ranges = [];
|
|
|
|
$rawRanges = explode(',', $parts[1] ?? '', 2);
|
|
|
|
|
|
|
|
foreach($rawRanges as $raw) {
|
|
|
|
$raw = explode('-', trim($raw), 2);
|
|
|
|
$start = trim($raw[0]);
|
|
|
|
$end = trim($raw[1] ?? '');
|
|
|
|
|
|
|
|
$ranges[] = $range = new stdClass;
|
|
|
|
|
|
|
|
if($start === '' && is_numeric($end)) {
|
|
|
|
$range->type = 'suffix-length';
|
2023-01-01 19:00:00 +00:00
|
|
|
$range->length = (int)$end;
|
2022-09-13 13:13:11 +00:00
|
|
|
} elseif(is_numeric($start) && $end === '') {
|
|
|
|
$range->type = 'start';
|
2023-01-01 19:00:00 +00:00
|
|
|
$range->start = (int)$start;
|
2022-09-13 13:13:11 +00:00
|
|
|
} else {
|
|
|
|
$range->type = 'range';
|
2023-01-01 19:00:00 +00:00
|
|
|
$range->start = (int)$start;
|
|
|
|
$range->end = (int)$end;
|
2022-09-13 13:13:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new RangeHeader($unit, $ranges);
|
|
|
|
}
|
|
|
|
}
|