-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Routing] Don't needlessly execute strtr's as they are fairly expensive #18230
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,11 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt | |
'%7C' => '|', | ||
); | ||
|
||
/** | ||
* @var string This regexp matches all characters that are not or should not be encoded by rawurlencode (see list in array above). | ||
*/ | ||
protected $urlEncodingSkipRegexp = '#[^-.~a-zA-Z0-9_/@:;,=+!*|]#'; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
|
@@ -182,19 +187,21 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa | |
|
||
if ('' === $url) { | ||
$url = '/'; | ||
} else if (preg_match($this->urlEncodingSkipRegexp, $url)) { | ||
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request) | ||
$url = strtr(rawurlencode($url), $this->decodedChars); | ||
} | ||
|
||
// the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request) | ||
$url = strtr(rawurlencode($url), $this->decodedChars); | ||
|
||
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3 | ||
// so we need to encode them as they are not used for this purpose here | ||
// otherwise we would generate a URI that, when followed by a user agent (e.g. browser), does not match this route | ||
$url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/')); | ||
if ('/..' === substr($url, -3)) { | ||
$url = substr($url, 0, -2).'%2E%2E'; | ||
} elseif ('/.' === substr($url, -2)) { | ||
$url = substr($url, 0, -1).'%2E'; | ||
if(false !== strpos($url, '/.')) { | ||
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. this could be a good idea. but the rest I don't think so. |
||
$url = strtr($url, array('/../' => '/%2E%2E/', '/./' => '/%2E/')); | ||
if ('/..' === substr($url, -3)) { | ||
$url = substr($url, 0, -2).'%2E%2E'; | ||
} elseif ('/.' === substr($url, -2)) { | ||
$url = substr($url, 0, -1).'%2E'; | ||
} | ||
} | ||
|
||
$schemeAuthority = ''; | ||
|
@@ -268,7 +275,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa | |
if ($extra && $query = http_build_query($extra, '', '&')) { | ||
// "/" and "?" can be left decoded for better user experience, see | ||
// http://tools.ietf.org/html/rfc3986#section-3.4 | ||
$url .= '?'.strtr($query, array('%2F' => '/')); | ||
$url .= '?'.(false === strpos($query, '%2F') ? $query : strtr($query, array('%2F' => '/'))); | ||
} | ||
|
||
return $url; | ||
|
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.
could it be private ?
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.
@stof It could be, but all other properties are protected as well, so I'd defiate from the 'default' in that class.