Skip to content

Commit 10a5d38

Browse files
committed
Also allow caching of the logout route
It is a bit more general. If the only argument is the requesttoken. So a get request will work with the CSRF protection. The route will be generated without the parameters and the request token will be added at the end. Before the route could not properly be cached as the request token was always changeing. However now it can be cached saving precious cpu cycles. Signed-off-by: Roeland Jago Douma <[email protected]>
1 parent 2ba75ac commit 10a5d38

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

lib/private/Route/CachingRouter.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23
/**
34
* @copyright Copyright (c) 2016, ownCloud, Inc.
45
*
@@ -50,16 +51,31 @@ public function __construct($cache, ILogger $logger) {
5051
* @param bool $absolute
5152
* @return string
5253
*/
53-
public function generate($name, $parameters = array(), $absolute = false) {
54+
public function generate($name, $parameters = [], $absolute = false): string {
55+
/*
56+
* If the only parameter is the request token we should do special
57+
* handling. As caching with the request token is not very useful since
58+
* it will change with each request.
59+
*/
60+
$requestToken = null;
61+
if (count($parameters) === 1 && isset($parameters['requesttoken'])) {
62+
$requestToken = $parameters['requesttoken'];
63+
$parameters = [];
64+
}
65+
5466
asort($parameters);
5567
$key = $this->context->getHost() . '#' . $this->context->getBaseUrl() . $name . sha1(json_encode($parameters)) . (int)$absolute;
56-
$cachedKey = $this->cache->get($key);
57-
if ($cachedKey) {
58-
return $cachedKey;
59-
} else {
68+
$url = $this->cache->get($key);
69+
if ($url === null) {
6070
$url = parent::generate($name, $parameters, $absolute);
6171
$this->cache->set($key, $url, 3600);
62-
return $url;
6372
}
73+
74+
// Add the request token if required
75+
if ($requestToken !== null) {
76+
$url .= '?requesttoken=' . $requestToken;
77+
}
78+
79+
return $url;
6480
}
6581
}

0 commit comments

Comments
 (0)