Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle 404 in CardDAV multi get - refs #829 #904

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/CardDAV/AddressBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ function getMultipleChildren(array $paths) {
$objs = $this->carddavBackend->getMultipleCards($this->addressBookInfo['id'], $paths);
$children = [];
foreach ($objs as $obj) {
$obj['acl'] = $this->getChildACL();
$children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
if (is_array($obj)) {
$obj['acl'] = $this->getChildACL();
$children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
}
}
return $children;

Expand Down
2 changes: 2 additions & 0 deletions lib/DAV/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabre\DAV;

use Sabre\DAV\Xml\Response\MultiStatus;
use Sabre\HTTP;
use Sabre\Uri;

Expand Down Expand Up @@ -422,6 +423,7 @@ function getAbsoluteUrl($url) {
*/
function parseMultiStatus($body) {

/** @var MultiStatus $multistatus */
$multistatus = $this->xml->expect('{DAV:}multistatus', $body);

$result = [];
Expand Down
32 changes: 16 additions & 16 deletions lib/DAV/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,30 +1013,28 @@ function getPropertiesIteratorForPath($path, $propertyNames = [], $depth = 0) {
* @return array
*/
function getPropertiesForMultiplePaths(array $paths, array $propertyNames = []) {

$result = [
];

$nodes = $this->tree->getMultipleNodes($paths);

foreach ($nodes as $path => $node) {

$propFind = new PropFind($path, $propertyNames);
$r = $this->getPropertiesByNode($propFind, $node);
if ($r) {
$result[$path] = $propFind->getResultForMultiStatus();
if (is_null($node)) {
$result[$path] = [];
$result[$path]['href'] = $path;

$resourceType = $this->getResourceTypeForNode($node);
if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
$result[$path]['href'] .= '/';
$result[$path]['status'] = 404;
} else {
$propFind = new PropFind($path, $propertyNames);
$r = $this->getPropertiesByNode($propFind, $node);
if ($r) {
$result[$path] = $propFind->getResultForMultiStatus();
$result[$path]['href'] = $path;
$resourceType = $this->getResourceTypeForNode($node);
if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
$result[$path]['href'] .= '/';
}
}
}

}

return $result;

}


Expand Down Expand Up @@ -1670,9 +1668,11 @@ function generateMultiStatus($fileProperties, $strip404s = false) {
if ($strip404s) {
unset($entry[404]);
}
$status = isset($entry['status']) ? $entry['status'] : null;
$response = new Xml\Element\Response(
ltrim($href, '/'),
$entry
$entry,
$status
);
$w->write([
'name' => '{DAV:}response',
Expand Down
24 changes: 21 additions & 3 deletions lib/DAV/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sabre\DAV;

use Sabre\DAV\Exception\NotFound;
use Sabre\Uri;

/**
Expand Down Expand Up @@ -266,20 +267,37 @@ function getMultipleNodes($paths) {

foreach ($parents as $parent => $children) {

$parentNode = $this->getNodeForPath($parent);
try {
$parentNode = $this->getNodeForPath($parent);
} catch (NotFound $ex) {
foreach ($children as $child) {
$fullPath = $parent . '/' . $child;
$result[$fullPath] = null;
}
continue;
}
if ($parentNode instanceof IMultiGet) {
foreach ($parentNode->getMultipleChildren($children) as $childNode) {
$fullPath = $parent . '/' . $childNode->getName();
$result[$fullPath] = $childNode;
$this->cache[$fullPath] = $childNode;
}
foreach ($children as $child) {
$fullPath = $parent . '/' . $child;
if (!isset($result[$fullPath])) {
$result[$fullPath] = null;
}
}
} else {
foreach ($children as $child) {
$fullPath = $parent . '/' . $child;
$result[$fullPath] = $this->getNodeForPath($fullPath);
try {
$result[$fullPath] = $this->getNodeForPath($fullPath);
} catch (NotFound $ex) {
$result[$fullPath] = null;
}
}
}

}

return $result;
Expand Down
7 changes: 3 additions & 4 deletions lib/DAV/Xml/Element/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Response implements Element {
protected $href;

/**
* Propertylist, ordered by HTTP status code
* Property list, ordered by HTTP status code
*
* @var array
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ function getResponseProperties() {
*/
function xmlSerialize(Writer $writer) {

if ($status = $this->getHTTPStatus()) {
if ($status = $this->getHttpStatus()) {
$writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
}
$writer->writeElement('{DAV:}href', $writer->contextUri . \Sabre\HTTP\encodePath($this->getHref()));
Expand All @@ -137,9 +137,8 @@ function xmlSerialize(Writer $writer) {
$writer->writeElement('{DAV:}prop', $properties);
$writer->writeElement('{DAV:}status', 'HTTP/1.1 ' . $status . ' ' . \Sabre\HTTP\Response::$statusCodes[$status]);
$writer->endElement(); // {DAV:}propstat

}
if ($empty) {
if ($empty && !$status) {
/*
* The WebDAV spec _requires_ at least one DAV:propstat to appear for
* every DAV:response. In some circumstances however, there are no
Expand Down
58 changes: 58 additions & 0 deletions tests/Sabre/CardDAV/MultiGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,62 @@ function testMultiGetVCard4() {
], $result);

}

function testMultiGet404() {

$request = HTTP\Sapi::createFromServerArray([
'REQUEST_METHOD' => 'REPORT',
'REQUEST_URI' => '/addressbooks/user1/book1',
]);

$request->setBody(
'<?xml version="1.0"?>
<c:addressbook-multiget xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:carddav">
<d:prop>
<d:getetag />
<c:address-data />
</d:prop>
<d:href>/addressbooks/user1/unknown/card1</d:href>
<d:href>/addressbooks/user1/book1/card1</d:href>
<d:href>/addressbooks/user1/book1/unknown-card</d:href>
</c:addressbook-multiget>'
);

$response = new HTTP\ResponseMock();

$this->server->httpRequest = $request;
$this->server->httpResponse = $response;

$this->server->exec();

$this->assertEquals(207, $response->status, 'Incorrect status code. Full response body:' . $response->body);

$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?>
<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:card="urn:ietf:params:xml:ns:carddav">
<d:response>
<d:status>HTTP/1.1 404 Not Found</d:status>
<d:href>/addressbooks/user1/unknown/card1</d:href>
</d:response>
<d:response>
<d:href>/addressbooks/user1/book1/card1</d:href>
<d:propstat>
<d:prop>
<d:getetag>"ffe3b42186ba156c84fc1581c273f01c"</d:getetag>
<card:address-data>BEGIN:VCARD
VERSION:3.0
UID:12345
END:VCARD</card:address-data>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
<d:response>
<d:status>HTTP/1.1 404 Not Found</d:status>
<d:href>/addressbooks/user1/book1/unknown-card</d:href>
</d:response>
</d:multistatus>', $response->body);

}


}
1 change: 1 addition & 0 deletions tests/Sabre/DAV/Sync/PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function testSyncInitialSyncCollection() {

$this->assertEquals(207, $response->status, 'Full response body:' . $response->body);

/** @var DAV\Xml\Response\MultiStatus $multiStatus */
$multiStatus = $this->server->xml->parse($response->getBodyAsString());

// Checking the sync-token
Expand Down
4 changes: 0 additions & 4 deletions tests/Sabre/DAVACL/PrincipalMatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ function testPrincipalMatch() {
<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns">
<d:status>HTTP/1.1 200 OK</d:status>
<d:href>/principals/user1</d:href>
<d:propstat>
<d:prop/>
<d:status>HTTP/1.1 418 I'm a teapot</d:status>
</d:propstat>
</d:multistatus>
XML;

Expand Down