Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace logout href to avoid new etag on every request
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Mar 6, 2018
commit 11b6cc3f68f55e1c5e725b737b39c0a7f79e0ec1
23 changes: 19 additions & 4 deletions core/Controller/NavigationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ public function getAppsNavigation(bool $absolute = false): DataResponse {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}

$etag = md5(json_encode($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);
$response->setETag($etag);
return $response;
}

Expand All @@ -77,15 +77,30 @@ public function getSettingsNavigation(bool $absolute = false): DataResponse {
if ($absolute) {
$navigation = $this->rewriteToAbsoluteUrls($navigation);
}
$etag = md5(json_encode($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);
$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));
}

/**
* Rewrite href attribute of navigation entries to an absolute URL
*
Expand Down
6 changes: 3 additions & 3 deletions tests/Core/Controller/NavigationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function testGetAppNavigationEtagMatch() {
$this->request->expects($this->once())
->method('getHeader')
->with('If-None-Match')
->willReturn(md5(json_encode($navigation)));
->willReturn(md5(json_encode(['files'])));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('link')
Expand All @@ -143,11 +143,11 @@ public function testGetAppNavigationEtagMatch() {
}

public function testGetSettingsNavigationEtagMatch() {
$navigation = [ ['id' => 'files', 'href' => '/index.php/apps/files', 'icon' => 'icon' ] ];
$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($navigation)));
->willReturn(md5(json_encode([ ['id' => 'logout', 'href' => 'logout', 'icon' => 'icon' ] ])));
$this->navigationManager->expects($this->once())
->method('getAll')
->with('settings')
Expand Down