Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<owncloud min-version="10.0" max-version="10.1" />
</dependencies>
<types>
<dav/>
<authentication/>
</types>
<sabre>
<plugins>
<plugin>OCA\Testing\Dav\SlowdownPlugin</plugin>
</plugins>
</sabre>
</info>
11 changes: 11 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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
);
88 changes: 88 additions & 0 deletions lib/Dav/SlowdownPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @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 <http://www.gnu.org/licenses/>
*
*/

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);
}
}
}
50 changes: 50 additions & 0 deletions lib/DavSlowdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @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 <http://www.gnu.org/licenses/>
*
*/

namespace OCA\Testing;

use OC\OCS\Result;

/**
*
* @author Artur Neumann <artur@jankaritech.com>
*
*/
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();
}
}