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; }
protected function parseBody(HttpRequest $request) { if (!$request->hasEntity()) { return new AllPropRequest(); } $contents = trim($request->getContents()); if ($contents == '') { return new AllPropRequest(); } try { $xml = (new XmlDocumentBuilder())->buildFromSource($contents); } catch (XmlParseException $e) { throw new BadRequestException($e); } $root = $xml->documentElement; if ($root->namespaceURI != WebDav::NS_DAV || $root->localName != 'propfind') { throw new WebDavException(WebDav::CODE_UNPROCESSABLE_ENTITY); } $xpath = new \DOMXPath($xml); $xpath->registerNamespace('D', WebDav::NS_DAV); $nodes = $xpath->query('/D:propfind/D:*'); if ($nodes->length == 0) { throw new WebDavException(WebDav::CODE_UNPROCESSABLE_ENTITY); } $nodes = $xpath->query('/D:propfind/D:propname'); if ($nodes->length == 1) { return new PropNameRequest(); } $nodes = $xpath->query('/D:propfind/D:allprop'); if ($nodes->length == 1) { return $this->parseAllPropRequest($nodes->item(0)->parentNode, $xpath); } $nodes = $xpath->query('/D:propfind/D:prop'); if ($nodes->length > 0) { return $this->parsePropfindRequest($nodes, $xpath); } throw new WebDavException(WebDav::CODE_UNPROCESSABLE_ENTITY); }
protected function sendEntity(StreamInterface $stream, HttpRequest $request, $compress) { if (!$request->hasEntity()) { $stream->write("Content-Length: 0\r\n\r\n"); return; } $buffer = new StringStream(); try { if ($compress) { $body = new DeflateOutputStream($buffer, DeflateOutputStream::GZIP); $body->setCloseCascade(false); } else { $body = $buffer; } $request->getEntity()->send($body); $body->close(); $stream->write(sprintf("Content-Length: %u\r\n\r\n", $buffer->tell())); $buffer->rewind(); Stream::pipe($buffer, $stream); } finally { $buffer->close(); } }
public function buildHttpRequest() { try { $method = $this->getRequestMethod(); $request = new HttpRequest($this->getRequestUri(), $method, $this->getProtocol()); $request->setRawUri($this->getRawRequestUri()); $request->setPathBase(trim($this->getBaseUri()->getPath(false), '/')); $request->setCookies($this->getCookies()); foreach ($this->getHeaders() as $name => $value) { $request->setHeader($name, $value); if ($name == 'content-type') { $mediaType = $request->getMediaType(); if ($mediaType->is(Http::FORM_ENCODED)) { if ($method != Http::METHOD_POST) { $fields = Uri::parseQuery(file_get_contents($this->getInputUrl())); $request->setEntity(new FormEntity($fields)); } else { $request->setEntity(new FormEntity($this->getPostParams())); } } elseif ($mediaType->is(Http::FORM_MULTIPART_ENCODED)) { if ($method != Http::METHOD_POST) { throw new \RuntimeException('Multipart requests must be POST'); } $request->setEntity(new MultipartFormEntity($this->getPostParams(), $this->getFiles())); } } } if (!$request->hasEntity()) { $request->setEntity(new StreamEntity(ResourceInputStream::fromUrl($this->getInputUrl()))); } } catch (BadRequestException $e) { throw $e; } catch (\Exception $e) { throw new BadRequestException($e); } return $request; }
/** * @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; }
/** * 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); }