]> source.dussan.org Git - nextcloud-server.git/commitdiff
Detect aborted connection in OC\Files\View and stop writing data to the output buffer 40285/head
authorBenjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
Wed, 6 Sep 2023 07:16:55 +0000 (09:16 +0200)
committerCôme Chilliet <91878298+come-nc@users.noreply.github.com>
Thu, 7 Sep 2023 07:32:32 +0000 (09:32 +0200)
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Files/View.php
lib/private/legacy/OC_Files.php
lib/public/Files/ConnectionLostException.php [new file with mode: 0644]

index 9bdd0bdc55ff69c71728ab1d83cbf59107d830ff..61cc6ec0cd96add8f3506c54e88c8794b6acf5f9 100644 (file)
@@ -302,6 +302,7 @@ return array(
     'OCP\\Files\\Config\\IMountProviderCollection' => $baseDir . '/lib/public/Files/Config/IMountProviderCollection.php',
     'OCP\\Files\\Config\\IRootMountProvider' => $baseDir . '/lib/public/Files/Config/IRootMountProvider.php',
     'OCP\\Files\\Config\\IUserMountCache' => $baseDir . '/lib/public/Files/Config/IUserMountCache.php',
+    'OCP\\Files\\ConnectionLostException' => $baseDir . '/lib/public/Files/ConnectionLostException.php',
     'OCP\\Files\\DavUtil' => $baseDir . '/lib/public/Files/DavUtil.php',
     'OCP\\Files\\EmptyFileNameException' => $baseDir . '/lib/public/Files/EmptyFileNameException.php',
     'OCP\\Files\\EntityTooLargeException' => $baseDir . '/lib/public/Files/EntityTooLargeException.php',
index 2cdd9c8d3ca9cd9233201f58680b33f64b8b9b17..c122625a2e26246c5db832d0e1ee5d7340df1996 100644 (file)
@@ -335,6 +335,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
         'OCP\\Files\\Config\\IMountProviderCollection' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IMountProviderCollection.php',
         'OCP\\Files\\Config\\IRootMountProvider' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IRootMountProvider.php',
         'OCP\\Files\\Config\\IUserMountCache' => __DIR__ . '/../../..' . '/lib/public/Files/Config/IUserMountCache.php',
+        'OCP\\Files\\ConnectionLostException' => __DIR__ . '/../../..' . '/lib/public/Files/ConnectionLostException.php',
         'OCP\\Files\\DavUtil' => __DIR__ . '/../../..' . '/lib/public/Files/DavUtil.php',
         'OCP\\Files\\EmptyFileNameException' => __DIR__ . '/../../..' . '/lib/public/Files/EmptyFileNameException.php',
         'OCP\\Files\\EntityTooLargeException' => __DIR__ . '/../../..' . '/lib/public/Files/EntityTooLargeException.php',
index 718159393100953dc862834128947fd2ac5df136..86651ab3e1a867a5fcb95e0b346fc57123c39352 100644 (file)
@@ -56,6 +56,7 @@ use OC\User\Manager as UserManager;
 use OCA\Files_Sharing\SharedMount;
 use OCP\Constants;
 use OCP\Files\Cache\ICacheEntry;
+use OCP\Files\ConnectionLostException;
 use OCP\Files\EmptyFileNameException;
 use OCP\Files\FileNameTooLongException;
 use OCP\Files\InvalidCharacterInPathException;
@@ -397,10 +398,11 @@ class View {
                }
                $handle = $this->fopen($path, 'rb');
                if ($handle) {
-                       $chunkSize = 524288; // 512 kB chunks
+                       $chunkSize = 524288; // 512 kiB chunks
                        while (!feof($handle)) {
                                echo fread($handle, $chunkSize);
                                flush();
+                               $this->checkConnectionStatus();
                        }
                        fclose($handle);
                        return $this->filesize($path);
@@ -423,7 +425,7 @@ class View {
                }
                $handle = $this->fopen($path, 'rb');
                if ($handle) {
-                       $chunkSize = 524288; // 512 kB chunks
+                       $chunkSize = 524288; // 512 kiB chunks
                        $startReading = true;
 
                        if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) {
@@ -453,6 +455,7 @@ class View {
                                        }
                                        echo fread($handle, $len);
                                        flush();
+                                       $this->checkConnectionStatus();
                                }
                                return ftell($handle) - $from;
                        }
@@ -462,6 +465,13 @@ class View {
                return false;
        }
 
+       private function checkConnectionStatus(): void {
+               $connectionStatus = \connection_status();
+               if ($connectionStatus !== CONNECTION_NORMAL) {
+                       throw new ConnectionLostException("Connection lost. Status: $connectionStatus");
+               }
+       }
+
        /**
         * @param string $path
         * @return mixed
index 7bc1fab94b6b4c7689d2035460ee7b597d80cd8b..911c713f840a97c29566076af58b9a5a9a0733df 100644 (file)
@@ -230,6 +230,10 @@ class OC_Files {
                        OC::$server->getLogger()->logException($ex);
                        $l = \OC::$server->getL10N('lib');
                        \OC_Template::printErrorPage($l->t('Cannot download file'), $ex->getMessage(), 200);
+               } catch (\OCP\Files\ConnectionLostException $ex) {
+                       self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
+                       OC::$server->getLogger()->logException($ex, ['level' => \OCP\ILogger::DEBUG]);
+                       \OC_Template::printErrorPage('Connection lost', $ex->getMessage(), 200);
                } catch (\Exception $ex) {
                        self::unlockAllTheFiles($dir, $files, $getType, $view, $filename);
                        OC::$server->getLogger()->logException($ex);
diff --git a/lib/public/Files/ConnectionLostException.php b/lib/public/Files/ConnectionLostException.php
new file mode 100644 (file)
index 0000000..8e5deb9
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2016, ownCloud, Inc.
+ *
+ * @author Côme Chilliet <come.chilliet@nextcloud.com>
+ *
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OCP\Files;
+
+/**
+ * Exception for lost connection with the
+ * @since 25.0.11
+ */
+class ConnectionLostException extends \RuntimeException {
+}