3 namespace BookStack\Http;
5 use BookStack\Util\WebSafeMimeSniffer;
6 use Illuminate\Http\Request;
9 * Helper wrapper for range-based stream response handling.
10 * Much of this used symfony/http-foundation as a reference during build.
11 * URL: https://github.com/symfony/http-foundation/blob/v6.0.20/BinaryFileResponse.php
12 * License: MIT license, Copyright (c) Fabien Potencier.
14 class RangeSupportedStream
16 protected string $sniffContent = '';
17 protected array $responseHeaders = [];
18 protected int $responseStatus = 200;
20 protected int $responseLength = 0;
21 protected int $responseOffset = 0;
23 public function __construct(
25 protected int $fileSize,
28 $this->responseLength = $this->fileSize;
29 $this->parseRequest($request);
33 * Sniff a mime type from the stream.
35 public function sniffMime(string $extension = ''): string
37 $offset = min(2000, $this->fileSize);
38 $this->sniffContent = fread($this->stream, $offset);
40 return (new WebSafeMimeSniffer())->sniff($this->sniffContent, $extension);
44 * Output the current stream to stdout before closing out the stream.
46 public function outputAndClose(): void
48 // End & flush the output buffer, if we're in one, otherwise we still use memory.
49 // Output buffer may or may not exist depending on PHP `output_buffering` setting.
50 // Ignore in testing since output buffers are used to gather a response.
51 if (!empty(ob_get_status()) && !app()->runningUnitTests()) {
55 $outStream = fopen('php://output', 'w');
56 $sniffLength = strlen($this->sniffContent);
57 $bytesToWrite = $this->responseLength;
59 if ($sniffLength > 0 && $this->responseOffset < $sniffLength) {
60 $sniffEnd = min($sniffLength, $bytesToWrite + $this->responseOffset);
61 $sniffOutLength = $sniffEnd - $this->responseOffset;
62 $sniffOutput = substr($this->sniffContent, $this->responseOffset, $sniffOutLength);
63 fwrite($outStream, $sniffOutput);
64 $bytesToWrite -= $sniffOutLength;
65 } else if ($this->responseOffset !== 0) {
66 fseek($this->stream, $this->responseOffset);
69 stream_copy_to_stream($this->stream, $outStream, $bytesToWrite);
71 fclose($this->stream);
75 public function getResponseHeaders(): array
77 return $this->responseHeaders;
80 public function getResponseStatus(): int
82 return $this->responseStatus;
85 protected function parseRequest(Request $request): void
87 $this->responseHeaders['Accept-Ranges'] = $request->isMethodSafe() ? 'bytes' : 'none';
89 $range = $this->getRangeFromRequest($request);
91 [$start, $end] = $range;
92 if ($start < 0 || $start > $end) {
93 $this->responseStatus = 416;
94 $this->responseHeaders['Content-Range'] = sprintf('bytes */%s', $this->fileSize);
96 $this->responseLength = $end < $this->fileSize ? $end - $start + 1 : -1;
97 $this->responseOffset = $start;
98 $this->responseStatus = 206;
99 $this->responseHeaders['Content-Range'] = sprintf('bytes %s-%s/%s', $start, $end, $this->fileSize);
100 $this->responseHeaders['Content-Length'] = $end - $start + 1;
104 if ($request->isMethod('HEAD')) {
105 $this->responseLength = 0;
109 protected function getRangeFromRequest(Request $request): ?array
111 $range = $request->headers->get('Range');
112 if (!$range || !$request->isMethod('GET') || !str_starts_with($range, 'bytes=')) {
116 if ($request->headers->has('If-Range')) {
120 [$start, $end] = explode('-', substr($range, 6), 2) + [0];
122 $end = ('' === $end) ? $this->fileSize - 1 : (int) $end;
125 $start = $this->fileSize - $end;
126 $end = $this->fileSize - 1;
128 $start = (int) $start;
131 $end = min($end, $this->fileSize - 1);
132 return [$start, $end];