1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Range requests: Added test cases to cover functionality

Fixed some found issues in the process.
This commit is contained in:
Dan Brown
2024-01-14 15:50:00 +00:00
parent d94762549a
commit 91d8d6eaaa
2 changed files with 111 additions and 10 deletions

View File

@ -13,8 +13,8 @@ use Illuminate\Http\Request;
*/
class RangeSupportedStream
{
protected string $sniffContent;
protected array $responseHeaders;
protected string $sniffContent = '';
protected array $responseHeaders = [];
protected int $responseStatus = 200;
protected int $responseLength = 0;
@ -53,16 +53,20 @@ class RangeSupportedStream
}
$outStream = fopen('php://output', 'w');
$sniffOffset = strlen($this->sniffContent);
$sniffLength = strlen($this->sniffContent);
$bytesToWrite = $this->responseLength;
if (!empty($this->sniffContent) && $this->responseOffset < $sniffOffset) {
$sniffOutput = substr($this->sniffContent, $this->responseOffset, min($sniffOffset, $this->responseLength));
if ($sniffLength > 0 && $this->responseOffset < $sniffLength) {
$sniffEnd = min($sniffLength, $bytesToWrite + $this->responseOffset);
$sniffOutLength = $sniffEnd - $this->responseOffset;
$sniffOutput = substr($this->sniffContent, $this->responseOffset, $sniffOutLength);
fwrite($outStream, $sniffOutput);
$bytesToWrite -= $sniffOutLength;
} else if ($this->responseOffset !== 0) {
fseek($this->stream, $this->responseOffset);
}
stream_copy_to_stream($this->stream, $outStream, $this->responseLength);
stream_copy_to_stream($this->stream, $outStream, $bytesToWrite);
fclose($this->stream);
fclose($outStream);
@ -124,10 +128,6 @@ class RangeSupportedStream
$start = (int) $start;
}
if ($start > $end) {
return null;
}
$end = min($end, $this->fileSize - 1);
return [$start, $end];
}