CopyEtagHeaderPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Vincent Petry <vincent@nextcloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Connector\Sabre;
  26. use Sabre\DAV\Exception\NotFound;
  27. use Sabre\DAV\Server;
  28. use Sabre\HTTP\RequestInterface;
  29. use Sabre\HTTP\ResponseInterface;
  30. /**
  31. * Copies the "Etag" header to "OC-Etag" after any request.
  32. * This is a workaround for setups that automatically strip
  33. * or mangle Etag headers.
  34. */
  35. class CopyEtagHeaderPlugin extends \Sabre\DAV\ServerPlugin {
  36. private ?Server $server = null;
  37. /**
  38. * This initializes the plugin.
  39. *
  40. * @param \Sabre\DAV\Server $server Sabre server
  41. *
  42. * @return void
  43. */
  44. public function initialize(\Sabre\DAV\Server $server) {
  45. $this->server = $server;
  46. $server->on('afterMethod:*', [$this, 'afterMethod']);
  47. $server->on('afterMove', [$this, 'afterMove']);
  48. }
  49. /**
  50. * After method, copy the "Etag" header to "OC-Etag" header.
  51. *
  52. * @param RequestInterface $request request
  53. * @param ResponseInterface $response response
  54. */
  55. public function afterMethod(RequestInterface $request, ResponseInterface $response) {
  56. $eTag = $response->getHeader('Etag');
  57. if (!empty($eTag)) {
  58. $response->setHeader('OC-ETag', $eTag);
  59. }
  60. }
  61. /**
  62. * Called after a node is moved.
  63. *
  64. * This allows the backend to move all the associated properties.
  65. *
  66. * @param string $source
  67. * @param string $destination
  68. * @return void
  69. */
  70. public function afterMove($source, $destination) {
  71. try {
  72. $node = $this->server->tree->getNodeForPath($destination);
  73. } catch (NotFound $e) {
  74. // Don't care
  75. return;
  76. }
  77. if ($node instanceof File) {
  78. $eTag = $node->getETag();
  79. $this->server->httpResponse->setHeader('OC-ETag', $eTag);
  80. $this->server->httpResponse->setHeader('ETag', $eTag);
  81. }
  82. }
  83. }