protected function getDestinationPath(Uri $baseUri, HttpRequest $request) { if (!$request->hasHeader('Destination')) { throw new BadRequestException(); } $dest = $request->getHeader('Destination'); if (preg_match("'^(?:https?:)?//'i", $dest)) { try { $uri = new Uri($dest); } catch (\Exception $e) { throw new BadRequestException($e); } if (0 !== strpos((string) $uri, rtrim($baseUri, '/') . '/')) { throw new WebDavException(WebDav::CODE_BAD_GATEWAY); } return Uri::decode(substr($uri->getPath(), strlen(rtrim($baseUri->getPath(), '/') . '/'))); } if (preg_match("'^/.*'", $dest)) { $path = '/' . trim($dest, '/'); $base = rtrim('/' . $baseUri->getPath(), '/') . '/'; if (0 !== strpos($path, $base)) { throw new WebDavException(WebDav::CODE_BAD_GATEWAY); } return Uri::decode(substr($path, strlen($base))); } throw new BadRequestException(); }
public function handle($path, Uri $baseUri, HttpRequest $request, StorageInterface $storage) { if (!$request->isPut()) { return; } if ($request->hasHeader('Content-Range')) { throw new BadRequestException(); } $stream = $request->hasEntity() ? $request->getEntity()->getInputStream() : new StringStream(); $created = false; $storage->beginTransaction(); try { try { $resource = $storage->findResource($path); if ($resource->isCollection()) { throw new MethodNotAllowedException(); } $resource = $storage->updateResource($resource, $stream); } catch (\OutOfBoundsException $e) { $parts = explode('/', $path); $name = array_pop($parts); try { $parent = $storage->findResource(implode('/', $parts)); } catch (\OutOfBoundsException $ex) { throw new WebDavException(WebDav::CODE_CONFLICT, $ex); } if (!$parent->isCollection()) { throw new WebDavException(WebDav::CODE_CONFLICT); } $resource = $storage->createResource($parent, $name, $stream); $created = true; } } catch (\Exception $e) { $storage->rollBack(); throw $e; } $storage->commit(); $response = new HttpResponse(empty($created) ? WebDav::CODE_NO_CONTENT : WebDav::CODE_CREATED); $response->setHeader('ETag', $resource->getEtag()); return $response; }
public function handle($path, Uri $baseUri, HttpRequest $request, StorageInterface $storage) { if (!$request->isDelete()) { return; } $resource = $storage->findResource($path); if ($resource->isCollection() && $request->hasHeader('Depth')) { $depth = $request->getHeader('Depth', 'infinity'); if ($depth != 'infinity') { throw new BadRequestException(); } } $storage->beginTransaction(); try { $storage->deleteResource($resource); } catch (\Exception $e) { $storage->rollBack(); throw $e; } $storage->commit(); return new HttpResponse(WebDav::CODE_NO_CONTENT); }
protected function handleUnlock(ResourceInterface $resource, Uri $baseUri, HttpRequest $request, LockStorageInterface $storage) { if (!$resource instanceof LockableResourceInterface) { throw new MethodNotAllowedException(); } if (!$resource->isLockSupported()) { throw new MethodNotAllowedException(); } if (!$resource->isLocked()) { throw new LockTokenMatchesRequestUriException(WebDav::CODE_CONFLICT); } if (!$request->hasHeader('Lock-Token')) { throw new BadRequestException(); } try { $tmp = $request->getHeader('Lock-Token', ''); $m = NULL; if (!preg_match("'^<?urn:webdav:lock:([0-9a-f\\-]{36})>?\$'i", $tmp, $m)) { throw new BadRequestException(); } $token = new UUID($m[1]); } catch (\InvalidArgumentException $e) { throw new BadRequestException($e); } $lockInfo = $resource->getLockInfo(); if ($token != $lockInfo->getToken() || $lockInfo->getExpires() < new \DateTime()) { throw new LockTokenMatchesRequestUriException(WebDav::CODE_CONFLICT); } $storage->beginTransaction(); try { $storage->removeLock($lockInfo); } catch (\Exception $e) { $storage->rollBack(); throw $e; } $storage->commit(); return new HttpResponse(Http::CODE_NO_CONTENT); }