From: Vincent Petry Date: Thu, 26 Mar 2015 15:06:43 +0000 (+0100) Subject: Copy Etag header to OC-Etag for sabre calls X-Git-Tag: v8.1.0alpha1~138^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=70acd58336925ade7ff42c8ea4759a67c09b9212;p=nextcloud-server.git Copy Etag header to OC-Etag for sabre calls --- diff --git a/apps/files/appinfo/remote.php b/apps/files/appinfo/remote.php index e260f85fdb0..bac372564b6 100644 --- a/apps/files/appinfo/remote.php +++ b/apps/files/appinfo/remote.php @@ -65,6 +65,7 @@ $server->on('beforeMethod', function () use ($server, $objectTree) { ) ) ); + $server->addPlugin(new \OC\Connector\Sabre\CopyEtagHeaderPlugin()); }, 30); // priority 30: after auth (10) and acl(20), before lock(50) and handling the request // And off we go! diff --git a/lib/private/connector/sabre/copyetagheaderplugin.php b/lib/private/connector/sabre/copyetagheaderplugin.php new file mode 100644 index 00000000000..9f5d74654cc --- /dev/null +++ b/lib/private/connector/sabre/copyetagheaderplugin.php @@ -0,0 +1,46 @@ + + * + * @license AGPL3 + */ + +namespace OC\Connector\Sabre; + +use \Sabre\HTTP\RequestInterface; +use \Sabre\HTTP\ResponseInterface; + +/** + * Copies the "Etag" header to "OC-Etag" after any request. + * This is a workaround for setups that automatically strip + * or mangle Etag headers. + */ +class CopyEtagHeaderPlugin extends \Sabre\DAV\ServerPlugin { + /** + * This initializes the plugin. + * + * @param \Sabre\DAV\Server $server Sabre server + * + * @return void + */ + public function initialize(\Sabre\DAV\Server $server) { + $server->on('afterMethod', array($this, 'afterMethod')); + } + + /** + * After method, copy the "Etag" header to "OC-Etag" header. + * + * @param RequestInterface $request request + * @param ResponseInterface $response response + */ + public function afterMethod(RequestInterface $request, ResponseInterface $response) { + $eTag = $response->getHeader('Etag'); + if (!empty($eTag)) { + $response->setHeader('OC-ETag', $eTag); + } + } +} diff --git a/tests/lib/connector/sabre/copyetagheaderplugintest.php b/tests/lib/connector/sabre/copyetagheaderplugintest.php new file mode 100644 index 00000000000..176f9c8d0ca --- /dev/null +++ b/tests/lib/connector/sabre/copyetagheaderplugintest.php @@ -0,0 +1,43 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ +class CopyEtagPluginTest extends \Test\TestCase { + + /** + * @var \OC\Connector\Sabre\CopyEtagHeaderPlugin + */ + private $plugin; + + public function setUp() { + parent::setUp(); + $this->server = new \Sabre\DAV\Server(); + $this->plugin = new \OC\Connector\Sabre\CopyEtagHeaderPlugin($this->tree); + $this->plugin->initialize($this->server); + } + + public function testCopyEtag() { + $request = new \Sabre\Http\Request(); + $response = new \Sabre\Http\Response(); + $response->setHeader('Etag', 'abcd'); + + $this->plugin->afterMethod($request, $response); + + $this->assertEquals('abcd', $response->getHeader('OC-Etag')); + } + + public function testNoopWhenEmpty() { + $request = new \Sabre\Http\Request(); + $response = new \Sabre\Http\Response(); + + $this->plugin->afterMethod($request, $response); + + $this->assertNull($response->getHeader('OC-Etag')); + } +}