[Pkg-owncloud-commits] [php-sabredav] 03/148: Using xml writer for some multistatus responses.
David Prévot
taffit at moszumanska.debian.org
Wed Apr 15 01:36:59 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit ebb19e577d40a21dc2e1ac9921704bf38fa5b893
Author: Evert Pot <evert at rooftopsolutions.nl>
Date: Fri Nov 14 21:01:21 2014 -0500
Using xml writer for some multistatus responses.
Works for DAV, probably not for DAVACL, CalDAV, CardDAV
---
lib/DAV/Browser/Plugin.php | 6 +-
lib/DAV/CorePlugin.php | 8 +-
lib/DAV/Locks/Plugin.php | 4 +-
lib/DAV/Property/GetLastModified.php | 77 --------------
lib/DAV/Property/SupportedLock.php | 78 --------------
lib/DAV/Property/SupportedReportSet.php | 126 -----------------------
lib/DAV/Server.php | 37 +++----
lib/DAV/TemporaryFileFilterPlugin.php | 4 +-
lib/DAV/XML/Element/Response.php | 4 +-
lib/DAV/XMLUtil.php | 29 ++++++
tests/Sabre/DAV/HTTPPreferParsingTest.php | 14 ++-
tests/Sabre/DAV/Property/GetLastModifiedTest.php | 75 --------------
tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php | 5 +-
13 files changed, 69 insertions(+), 398 deletions(-)
diff --git a/lib/DAV/Browser/Plugin.php b/lib/DAV/Browser/Plugin.php
index 253b4bd..a4b0ca9 100644
--- a/lib/DAV/Browser/Plugin.php
+++ b/lib/DAV/Browser/Plugin.php
@@ -636,9 +636,9 @@ HTML;
$mapping = [
'Sabre\\DAV\\Property\\IHref' => 'href',
'Sabre\\DAV\\Property\\HrefList' => 'hreflist',
- 'Sabre\\DAV\\Property\\SupportedMethodSet' => 'valuelist',
- 'Sabre\\DAV\\Property\\ResourceType' => 'xmlvaluelist',
- 'Sabre\\DAV\\Property\\SupportedReportSet' => 'xmlvaluelist',
+ 'Sabre\\DAV\\XML\\Property\\SupportedMethodSet' => 'valuelist',
+ 'Sabre\\DAV\\XML\\Property\\ResourceType' => 'xmlvaluelist',
+ 'Sabre\\DAV\\XML\\Property\\SupportedReportSet' => 'xmlvaluelist',
'Sabre\\DAVACL\\Property\\CurrentUserPrivilegeSet' => 'xmlvaluelist',
'Sabre\\DAVACL\\Property\\SupportedPrivilegeSet' => 'supported-privilege-set',
];
diff --git a/lib/DAV/CorePlugin.php b/lib/DAV/CorePlugin.php
index 863f029..dd27dda 100644
--- a/lib/DAV/CorePlugin.php
+++ b/lib/DAV/CorePlugin.php
@@ -790,7 +790,7 @@ class CorePlugin extends ServerPlugin {
$propFind->handle('{DAV:}getlastmodified', function() use ($node) {
$lm = $node->getLastModified();
if ($lm) {
- return new Property\GetLastModified($lm);
+ return new XML\Property\GetLastModified($lm);
}
});
@@ -819,13 +819,13 @@ class CorePlugin extends ServerPlugin {
foreach($this->server->getPlugins() as $plugin) {
$reports = array_merge($reports, $plugin->getSupportedReportSet($propFind->getPath()));
}
- return new Property\SupportedReportSet($reports);
+ return new XML\Property\SupportedReportSet($reports);
});
$propFind->handle('{DAV:}resourcetype', function() use ($node) {
- return new Property\ResourceType($this->server->getResourceTypeForNode($node));
+ return new XML\Property\ResourceType($this->server->getResourceTypeForNode($node));
});
$propFind->handle('{DAV:}supported-method-set', function() use ($propFind) {
- return new Property\SupportedMethodSet(
+ return new XML\Property\SupportedMethodSet(
$this->server->getAllowedMethods($propFind->getPath())
);
});
diff --git a/lib/DAV/Locks/Plugin.php b/lib/DAV/Locks/Plugin.php
index b5604aa..69b2949 100644
--- a/lib/DAV/Locks/Plugin.php
+++ b/lib/DAV/Locks/Plugin.php
@@ -92,10 +92,10 @@ class Plugin extends DAV\ServerPlugin {
function propFind(DAV\PropFind $propFind, DAV\INode $node) {
$propFind->handle('{DAV:}supportedlock', function() {
- return new DAV\Property\SupportedLock(!!$this->locksBackend);
+ return new DAV\XML\Property\SupportedLock(!!$this->locksBackend);
});
$propFind->handle('{DAV:}lockdiscovery', function() use ($propFind) {
- return new DAV\Property\LockDiscovery(
+ return new DAV\XML\Property\LockDiscovery(
$this->getLocks( $propFind->getPath() )
);
});
diff --git a/lib/DAV/Property/GetLastModified.php b/lib/DAV/Property/GetLastModified.php
deleted file mode 100644
index 21603b9..0000000
--- a/lib/DAV/Property/GetLastModified.php
+++ /dev/null
@@ -1,77 +0,0 @@
-<?php
-
-namespace Sabre\DAV\Property;
-
-use Sabre\DAV;
-use Sabre\HTTP;
-
-/**
- * This property represents the {DAV:}getlastmodified property.
- *
- * Although this is normally a simple property, windows requires us to add
- * some new attributes.
- *
- * This class uses unix timestamps internally, and converts them to RFC 1123 times for
- * serialization
- *
- * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
-class GetLastModified extends DAV\Property {
-
- /**
- * time
- *
- * @var int
- */
- public $time;
-
- /**
- * __construct
- *
- * @param int|DateTime $time
- */
- function __construct($time) {
-
- if ($time instanceof \DateTime) {
- $this->time = $time;
- } elseif (is_int($time) || ctype_digit($time)) {
- $this->time = new \DateTime('@' . $time);
- } else {
- $this->time = new \DateTime($time);
- }
-
- // Setting timezone to UTC
- $this->time->setTimezone(new \DateTimeZone('UTC'));
-
- }
-
- /**
- * serialize
- *
- * @param DAV\Server $server
- * @param \DOMElement $prop
- * @return void
- */
- function serialize(DAV\Server $server, \DOMElement $prop) {
-
- //$prop->setAttribute('xmlns:b','urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/');
- //$prop->setAttribute('b:dt','dateTime.rfc1123');
- $prop->nodeValue = HTTP\Util::toHTTPDate($this->time);
-
- }
-
- /**
- * getTime
- *
- * @return \DateTime
- */
- function getTime() {
-
- return $this->time;
-
- }
-
-}
-
diff --git a/lib/DAV/Property/SupportedLock.php b/lib/DAV/Property/SupportedLock.php
deleted file mode 100644
index a49f621..0000000
--- a/lib/DAV/Property/SupportedLock.php
+++ /dev/null
@@ -1,78 +0,0 @@
-<?php
-
-namespace Sabre\DAV\Property;
-
-use Sabre\DAV;
-
-/**
- * This class represents the {DAV:}supportedlock property
- *
- * This property contains information about what kind of locks
- * this server supports.
- *
- * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
-class SupportedLock extends DAV\Property {
-
- /**
- * supportsLocks
- *
- * @var mixed
- */
- public $supportsLocks = false;
-
- /**
- * __construct
- *
- * @param mixed $supportsLocks
- */
- function __construct($supportsLocks) {
-
- $this->supportsLocks = $supportsLocks;
-
- }
-
- /**
- * serialize
- *
- * @param DAV\Server $server
- * @param \DOMElement $prop
- * @return void
- */
- function serialize(DAV\Server $server,\DOMElement $prop) {
-
- $doc = $prop->ownerDocument;
-
- if (!$this->supportsLocks) return null;
-
- $lockEntry1 = $doc->createElement('d:lockentry');
- $lockEntry2 = $doc->createElement('d:lockentry');
-
- $prop->appendChild($lockEntry1);
- $prop->appendChild($lockEntry2);
-
- $lockScope1 = $doc->createElement('d:lockscope');
- $lockScope2 = $doc->createElement('d:lockscope');
- $lockType1 = $doc->createElement('d:locktype');
- $lockType2 = $doc->createElement('d:locktype');
-
- $lockEntry1->appendChild($lockScope1);
- $lockEntry1->appendChild($lockType1);
- $lockEntry2->appendChild($lockScope2);
- $lockEntry2->appendChild($lockType2);
-
- $lockScope1->appendChild($doc->createElement('d:exclusive'));
- $lockScope2->appendChild($doc->createElement('d:shared'));
-
- $lockType1->appendChild($doc->createElement('d:write'));
- $lockType2->appendChild($doc->createElement('d:write'));
-
- //$frag->appendXML('<d:lockentry><d:lockscope><d:exclusive /></d:lockscope><d:locktype><d:write /></d:locktype></d:lockentry>');
- //$frag->appendXML('<d:lockentry><d:lockscope><d:shared /></d:lockscope><d:locktype><d:write /></d:locktype></d:lockentry>');
-
- }
-
-}
-
diff --git a/lib/DAV/Property/SupportedReportSet.php b/lib/DAV/Property/SupportedReportSet.php
deleted file mode 100644
index 28a28d1..0000000
--- a/lib/DAV/Property/SupportedReportSet.php
+++ /dev/null
@@ -1,126 +0,0 @@
-<?php
-
-namespace Sabre\DAV\Property;
-
-use Sabre\DAV;
-
-/**
- * supported-report-set property.
- *
- * This property is defined in RFC3253, but since it's
- * so common in other webdav-related specs, it is part of the core server.
- *
- * @copyright Copyright (C) 2007-2014 fruux GmbH (https://fruux.com/).
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
-class SupportedReportSet extends DAV\Property {
-
- /**
- * List of reports
- *
- * @var array
- */
- protected $reports = [];
-
- /**
- * Creates the property
- *
- * Any reports passed in the constructor
- * should be valid report-types in clark-notation.
- *
- * Either a string or an array of strings must be passed.
- *
- * @param mixed $reports
- */
- function __construct($reports = null) {
-
- if (!is_null($reports))
- $this->addReport($reports);
-
- }
-
- /**
- * Adds a report to this property
- *
- * The report must be a string in clark-notation.
- * Multiple reports can be specified as an array.
- *
- * @param mixed $report
- * @return void
- */
- function addReport($report) {
-
- if (!is_array($report)) $report = [$report];
-
- foreach($report as $r) {
-
- if (!preg_match('/^{([^}]*)}(.*)$/',$r))
- throw new DAV\Exception('Reportname must be in clark-notation');
-
- $this->reports[] = $r;
-
- }
-
- }
-
- /**
- * Returns the list of supported reports
- *
- * @return array
- */
- function getValue() {
-
- return $this->reports;
-
- }
-
- /**
- * Returns true or false if the property contains a specific report.
- *
- * @param string $reportName
- * @return bool
- */
- function has($reportName) {
-
- return in_array(
- $reportName,
- $this->reports
- );
-
- }
-
- /**
- * Serializes the node
- *
- * @param DAV\Server $server
- * @param \DOMElement $prop
- * @return void
- */
- function serialize(DAV\Server $server, \DOMElement $prop) {
-
- foreach($this->reports as $reportName) {
-
- $supportedReport = $prop->ownerDocument->createElement('d:supported-report');
- $prop->appendChild($supportedReport);
-
- $report = $prop->ownerDocument->createElement('d:report');
- $supportedReport->appendChild($report);
-
- preg_match('/^{([^}]*)}(.*)$/',$reportName,$matches);
-
- list(, $namespace, $element) = $matches;
-
- $prefix = isset($server->xmlNamespaces[$namespace])?$server->xmlNamespaces[$namespace]:null;
-
- if ($prefix) {
- $report->appendChild($prop->ownerDocument->createElement($prefix . ':' . $element));
- } else {
- $report->appendChild($prop->ownerDocument->createElementNS($namespace, 'x:' . $element));
- }
-
- }
-
- }
-
-}
diff --git a/lib/DAV/Server.php b/lib/DAV/Server.php
index dccee88..28c6b2a 100644
--- a/lib/DAV/Server.php
+++ b/lib/DAV/Server.php
@@ -91,10 +91,7 @@ class Server extends EventEmitter {
*
* @var array
*/
- public $xmlNamespaces = [
- 'DAV:' => 'd',
- 'http://sabredav.org/ns' => 's',
- ];
+ public $xmlNamespaces = null;
/**
* The propertymap can be used to map properties from
@@ -233,6 +230,7 @@ class Server extends EventEmitter {
}
$this->xml = new XMLUtil();
+ $this->xmlNamespaces =& $this->xml->namespaceMap;
$this->sapi = new HTTP\Sapi();
$this->httpResponse = new HTTP\Response();
$this->httpRequest = $this->sapi->getRequest();
@@ -859,7 +857,7 @@ class Server extends EventEmitter {
$headers[$header] = $properties[$property];
// GetLastModified gets special cased
- } elseif ($properties[$property] instanceof Property\GetLastModified) {
+ } elseif ($properties[$property] instanceof XML\Property\GetLastModified) {
$headers[$header] = HTTP\Util::toHTTPDate($properties[$property]->getTime());
}
@@ -1644,33 +1642,26 @@ class Server extends EventEmitter {
*/
function generateMultiStatus(array $fileProperties, $strip404s = false) {
- $dom = new \DOMDocument('1.0','utf-8');
- //$dom->formatOutput = true;
- $multiStatus = $dom->createElement('d:multistatus');
- $dom->appendChild($multiStatus);
-
- // Adding in default namespaces
- foreach($this->xmlNamespaces as $namespace=>$prefix) {
-
- $multiStatus->setAttribute('xmlns:' . $prefix,$namespace);
-
- }
+ $xml = [];
foreach($fileProperties as $entry) {
$href = $entry['href'];
unset($entry['href']);
-
- if ($strip404s && isset($entry[404])) {
+ if ($strip404s) {
unset($entry[404]);
}
-
- $response = new Property\Response($href,$entry);
- $response->serialize($this,$multiStatus);
+ $response = new XML\Element\Response(
+ '/' . ltrim($href,'/'),
+ $entry
+ );
+ $xml[] = [
+ 'name' => '{DAV:}response',
+ 'value' => $response
+ ];
}
-
- return $dom->saveXML();
+ return $this->xml->write(['{DAV:}multistatus' => $xml]);
}
diff --git a/lib/DAV/TemporaryFileFilterPlugin.php b/lib/DAV/TemporaryFileFilterPlugin.php
index b296c26..50e6709 100644
--- a/lib/DAV/TemporaryFileFilterPlugin.php
+++ b/lib/DAV/TemporaryFileFilterPlugin.php
@@ -267,9 +267,9 @@ class TemporaryFileFilterPlugin extends ServerPlugin {
$properties = [
'href' => $request->getPath(),
200 => [
- '{DAV:}getlastmodified' => new Property\GetLastModified(filemtime($tempLocation)),
+ '{DAV:}getlastmodified' => new XML\Property\GetLastModified(filemtime($tempLocation)),
'{DAV:}getcontentlength' => filesize($tempLocation),
- '{DAV:}resourcetype' => new Property\ResourceType(null),
+ '{DAV:}resourcetype' => new XML\Property\ResourceType(null),
'{'.Server::NS_SABREDAV.'}tempFile' => true,
],
diff --git a/lib/DAV/XML/Element/Response.php b/lib/DAV/XML/Element/Response.php
index 6cbe474..b3bd9bf 100644
--- a/lib/DAV/XML/Element/Response.php
+++ b/lib/DAV/XML/Element/Response.php
@@ -122,7 +122,7 @@ class Response implements Element {
public function serializeXml(Writer $writer) {
if ($status = $this->getHTTPStatus()) {
- $writer->writeElement('{DAV:}status', \Sabre\HTTP\Response::getStatusMessage($status));
+ $writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
}
$writer->writeElement('{DAV:}href', $writer->baseUri . $this->getHref());
foreach($this->getResponseProperties() as $status => $properties) {
@@ -133,7 +133,7 @@ class Response implements Element {
}
$writer->startElement('{DAV:}propstat');
$writer->writeElement('{DAV:}prop', $properties);
- $writer->writeElement('{DAV:}status', \Sabre\HTTP\Response::getStatusMessage($status));
+ $writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
$writer->endElement(); // {DAV:}propstat
}
diff --git a/lib/DAV/XMLUtil.php b/lib/DAV/XMLUtil.php
index af63b9b..49fa202 100644
--- a/lib/DAV/XMLUtil.php
+++ b/lib/DAV/XMLUtil.php
@@ -21,6 +21,19 @@ class XMLUtil {
public $elementMap = [];
/**
+ * This is a default list of namespaces.
+ *
+ * If you are defining your own custom namespace, add it here to reduce
+ * bandwidth and improve legibility of xml bodies.
+ *
+ * @var array
+ */
+ public $namespaceMap = [
+ 'DAV:' => 'd',
+ 'http://sabredav.org/ns' => 's',
+ ];
+
+ /**
* Parses an XML file.
* This method parses an xml file and maps all known properties to their
* respective objects.
@@ -42,6 +55,22 @@ class XMLUtil {
}
/**
+ * Generates an XML document and returns the output as a string.
+ *
+ * @param mixed $output
+ * @return string
+ */
+ function write($output) {
+
+ $writer = new XML\Writer();
+ $writer->namespaceMap = $this->namespaceMap;
+ $writer->openMemory('1.0');
+ $writer->write($output);
+ return $writer->outputMemory();
+
+ }
+
+ /**
* Returns the 'clark notation' for an element.
*
* For example, and element encoded as:
diff --git a/tests/Sabre/DAV/HTTPPreferParsingTest.php b/tests/Sabre/DAV/HTTPPreferParsingTest.php
index 765a59a..3f61c87 100644
--- a/tests/Sabre/DAV/HTTPPreferParsingTest.php
+++ b/tests/Sabre/DAV/HTTPPreferParsingTest.php
@@ -131,10 +131,12 @@ BLA
$response = $this->request($request);
- $this->assertEquals(207, $response->getStatus(), $response->getBodyAsString());
+ $body = $response->getBodyAsString();
- $this->assertTrue(strpos($response->body, 'resourcetype')!==false);
- $this->assertTrue(strpos($response->body, 'something')===false);
+ $this->assertEquals(207, $response->getStatus(), $body);
+
+ $this->assertTrue(strpos($body, 'resourcetype')!==false, $body);
+ $this->assertTrue(strpos($body, 'something')===false, $body);
}
@@ -193,9 +195,11 @@ BLA
$response = $this->request($request);
+ $body = $response->getBodyAsString();
+
$this->assertEquals(207, $response->status);
- $this->assertTrue(strpos($response->body, 'something')!==false);
- $this->assertTrue(strpos($response->body, '403 Forbidden')!==false);
+ $this->assertTrue(strpos($body, 'something')!==false);
+ $this->assertTrue(strpos($body, '403 Forbidden')!==false, $body);
}
}
diff --git a/tests/Sabre/DAV/Property/GetLastModifiedTest.php b/tests/Sabre/DAV/Property/GetLastModifiedTest.php
deleted file mode 100644
index 6278557..0000000
--- a/tests/Sabre/DAV/Property/GetLastModifiedTest.php
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-
-namespace Sabre\DAV\Property;
-
-use Sabre\DAV;
-use Sabre\HTTP;
-
-class GetLastModifiedTest extends \PHPUnit_Framework_TestCase {
-
- function testConstructDateTime() {
-
- $dt = new \DateTime('2010-03-14 16:35', new \DateTimeZone('UTC'));
- $lastMod = new GetLastModified($dt);
- $this->assertEquals($dt->format(\DateTime::ATOM), $lastMod->getTime()->format(\DateTime::ATOM));
-
- }
-
- function testConstructString() {
-
- $dt = new \DateTime('2010-03-14 16:35', new \DateTimeZone('UTC'));
- $lastMod = new GetLastModified('2010-03-14 16:35');
- $this->assertEquals($dt->format(\DateTime::ATOM), $lastMod->getTime()->format(\DateTime::ATOM));
-
- }
-
- function testConstructInt() {
-
- $dt = new \DateTime('2010-03-14 16:35', new \DateTimeZone('UTC'));
- $lastMod = new GetLastModified((int)$dt->format('U'));
- $this->assertEquals($dt->format(\DateTime::ATOM), $lastMod->getTime()->format(\DateTime::ATOM));
-
- }
-
- function testSerialize() {
-
- $dt = new \DateTime('2010-03-14 16:35', new \DateTimeZone('UTC'));
- $lastMod = new GetLastModified($dt);
-
- $doc = new \DOMDocument();
- $root = $doc->createElement('d:getlastmodified');
- $root->setAttribute('xmlns:d','DAV:');
-
- $doc->appendChild($root);
- $server = new DAV\Server();
-
- $lastMod->serialize($server, $root);
-
- $xml = $doc->saveXML();
-
- /*
- $this->assertEquals(
-'<?xml version="1.0"?>
-<d:getlastmodified xmlns:d="DAV:" xmlns:b="urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/" b:dt="dateTime.rfc1123">' .
-HTTP\Util::toHTTPDate($dt) .
-'</d:getlastmodified>
-', $xml);
- */
- $this->assertEquals(
-'<?xml version="1.0"?>
-<d:getlastmodified xmlns:d="DAV:">' .
-HTTP\Util::toHTTPDate($dt) .
-'</d:getlastmodified>
-', $xml);
-
- $ok = false;
- try {
- GetLastModified::unserialize(DAV\XMLUtil::loadDOMDocument($xml)->firstChild, array());
- } catch (DAV\Exception $e) {
- $ok = true;
- }
- if (!$ok) $this->markTestFailed('Unserialize should not be supported');
-
- }
-
-}
diff --git a/tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php b/tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php
index bc821b3..053db53 100644
--- a/tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php
+++ b/tests/Sabre/DAV/ServerPropsInfiniteDepthTest.php
@@ -86,7 +86,10 @@ class ServerPropsInfiniteDepthTest extends AbstractServer {
$this->sendRequest($xml);
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
+ $body = $this->response->getBodyAsString();
+ $this->assertEquals(207, $this->response->getStatus(), $body);
+
+ $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$body);
$xml = simplexml_load_string($body);
$xml->registerXPathNamespace('d','urn:DAV');
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git
More information about the Pkg-owncloud-commits
mailing list