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
33 changes: 31 additions & 2 deletions core/Controller/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OC\Core\Controller;

use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\INavigationManager;
Expand Down Expand Up @@ -54,7 +55,14 @@ public function getAppsNavigation(bool $absolute = false): DataResponse {
if ($absolute) {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}
return new DataResponse($navigation);

$etag = $this->generateETag($navigation);
if ($this->request->getHeader('If-None-Match') === $etag) {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
$response = new DataResponse($navigation);
$response->setETag($etag);
return $response;
}

/**
Expand All @@ -69,7 +77,28 @@ public function getSettingsNavigation(bool $absolute = false): DataResponse {
if ($absolute) {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}
return new DataResponse($navigation);
$etag = $this->generateETag($navigation);
if ($this->request->getHeader('If-None-Match') === $etag) {
return new DataResponse([], Http::STATUS_NOT_MODIFIED);
}
$response = new DataResponse($navigation);
$response->setETag($etag);
return $response;
}

/**
* Generate an ETag for a list of navigation entries
*
* @param array $navigation
* @return string
*/
private function generateETag(array $navigation): string {
foreach ($navigation as &$nav) {
if ($nav['id'] === 'logout') {
$nav['href'] = 'logout';
}
}
return md5(json_encode($navigation));
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Core/Controller/NavigationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Tests\Core\Controller;

use OC\Core\Controller\NavigationController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\INavigationManager;
use OCP\IRequest;
Expand Down Expand Up @@ -126,4 +127,34 @@ public function testGetSettingsNavigation($absolute) {
}
}

public function testGetAppNavigationEtagMatch() {
$navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
$this->request->expects($this->once())
->method('getHeader')
->with('If-None-Match')
->willReturn(md5(json_encode($navigation)));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('link')
->willReturn($navigation);
$actual = $this->controller->getAppsNavigation();
$this->assertInstanceOf(DataResponse::class, $actual);
$this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
}

public function testGetSettingsNavigationEtagMatch() {
$navigation = [ ['id' => 'logout', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
$this->request->expects($this->once())
->method('getHeader')
->with('If-None-Match')
->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ])));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('settings')
->willReturn($navigation);
$actual = $this->controller->getSettingsNavigation();
$this->assertInstanceOf(DataResponse::class, $actual);
$this->assertEquals(Http::STATUS_NOT_MODIFIED, $actual->getStatus());
}

}