Skip to content

Commit

Permalink
removed the need for a proxy route for rendering strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 23, 2013
1 parent b9f0e17 commit ad82893
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 274 deletions.
Expand Up @@ -22,15 +22,13 @@
<service id="http_content_renderer.strategy.default" class="%http_content_renderer.strategy.default.class%">
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="http_kernel" />
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
</service>

<service id="http_content_renderer.strategy.hinclude" class="%http_content_renderer.strategy.hinclude.class%">
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="templating" on-invalid="null" />
<argument type="service" id="uri_signer" />
<argument>%http_content_renderer.strategy.hinclude.global_template%</argument>
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
</service>

<!-- FIXME: make the listener registration optional via a configuration setting? -->
Expand Down
Expand Up @@ -22,7 +22,6 @@
<tag name="kernel.content_renderer_strategy" />
<argument type="service" id="esi" />
<argument type="service" id="http_content_renderer.strategy.default" />
<call method="setUrlGenerator"><argument type="service" id="router" /></call>
</service>
</services>
</container>

This file was deleted.

Expand Up @@ -47,7 +47,7 @@ public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();

if ('_proxy' !== $request->attributes->get('_route')) {
if ('/_proxy' !== rawurldecode($request->getPathInfo())) {
return;
}

Expand Down Expand Up @@ -92,7 +92,7 @@ protected function getLocalIpAddresses()
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(array('onKernelRequest', 16)),
KernelEvents::REQUEST => array(array('onKernelRequest', 48)),
);
}
}
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DefaultRenderingStrategy extends GeneratorAwareRenderingStrategy
class DefaultRenderingStrategy extends ProxyAwareRenderingStrategy
{
private $kernel;

Expand Down
Expand Up @@ -21,7 +21,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class EsiRenderingStrategy extends GeneratorAwareRenderingStrategy
class EsiRenderingStrategy extends ProxyAwareRenderingStrategy
{
private $esi;
private $defaultStrategy;
Expand Down

This file was deleted.

Expand Up @@ -22,7 +22,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HIncludeRenderingStrategy extends GeneratorAwareRenderingStrategy
class HIncludeRenderingStrategy extends ProxyAwareRenderingStrategy
{
private $templating;
private $globalDefaultTemplate;
Expand Down
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\RenderingStrategy;

use Symfony\Component\HttpKernel\Controller\ControllerReference;
use Symfony\Component\HttpFoundation\Request;

/**
* Adds the possibility to generate a proxy URI for a given Controller.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class ProxyAwareRenderingStrategy implements RenderingStrategyInterface
{
/**
* Generates a proxy URI for a given controller.
*
* @param ControllerReference $reference A ControllerReference instance
* @param Request $request A Request instance
*
* @return string A proxy URI
*/
protected function generateProxyUri(ControllerReference $reference, Request $request)
{
if (!isset($reference->attributes['_format'])) {
$reference->attributes['_format'] = $request->getRequestFormat();
}

$reference->attributes['_controller'] = $reference->controller;

$reference->query['path'] = http_build_query($reference->attributes, '', '&');

return $request->getUriForPath('/_proxy?'.http_build_query($reference->query, '', '&'));
}
}
Expand Up @@ -28,10 +28,10 @@ protected function setUp()

public function testOnlyTriggeredOnProxyRoute()
{
$request = Request::create('http://example.com/foo?path=foo%3D=bar');
$request = Request::create('http://example.com/foo?path=foo%3Dbar%26_controller%3Dfoo');

$listener = new RouterProxyListener(new UriSigner('foo'));
$event = $this->createGetResponseEvent($request, 'foobar');
$event = $this->createGetResponseEvent($request);

$expected = $request->attributes->all();

Expand All @@ -46,7 +46,7 @@ public function testOnlyTriggeredOnProxyRoute()
*/
public function testAccessDeniedWithNonSafeMethods()
{
$request = Request::create('http://example.com/foo', 'POST');
$request = Request::create('http://example.com/_proxy', 'POST');

$listener = new RouterProxyListener(new UriSigner('foo'));
$event = $this->createGetResponseEvent($request);
Expand All @@ -59,7 +59,7 @@ public function testAccessDeniedWithNonSafeMethods()
*/
public function testAccessDeniedWithNonLocalIps()
{
$request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
$request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));

$listener = new RouterProxyListener(new UriSigner('foo'));
$event = $this->createGetResponseEvent($request);
Expand All @@ -72,48 +72,30 @@ public function testAccessDeniedWithNonLocalIps()
*/
public function testAccessDeniedWithWrongSignature()
{
$request = Request::create('http://example.com/foo', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
$request = Request::create('http://example.com/_proxy', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));

$listener = new RouterProxyListener(new UriSigner('foo'));
$event = $this->createGetResponseEvent($request);

$listener->onKernelRequest($event);
}

public function testWithSignatureAndNoPath()
public function testWithSignature()
{
$signer = new UriSigner('foo');
$request = Request::create($signer->sign('http://example.com/foo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
$request = Request::create($signer->sign('http://example.com/_proxy?path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));

$listener = new RouterProxyListener($signer);
$event = $this->createGetResponseEvent($request);

$listener->onKernelRequest($event);

$this->assertEquals(array('foo' => 'foo'), $request->attributes->get('_route_params'));
$this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params'));
$this->assertFalse($request->query->has('path'));
}

public function testWithSignatureAndPath()
private function createGetResponseEvent(Request $request)
{
$signer = new UriSigner('foo');
$request = Request::create($signer->sign('http://example.com/foo?path=bar%3Dbar'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));

$listener = new RouterProxyListener($signer);
$event = $this->createGetResponseEvent($request);

$listener->onKernelRequest($event);

$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $request->attributes->get('_route_params'));
$this->assertFalse($request->query->has('path'));
}

private function createGetResponseEvent(Request $request, $route = '_proxy')
{
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$request->attributes->set('_route', $route);
$request->attributes->set('_route_params', array('foo' => 'foo'));

return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
return new GetResponseEvent($this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'), $request, HttpKernelInterface::MASTER_REQUEST);
}
}

This file was deleted.

Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\EventDispatcher\EventDispatcher;

class DefaultRenderingStrategyTest extends AbstractRenderingStrategyTest
class DefaultRenderingStrategyTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
Expand All @@ -41,7 +41,6 @@ public function testRender()
public function testRenderWithControllerReference()
{
$strategy = new DefaultRenderingStrategy($this->getKernel($this->returnValue(new Response('foo'))));
$strategy->setUrlGenerator($this->getUrlGenerator());

$this->assertEquals('foo', $strategy->render(new ControllerReference('main_controller', array(), array()), Request::create('/'))->getContent());
}
Expand Down
Expand Up @@ -16,17 +16,13 @@
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpFoundation\Request;

class EsiRenderingStrategyTest extends AbstractRenderingStrategyTest
class EsiRenderingStrategyTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}

if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
$this->markTestSkipped('The "Routing" component is not available');
}
}

public function testRenderFallbackToDefaultStrategyIfNoRequest()
Expand All @@ -44,15 +40,14 @@ public function testRenderFallbackToDefaultStrategyIfEsiNotSupported()
public function testRender()
{
$strategy = new EsiRenderingStrategy(new Esi(), $this->getDefaultStrategy());
$strategy->setUrlGenerator($this->getUrlGenerator());

$request = Request::create('/');
$request->headers->set('Surrogate-Capability', 'ESI/1.0');

$this->assertEquals('<esi:include src="/" />', $strategy->render('/', $request)->getContent());
$this->assertEquals("<esi:comment text=\"This is a comment\" />\n<esi:include src=\"/\" />", $strategy->render('/', $request, array('comment' => 'This is a comment'))->getContent());
$this->assertEquals('<esi:include src="/" alt="foo" />', $strategy->render('/', $request, array('alt' => 'foo'))->getContent());
$this->assertEquals('<esi:include src="/main_controller.html" alt="/alt_controller.html" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent());
$this->assertEquals('<esi:include src="http://localhost/_proxy?path=_format%3Dhtml%26_controller%3Dmain_controller" alt="http://localhost/_proxy?path=_format%3Dhtml%26_controller%3Dalt_controller" />', $strategy->render(new ControllerReference('main_controller', array(), array()), $request, array('alt' => new ControllerReference('alt_controller', array(), array())))->getContent());
}

private function getDefaultStrategy($called = false)
Expand Down

0 comments on commit ad82893

Please sign in to comment.