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; }
/** * @Route("POST /deployments/archive") */ public function deployArchive(HttpRequest $request, $name = '', array $extensions = []) { if (!$request->hasEntity()) { throw new \RuntimeException('No file has been uploaded'); } $builder = $this->repositoryService->createDeployment($name); $builder->addExtensions($extensions); $in = $request->getEntity()->getInputStream(); $archive = tempnam(sys_get_temp_dir(), 'era'); $fp = fopen($archive, 'wb'); try { while (false !== ($chunk = $in->read())) { fwrite($fp, $chunk); } } finally { @fclose($fp); } $builder->addArchive($archive); $deployment = $this->repositoryService->deploy($builder); $definitions = $this->repositoryService->createProcessDefinitionQuery()->deploymentId($deployment->getId())->findAll(); $response = new HttpResponse(Http::CODE_CREATED); $response->setHeader('Location', $this->uri->generate('../show-deployment', ['id' => $deployment->getId()])); $response->setEntity(new JsonEntity(['deployment' => $deployment, 'definitions' => $definitions, 'resources' => array_values($deployment->findResources()), '_links' => ['self' => $this->uri->generate('../show-deployment', ['id' => $deployment->getId()])]])); return $response; }
protected function sendChunkedEntity(StreamInterface $stream, HttpRequest $request, $compress) { $stream->write("\r\n"); $body = new ChunkEncodedOutputStream($stream); $body->setCloseCascade(false); if ($compress) { $body = new DeflateOutputStream($body, DeflateOutputStream::GZIP); } try { $request->getEntity()->send($body); } finally { $body->close(); } }
/** * {@inheritdoc} */ public function updateCredentials(HttpRequest $request) { $this->setStatus(self::AUTHENTICATION_NEEDED); $path = trim($request->getUri()->getPath(false), '/'); $logoutPath = trim((new Uri($this->auth->getLogoutUri()))->getPath(false), '/'); if ($path === $logoutPath) { return; } $session = $this->securityContext->getSession(); if ($session->isInitialized()) { $data = (array) $session->get($this->auth->getKey(), NULL); $identity = NULL; if (isset($data[FormAuthenticationProvider::SESSION_IDENTITY])) { $identity = (string) $data[FormAuthenticationProvider::SESSION_IDENTITY]; } if ($identity !== NULL) { $principal = $this->auth->getPrincipalProvider()->findPrincipal($identity); if ($principal !== NULL) { $this->setPrincipal($principal); return $this->setStatus(self::AUTHENTICATION_SUCCESSFUL); } } } if ($request->isPost(false) && $request->getMediaType()->is(Http::FORM_ENCODED)) { $fields = $request->getEntity()->getFields(); $data = isset($fields['auth']) ? (array) $fields['auth'] : []; $data = isset($data[$this->auth->getKey()]) ? (array) $data[$this->auth->getKey()] : []; if (array_key_exists(FormAuthenticationProvider::FIELD_USERNAME, $data)) { $this->username = (string) $data[FormAuthenticationProvider::FIELD_USERNAME]; } if (array_key_exists(FormAuthenticationProvider::FIELD_PASSWORD, $data)) { $this->password = (string) $data[FormAuthenticationProvider::FIELD_PASSWORD]; } if (array_key_exists(FormAuthenticationProvider::FIELD_GUARD, $data)) { $guard = (string) $data[FormAuthenticationProvider::FIELD_GUARD]; $data = (array) $session->get($this->auth->getKey(), NULL); if (array_key_exists(FormAuthenticationProvider::SESSION_GUARD, $data)) { if ((string) $data[FormAuthenticationProvider::SESSION_GUARD] == $guard) { $this->guarded = true; } } } return $this->setStatus(self::AUTHENTICATION_NEEDED); } }
/** * Compute the content MD5 hash of the request body (streams request body contents to a * temporary file and incrementaly computes the hash value replacing the requests input * URL with the URL of the created file). * * @param HttpRequest $request * @return string */ protected function computeContentMd5(HttpRequest $request) { if (!$request->hasEntity()) { return md5(''); } $hash = hash_init('md5'); $in = $request->getEntity()->getInputStream(); $tmp = new \SplTempFileObject(); $fp = $tmp->openFile('wb', false); try { flock($fp, LOCK_EX); while (false !== ($chunk = $in->read())) { hash_update($hash, $chunk); fwrite($fp, $chunk); } } finally { @fclose($fp); } $request->setEntity(new StreamEntity($tmp->openFile('rb', false))); return hash_final($hash); }