PropfindCompressionPlugin.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 OCA\DAV\Connector\Sabre;
  8. use Sabre\DAV\ServerPlugin;
  9. use Sabre\HTTP\Request;
  10. use Sabre\HTTP\Response;
  11. class PropfindCompressionPlugin extends ServerPlugin {
  12. /**
  13. * Reference to main server object
  14. *
  15. * @var Server
  16. */
  17. private $server;
  18. /**
  19. * This initializes the plugin.
  20. *
  21. * This function is called by \Sabre\DAV\Server, after
  22. * addPlugin is called.
  23. *
  24. * This method should set up the required event subscriptions.
  25. *
  26. * @param \Sabre\DAV\Server $server
  27. * @return void
  28. */
  29. public function initialize(\Sabre\DAV\Server $server) {
  30. $this->server = $server;
  31. $this->server->on('afterMethod:PROPFIND', [$this, 'compressResponse'], 100);
  32. }
  33. public function compressResponse(Request $request, Response $response) {
  34. $header = $request->getHeader('Accept-Encoding');
  35. if ($header === null) {
  36. return $response;
  37. }
  38. if (str_contains($header, 'gzip')) {
  39. $body = $response->getBody();
  40. if (is_string($body)) {
  41. $response->setHeader('Content-Encoding', 'gzip');
  42. $response->setBody(gzencode($body));
  43. }
  44. }
  45. return $response;
  46. }
  47. }