-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel][HttpCache][RFC] SSI kernel proxy #6766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
323dc7f
SSI-Proxy
kingcrunch b22131a
Refactoring into IncludeProxy-namespace with SSI-/ESI-IncludeStrategy
kingcrunch 30042de
Remove ESI from HttpCache
kingcrunch 9945565
Updated compatibility to symfony
kingcrunch 3ca14d3
Extracted closure
kingcrunch 1d36a4e
Remove interface
kingcrunch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Symfony/Bundle/FrameworkBundle/Resources/config/ssi.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="ssi_listener.class">Symfony\Component\HttpKernel\EventListener\SsiListener</parameter> | ||
<parameter key="http_content_renderer.strategy.ssi.class">Symfony\Component\HttpKernel\RenderingStrategy\SsiRenderingStrategy</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="ssi_listener" class="%ssi_listener.class%"> | ||
<tag name="kernel.event_subscriber"/> | ||
</service> | ||
|
||
<service id="http_content_renderer.strategy.ssi" class="%http_content_renderer.strategy.ssi.class%"> | ||
<tag name="kernel.content_renderer_strategy" /> | ||
<argument type="service" id="http_content_renderer.strategy.default" /> | ||
<argument type="service" id="http_content_renderer.strategy.default"/> | ||
<call method="setProxyPath"><argument>%http_content_renderer.proxy_path%</argument></call> | ||
</service> | ||
</services> | ||
</container> |
53 changes: 53 additions & 0 deletions
53
src/Symfony/Component/HttpKernel/EventListener/SsiListener.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?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\EventListener; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* SsiListener adds a Surrogate-Control HTTP header when the Response needs to be parsed for SSI. | ||
* | ||
* @author Sebastian Krebs <krebs.seb@gmail.com> | ||
*/ | ||
class SsiListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* Filters the Response. | ||
* | ||
* @param FilterResponseEvent $event A FilterResponseEvent instance | ||
*/ | ||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) { | ||
$this->updateResponseHeader($event->getResponse()); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
); | ||
} | ||
|
||
private function updateResponseHeader (Response $response) | ||
{ | ||
if (false !== strpos($response->getContent(), '<!--#include')) { | ||
$header = $response->headers->get('Surrogate-Control'); | ||
$response->headers->set('Surrogate-Control', ($header ? $header . ', ' : '') . 'content=SSI/1.0'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/HttpKernel/IncludeProxy/EsiIncludeStrategy.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace Symfony\Component\HttpKernel\IncludeProxy; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
/** | ||
* @author Sebastian Krebs <krebs.seb@gmail.com> | ||
*/ | ||
class EsiIncludeStrategy implements IncludeStrategyInterface | ||
{ | ||
public function getName () | ||
{ | ||
return 'ESI/1.0'; | ||
} | ||
|
||
public function handle (HttpKernelInterface $kernel, Request $request, Response $response) | ||
{ | ||
$extractor = function ($attributes) { | ||
$options = array(); | ||
preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $attributes[1], $matches, PREG_SET_ORDER); | ||
foreach ($matches as $set) { | ||
$options[$set[1]] = $set[2]; | ||
} | ||
|
||
return array( | ||
isset($options['src']) ? $options['virtual'] : null, | ||
isset($options['alt']) ? $options['alt'] : null, | ||
isset($options['onerror']) && $options['onerror'] == 'continue' | ||
); | ||
|
||
}; | ||
return preg_replace_callback('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', new IncludeHandler($kernel, $request, $response, $extractor), $response->getContent()); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Component/HttpKernel/IncludeProxy/IncludeHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
namespace Symfony\Component\HttpKernel\IncludeProxy; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
/** | ||
* Sebastian Krebs <krebs.seb@gmail.com> | ||
* | ||
* This is a helper class, that act as and replace the closure of the | ||
* IncludeStrategy-implementations. | ||
* | ||
* @internal | ||
*/ | ||
class IncludeHandler | ||
{ | ||
private $kernel; | ||
private $request; | ||
private $response; | ||
private $extractor; | ||
|
||
public function __construct (HttpKernelInterface $kernel, Request $request, Response $response, $extractor) | ||
{ | ||
$this->kernel = $kernel; | ||
$this->request = $request; | ||
$this->response = $response; | ||
$this->extractor = $extractor; | ||
} | ||
|
||
public function __invoke ($attributes) | ||
{ | ||
list($source, $alternative, $ignoreErrors) = call_user_func($this->extractor, $attributes); | ||
return $this->handle($source, $alternative, $ignoreErrors); | ||
} | ||
|
||
private function handle ($source, $alternative, $ignoreErrors) | ||
{ | ||
if (!$source) { | ||
throw new \RuntimeException('Unable to process an include tag without a source attribute.'); | ||
} | ||
|
||
try { | ||
$subResponse = $this->request($source); | ||
$this->updateHeaders($this->response, $subResponse); | ||
return $subResponse->getContent(); | ||
} catch (\Exception $e) { | ||
if ($alternative) { | ||
return $this->handle($alternative, null, $ignoreErrors); | ||
} | ||
|
||
if ($ignoreErrors) { | ||
throw $e; | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
private function request ($source) | ||
{ | ||
$subRequest = Request::create($source, 'GET', array(), $this->request->cookies->all(), array(), $this->request->server->all()); | ||
|
||
$response = $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true); | ||
|
||
if (!$response->isSuccessful()) { | ||
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $subRequest->getUri(), $response->getStatusCode())); | ||
} | ||
return $response; | ||
} | ||
|
||
private function updateHeaders (Response $response, Response $subResponse) | ||
{ | ||
if ($this->response->isCacheable() && $subResponse->isCacheable()) { | ||
$maxAge = min($response->headers->getCacheControlDirective('max-age'), $subResponse->headers->getCacheControlDirective('max-age')); | ||
$sMaxAge = min($response->headers->getCacheControlDirective('s-maxage'), $subResponse->headers->getCacheControlDirective('s-maxage')); | ||
$response->setSharedMaxAge($sMaxAge); | ||
$response->setMaxAge($maxAge); | ||
} else { | ||
$this->response->headers->set('Cache-Control', 'no-cache, must-revalidate'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor issue ..
{
needs to be on the next line