/** * Fetches properties for a path. * * This method received a PropFind object, which contains all the * information about the properties that need to be fetched. * * Ususually you would just want to call 'get404Properties' on this object, * as this will give you the _exact_ list of properties that need to be * fetched, and haven't yet. * * @param string $path * @param PropFind $propFind * @return void */ public function propFind($path, PropFind $propFind) { $propertyNames = $propFind->get404Properties(); if (!$propertyNames) { return; } // error_log("propFind: path($path), " . print_r($propertyNames, true)); $cachedNodes = \CB\Cache::get('DAVNodes'); // error_log("propFind: " . print_r($cachedNodes, true)); $path = trim($path, '/'); $path = str_replace('\\', '/', $path); // Node with $path is not in cached nodes, return if (!array_key_exists($path, $cachedNodes)) { return; } $node = $cachedNodes[$path]; // while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { // $propFind->set($row['name'], $row['value']); // } foreach ($propertyNames as $prop) { if ($prop == '{DAV:}creationdate') { $dttm = new \DateTime($node['cdate']); // $dttm->getTimestamp() $propFind->set($prop, \Sabre\HTTP\Util::toHTTPDate($dttm)); } elseif ($prop == '{urn:schemas-microsoft-com:office:office}modifiedby' or $prop == '{DAV:}getmodifiedby') { // This has to be revised, because the User.login differs from User.DisplayName // moreover, during an edit, Word will check for File Properties and we // tell Word that the file is modified by another user // $propFind->set($prop, \CB\User::getDisplayName($node['uid'])); } } }
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'); } }
/** * serialize * * @param DAV\Server $server * @param \DOMElement $prop * @return void */ public function serialize(DAV\Server $server, \DOMElement $prop) { $doc = $prop->ownerDocument; //$prop->setAttribute('xmlns:b','urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/'); //$prop->setAttribute('b:dt','dateTime.rfc1123'); $prop->nodeValue = HTTP\Util::toHTTPDate($this->time); }
function testHEAD() { $request = new HTTP\Request('HEAD', '/test.txt'); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(['X-Sabre-Version' => [DAV\Version::VERSION], 'Content-Type' => ['application/octet-stream'], 'Content-Length' => [13], 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 'ETag' => ['"' . md5_file($this->tempDir . '/test.txt') . '"']], $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('', $this->response->body); }
function setUp() { parent::setUp(); $this->server->createFile('files/test.txt', 'Test contents'); $this->lastModified = HTTP\Util::toHTTPDate(new DateTime('@' . $this->server->tree->getNodeForPath('files/test.txt')->getLastModified())); $stream = popen('echo "Test contents"', 'r'); $streamingFile = new Mock\StreamingFile('no-seeking.txt', $stream); $streamingFile->setSize(12); $this->server->tree->getNodeForPath('files')->addNode($streamingFile); }
function testHEAD() { $serverVars = array('REQUEST_URI' => '/test.txt', 'REQUEST_METHOD' => 'HEAD'); $request = new HTTP\Request($serverVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('Content-Type' => 'application/octet-stream', 'Content-Length' => 13, 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))), 'ETag' => '"' . md5_file($this->tempDir . '/test.txt') . '"'), $this->response->headers); $this->assertEquals('HTTP/1.1 200 OK', $this->response->status); $this->assertEquals('', $this->response->body); }
function testBaseUri() { $serverVars = ['REQUEST_URI' => '/blabla/test.txt', 'REQUEST_METHOD' => 'GET']; $filename = $this->tempDir . '/test.txt'; $request = HTTP\Sapi::createFromServerArray($serverVars); $this->server->setBaseUri('/blabla/'); $this->assertEquals('/blabla/', $this->server->getBaseUri()); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(['X-Sabre-Version' => [Version::VERSION], 'Content-Type' => ['application/octet-stream'], 'Content-Length' => [13], 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($filename)))], 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"']], $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }
/** * This method checks the main HTTP preconditions. * * Currently these are: * * If-Match * * If-None-Match * * If-Modified-Since * * If-Unmodified-Since * * The method will return true if all preconditions are met * The method will return false, or throw an exception if preconditions * failed. If false is returned the operation should be aborted, and * the appropriate HTTP response headers are already set. * * Normally this method will throw 412 Precondition Failed for failures * related to If-None-Match, If-Match and If-Unmodified Since. It will * set the status to 304 Not Modified for If-Modified_since. * * @param RequestInterface $request * @param ResponseInterface $response * @return bool */ function checkPreconditions(RequestInterface $request, ResponseInterface $response) { $path = $request->getPath(); $node = null; $lastMod = null; $etag = null; if ($ifMatch = $request->getHeader('If-Match')) { // If-Match contains an entity tag. Only if the entity-tag // matches we are allowed to make the request succeed. // If the entity-tag is '*' we are only allowed to make the // request succeed if a resource exists at that url. try { $node = $this->tree->getNodeForPath($path); } catch (Exception\NotFound $e) { throw new Exception\PreconditionFailed('An If-Match header was specified and the resource did not exist', 'If-Match'); } // Only need to check entity tags if they are not * if ($ifMatch !== '*') { // There can be multiple ETags $ifMatch = explode(',', $ifMatch); $haveMatch = false; foreach ($ifMatch as $ifMatchItem) { // Stripping any extra spaces $ifMatchItem = trim($ifMatchItem, ' '); $etag = $node instanceof IFile ? $node->getETag() : null; if ($etag === $ifMatchItem) { $haveMatch = true; } else { // Evolution has a bug where it sometimes prepends the " // with a \. This is our workaround. if (str_replace('\\"', '"', $ifMatchItem) === $etag) { $haveMatch = true; } } } if (!$haveMatch) { if ($etag) $response->setHeader('ETag', $etag); throw new Exception\PreconditionFailed('An If-Match header was specified, but none of the specified the ETags matched.', 'If-Match'); } } } if ($ifNoneMatch = $request->getHeader('If-None-Match')) { // The If-None-Match header contains an ETag. // Only if the ETag does not match the current ETag, the request will succeed // The header can also contain *, in which case the request // will only succeed if the entity does not exist at all. $nodeExists = true; if (!$node) { try { $node = $this->tree->getNodeForPath($path); } catch (Exception\NotFound $e) { $nodeExists = false; } } if ($nodeExists) { $haveMatch = false; if ($ifNoneMatch === '*') $haveMatch = true; else { // There might be multiple ETags $ifNoneMatch = explode(',', $ifNoneMatch); $etag = $node instanceof IFile ? $node->getETag() : null; foreach ($ifNoneMatch as $ifNoneMatchItem) { // Stripping any extra spaces $ifNoneMatchItem = trim($ifNoneMatchItem, ' '); if ($etag === $ifNoneMatchItem) $haveMatch = true; } } if ($haveMatch) { if ($etag) $response->setHeader('ETag', $etag); if ($request->getMethod() === 'GET') { $response->setStatus(304); return false; } else { throw new Exception\PreconditionFailed('An If-None-Match header was specified, but the ETag matched (or * was specified).', 'If-None-Match'); } } } } if (!$ifNoneMatch && ($ifModifiedSince = $request->getHeader('If-Modified-Since'))) { // The If-Modified-Since header contains a date. We // will only return the entity if it has been changed since // that date. If it hasn't been changed, we return a 304 // header // Note that this header only has to be checked if there was no If-None-Match header // as per the HTTP spec. $date = HTTP\Util::parseHTTPDate($ifModifiedSince); if ($date) { if (is_null($node)) { $node = $this->tree->getNodeForPath($path); } $lastMod = $node->getLastModified(); if ($lastMod) { $lastMod = new \DateTime('@' . $lastMod); if ($lastMod <= $date) { $response->setStatus(304); $response->setHeader('Last-Modified', HTTP\Util::toHTTPDate($lastMod)); return false; } } } } if ($ifUnmodifiedSince = $request->getHeader('If-Unmodified-Since')) { // The If-Unmodified-Since will allow allow the request if the // entity has not changed since the specified date. $date = HTTP\Util::parseHTTPDate($ifUnmodifiedSince); // We must only check the date if it's valid if ($date) { if (is_null($node)) { $node = $this->tree->getNodeForPath($path); } $lastMod = $node->getLastModified(); if ($lastMod) { $lastMod = new \DateTime('@' . $lastMod); if ($lastMod > $date) { throw new Exception\PreconditionFailed('An If-Unmodified-Since header was specified, but the entity has been changed since the specified date.', 'If-Unmodified-Since'); } } } } // Now the hardest, the If: header. The If: header can contain multiple // urls, ETags and so-called 'state tokens'. // // Examples of state tokens include lock-tokens (as defined in rfc4918) // and sync-tokens (as defined in rfc6578). // // The only proper way to deal with these, is to emit events, that a // Sync and Lock plugin can pick up. $ifConditions = $this->getIfConditions($request); foreach ($ifConditions as $kk => $ifCondition) { foreach ($ifCondition['tokens'] as $ii => $token) { $ifConditions[$kk]['tokens'][$ii]['validToken'] = false; } } // Plugins are responsible for validating all the tokens. // If a plugin deemed a token 'valid', it will set 'validToken' to // true. $this->emit('validateTokens', [ $request, &$ifConditions ]); // Now we're going to analyze the result. // Every ifCondition needs to validate to true, so we exit as soon as // we have an invalid condition. foreach ($ifConditions as $ifCondition) { $uri = $ifCondition['uri']; $tokens = $ifCondition['tokens']; // We only need 1 valid token for the condition to succeed. foreach ($tokens as $token) { $tokenValid = $token['validToken'] || !$token['token']; $etagValid = false; if (!$token['etag']) { $etagValid = true; } // Checking the ETag, only if the token was already deamed // valid and there is one. if ($token['etag'] && $tokenValid) { // The token was valid, and there was an ETag. We must // grab the current ETag and check it. $node = $this->tree->getNodeForPath($uri); $etagValid = $node instanceof IFile && $node->getETag() == $token['etag']; } if (($tokenValid && $etagValid) ^ $token['negate']) { // Both were valid, so we can go to the next condition. continue 2; } } // If we ended here, it means there was no valid ETag + token // combination found for the current condition. This means we fail! throw new Exception\PreconditionFailed('Failed to find a valid token/etag combination for ' . $uri, 'If'); } return true; }
/** * @depends testRange */ function testIfRangeModificationDateModified() { $node = $this->server->tree->getNodeForPath('test.txt'); $serverVars = array('REQUEST_URI' => '/test.txt', 'REQUEST_METHOD' => 'GET', 'HTTP_RANGE' => 'bytes=2-5', 'HTTP_IF_RANGE' => '-2 years'); $request = HTTP\Sapi::createFromServerArray($serverVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('X-Sabre-Version' => [Version::VERSION], 'Content-Type' => ['application/octet-stream'], 'Content-Length' => [13], 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 'ETag' => ['"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"']), $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }
/** * This event is triggered after GET requests. * * This is used to transform data into jCal, if this was requested. * * @param RequestInterface $request * @param ResponseInterface $response * @return void */ function httpAfterGet(RequestInterface $request, ResponseInterface $response) { if (strpos($response->getHeader('Content-Type'), 'text/calendar') === false) { return; } $result = HTTP\Util::negotiate($request->getHeader('Accept'), ['text/calendar', 'application/calendar+json']); if ($result !== 'application/calendar+json') { // Do nothing return; } // Transforming. $vobj = VObject\Reader::read($response->getBody()); $jsonBody = json_encode($vobj->jsonSerialize()); $response->setBody($jsonBody); $response->setHeader('Content-Type', 'application/calendar+json'); $response->setHeader('Content-Length', strlen($jsonBody)); }
/** * Intercepts GET requests on calendar urls ending with ?export. * * @param RequestInterface $request * @param ResponseInterface $response * @return bool */ function httpGet(RequestInterface $request, ResponseInterface $response) { $queryParams = $request->getQueryParameters(); if (!array_key_exists('export', $queryParams)) { return; } $path = $request->getPath(); $node = $this->server->getProperties($path, ['{DAV:}resourcetype', '{DAV:}displayname', '{http://sabredav.org/ns}sync-token', '{DAV:}sync-token', '{http://apple.com/ns/ical/}calendar-color']); if (!isset($node['{DAV:}resourcetype']) || !$node['{DAV:}resourcetype']->is('{' . Plugin::NS_CALDAV . '}calendar')) { return; } // Marking the transactionType, for logging purposes. $this->server->transactionType = 'get-calendar-export'; $properties = $node; $start = null; $end = null; $expand = false; $componentType = false; if (isset($queryParams['start'])) { if (!ctype_digit($queryParams['start'])) { throw new BadRequest('The start= parameter must contain a unix timestamp'); } $start = DateTime::createFromFormat('U', $queryParams['start']); } if (isset($queryParams['end'])) { if (!ctype_digit($queryParams['end'])) { throw new BadRequest('The end= parameter must contain a unix timestamp'); } $end = DateTime::createFromFormat('U', $queryParams['end']); } if (isset($queryParams['expand']) && !!$queryParams['expand']) { if (!$start || !$end) { throw new BadRequest('If you\'d like to expand recurrences, you must specify both a start= and end= parameter.'); } $expand = true; $componentType = 'VEVENT'; } if (isset($queryParams['componentType'])) { if (!in_array($queryParams['componentType'], ['VEVENT', 'VTODO', 'VJOURNAL'])) { throw new BadRequest('You are not allowed to search for components of type: ' . $queryParams['componentType'] . ' here'); } $componentType = $queryParams['componentType']; } $format = \Sabre\HTTP\Util::Negotiate($request->getHeader('Accept'), ['text/calendar', 'application/calendar+json']); if (isset($queryParams['accept'])) { if ($queryParams['accept'] === 'application/calendar+json' || $queryParams['accept'] === 'jcal') { $format = 'application/calendar+json'; } } if (!$format) { $format = 'text/calendar'; } $this->generateResponse($path, $start, $end, $expand, $componentType, $format, $properties, $response); // Returning false to break the event chain return false; }
/** * Makes sure the supplied value is a valid RFC2616 date. * * If we would just use strtotime to get a valid timestamp, we have no way of checking if a * user just supplied the word 'now' for the date header. * * This function also makes sure the Date header is within 15 minutes of the operating * system date, to prevent replay attacks. * * @param string $dateHeader * @return bool */ protected function validateRFC2616Date($dateHeader) { $date = Util::parseHTTPDate($dateHeader); // Unknown format if (!$date) { $this->errorCode = self::ERR_INVALIDDATEFORMAT; return false; } $min = new \DateTime('-15 minutes'); $max = new \DateTime('+15 minutes'); // We allow 15 minutes around the current date/time if ($date > $max || $date < $min) { $this->errorCode = self::ERR_REQUESTTIMESKEWED; return false; } return $date; }
function testBaseUri() { $serverVars = array('REQUEST_URI' => '/blabla/test.txt', 'REQUEST_METHOD' => 'GET'); $request = HTTP\Sapi::createFromServerArray($serverVars); $this->server->setBaseUri('/blabla/'); $this->assertEquals('/blabla/', $this->server->getBaseUri()); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('X-Sabre-Version' => Version::VERSION, 'Content-Type' => 'application/octet-stream', 'Content-Length' => 13, 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))), $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }
/** * @depends testRange * @covers \Sabre\DAV\Server::httpGet */ function testIfRangeModificationDateModified() { $node = $this->server->tree->getNodeForPath('test.txt'); $serverVars = array('REQUEST_URI' => '/test.txt', 'REQUEST_METHOD' => 'GET', 'HTTP_RANGE' => 'bytes=2-5', 'HTTP_IF_RANGE' => '-2 years'); $request = new HTTP\Request($serverVars); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('Content-Type' => 'application/octet-stream', 'Content-Length' => 13, 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt'))), 'ETag' => '"' . md5(file_get_contents(SABRE_TEMPDIR . '/test.txt')) . '"'), $this->response->headers); $this->assertEquals('HTTP/1.1 200 OK', $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }
/** * This helper function performs the content-type negotiation for vcards. * * It will return one of the following strings: * 1. vcard3 * 2. vcard4 * 3. jcard * * It defaults to vcard3. * * @param string $input * @param string $mimeType * @return string */ protected function negotiateVCard($input, &$mimeType = null) { $result = HTTP\Util::negotiate( $input, [ // Most often used mime-type. Version 3 'text/x-vcard', // The correct standard mime-type. Defaults to version 3 as // well. 'text/vcard', // vCard 4 'text/vcard; version=4.0', // vCard 3 'text/vcard; version=3.0', // jCard 'application/vcard+json', ] ); $mimeType = $result; switch ($result) { default : case 'text/x-vcard' : case 'text/vcard' : case 'text/vcard; version=3.0' : $mimeType = 'text/vcard'; return 'vcard3'; case 'text/vcard; version=4.0' : return 'vcard4'; case 'application/vcard+json' : return 'jcard'; // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd }
/** * The serialize method is called during xml writing. * * It should use the $writer argument to encode this object into XML. * * Important note: it is not needed to create the parent element. The * parent element is already created, and we only have to worry about * attributes, child elements and text (if any). * * Important note 2: If you are writing any new elements, you are also * responsible for closing them. * * @param Writer $writer * @return void */ function xmlSerialize(Writer $writer) { $writer->write(HTTP\Util::toHTTPDate($this->time)); }
/** * This helper function performs the content-type negotiation for vcards. * * It will return one of the following strings: * 1. vcard3 * 2. vcard4 * 3. jcard * * It defaults to vcard3. * * @param string $input * @param string $mimeType * @return string */ protected function negotiateVCard($input, &$mimeType = null) { $result = HTTP\Util::negotiate($input, ['text/x-vcard', 'text/vcard', 'text/vcard; version=4.0', 'text/vcard; version=3.0', 'application/vcard+json']); $mimeType = $result; switch ($result) { default: case 'text/x-vcard': case 'text/vcard': case 'text/vcard; version=3.0': $mimeType = 'text/vcard'; return 'vcard3'; case 'text/vcard; version=4.0': return 'vcard4'; case 'application/vcard+json': return 'jcard'; // @codeCoverageIgnoreStart } // @codeCoverageIgnoreEnd }
/** * This method checks the main HTTP preconditions. * * Currently these are: * * If-Match * * If-None-Match * * If-Modified-Since * * If-Unmodified-Since * * The method will return true if all preconditions are met * The method will return false, or throw an exception if preconditions * failed. If false is returned the operation should be aborted, and * the appropriate HTTP response headers are already set. * * Normally this method will throw 412 Precondition Failed for failures * related to If-None-Match, If-Match and If-Unmodified Since. It will * set the status to 304 Not Modified for If-Modified_since. * * If the $handleAsGET argument is set to true, it will also return 304 * Not Modified for failure of the If-None-Match precondition. This is the * desired behaviour for HTTP GET and HTTP HEAD requests. * * @param bool $handleAsGET * @return bool */ public function checkPreconditions($handleAsGET = false) { $uri = $this->getRequestUri(); $node = null; $lastMod = null; $etag = null; if ($ifMatch = $this->httpRequest->getHeader('If-Match')) { // If-Match contains an entity tag. Only if the entity-tag // matches we are allowed to make the request succeed. // If the entity-tag is '*' we are only allowed to make the // request succeed if a resource exists at that url. try { $node = $this->tree->getNodeForPath($uri); } catch (Exception\NotFound $e) { throw new Exception\PreconditionFailed('An If-Match header was specified and the resource did not exist', 'If-Match'); } // Only need to check entity tags if they are not * if ($ifMatch !== '*') { // There can be multiple etags $ifMatch = explode(',', $ifMatch); $haveMatch = false; foreach ($ifMatch as $ifMatchItem) { // Stripping any extra spaces $ifMatchItem = trim($ifMatchItem, ' '); $etag = $node->getETag(); if ($etag === $ifMatchItem) { $haveMatch = true; } else { // Evolution has a bug where it sometimes prepends the " // with a \. This is our workaround. if (str_replace('\\"', '"', $ifMatchItem) === $etag) { $haveMatch = true; } } } if (!$haveMatch) { throw new Exception\PreconditionFailed('An If-Match header was specified, but none of the specified the ETags matched.', 'If-Match'); } } } if ($ifNoneMatch = $this->httpRequest->getHeader('If-None-Match')) { // The If-None-Match header contains an etag. // Only if the ETag does not match the current ETag, the request will succeed // The header can also contain *, in which case the request // will only succeed if the entity does not exist at all. $nodeExists = true; if (!$node) { try { $node = $this->tree->getNodeForPath($uri); } catch (Exception\NotFound $e) { $nodeExists = false; } } if ($nodeExists) { $haveMatch = false; if ($ifNoneMatch === '*') { $haveMatch = true; } else { // There might be multiple etags $ifNoneMatch = explode(',', $ifNoneMatch); $etag = $node->getETag(); foreach ($ifNoneMatch as $ifNoneMatchItem) { // Stripping any extra spaces $ifNoneMatchItem = trim($ifNoneMatchItem, ' '); if ($etag === $ifNoneMatchItem) { $haveMatch = true; } } } if ($haveMatch) { if ($handleAsGET) { $this->httpResponse->sendStatus(304); return false; } else { throw new Exception\PreconditionFailed('An If-None-Match header was specified, but the ETag matched (or * was specified).', 'If-None-Match'); } } } } if (!$ifNoneMatch && ($ifModifiedSince = $this->httpRequest->getHeader('If-Modified-Since'))) { // The If-Modified-Since header contains a date. We // will only return the entity if it has been changed since // that date. If it hasn't been changed, we return a 304 // header // Note that this header only has to be checked if there was no If-None-Match header // as per the HTTP spec. $date = HTTP\Util::parseHTTPDate($ifModifiedSince); if ($date) { if (is_null($node)) { $node = $this->tree->getNodeForPath($uri); } $lastMod = $node->getLastModified(); if ($lastMod) { $lastMod = new \DateTime('@' . $lastMod); if ($lastMod <= $date) { $this->httpResponse->sendStatus(304); $this->httpResponse->setHeader('Last-Modified', HTTP\Util::toHTTPDate($lastMod)); return false; } } } } if ($ifUnmodifiedSince = $this->httpRequest->getHeader('If-Unmodified-Since')) { // The If-Unmodified-Since will allow allow the request if the // entity has not changed since the specified date. $date = HTTP\Util::parseHTTPDate($ifUnmodifiedSince); // We must only check the date if it's valid if ($date) { if (is_null($node)) { $node = $this->tree->getNodeForPath($uri); } $lastMod = $node->getLastModified(); if ($lastMod) { $lastMod = new \DateTime('@' . $lastMod); if ($lastMod > $date) { throw new Exception\PreconditionFailed('An If-Unmodified-Since header was specified, but the entity has been changed since the specified date.', 'If-Unmodified-Since'); } } } } return true; }
function testBaseUri() { $serverVars = array('REQUEST_URI' => '/blabla/test.txt', 'REQUEST_METHOD' => 'GET'); $request = new HTTP\Request($serverVars); $this->server->setBaseUri('/blabla/'); $this->assertEquals('/blabla/', $this->server->getBaseUri()); $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(array('Content-Type' => 'application/octet-stream', 'Content-Length' => 13, 'Last-Modified' => HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))), $this->response->headers); $this->assertEquals('HTTP/1.1 200 OK', $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }
/** * @depends testRange */ function testIfRangeModificationDateModified() { $node = $this->server->tree->getNodeForPath('test.txt'); $request = new HTTP\Request('GET', '/test.txt', ['Range' => 'bytes=2-5', 'If-Range' => '-2 years']); $filename = SABRE_TEMPDIR . '/test.txt'; $this->server->httpRequest = $request; $this->server->exec(); $this->assertEquals(['X-Sabre-Version' => [Version::VERSION], 'Content-Type' => ['application/octet-stream'], 'Content-Length' => [13], 'Last-Modified' => [HTTP\Util::toHTTPDate(new \DateTime('@' . filemtime($this->tempDir . '/test.txt')))], 'ETag' => ['"' . sha1(fileinode($filename) . filesize($filename) . filemtime($filename)) . '"']], $this->response->getHeaders()); $this->assertEquals(200, $this->response->status); $this->assertEquals('Test contents', stream_get_contents($this->response->body)); }