Skip to content

Instantly share code, notes, and snippets.

@ThijsFeryn
Created September 2, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThijsFeryn/abe63b6ac2e73fb35b188a6559ad97eb to your computer and use it in GitHub Desktop.
Save ThijsFeryn/abe63b6ac2e73fb35b188a6559ad97eb to your computer and use it in GitHub Desktop.
edgestash negotiation in Symfony
<?php
namespace App\EdgestashBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class EdgestashSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['request']
],
KernelEvents::RESPONSE => [
['response',-10]
],
];
}
public function request(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->headers->has('Surrogate-Capability') &&
false !== strpos(
$request->headers->get('Surrogate-Capability'),
'edgestash="EDGESTASH/2.1"'
)
) {
$request->attributes->set('edgestash',true);
} else {
$request->attributes->set('edgestash',false);
}
}
public function response(FilterResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
if($request->attributes->has('edgestash') && $request->attributes->get('edgestash') !== false) {
$response->headers->set('Surrogate-Control','edgestash="EDGESTASH/2.1"',false);
if($request->attributes->has('edgestash-json-urls')
&& count($request->attributes->get('edgestash-json-urls')) > 0) {
$urls = array_unique($request->attributes->get('edgestash-json-urls'));
foreach ($urls as $url) {
$response->headers->set('Link','<'.$url.'>; rel=edgestash',false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment