Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added a secure parameter to UrlGenerator to generate HTTPS-Urls #534

Closed
wants to merge 3 commits into from

4 participants

@thaberkern

Added a secure parameter to UrlGenerator to generate HTTPS-Urls even if the current page doesn't use HTTPS.

Context-Is_Secure: true, $secure-Parameter: null => URL: https
Context-Is_Secure: false, $secure-Parameter: null => URL: http
Context-Is_Secure: true, $secure-Parameter: true => URL: https
Context-Is_Secure: true, $secure-Parameter: false => URL: http
Context-Is_Secure: false, $secure-Parameter: true => URL: https
Context-Is_Secure: false, $secure-Parameter: false => URL: http

src/Symfony/Component/Routing/Generator/UrlGenerator.php
@@ -127,8 +129,13 @@ class UrlGenerator implements UrlGeneratorInterface
$url = (isset($this->context['base_url']) ? $this->context['base_url'] : '').$url;
if ($absolute && isset($this->context['host'])) {
- $isSecure = (isset($this->context['is_secure']) && $this->context['is_secure']);
+ if ($secure == null) {
+ $isSecure = (isset($this->context['is_secure']) && $this->context['is_secure']);
+ } else {
+ $isSecure = $secure;
+ }
@lsmith77 Collaborator

i think we should prefer the ternary syntax in case of such assignments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@thaberkern

Any other comments on this? Any chance this will be merged?

@Seldaek

I like the approach of http://trac.symfony-project.org/ticket/9686 better, because you define which routes should be served how. This could even be enforced by specifying the _scheme in the requirements maybe.

@fabpot
Owner

I have implemented something more like what @Seldaek describes in the ticket. Here is the doc for it: symfony/symfony-docs@068de1f

@fabpot fabpot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 12, 2011
  1. @thaberkern
  2. @thaberkern

    Removed netbeans files

    thaberkern authored
Commits on Apr 13, 2011
  1. @thaberkern

    Using ternery operator

    thaberkern authored
This page is out of date. Refresh to see the latest.
View
4 src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
@@ -45,9 +45,9 @@ public function getPath($name, array $parameters = array())
return $this->generator->generate($name, $parameters, false);
}
- public function getUrl($name, array $parameters = array())
+ public function getUrl($name, array $parameters = array(), $secure = null)
{
- return $this->generator->generate($name, $parameters, true);
+ return $this->generator->generate($name, $parameters, true, $secure);
}
/**
View
7 src/Symfony/Component/Routing/Generator/UrlGenerator.php
@@ -58,12 +58,14 @@ public function setContext(array $context = array())
* @param string $name The name of the route
* @param array $parameters An array of parameters
* @param Boolean $absolute Whether to generate an absolute URL
+ * @param mixed $secure Whether to genereate a https URL (only for absolute URLs).
+ * Can be either a boolean or null (security context of the current page is used)
*
* @return string The generated URL
*
* @throws \InvalidArgumentException When route doesn't exist
*/
- public function generate($name, array $parameters = array(), $absolute = false)
+ public function generate($name, array $parameters = array(), $absolute = false, $secure = null)
{
if (null === $route = $this->routes->get($name)) {
throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name));
@@ -127,8 +129,9 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
$url = (isset($this->context['base_url']) ? $this->context['base_url'] : '').$url;
if ($absolute && isset($this->context['host'])) {
- $isSecure = (isset($this->context['is_secure']) && $this->context['is_secure']);
+ $isSecure = $secure == null ? (isset($this->context['is_secure']) && $this->context['is_secure']) : $secure;
$port = isset($this->context['port']) ? $this->context['port'] : 80;
+
$urlBeginning = 'http'.($isSecure ? 's' : '').'://'.$this->context['host'];
if (($isSecure && $port != 443) || (!$isSecure && $port != 80)) {
$urlBeginning .= ':'.$port;
View
4 src/Symfony/Component/Routing/Generator/UrlGeneratorInterface.php
@@ -24,8 +24,10 @@
* @param string $name The name of the route
* @param array $parameters An array of parameters
* @param Boolean $absolute Whether to generate an absolute URL
+ * @param mixed $secure Whether to genereate a https URL (only for absolute URLs).
+ * Can be either a boolean or null (security context of the current page is used)
*
* @return string The generated URL
*/
- function generate($name, array $parameters = array(), $absolute = false);
+ function generate($name, array $parameters = array(), $absolute = false, $secure = null);
}
View
128 tests/Symfony/Tests/Component/Config/Definition/FinalizationTest.php
@@ -1,65 +1,65 @@
-<?php
-
-namespace Symfony\Tests\Component\Config\Definition;
-
-use Symfony\Component\Config\Definition\Builder\TreeBuilder;
-use Symfony\Component\Config\Definition\Processor;
-use Symfony\Component\Config\Definition\NodeInterface;
-
-class FinalizationTest extends \PHPUnit_Framework_TestCase
-{
-
- public function testUnsetKeyWithDeepHierarchy()
- {
- $tb = new TreeBuilder();
- $tree = $tb
- ->root('config', 'array')
- ->children()
- ->node('level1', 'array')
- ->canBeUnset()
- ->children()
- ->node('level2', 'array')
- ->canBeUnset()
- ->children()
- ->node('somevalue', 'scalar')->end()
- ->node('anothervalue', 'scalar')->end()
- ->end()
- ->end()
- ->node('level1_scalar', 'scalar')->end()
- ->end()
- ->end()
- ->end()
- ->end()
- ->buildTree()
- ;
-
- $a = array(
- 'level1' => array(
- 'level2' => array(
- 'somevalue' => 'foo',
- 'anothervalue' => 'bar',
- ),
- 'level1_scalar' => 'foo',
- ),
- );
-
- $b = array(
- 'level1' => array(
- 'level2' => false,
- ),
- );
-
- $this->assertEquals(array(
- 'level1' => array(
- 'level1_scalar' => 'foo',
- ),
- ), $this->process($tree, array($a, $b)));
- }
-
- protected function process(NodeInterface $tree, array $configs)
- {
- $processor = new Processor();
-
- return $processor->process($tree, $configs);
- }
+<?php
+
+namespace Symfony\Tests\Component\Config\Definition;
+
+use Symfony\Component\Config\Definition\Builder\TreeBuilder;
+use Symfony\Component\Config\Definition\Processor;
+use Symfony\Component\Config\Definition\NodeInterface;
+
+class FinalizationTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testUnsetKeyWithDeepHierarchy()
+ {
+ $tb = new TreeBuilder();
+ $tree = $tb
+ ->root('config', 'array')
+ ->children()
+ ->node('level1', 'array')
+ ->canBeUnset()
+ ->children()
+ ->node('level2', 'array')
+ ->canBeUnset()
+ ->children()
+ ->node('somevalue', 'scalar')->end()
+ ->node('anothervalue', 'scalar')->end()
+ ->end()
+ ->end()
+ ->node('level1_scalar', 'scalar')->end()
+ ->end()
+ ->end()
+ ->end()
+ ->end()
+ ->buildTree()
+ ;
+
+ $a = array(
+ 'level1' => array(
+ 'level2' => array(
+ 'somevalue' => 'foo',
+ 'anothervalue' => 'bar',
+ ),
+ 'level1_scalar' => 'foo',
+ ),
+ );
+
+ $b = array(
+ 'level1' => array(
+ 'level2' => false,
+ ),
+ );
+
+ $this->assertEquals(array(
+ 'level1' => array(
+ 'level1_scalar' => 'foo',
+ ),
+ ), $this->process($tree, array($a, $b)));
+ }
+
+ protected function process(NodeInterface $tree, array $configs)
+ {
+ $processor = new Processor();
+
+ return $processor->process($tree, $configs);
+ }
}
View
100 tests/Symfony/Tests/Component/Config/Definition/ScalarNodeTest.php
@@ -1,51 +1,51 @@
-<?php
-
-namespace Symfony\Tests\Component\Config\Definition;
-
-use Symfony\Component\Config\Definition\ScalarNode;
-
-class ScalarNodeTest extends \PHPUnit_Framework_TestCase
-{
- /**
- * @dataProvider getValidValues
- */
- public function testNormalize($value)
- {
- $node = new ScalarNode('test');
- $this->assertSame($value, $node->normalize($value));
- }
-
- public function getValidValues()
- {
- return array(
- array(false),
- array(true),
- array(null),
- array(''),
- array('foo'),
- array(0),
- array(1),
- array(0.0),
- array(0.1),
- );
- }
-
- /**
- * @dataProvider getInvalidValues
- * @expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException
- */
- public function testNormalizeThrowsExceptionOnInvalidValues($value)
- {
- $node = new ScalarNode('test');
- $node->normalize($value);
- }
-
- public function getInvalidValues()
- {
- return array(
- array(array()),
- array(array('foo' => 'bar')),
- array(new \stdClass()),
- );
- }
+<?php
+
+namespace Symfony\Tests\Component\Config\Definition;
+
+use Symfony\Component\Config\Definition\ScalarNode;
+
+class ScalarNodeTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider getValidValues
+ */
+ public function testNormalize($value)
+ {
+ $node = new ScalarNode('test');
+ $this->assertSame($value, $node->normalize($value));
+ }
+
+ public function getValidValues()
+ {
+ return array(
+ array(false),
+ array(true),
+ array(null),
+ array(''),
+ array('foo'),
+ array(0),
+ array(1),
+ array(0.0),
+ array(0.1),
+ );
+ }
+
+ /**
+ * @dataProvider getInvalidValues
+ * @expectedException Symfony\Component\Config\Definition\Exception\InvalidTypeException
+ */
+ public function testNormalizeThrowsExceptionOnInvalidValues($value)
+ {
+ $node = new ScalarNode('test');
+ $node->normalize($value);
+ }
+
+ public function getInvalidValues()
+ {
+ return array(
+ array(array()),
+ array(array('foo' => 'bar')),
+ array(new \stdClass()),
+ );
+ }
}
View
30 tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php
@@ -186,4 +186,34 @@ public function testGenerateForRouteWithInvalidManditoryParameter()
$this->routeCollection->add('test', $route);
$this->generator->generate('test', array('foo' => 'bar'), true);
}
+
+ public function testGenerateSecureUrlViaParameter()
+ {
+ $this->routeCollection->add('test', new Route('/testing'));
+ $this->generator->setContext(array(
+ 'base_url'=>'/app.php',
+ 'method'=>'GET',
+ 'host'=>'localhost',
+ 'port'=>80,
+ 'is_secure'=>false));
+
+ $url = $this->generator->generate('test', array('foo' => 'bar'), true, true);
+
+ $this->assertEquals('https://localhost/app.php/testing?foo=bar', $url);
+ }
+
+ public function testGenerateUnsecureSecureUrlViaParameter()
+ {
+ $this->routeCollection->add('test', new Route('/testing'));
+ $this->generator->setContext(array(
+ 'base_url'=>'/app.php',
+ 'method'=>'GET',
+ 'host'=>'localhost',
+ 'port'=>80,
+ 'is_secure'=>true));
+
+ $url = $this->generator->generate('test', array('foo' => 'bar'), true, false);
+
+ $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
+ }
}
Something went wrong with that request. Please try again.