You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CopyEtagHeaderPlugin.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\HTTP\RequestInterface;
  27. use Sabre\HTTP\ResponseInterface;
  28. /**
  29. * Copies the "Etag" header to "OC-Etag" after any request.
  30. * This is a workaround for setups that automatically strip
  31. * or mangle Etag headers.
  32. */
  33. class CopyEtagHeaderPlugin extends \Sabre\DAV\ServerPlugin {
  34. /** @var \Sabre\DAV\Server */
  35. private $server;
  36. /**
  37. * This initializes the plugin.
  38. *
  39. * @param \Sabre\DAV\Server $server Sabre server
  40. *
  41. * @return void
  42. */
  43. public function initialize(\Sabre\DAV\Server $server) {
  44. $this->server = $server;
  45. $server->on('afterMethod:*', [$this, 'afterMethod']);
  46. $server->on('afterMove', [$this, 'afterMove']);
  47. }
  48. /**
  49. * After method, copy the "Etag" header to "OC-Etag" header.
  50. *
  51. * @param RequestInterface $request request
  52. * @param ResponseInterface $response response
  53. */
  54. public function afterMethod(RequestInterface $request, ResponseInterface $response) {
  55. $eTag = $response->getHeader('Etag');
  56. if (!empty($eTag)) {
  57. $response->setHeader('OC-ETag', $eTag);
  58. }
  59. }
  60. /**
  61. * Called after a node is moved.
  62. *
  63. * This allows the backend to move all the associated properties.
  64. *
  65. * @param string $source
  66. * @param string $destination
  67. * @return void
  68. */
  69. public function afterMove($source, $destination) {
  70. $node = $this->server->tree->getNodeForPath($destination);
  71. if ($node instanceof File) {
  72. $eTag = $node->getETag();
  73. $this->server->httpResponse->setHeader('OC-ETag', $eTag);
  74. $this->server->httpResponse->setHeader('ETag', $eTag);
  75. }
  76. }
  77. }