-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dav): introduce paginate with custom headers #48662
Merged
+237
−6
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Paginate; | ||
|
||
/** | ||
* Save a copy of the first X items into a separate iterator | ||
* | ||
* This allows us to pass the iterator to the cache while keeping a copy | ||
* of the required items. | ||
* | ||
* @extends \AppendIterator<int, int, \Iterator<int, int>> | ||
*/ | ||
class LimitedCopyIterator extends \AppendIterator { | ||
private array $skipped = []; | ||
private array $copy = []; | ||
|
||
public function __construct(\Traversable $iterator, int $count, int $offset = 0) { | ||
parent::__construct(); | ||
|
||
if (!$iterator instanceof \Iterator) { | ||
$iterator = new \IteratorIterator($iterator); | ||
} | ||
$iterator = new \NoRewindIterator($iterator); | ||
|
||
$i = 0; | ||
while ($iterator->valid() && ++$i <= $offset) { | ||
$this->skipped[] = $iterator->current(); | ||
$iterator->next(); | ||
} | ||
|
||
while ($iterator->valid() && count($this->copy) < $count) { | ||
$this->copy[] = $iterator->current(); | ||
$iterator->next(); | ||
} | ||
|
||
$this->append(new \ArrayIterator($this->skipped)); | ||
$this->append($this->getRequestedItems()); | ||
$this->append($iterator); | ||
} | ||
|
||
public function getRequestedItems(): \Iterator { | ||
return new \ArrayIterator($this->copy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\DAV\Paginate; | ||
|
||
use Generator; | ||
use OCP\ICache; | ||
use OCP\ICacheFactory; | ||
use OCP\IDBConnection; | ||
use OCP\Security\ISecureRandom; | ||
|
||
class PaginateCache { | ||
public const TTL = 60 * 60; | ||
private const CACHE_COUNT_SUFFIX = 'count'; | ||
|
||
private ICache $cache; | ||
|
||
public function __construct( | ||
private IDBConnection $database, | ||
private ISecureRandom $random, | ||
ICacheFactory $cacheFactory, | ||
) { | ||
$this->cache = $cacheFactory->createDistributed('pagination_'); | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param \Iterator $items | ||
* @return array{'token': string, 'count': int} | ||
*/ | ||
public function store(string $uri, \Iterator $items): array { | ||
$token = $this->random->generate(32); | ||
$cacheKey = $this->buildCacheKey($uri, $token); | ||
|
||
$count = 0; | ||
foreach ($items as $item) { | ||
// Add small margin to avoid fetching valid count and then expired entries | ||
$this->cache->set($cacheKey . $count, $item, self::TTL + 60); | ||
++$count; | ||
} | ||
$this->cache->set($cacheKey . self::CACHE_COUNT_SUFFIX, $count, self::TTL); | ||
|
||
return ['token' => $token, 'count' => $count]; | ||
} | ||
|
||
/** | ||
* @return Generator<mixed> | ||
*/ | ||
public function get(string $uri, string $token, int $offset, int $count): Generator { | ||
$cacheKey = $this->buildCacheKey($uri, $token); | ||
$nbItems = $this->cache->get($cacheKey . self::CACHE_COUNT_SUFFIX); | ||
if (!$nbItems || $offset > $nbItems) { | ||
return []; | ||
} | ||
|
||
$lastItem = min($nbItems, $offset + $count); | ||
for ($i = $offset; $i < $lastItem; ++$i) { | ||
yield $this->cache->get($cacheKey . $i); | ||
} | ||
} | ||
|
||
public function exists(string $uri, string $token): bool { | ||
return $this->cache->get($this->buildCacheKey($uri, $token) . self::CACHE_COUNT_SUFFIX) > 0; | ||
} | ||
|
||
private function buildCacheKey(string $uri, string $token): string { | ||
return $token . '_' . crc32($uri) . '_'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
namespace OCA\DAV\Paginate; | ||
|
||
use Sabre\DAV\Server; | ||
use Sabre\DAV\ServerPlugin; | ||
use Sabre\HTTP\RequestInterface; | ||
use Sabre\HTTP\ResponseInterface; | ||
|
||
class PaginatePlugin extends ServerPlugin { | ||
public const PAGINATE_HEADER = 'X-NC-Paginate'; | ||
public const PAGINATE_TOTAL_HEADER = 'X-NC-Paginate-Total'; | ||
public const PAGINATE_TOKEN_HEADER = 'X-NC-Paginate-Token'; | ||
public const PAGINATE_OFFSET_HEADER = 'X-NC-Paginate-Offset'; | ||
public const PAGINATE_COUNT_HEADER = 'X-NC-Paginate-Count'; | ||
|
||
/** @var Server */ | ||
private $server; | ||
|
||
public function __construct( | ||
private PaginateCache $cache, | ||
private int $pageSize = 100, | ||
) { | ||
} | ||
|
||
public function initialize(Server $server): void { | ||
$this->server = $server; | ||
$server->on('beforeMultiStatus', [$this, 'onMultiStatus']); | ||
$server->on('method:SEARCH', [$this, 'onMethod'], 1); | ||
$server->on('method:PROPFIND', [$this, 'onMethod'], 1); | ||
$server->on('method:REPORT', [$this, 'onMethod'], 1); | ||
} | ||
|
||
public function getFeatures(): array { | ||
return ['nc-paginate']; | ||
} | ||
|
||
public function onMultiStatus(&$fileProperties): void { | ||
$request = $this->server->httpRequest; | ||
if (is_array($fileProperties)) { | ||
$fileProperties = new \ArrayIterator($fileProperties); | ||
} | ||
$url = $request->getUrl(); | ||
if ( | ||
$request->hasHeader(self::PAGINATE_HEADER) && | ||
(!$request->hasHeader(self::PAGINATE_TOKEN_HEADER) || !$this->cache->exists($url, $request->getHeader(self::PAGINATE_TOKEN_HEADER))) | ||
) { | ||
$pageSize = (int)$request->getHeader(self::PAGINATE_COUNT_HEADER) ?: $this->pageSize; | ||
$offset = (int)$request->getHeader(self::PAGINATE_OFFSET_HEADER); | ||
$copyIterator = new LimitedCopyIterator($fileProperties, $pageSize, $offset); | ||
['token' => $token, 'count' => $count] = $this->cache->store($url, $copyIterator); | ||
|
||
$fileProperties = $copyIterator->getRequestedItems(); | ||
$this->server->httpResponse->addHeader(self::PAGINATE_HEADER, 'true'); | ||
$this->server->httpResponse->addHeader(self::PAGINATE_TOKEN_HEADER, $token); | ||
$this->server->httpResponse->addHeader(self::PAGINATE_TOTAL_HEADER, (string)$count); | ||
$request->setHeader(self::PAGINATE_TOKEN_HEADER, $token); | ||
} | ||
} | ||
|
||
public function onMethod(RequestInterface $request, ResponseInterface $response) { | ||
$url = $this->server->httpRequest->getUrl(); | ||
if ( | ||
$request->hasHeader(self::PAGINATE_TOKEN_HEADER) && | ||
$request->hasHeader(self::PAGINATE_OFFSET_HEADER) && | ||
$this->cache->exists($url, $request->getHeader(self::PAGINATE_TOKEN_HEADER)) | ||
) { | ||
$token = $request->getHeader(self::PAGINATE_TOKEN_HEADER); | ||
$offset = (int)$request->getHeader(self::PAGINATE_OFFSET_HEADER); | ||
$count = (int)$request->getHeader(self::PAGINATE_COUNT_HEADER) ?: $this->pageSize; | ||
|
||
$items = $this->cache->get($url, $token, $offset, $count); | ||
|
||
$response->setStatus(207); | ||
$response->addHeader(self::PAGINATE_HEADER, 'true'); | ||
$response->setHeader('Content-Type', 'application/xml; charset=utf-8'); | ||
$response->setHeader('Vary', 'Brief,Prefer'); | ||
|
||
$prefer = $this->server->getHTTPPrefer(); | ||
$minimal = $prefer['return'] === 'minimal'; | ||
|
||
$data = $this->server->generateMultiStatus($items, $minimal); | ||
$response->setBody($data); | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also be added to ServerFactory?
(in
apps/dav/lib/Connector/Sabre
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if there is a clear rule for that :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not remember the details, I think @skjnldsv knows.
From what I recall one is used for public pages and the other one for authenticated webdav or something like that? Which is why they do not have the exact same list of plugins loaded.
But quite frankly it should all be moved to the server factory, even if it stays 2 separate methods if needed.