2022-09-13 13:13:11 +00:00
|
|
|
<?php
|
|
|
|
// ContentRangeHeader.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 Index\Http\HttpHeader;
|
|
|
|
|
|
|
|
class ContentRangeHeader {
|
|
|
|
private string $unit;
|
|
|
|
private int $rangeStart = -1;
|
|
|
|
private int $rangeEnd = -1;
|
|
|
|
private int $size = -1;
|
|
|
|
|
|
|
|
public function __construct(string $unit, int $rangeStart, int $rangeEnd, int $size) {
|
|
|
|
$this->unit = $unit;
|
|
|
|
$this->rangeStart = $rangeStart;
|
|
|
|
$this->rangeEnd = $rangeEnd;
|
|
|
|
$this->size = $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUnit(): string {
|
|
|
|
return $this->unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isBytes(): bool {
|
|
|
|
return $this->unit === 'bytes';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSize(): int {
|
|
|
|
return $this->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isUnknownSize(): bool {
|
|
|
|
return $this->size < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isUnspecifiedRange(): bool {
|
|
|
|
return $this->rangeStart < 0 || $this->rangeEnd < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRangeStart(): int {
|
|
|
|
return $this->rangeStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRangeEnd(): int {
|
|
|
|
return $this->rangeEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function parse(HttpHeader $header): ContentRangeHeader {
|
|
|
|
$parts = explode(' ', trim($header->getFirstLine()), 2);
|
|
|
|
$unit = array_shift($parts);
|
|
|
|
|
|
|
|
$parts = explode('/', $parts[1] ?? '', 2);
|
|
|
|
|
|
|
|
$size = trim($parts[1] ?? '*');
|
|
|
|
if($size !== '*')
|
2023-01-01 19:00:00 +00:00
|
|
|
$size = max(0, (int)$size);
|
2022-09-13 13:13:11 +00:00
|
|
|
else
|
|
|
|
$size = -1;
|
|
|
|
|
|
|
|
$range = trim($parts[0]);
|
|
|
|
|
|
|
|
if($range !== '*') {
|
|
|
|
$parts = explode('-', $range, 2);
|
2023-01-01 19:00:00 +00:00
|
|
|
$rangeStart = (int)trim($parts[0]);
|
|
|
|
$rangeEnd = (int)trim($parts[1] ?? '0');
|
2022-09-13 13:13:11 +00:00
|
|
|
} else
|
|
|
|
$rangeStart = $rangeEnd = -1;
|
|
|
|
|
|
|
|
return new ContentRangeHeader($unit, $rangeStart, $rangeEnd, $size);
|
|
|
|
}
|
|
|
|
}
|