@@ -75,6 +75,11 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
75
75
'%7C ' => '| ' ,
76
76
);
77
77
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
+
78
83
/**
79
84
* Constructor.
80
85
*
@@ -182,19 +187,21 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
182
187
183
188
if ('' === $ url ) {
184
189
$ 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 );
185
193
}
186
194
187
- // the contexts base URL is already encoded (see Symfony\Component\HttpFoundation\Request)
188
- $ url = strtr (rawurlencode ($ url ), $ this ->decodedChars );
189
-
190
195
// the path segments "." and ".." are interpreted as relative reference when resolving a URI; see http://tools.ietf.org/html/rfc3986#section-3.3
191
196
// so we need to encode them as they are not used for this purpose here
192
197
// 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
+ }
198
205
}
199
206
200
207
$ schemeAuthority = '' ;
@@ -268,7 +275,7 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
268
275
if ($ extra && $ query = http_build_query ($ extra , '' , '& ' )) {
269
276
// "/" and "?" can be left decoded for better user experience, see
270
277
// 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 ' => '/ ' ) ));
272
279
}
273
280
274
281
return $ url ;
0 commit comments