From 4bdb473aae8307d4be59bc7975eca4e6e962c739 Mon Sep 17 00:00:00 2001 From: Daniel Kesselberg Date: Wed, 27 Mar 2024 17:48:43 +0100 Subject: [PATCH] perf(dav): skip non-calendar requests in webcal caching plugin The webcal caching plugin is active when the X-NC-CalDAV-Webcal-Caching header is there. findPrincipalByUrl sends a request for /remote.php/dav/principals/users/admin/ using the caldavService which sets the header by default.[^1] As X-NC-CalDAV-Webcal-Caching only impacts calendar requests, we can skip non-calendar requests. [^1]: https://github.com/nextcloud/calendar/blob/b3670f1805ef9ef952d6abe4e5334e37b5a14133/src/services/caldavService.js#L43 Signed-off-by: Daniel Kesselberg --- apps/dav/lib/CalDAV/WebcalCaching/Plugin.php | 4 ++ .../unit/CalDAV/WebcalCaching/PluginTest.php | 58 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php b/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php index 3dd8a7c81e5..8d4714d313a 100644 --- a/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php +++ b/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php @@ -98,6 +98,10 @@ class Plugin extends ServerPlugin { } $path = $request->getPath(); + if (!str_starts_with($path, 'calendars/')) { + return; + } + $pathParts = explode('/', ltrim($path, '/')); if (\count($pathParts) < 2) { return; diff --git a/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php b/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php index c99a859ac16..24b72117658 100644 --- a/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php +++ b/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php @@ -25,6 +25,10 @@ namespace OCA\DAV\Tests\unit\CalDAV\WebcalCaching; use OCA\DAV\CalDAV\WebcalCaching\Plugin; use OCP\IRequest; +use Sabre\DAV\Server; +use Sabre\DAV\Tree; +use Sabre\HTTP\Request; +use Sabre\HTTP\Response; class PluginTest extends \Test\TestCase { public function testDisabled(): void { @@ -60,4 +64,58 @@ class PluginTest extends \Test\TestCase { $this->assertEquals(true, $plugin->isCachingEnabledForThisRequest()); } + + public function testSkipNonCalendarRequest(): void { + $request = $this->createMock(IRequest::class); + $request->expects($this->once()) + ->method('isUserAgent') + ->with(Plugin::ENABLE_FOR_CLIENTS) + ->willReturn(false); + + $request->expects($this->once()) + ->method('getHeader') + ->with('X-NC-CalDAV-Webcal-Caching') + ->willReturn('On'); + + $sabreRequest = new Request('REPORT', '/remote.php/dav/principals/users/admin/'); + $sabreRequest->setBaseUrl('/remote.php/dav/'); + + $tree = $this->createMock(Tree::class); + $tree->expects($this->never()) + ->method('getNodeForPath'); + + $server = new Server($tree); + + $plugin = new Plugin($request); + $plugin->initialize($server); + + $plugin->beforeMethod($sabreRequest, new Response()); + } + + public function testProcessCalendarRequest(): void { + $request = $this->createMock(IRequest::class); + $request->expects($this->once()) + ->method('isUserAgent') + ->with(Plugin::ENABLE_FOR_CLIENTS) + ->willReturn(false); + + $request->expects($this->once()) + ->method('getHeader') + ->with('X-NC-CalDAV-Webcal-Caching') + ->willReturn('On'); + + $sabreRequest = new Request('REPORT', '/remote.php/dav/calendars/admin/personal/'); + $sabreRequest->setBaseUrl('/remote.php/dav/'); + + $tree = $this->createMock(Tree::class); + $tree->expects($this->once()) + ->method('getNodeForPath'); + + $server = new Server($tree); + + $plugin = new Plugin($request); + $plugin->initialize($server); + + $plugin->beforeMethod($sabreRequest, new Response()); + } } -- 2.39.5