75 lines
1.8 KiB
PHP
75 lines
1.8 KiB
PHP
|
<?php
|
||
|
// ContentRangeHeader.php
|
||
|
// Created: 2022-02-14
|
||
|
// Updated: 2022-02-27
|
||
|
|
||
|
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 !== '*')
|
||
|
$size = max(0, intval($size));
|
||
|
else
|
||
|
$size = -1;
|
||
|
|
||
|
$range = trim($parts[0]);
|
||
|
|
||
|
if($range !== '*') {
|
||
|
$parts = explode('-', $range, 2);
|
||
|
$rangeStart = intval(trim($parts[0]));
|
||
|
$rangeEnd = intval(trim($parts[1] ?? '0'));
|
||
|
} else
|
||
|
$rangeStart = $rangeEnd = -1;
|
||
|
|
||
|
return new ContentRangeHeader($unit, $rangeStart, $rangeEnd, $size);
|
||
|
}
|
||
|
}
|