NotModifiedMiddleware.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\AppFramework\Middleware;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Response;
  10. use OCP\AppFramework\Middleware;
  11. use OCP\IRequest;
  12. class NotModifiedMiddleware extends Middleware {
  13. /** @var IRequest */
  14. private $request;
  15. public function __construct(IRequest $request) {
  16. $this->request = $request;
  17. }
  18. public function afterController($controller, $methodName, Response $response) {
  19. $etagHeader = $this->request->getHeader('IF_NONE_MATCH');
  20. if ($etagHeader !== '' && $response->getETag() !== null && trim($etagHeader) === '"' . $response->getETag() . '"') {
  21. $response->setStatus(Http::STATUS_NOT_MODIFIED);
  22. return $response;
  23. }
  24. $modifiedSinceHeader = $this->request->getHeader('IF_MODIFIED_SINCE');
  25. if ($modifiedSinceHeader !== '' && $response->getLastModified() !== null && trim($modifiedSinceHeader) === $response->getLastModified()->format(\DateTimeInterface::RFC2822)) {
  26. $response->setStatus(Http::STATUS_NOT_MODIFIED);
  27. return $response;
  28. }
  29. return $response;
  30. }
  31. }