Range requests: Added basic HTTP range support

This commit is contained in:
Dan Brown 2024-01-07 20:34:03 +00:00
parent b4d9029dc3
commit d94762549a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 103 additions and 20 deletions

View File

@ -2,7 +2,6 @@
namespace BookStack\Http; namespace BookStack\Http;
use BookStack\Util\WebSafeMimeSniffer;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpFoundation\StreamedResponse;
@ -19,7 +18,7 @@ class DownloadResponseFactory
*/ */
public function directly(string $content, string $fileName): Response public function directly(string $content, string $fileName): Response
{ {
return response()->make($content, 200, $this->getHeaders($fileName)); return response()->make($content, 200, $this->getHeaders($fileName, strlen($content)));
} }
/** /**
@ -27,10 +26,13 @@ class DownloadResponseFactory
*/ */
public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse public function streamedDirectly($stream, string $fileName, int $fileSize): StreamedResponse
{ {
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers); $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request);
return response()->stream(function () use ($rangeStream) { $headers = array_merge($this->getHeaders($fileName, $fileSize), $rangeStream->getResponseHeaders());
$rangeStream->outputAndClose(); return response()->stream(
}, 200, $this->getHeaders($fileName)); fn() => $rangeStream->outputAndClose(),
$rangeStream->getResponseStatus(),
$headers,
);
} }
/** /**
@ -40,24 +42,28 @@ class DownloadResponseFactory
*/ */
public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse public function streamedInline($stream, string $fileName, int $fileSize): StreamedResponse
{ {
$rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request->headers); $rangeStream = new RangeSupportedStream($stream, $fileSize, $this->request);
$mime = $rangeStream->sniffMime(); $mime = $rangeStream->sniffMime();
$headers = array_merge($this->getHeaders($fileName, $fileSize, $mime), $rangeStream->getResponseHeaders());
return response()->stream(function () use ($rangeStream) { return response()->stream(
$rangeStream->outputAndClose(); fn() => $rangeStream->outputAndClose(),
}, 200, $this->getHeaders($fileName, $mime)); $rangeStream->getResponseStatus(),
$headers,
);
} }
/** /**
* Get the common headers to provide for a download response. * Get the common headers to provide for a download response.
*/ */
protected function getHeaders(string $fileName, string $mime = 'application/octet-stream'): array protected function getHeaders(string $fileName, int $fileSize, string $mime = 'application/octet-stream'): array
{ {
$disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline'; $disposition = ($mime === 'application/octet-stream') ? 'attachment' : 'inline';
$downloadName = str_replace('"', '', $fileName); $downloadName = str_replace('"', '', $fileName);
return [ return [
'Content-Type' => $mime, 'Content-Type' => $mime,
'Content-Length' => $fileSize,
'Content-Disposition' => "{$disposition}; filename=\"{$downloadName}\"", 'Content-Disposition' => "{$disposition}; filename=\"{$downloadName}\"",
'X-Content-Type-Options' => 'nosniff', 'X-Content-Type-Options' => 'nosniff',
]; ];

View File

@ -3,17 +3,30 @@
namespace BookStack\Http; namespace BookStack\Http;
use BookStack\Util\WebSafeMimeSniffer; use BookStack\Util\WebSafeMimeSniffer;
use Symfony\Component\HttpFoundation\HeaderBag; use Illuminate\Http\Request;
/**
* Helper wrapper for range-based stream response handling.
* Much of this used symfony/http-foundation as a reference during build.
* URL: https://github.com/symfony/http-foundation/blob/v6.0.20/BinaryFileResponse.php
* License: MIT license, Copyright (c) Fabien Potencier.
*/
class RangeSupportedStream class RangeSupportedStream
{ {
protected string $sniffContent; protected string $sniffContent;
protected array $responseHeaders;
protected int $responseStatus = 200;
protected int $responseLength = 0;
protected int $responseOffset = 0;
public function __construct( public function __construct(
protected $stream, protected $stream,
protected int $fileSize, protected int $fileSize,
protected HeaderBag $requestHeaders, Request $request,
) { ) {
$this->responseLength = $this->fileSize;
$this->parseRequest($request);
} }
/** /**
@ -40,18 +53,82 @@ class RangeSupportedStream
} }
$outStream = fopen('php://output', 'w'); $outStream = fopen('php://output', 'w');
$offset = 0; $sniffOffset = strlen($this->sniffContent);
if (!empty($this->sniffContent)) { if (!empty($this->sniffContent) && $this->responseOffset < $sniffOffset) {
fwrite($outStream, $this->sniffContent); $sniffOutput = substr($this->sniffContent, $this->responseOffset, min($sniffOffset, $this->responseLength));
$offset = strlen($this->sniffContent); fwrite($outStream, $sniffOutput);
} else if ($this->responseOffset !== 0) {
fseek($this->stream, $this->responseOffset);
} }
$toWrite = $this->fileSize - $offset; stream_copy_to_stream($this->stream, $outStream, $this->responseLength);
stream_copy_to_stream($this->stream, $outStream, $toWrite);
fpassthru($this->stream);
fclose($this->stream); fclose($this->stream);
fclose($outStream); fclose($outStream);
} }
public function getResponseHeaders(): array
{
return $this->responseHeaders;
}
public function getResponseStatus(): int
{
return $this->responseStatus;
}
protected function parseRequest(Request $request): void
{
$this->responseHeaders['Accept-Ranges'] = $request->isMethodSafe() ? 'bytes' : 'none';
$range = $this->getRangeFromRequest($request);
if ($range) {
[$start, $end] = $range;
if ($start < 0 || $start > $end) {
$this->responseStatus = 416;
$this->responseHeaders['Content-Range'] = sprintf('bytes */%s', $this->fileSize);
} elseif ($end - $start < $this->fileSize - 1) {
$this->responseLength = $end < $this->fileSize ? $end - $start + 1 : -1;
$this->responseOffset = $start;
$this->responseStatus = 206;
$this->responseHeaders['Content-Range'] = sprintf('bytes %s-%s/%s', $start, $end, $this->fileSize);
$this->responseHeaders['Content-Length'] = $end - $start + 1;
}
}
if ($request->isMethod('HEAD')) {
$this->responseLength = 0;
}
}
protected function getRangeFromRequest(Request $request): ?array
{
$range = $request->headers->get('Range');
if (!$range || !$request->isMethod('GET') || !str_starts_with($range, 'bytes=')) {
return null;
}
if ($request->headers->has('If-Range')) {
return null;
}
[$start, $end] = explode('-', substr($range, 6), 2) + [0];
$end = ('' === $end) ? $this->fileSize - 1 : (int) $end;
if ('' === $start) {
$start = $this->fileSize - $end;
$end = $this->fileSize - 1;
} else {
$start = (int) $start;
}
if ($start > $end) {
return null;
}
$end = min($end, $this->fileSize - 1);
return [$start, $end];
}
} }