From e480fd39a2775bfb5c11dcc1adc39e7361cb64e5 Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Mon, 19 Aug 2024 13:28:04 +0200 Subject: [PATCH] fix(caldav): add missing handlers Signed-off-by: Anna Larch --- .../lib/CalDAV/WebcalCaching/Connection.php | 145 +++++++----------- .../WebcalCaching/RefreshWebcalService.php | 2 +- .../CalDAV/WebcalCaching/ConnectionTest.php | 27 +--- 3 files changed, 66 insertions(+), 108 deletions(-) diff --git a/apps/dav/lib/CalDAV/WebcalCaching/Connection.php b/apps/dav/lib/CalDAV/WebcalCaching/Connection.php index 9d626f2db6dd6..3c975505539e0 100644 --- a/apps/dav/lib/CalDAV/WebcalCaching/Connection.php +++ b/apps/dav/lib/CalDAV/WebcalCaching/Connection.php @@ -9,15 +9,11 @@ namespace OCA\DAV\CalDAV\WebcalCaching; use Exception; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Middleware; +use GuzzleHttp\RequestOptions; use OCP\Http\Client\IClientService; use OCP\Http\Client\LocalServerException; use OCP\IAppConfig; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerInterface; -use Sabre\DAV\Xml\Property\Href; use Sabre\VObject\Reader; class Connection { @@ -29,107 +25,82 @@ public function __construct(private IClientService $clientService, /** * gets webcal feed from remote server */ - public function queryWebcalFeed(array $subscription, array &$mutations): ?string { - $client = $this->clientService->newClient(); - - $didBreak301Chain = false; - $latestLocation = null; - - $handlerStack = HandlerStack::create(); - $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { - return $request - ->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml') - ->withHeader('User-Agent', 'Nextcloud Webcal Service'); - })); - $handlerStack->push(Middleware::mapResponse(function (ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { - if (!$didBreak301Chain) { - if ($response->getStatusCode() !== 301) { - $didBreak301Chain = true; - } else { - $latestLocation = $response->getHeader('Location'); - } - } - return $response; - })); - - $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no'); + public function queryWebcalFeed(array $subscription): ?string { $subscriptionId = $subscription['id']; $url = $this->cleanURL($subscription['source']); if ($url === null) { return null; } - try { - $params = [ - 'allow_redirects' => [ - 'redirects' => 10 - ], - 'handler' => $handlerStack, - 'nextcloud' => [ - 'allow_local_address' => $allowLocalAccess === 'yes', - ] - ]; - - $user = parse_url($subscription['source'], PHP_URL_USER); - $pass = parse_url($subscription['source'], PHP_URL_PASS); - if ($user !== null && $pass !== null) { - $params['auth'] = [$user, $pass]; - } + $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no'); + + $params = [ + 'nextcloud' => [ + 'allow_local_address' => $allowLocalAccess === 'yes', + ], + RequestOptions::HEADERS => [ + 'User-Agent' => 'Nextcloud Webcal Service', + 'Accept' => 'text/calendar, application/calendar+json, application/calendar+xml', + ], + ]; + + $user = parse_url($subscription['source'], PHP_URL_USER); + $pass = parse_url($subscription['source'], PHP_URL_PASS); + if ($user !== null && $pass !== null) { + $params[RequestOptions::AUTH] = [$user, $pass]; + } + try { + $client = $this->clientService->newClient(); $response = $client->get($url, $params); - $body = $response->getBody(); - - if ($latestLocation !== null) { - $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); - } - - $contentType = $response->getHeader('Content-Type'); - $contentType = explode(';', $contentType, 2)[0]; - switch ($contentType) { - case 'application/calendar+json': - try { - $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING); - } catch (Exception $ex) { - // In case of a parsing error return null - $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); - return null; - } - return $jCalendar->serialize(); - - case 'application/calendar+xml': - try { - $xCalendar = Reader::readXML($body); - } catch (Exception $ex) { - // In case of a parsing error return null - $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); - return null; - } - return $xCalendar->serialize(); - - case 'text/calendar': - default: - try { - $vCalendar = Reader::read($body); - } catch (Exception $ex) { - // In case of a parsing error return null - $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); - return null; - } - return $vCalendar->serialize(); - } } catch (LocalServerException $ex) { $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [ 'exception' => $ex, ]); - return null; } catch (Exception $ex) { $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [ 'exception' => $ex, ]); - return null; } + + $body = $response->getBody(); + + $contentType = $response->getHeader('Content-Type'); + $contentType = explode(';', $contentType, 2)[0]; + switch ($contentType) { + case 'application/calendar+json': + try { + $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING); + } catch (Exception $ex) { + // In case of a parsing error return null + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); + return null; + } + return $jCalendar->serialize(); + + case 'application/calendar+xml': + try { + $xCalendar = Reader::readXML($body); + } catch (Exception $ex) { + // In case of a parsing error return null + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); + return null; + } + return $xCalendar->serialize(); + + case 'text/calendar': + default: + try { + $vCalendar = Reader::read($body); + } catch (Exception $ex) { + // In case of a parsing error return null + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); + return null; + } + return $vCalendar->serialize(); + } } /** diff --git a/apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php b/apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php index 102571c8ef9ef..8ab3ecc202ab1 100644 --- a/apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php +++ b/apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php @@ -55,7 +55,7 @@ public function refreshSubscription(string $principalUri, string $uri) { } - $webcalData = $this->connection->queryWebcalFeed($subscription, $mutations); + $webcalData = $this->connection->queryWebcalFeed($subscription); if (!$webcalData) { return; } diff --git a/apps/dav/tests/unit/CalDAV/WebcalCaching/ConnectionTest.php b/apps/dav/tests/unit/CalDAV/WebcalCaching/ConnectionTest.php index 20f61120af7e0..7ec4722e905dd 100644 --- a/apps/dav/tests/unit/CalDAV/WebcalCaching/ConnectionTest.php +++ b/apps/dav/tests/unit/CalDAV/WebcalCaching/ConnectionTest.php @@ -7,7 +7,6 @@ */ namespace OCA\DAV\Tests\unit\CalDAV\WebcalCaching; -use GuzzleHttp\HandlerStack; use OCA\DAV\CalDAV\WebcalCaching\Connection; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; @@ -48,7 +47,6 @@ public function testLocalUrl($source) { 'source' => $source, 'lastmodified' => 0, ]; - $mutation = []; $client = $this->createMock(IClient::class); $this->clientService->expects(self::once()) @@ -69,7 +67,7 @@ public function testLocalUrl($source) { ->method('warning') ->with("Subscription 42 was not refreshed because it violates local access rules", ['exception' => $localServerException]); - $this->connection->queryWebcalFeed($subscription, $mutation); + $this->connection->queryWebcalFeed($subscription); } public function testInvalidUrl(): void { @@ -83,22 +81,14 @@ public function testInvalidUrl(): void { 'source' => '!@#$', 'lastmodified' => 0, ]; - $mutation = []; $client = $this->createMock(IClient::class); - $this->clientService->expects(self::once()) - ->method('newClient') - ->with() - ->willReturn($client); - $this->config->expects(self::once()) - ->method('getValueString') - ->with('dav', 'webcalAllowLocalAccess', 'no') - ->willReturn('no'); - + $this->config->expects(self::never()) + ->method('getValueString'); $client->expects(self::never()) ->method('get'); - $this->connection->queryWebcalFeed($subscription, $mutation); + $this->connection->queryWebcalFeed($subscription); } @@ -120,7 +110,6 @@ public function testConnection(string $url, string $result, string $contentType) 'source' => $url, 'lastmodified' => 0, ]; - $mutation = []; $this->clientService->expects($this->once()) ->method('newClient') @@ -134,9 +123,7 @@ public function testConnection(string $url, string $result, string $contentType) $client->expects($this->once()) ->method('get') - ->with('https://foo.bar/bla2', $this->callback(function ($obj) { - return $obj['allow_redirects']['redirects'] === 10 && $obj['handler'] instanceof HandlerStack; - })) + ->with('https://foo.bar/bla2') ->willReturn($response); $response->expects($this->once()) @@ -148,9 +135,9 @@ public function testConnection(string $url, string $result, string $contentType) ->with('Content-Type') ->willReturn($contentType); - $this->connection->queryWebcalFeed($subscription, $mutation); - + $this->connection->queryWebcalFeed($subscription); } + public static function runLocalURLDataProvider(): array { return [ ['localhost/foo.bar'],