/** * This event is triggered before any HTTP request is handled. * * We use this to intercept GET calls to notification nodes, and return the * proper response. * * @param string $method * @param string $path * @return void */ public function beforeMethod($method, $path) { if ($method !== 'GET') { return; } try { $node = $this->server->tree->getNodeForPath($path); } catch (Sabre_DAV_Exception_NotFound $e) { return; } if (!$node instanceof Sabre_CalDAV_Notifications_INode) { return; } if (!$this->server->checkPreconditions(true)) { return false; } $dom = new DOMDocument('1.0', 'UTF-8'); $dom->formatOutput = true; $root = $dom->createElement('cs:notification'); foreach ($this->server->xmlNamespaces as $namespace => $prefix) { $root->setAttribute('xmlns:' . $prefix, $namespace); } $dom->appendChild($root); $node->getNotificationType()->serializeBody($this->server, $root); $this->server->httpResponse->setHeader('Content-Type', 'application/xml'); $this->server->httpResponse->setHeader('ETag', $node->getETag()); $this->server->httpResponse->sendStatus(200); $this->server->httpResponse->sendBody($dom->saveXML()); return false; }
/** * Patch an uri * * The WebDAV patch request can be used to modify only a part of an * existing resource. If the resource does not exist yet and the first * offset is not 0, the request fails * * @param string $uri * @return void */ protected function httpPatch($uri) { // Get the node. Will throw a 404 if not found $node = $this->server->tree->getNodeForPath($uri); if (!$node instanceof Sabre_DAV_PartialUpdate_IFile) { throw new Sabre_DAV_Exception_MethodNotAllowed('The target resource does not support the PATCH method.'); } $range = $this->getHTTPUpdateRange(); if (!$range) { throw new Sabre_DAV_Exception_BadRequest('No valid "X-Update-Range" found in the headers'); } $contentType = strtolower($this->server->httpRequest->getHeader('Content-Type')); if ($contentType != 'application/x-sabredav-partialupdate') { throw new Sabre_DAV_Exception_UnsupportedMediaType('Unknown Content-Type header "' . $contentType . '"'); } $len = $this->server->httpRequest->getHeader('Content-Length'); // Load the begin and end data $start = $range[0] ? $range[0] : 0; $end = $range[1] ? $range[1] : $len - 1; // Check consistency if ($end < $start) { throw new Sabre_DAV_Exception_RequestedRangeNotSatisfiable('The end offset (' . $range[1] . ') is lower than the start offset (' . $range[0] . ')'); } if ($end - $start + 1 != $len) { throw new Sabre_DAV_Exception_RequestedRangeNotSatisfiable('Actual data length (' . $len . ') is not consistent with begin (' . $range[0] . ') and end (' . $range[1] . ') offsets'); } // Checking If-None-Match and related headers. if (!$this->server->checkPreconditions()) { return; } if (!$this->server->broadcastEvent('beforeWriteContent', array($uri, $node, null))) { return; } $body = $this->server->httpRequest->getBody(); $etag = $node->putRange($body, $start - 1); $this->server->broadcastEvent('afterWriteContent', array($uri, $node)); $this->server->httpResponse->setHeader('Content-Length', '0'); if ($etag) { $this->server->httpResponse->setHeader('ETag', $etag); } $this->server->httpResponse->sendStatus(204); return false; }
/** * @covers Sabre_DAV_Server::checkPreconditions */ public function testIfUnmodifiedSinceInvalidDate() { $root = new Sabre_DAV_SimpleCollection('root', array(new Sabre_DAV_ServerPreconditionsNode())); $server = new Sabre_DAV_Server($root); $httpRequest = new Sabre_HTTP_Request(array('HTTP_IF_UNMODIFIED_SINCE' => 'Sun, 06 Nov 1984 08:49:37 CET', 'REQUEST_URI' => '/foo')); $server->httpRequest = $httpRequest; $server->httpResponse = new Sabre_HTTP_ResponseMock(); $this->assertTrue($server->checkPreconditions()); }