Exemplo n.º 1
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);
 }