From 490c09e56b86d2382d0aaf8f6f71cba66e345cd5 Mon Sep 17 00:00:00 2001
From: Artur Neumann
Date: Thu, 6 Sep 2018 21:47:19 +0545
Subject: [PATCH] allow to slowdown dav requests
---
appinfo/info.xml | 6 +++
appinfo/routes.php | 11 +++++
lib/Dav/SlowdownPlugin.php | 88 ++++++++++++++++++++++++++++++++++++++
lib/DavSlowdown.php | 50 ++++++++++++++++++++++
4 files changed, 155 insertions(+)
create mode 100644 lib/Dav/SlowdownPlugin.php
create mode 100644 lib/DavSlowdown.php
diff --git a/appinfo/info.xml b/appinfo/info.xml
index a76cec9..89ff2ed 100644
--- a/appinfo/info.xml
+++ b/appinfo/info.xml
@@ -10,6 +10,12 @@
+
+
+
+ OCA\Testing\Dav\SlowdownPlugin
+
+
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 85e4f6d..4dcab93 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -31,6 +31,7 @@
use OCP\API;
use OCA\Testing\Opcache;
use OCA\Testing\Logfile;
+use OCA\Testing\DavSlowdown;
$config = new Config(
\OC::$server->getConfig(),
@@ -204,3 +205,13 @@
'testing',
API::ADMIN_AUTH
);
+
+$davSlowDown = new DavSlowdown();
+
+API::register(
+ 'put',
+ '/apps/testing/api/v1/davslowdown/{method}/{seconds}',
+ [$davSlowDown, 'setSlowdown'],
+ 'testing',
+ API::ADMIN_AUTH
+);
diff --git a/lib/Dav/SlowdownPlugin.php b/lib/Dav/SlowdownPlugin.php
new file mode 100644
index 0000000..f6caa60
--- /dev/null
+++ b/lib/Dav/SlowdownPlugin.php
@@ -0,0 +1,88 @@
+
+ * @copyright Copyright (c) 2018 Artur Neumann artur@jankaritech.com
+ *
+ * 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
+ *
+ */
+
+namespace OCA\Testing\Dav;
+
+use OCP\ILogger;
+use Sabre\DAV\Server;
+use Sabre\DAV\ServerPlugin;
+use Sabre\HTTP\RequestInterface;
+use Sabre\HTTP\ResponseInterface;
+
+/**
+ * Sabre plugin for the the file firewall:
+ */
+class SlowdownPlugin extends ServerPlugin {
+ const NS_OWNCLOUD = 'http://owncloud.org/ns';
+
+ /**
+ * @var Server $server
+ */
+ private $server;
+
+ /**
+ * @var ILogger
+ */
+ private $logger;
+
+ /**
+ * SlowdownPlugin plugin
+ *
+ * @param ILogger $logger
+ */
+ public function __construct(ILogger $logger) {
+ $this->logger = $logger;
+ }
+
+ /**
+ * registers an event for every method mentioned in 'dav.slowdown' setting
+ *
+ * @param Server $server
+ *
+ * @return void
+ */
+ public function initialize(Server $server) {
+ $this->server = $server;
+ $slowDown = \OC::$server->getConfig()->getSystemValue('dav.slowdown', '{}');
+ $this->slowDownSettings = \json_decode($slowDown, true);
+ foreach ($this->slowDownSettings as $method => $seconds) {
+ $this->server->on("method:$method", [$this, 'sleep'], 90);
+ }
+ }
+
+ /**
+ *
+ * @param RequestInterface $request request object
+ * @param ResponseInterface $response response object
+ * @throws \Sabre\DAV\Exception\Forbidden
+ * @return boolean
+ */
+ public function sleep(
+ RequestInterface $request, ResponseInterface $response
+ ) {
+ $timeToSleep = $this->slowDownSettings[\strtoupper($request->getMethod())];
+ $this->logger->info("time to sleep $timeToSleep");
+ for ($i = 0; $i <= $timeToSleep; $i++) {
+ $this->logger->info("sleeping $i ...");
+ \sleep(1);
+ }
+ }
+}
diff --git a/lib/DavSlowdown.php b/lib/DavSlowdown.php
new file mode 100644
index 0000000..b939450
--- /dev/null
+++ b/lib/DavSlowdown.php
@@ -0,0 +1,50 @@
+
+ * @copyright Copyright (c) 2018 Artur Neumann artur@jankaritech.com
+ *
+ * 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
+ *
+ */
+
+namespace OCA\Testing;
+
+use OC\OCS\Result;
+
+/**
+ *
+ * @author Artur Neumann
+ *
+ */
+class DavSlowdown {
+
+ /**
+ * save the settings in a system setting
+ *
+ * @param array $parameters
+ *
+ * @return Result
+ */
+ public function setSlowdown($parameters) {
+ $method = \strtoupper($parameters['method']);
+ $seconds = (int)$parameters['seconds'];
+ $slowDown = \OC::$server->getConfig()->getSystemValue('dav.slowdown', '{}');
+ $slowDown = \json_decode($slowDown, true);
+ $slowDown[$method] = $seconds;
+ \OC::$server->getConfig()->setSystemValue('dav.slowdown', \json_encode($slowDown));
+
+ return new Result();
+ }
+}