8000 [Routing] Don't needlessly execute strtr's as they are fairly expensive · symfony/symfony@b3da6a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3da6a1

Browse files
committed
[Routing] Don't needlessly execute strtr's as they are fairly expensive
1 parent f4c18db commit b3da6a1

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Symfony/Component/Routing/Generator/UrlGenerator.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
7575
'%7C' => '|',
7676
);
7777

78+
/**
79+
* @var string This regexp matches all characters that are not or should not be encoded by rawurlencode (see list in array above).
80+
*/
81+
private $urlEncodingSkipRegexp = '#[^-.~a-zA-Z0-9_/@:;,=+!*|]#';
82+
7883
/**
7984
* Constructor.
8085
*
@@ -182,19 +187,21 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
182187

183188
if ('' === $url) {
184189
$url = '/';
190+
} else if (preg_match($this->urlEncodingSkipRegexp, $url)) {
191+
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
192+
$url = strtr(rawurlencode($url), $this->decodedChars);
185193
}
186194

187-
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
188-
$url = strtr(rawurlencode($url), $this->decodedChars);
189-
190195
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3
191196
// so we need to encode them as they are not used for this purpose here
192197
// otherwise we would generate a URI that, when followed by a user agent (e.g. browser), does not match this route
193-
$url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/'));
194-
if ('/..' === substr($url, -3)) {
195-
$url = substr($url, 0, -2).'%2E%2E';
196-
} elseif ('/.' === substr($url, -2)) {
197-
$url = substr($url, 0, -1).'%2E';
198+
if(false !== strpos($url, '/.')) {
199+
$url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/'));
200+
if ('/..' === substr($url, -3)) {
201+
$url = substr($url, 0, -2).'%2E%2E';
202+
} elseif ('/.' === substr($url, -2)) {
203+
$url = substr($url, 0, -1).'%2E';
204+
}
198205
}
199206

200207
$schemeAuthority = '';
@@ -268,7 +275,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
268275
if ($extra && $query = http_build_query($extra, '', '&')) {
269276
// "/" and "?" can be left decoded for better user experience, see
270277
// http://tools.ietf.org/html/rfc3986#section-3.4
271-
$url .= '?'.strtr($query, array('%2F' => '/'));
278+
$url .= '?'.(false === strpos($query, '%2F') ? $query : strtr($query, array('%2F' => '/')));
272279
}
273280

274281
return $url;

0 commit comments

Comments
 (0)
0