vendor/can/rest/src/Can/RestBundle/EventListener/WantDigestListener.php line 39

Open in your IDE?
  1. <?php
  2. namespace Can\RestBundle\EventListener;
  3. use Can\RestBundle\CanRestBundle;
  4. use Can\RestBundle\Negotiation\NegotiationConfiguration;
  5. use Can\RestBundle\Negotiation\NegotiationHeader;
  6. use Can\RestBundle\Negotiation\StopNegotiationException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
  11. use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
  12. /**
  13.  * Performs the instance digest negotiation.
  14.  *
  15.  * This is supposed to be performed after actual content negotiation takes
  16.  * place, so if an error occurs, the error response may be generated in the
  17.  * appropriate format, if necessary.
  18.  *
  19.  * @group negotiation
  20.  *
  21.  * @package can/rest-bundle
  22.  * @author lechecacharro <lechecacharro@gmail.com>
  23.  */
  24. class WantDigestListener extends NegotiationListener
  25. {
  26.     /**
  27.      * Performs the instance digest negotiation.
  28.      *
  29.      * @param RequestEvent $event
  30.      *
  31.      * @throws NotAcceptableHttpException
  32.      * @throws UnprocessableEntityHttpException
  33.      */
  34.     public function onRequest(RequestEvent $event): void
  35.     {
  36.         if (! $this->configuration->isAcceptInstanceDigests()) {
  37.             return;
  38.         }
  39.         if (! $event->isMasterRequest()) {
  40.             return;
  41.         }
  42.         $request $event->getRequest();
  43.         if (! $request->attributes->get(CanRestBundle::ATTR_SERVICE_ZONE)) {
  44.             return;
  45.         }
  46.         $wantDigest $this->getWantDigest($request);
  47.         if (! $wantDigest) {
  48.             return;
  49.         }
  50.         $digest null;
  51.         try {
  52.             $digest $this->negotiate($this->provider->getDigestNegotiator(), $wantDigest);
  53.         } catch (StopNegotiationException $e) {
  54.             // Failthrough
  55.         }
  56.         if (null === $digest) {
  57.             if ($this->configuration->isStrictDigestPolicy()) {
  58.                 $event->setResponse($this->createDigestClientErrorResponse());
  59.             } else {
  60.                 // Just choose an arbitrary ingest algorithm and continue --
  61.                 // currently we choose the one that requires less effort to
  62.                 // the server
  63.                 $digest $this->selectArbitraryDigest();
  64.             }
  65.         }
  66.         $request->attributes->set(CanRestBundle::ATTR_NEGOTIATION_DIGEST$digest);
  67.     }
  68.     /**
  69.      * @param Request $request
  70.      *
  71.      * @return string | null
  72.      */
  73.     private function getWantDigest(Request $request): ?string
  74.     {
  75.         return $this->getNegotiationHeader($requestNegotiationHeader::WANT_DIGEST);
  76.     }
  77.     /**
  78.      * @return Response
  79.      */
  80.     private function createDigestClientErrorResponse(): Response
  81.     {
  82.         $response = new Response(''$this->configuration->getDigestClientErrorStatus());
  83.         $response->headers->set(NegotiationHeader::WANT_DIGESTimplode(', '$this->configuration->getDigests()));
  84.         return $response;
  85.     }
  86.     /**
  87.      * Note that it is safe to assume that the list of digests algorithm names
  88.      * is not empty, as the {@link NegotiationConfiguration::isAcceptInstanceDigests()}
  89.      * method has already been called.
  90.      *
  91.      * @return string
  92.      */
  93.     private function selectArbitraryDigest(): string
  94.     {
  95.         // FIXME
  96.         // Use a priority list; do not choose at random
  97.         return $this->configuration->getDigests()[0];
  98.     }
  99. }