Example #1
0
 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;
 }
Example #2
0
 /**
  * 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);
 }