From 8a9949521f7e3866976cd9df419965ca93de2a0c Mon Sep 17 00:00:00 2001 From: Mike Gladysch Date: Fri, 13 Jan 2023 15:29:48 +0100 Subject: [PATCH] [HttpFoundation] Fix memory limit problems in BinaryFileResponse --- .../HttpFoundation/BinaryFileResponse.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php index 6d7b80ad1212c..3d0f92d02b9d4 100644 --- a/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php +++ b/src/Symfony/Component/HttpFoundation/BinaryFileResponse.php @@ -34,7 +34,7 @@ class BinaryFileResponse extends Response protected $offset = 0; protected $maxlen = -1; protected $deleteFileAfterSend = false; - protected $chunkSize = 8 * 1024; + protected $chunkSize = 16 * 1024; /** * @param \SplFileInfo|string $file The file to stream @@ -267,7 +267,7 @@ public function prepare(Request $request) $range = $request->headers->get('Range'); if (str_starts_with($range, 'bytes=')) { - [$start, $end] = explode('-', substr($range, 6), 2) + [0]; + [$start, $end] = explode('-', substr($range, 6), 2) + [1 => 0]; $end = ('' === $end) ? $fileSize - 1 : (int) $end; @@ -341,14 +341,15 @@ public function sendContent() $length = $this->maxlen; while ($length && !feof($file)) { - $read = ($length > $this->chunkSize) ? $this->chunkSize : $length; - $length -= $read; + $read = $length > $this->chunkSize || 0 > $length ? $this->chunkSize : $length; + $read = stream_copy_to_stream($file, $out, $read); - stream_copy_to_stream($file, $out, $read); - - if (connection_aborted()) { + if (false === $read || connection_aborted()) { break; } + if (0 < $length) { + $length -= $read; + } } fclose($out);