-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] HttpRenderer fix + enhancements #6801
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
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @see Symfony\Component\HttpKernel\HttpContentRenderer | ||
* @see Symfony\Component\HttpKernel\HttpContentRendererInterface | ||
* @see Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface | ||
*/ | ||
class ControllerReference | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class HttpContentRenderer implements EventSubscriberInterface | ||
class HttpContentRenderer implements HttpContentRendererInterface, EventSubscriberInterface | ||
{ | ||
private $debug; | ||
private $strategies; | ||
|
@@ -33,28 +33,37 @@ class HttpContentRenderer implements EventSubscriberInterface | |
* Constructor. | ||
* | ||
* @param RenderingStrategyInterface[] $strategies An array of RenderingStrategyInterface instances | ||
* @param Boolean $debug Whether the debug mode is enabled or not | ||
* @param Boolean $debug Whether the debug mode is enabled | ||
*/ | ||
public function __construct(array $strategies = array(), $debug = false) | ||
{ | ||
$this->strategies = array(); | ||
foreach ($strategies as $strategy) { | ||
$this->addStrategy($strategy); | ||
} | ||
$this->debug = $debug; | ||
$this->debug = (Boolean) $debug; | ||
$this->requests = array(); | ||
} | ||
|
||
/** | ||
* Adds a rendering strategy. | ||
* | ||
* @param RenderingStrategyInterface $strategy A RenderingStrategyInterface instance | ||
* {@inheritdoc} | ||
*/ | ||
public function addStrategy(RenderingStrategyInterface $strategy) | ||
{ | ||
$this->strategies[$strategy->getName()] = $strategy; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::REQUEST => 'onKernelRequest', | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
); | ||
} | ||
|
||
/** | ||
* Stores the Request object. | ||
* | ||
|
@@ -76,22 +85,7 @@ public function onKernelResponse(FilterResponseEvent $event) | |
} | ||
|
||
/** | ||
* Renders a URI and returns the Response content. | ||
* | ||
* When the Response is a StreamedResponse, the content is streamed immediately | ||
* instead of being returned. | ||
* | ||
* Available options: | ||
* | ||
* * ignore_errors: true to return an empty string in case of an error | ||
* | ||
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance | ||
* @param string $strategy The strategy to use for the rendering | ||
* @param array $options An array of options | ||
* | ||
* @return string|null The Response content or null when the Response is streamed | ||
* | ||
* @throws \InvalidArgumentException when the strategy does not exist | ||
* {@inheritdoc} | ||
*/ | ||
public function render($uri, $strategy = 'default', array $options = array()) | ||
{ | ||
|
@@ -106,15 +100,9 @@ public function render($uri, $strategy = 'default', array $options = array()) | |
return $this->strategies[$strategy]->render($uri, $this->requests ? $this->requests[0] : null, $options); | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
KernelEvents::REQUEST => 'onKernelRequest', | ||
KernelEvents::RESPONSE => 'onKernelResponse', | ||
); | ||
} | ||
|
||
// to be removed in 2.3 | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fixOptions(array $options) | ||
{ | ||
// support for the standalone option is @deprecated in 2.2 and replaced with the strategy option | ||
|
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/HttpKernel/HttpContentRendererInterface.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,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ControllerReference; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\RenderingStrategy\RenderingStrategyInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Interface to be implemented by Http content renderers. | ||
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
interface HttpContentRendererInterface | ||
{ | ||
/** | ||
* Adds a rendering strategy. | ||
* | ||
* @param RenderingStrategyInterface $strategy A RenderingStrategyInterface instance | ||
*/ | ||
public function addStrategy(RenderingStrategyInterface $strategy); | ||
|
||
/** | ||
* Renders a URI and returns the Response content. | ||
* | ||
* When the Response is a StreamedResponse, the content is streamed immediately | ||
* instead of being returned. | ||
* | ||
* Available options: | ||
* | ||
* * ignore_errors: true to return an empty string in case of an error | ||
* | ||
* @param string|ControllerReference $uri A URI as a string or a ControllerReference instance | ||
* @param string $strategy The strategy to use for the rendering | ||
* @param array $options An array of options | ||
* | ||
* @return string|null The Response content or null when the Response is streamed | ||
* | ||
* @throws \InvalidArgumentException when the strategy does not exist | ||
*/ | ||
public function render($uri, $strategy = 'default', array $options = array()); | ||
|
||
/** | ||
* BC support | ||
* | ||
* @param array $options | ||
* | ||
* @return array | ||
* | ||
* @deprecated fixOptions will be removed in 2.3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then should it really be part of the interface ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I think it is used by external classes (it's public) |
||
*/ | ||
public function fixOptions(array $options); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
* | ||
* @see Symfony\Component\HttpKernel\HttpContentRenderer | ||
* @see Symfony\Component\HttpKernel\HttpContentRendererInterface | ||
*/ | ||
interface RenderingStrategyInterface | ||
{ | ||
|
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 |
---|---|---|
|
@@ -16,9 +16,9 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class UriSigner | ||
class UriSigner implements UriSignerInterface | ||
{ | ||
private $secret; | ||
protected $secret; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -31,44 +31,31 @@ public function __construct($secret) | |
} | ||
|
||
/** | ||
* Signs a URI. | ||
* | ||
* The given URI is signed by adding a _hash query string parameter | ||
* which value depends on the URI and the secret. | ||
* | ||
* @param string $uri A URI to sign | ||
* | ||
* @return string The signed URI | ||
* {@inheritdoc} | ||
*/ | ||
public function sign($uri) | ||
{ | ||
return $uri.(false === (strpos($uri, '?')) ? '?' : '&').'_hash='.$this->computeHash($uri); | ||
} | ||
|
||
/** | ||
* Checks that a URI contains the correct hash. | ||
* | ||
* The _hash query string parameter must be the last one | ||
* (as it is generated that way by the sign() method, it should | ||
* never be a problem). | ||
* | ||
* @param string $uri A signed URI | ||
* | ||
* @return Boolean True if the URI is signed correctly, false otherwise | ||
* {@inheritdoc} | ||
*/ | ||
public function check($uri) | ||
{ | ||
if (!preg_match('/(\?|&)_hash=(.+?)$/', $uri, $matches, PREG_OFFSET_CAPTURE)) { | ||
if (!preg_match('/(.*)(?:\?|&)_hash=(.+?)$/', $uri, $matches)) { | ||
return false; | ||
} | ||
|
||
// the naked URI is the URI without the _hash parameter (we need to keep the ? if there is some other parameters after) | ||
$nakedUri = substr($uri, 0, $matches[0][1]).substr($uri, $matches[0][1] + strlen($matches[0][0])); | ||
|
||
return $this->computeHash($nakedUri) === $matches[2][0]; | ||
return $this->computeHash($matches[1]) === $matches[2]; | ||
} | ||
|
||
private function computeHash($uri) | ||
/** | ||
* @param string $uri | ||
* | ||
* @return string A signature | ||
*/ | ||
protected function computeHash($uri) | ||
{ | ||
return urlencode(base64_encode(hash_hmac('sha1', $uri, $this->secret, true))); | ||
} | ||
|
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.
Why is it useful?
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.
to implement without extending, what do you think ?
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.
I did not create this interface because I don't see why you would want to have another implementation. And you don't need an interface for extending anyway.
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.
Why not ? (Sorry I can not think of any better reply)
If you implement this is an other way, you would probably not be interested to extend ?
But I can revert if you prefer (both ?)