Example #1
0
 public function render($uri, Request $request, array $options = array())
 {
     if ($this->substitute) {
         return $this->defaultStrategy->render($uri, $request, $options);
     }
     if ($this->useHeader && !\strpos($request->headers->get('Surrogate-Capability', ''), 'SSI/1.0')) {
         return $this->defaultStrategy->render($uri, $request, $options);
     }
     if ($uri instanceof ControllerReference) {
         $uri = $this->generateFragmentUri($uri, $request);
     }
     $uri = $this->signer->sign((substr($uri, 0, 1) == "/" ? $request->getSchemeAndHttpHost() : "") . $uri);
     if (!\strncmp($uri, $request->getSchemeAndHttpHost(), \strlen($request->getSchemeAndHttpHost()))) {
         $uri = \substr($uri, \strlen($request->getSchemeAndHttpHost()));
     }
     return new Response(\sprintf('<!--#include virtual="%s" -->', $uri));
 }
 public function register(Application $app)
 {
     if (!class_exists('Symfony\\Component\\HttpFoundation\\RequestStack')) {
         throw new \LogicException('The HTTP Fragment service provider only works with Symfony 2.4+.');
     }
     $app['fragment.handler'] = $app->share(function ($app) {
         if (Kernel::VERSION_ID >= 20800) {
             return new FragmentHandler($app['request_stack'], $app['fragment.renderers'], $app['debug']);
         }
         return new FragmentHandler($app['fragment.renderers'], $app['debug'], $app['request_stack']);
     });
     $app['fragment.renderer.inline'] = $app->share(function ($app) {
         $renderer = new InlineFragmentRenderer($app['kernel'], $app['dispatcher']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     });
     $app['fragment.renderer.hinclude'] = $app->share(function ($app) {
         $renderer = new HIncludeFragmentRenderer(null, $app['uri_signer'], $app['fragment.renderer.hinclude.global_template'], $app['charset']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     });
     $app['fragment.renderer.esi'] = $app->share(function ($app) {
         $renderer = new EsiFragmentRenderer($app['http_cache.esi'], $app['fragment.renderer.inline']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     });
     $app['fragment.listener'] = $app->share(function ($app) {
         return new FragmentListener($app['uri_signer'], $app['fragment.path']);
     });
     $app['uri_signer'] = $app->share(function ($app) {
         return new UriSigner($app['uri_signer.secret']);
     });
     $app['uri_signer.secret'] = md5(__DIR__);
     $app['fragment.path'] = '/_fragment';
     $app['fragment.renderer.hinclude.global_template'] = null;
     $app['fragment.renderers'] = $app->share(function ($app) {
         $renderers = array($app['fragment.renderer.inline'], $app['fragment.renderer.hinclude']);
         if (isset($app['http_cache.esi'])) {
             $renderers[] = $app['fragment.renderer.esi'];
         }
         return $renderers;
     });
 }
 public function register(Container $app)
 {
     $app['fragment.handler'] = function ($app) {
         if (Kernel::VERSION_ID >= 20800) {
             return new FragmentHandler($app['request_stack'], $app['fragment.renderers'], $app['debug']);
         }
         return new FragmentHandler($app['fragment.renderers'], $app['debug'], $app['request_stack']);
     };
     $app['fragment.renderer.inline'] = function ($app) {
         $renderer = new InlineFragmentRenderer($app['kernel'], $app['dispatcher']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     };
     $app['fragment.renderer.hinclude'] = function ($app) {
         $renderer = new HIncludeFragmentRenderer(null, $app['uri_signer'], $app['fragment.renderer.hinclude.global_template'], $app['charset']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     };
     $app['fragment.renderer.esi'] = function ($app) {
         $renderer = new EsiFragmentRenderer($app['http_cache.esi'], $app['fragment.renderer.inline']);
         $renderer->setFragmentPath($app['fragment.path']);
         return $renderer;
     };
     $app['fragment.listener'] = function ($app) {
         return new FragmentListener($app['uri_signer'], $app['fragment.path']);
     };
     $app['uri_signer'] = function ($app) {
         return new UriSigner($app['uri_signer.secret']);
     };
     $app['uri_signer.secret'] = md5(__DIR__);
     $app['fragment.path'] = '/_fragment';
     $app['fragment.renderer.hinclude.global_template'] = null;
     $app['fragment.renderers'] = function ($app) {
         $renderers = array($app['fragment.renderer.inline'], $app['fragment.renderer.hinclude']);
         if (isset($app['http_cache.esi'])) {
             $renderers[] = $app['fragment.renderer.esi'];
         }
         return $renderers;
     };
 }
 public function testExceptionInSubRequestsDoesNotMangleOutputBuffers()
 {
     $resolver = $this->getMock('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface');
     $resolver->expects($this->once())->method('getController')->will($this->returnValue(function () {
         ob_start();
         echo 'bar';
         throw new \RuntimeException();
     }));
     $resolver->expects($this->once())->method('getArguments')->will($this->returnValue(array()));
     $kernel = new HttpKernel(new EventDispatcher(), $resolver);
     $renderer = new InlineFragmentRenderer($kernel);
     // simulate a main request with output buffering
     ob_start();
     echo 'Foo';
     // simulate a sub-request with output buffering and an exception
     $renderer->render('/', Request::create('/'), array('ignore_errors' => true));
     $this->assertEquals('Foo', ob_get_clean());
 }
 public function testESIHeaderIsKeptInSubrequest()
 {
     $expectedSubRequest = Request::create('/');
     $expectedSubRequest->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
         $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
         $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
     }
     $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($expectedSubRequest));
     $request = Request::create('/');
     $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     $strategy->render('/', $request);
 }
 public function testHeadersPossiblyResultingIn304AreNotAssignedToSubrequest()
 {
     $expectedSubRequest = Request::create('/');
     if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
         $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
         $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
     }
     $strategy = new InlineFragmentRenderer($this->getKernelExpectingRequest($expectedSubRequest));
     $request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_IF_MODIFIED_SINCE' => 'Fri, 01 Jan 2016 00:00:00 GMT', 'HTTP_IF_NONE_MATCH' => '*'));
     $strategy->render('/', $request);
 }
 public function testESIHeaderIsKeptInSubrequest()
 {
     $expectedSubRequest = Request::create('/');
     $expectedSubRequest->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     if (Request::getTrustedHeaderName(Request::HEADER_CLIENT_IP)) {
         $expectedSubRequest->headers->set('x-forwarded-for', array('127.0.0.1'));
         $expectedSubRequest->server->set('HTTP_X_FORWARDED_FOR', '127.0.0.1');
     }
     $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
     $kernel->expects($this->any())->method('handle')->with($expectedSubRequest);
     $strategy = new InlineFragmentRenderer($kernel);
     $request = Request::create('/');
     $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     $strategy->render('/', $request);
 }
 /**
  * {@inheritdoc}
  */
 protected function createSubRequest($uri, Request $request)
 {
     $subRequest = parent::createSubRequest($uri, $request);
     $subRequest->attributes->set('server_cache', true);
     return $subRequest;
 }
 public function testESIHeaderIsKeptInSubrequest()
 {
     $expectedSubRequest = Request::create('/');
     $expectedSubRequest->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     $kernel = $this->getMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
     $kernel->expects($this->any())->method('handle')->with($expectedSubRequest);
     $strategy = new InlineFragmentRenderer($kernel);
     $request = Request::create('/');
     $request->headers->set('Surrogate-Capability', 'abc="ESI/1.0"');
     $strategy->render('/', $request);
 }