8000 [Routing] Support UTF-8 in paths and parameters · symfony/symfony@4be2ca0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4be2ca0

Browse files
committed
[Routing] Support UTF-8 in paths and parameters
1 parent 64ace10 commit 4be2ca0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+381
-206
lines changed

UPGRADE-4.0.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ HttpKernel
132132
have your own `ControllerResolverInterface` implementation, you should
133133
inject an `ArgumentResolverInterface` instance.
134134

135+
136+
Router
137+
------
138+
* URLs are now assumed to be encoded in UTF-8. Regular expressions for route
139+
requirements now use the PCRE_UTF8 flag to allow matching non-ASCII
140+
characters. To support legacy URLs in other encodings, the default encoding
141+
can be overridden using the `charset` option.
142+
135143
Serializer
136144
----------
137145

src/Symfony/Bundle/FrameworkBundle/Command/RouterMatchCommand.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
9090
$context->setHost($host);
9191
}
9292

93-
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context);
93+
$pathInfo = < E7F5 span class="pl-c1 x x-first">$input->getArgument('path_info');
9494

95-
$traces = $matcher->getTraces($input->getArgument('path_info'));
95+
$charset = mb_detect_encoding($pathInfo, null, true);
96+
$matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context, $charset);
97+
98+
$traces = $matcher->getTraces($pathInfo);
9699

97100
$io->newLine();
98101

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<parameter key="router.options.matcher_dumper_class">Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper</parameter>
1414
<parameter key="router.options.matcher.cache_class">%router.cache_class_prefix%UrlMatcher</parameter>
1515
<parameter key="router.options.generator.cache_class">%router.cache_class_prefix%UrlGenerator</parameter>
16+
<parameter key="router.options.charset">%kernel.charset%</parameter>
1617
<parameter key="router.request_context.host">localhost</parameter>
1718
<parameter key="router.request_context.scheme">http</parameter>
1819
<parameter key="router.request_context.base_url"></parameter>
@@ -66,6 +67,7 @@
6667
<argument key="matcher_base_class">%router.options.matcher_base_class%</argument>
6768
<argument key="matcher_dumper_class">%router.options.matcher_dumper_class%</argument>
6869
<argument key="matcher_cache_class">%router.options.matcher.cache_class%</argument>
70+
<argument key="charset">%router.options.charset%</argument>
6971
</argument>
7072
<argument type="service" id="router.request_context" on-invalid="ignore" />
7173
<argument type="service" id="logger" on-invalid="ignore" />

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ public function __construct(ContainerInterface $container, $resource, array $opt
4242

4343
$ 23CB this->resource = $resource;
4444
$this->context = $context ?: new RequestContext();
45+
46+
if (array_key_exists('charset', $options)) {
47+
try {
48+
$this->setOption('charset', $options['charset']);
49+
} catch (\InvalidArgumentException $e) {
50+
// symfony/routing version 3.x does not support the charset option.
51+
unset($options['charset']);
52+
}
53+
}
54+
4555
$this->setOptions($options);
4656
}
4757

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ protected function createContainer(array $data = array())
639639
'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
640640
'kernel.cache_dir' => __DIR__,
641641
'kernel.debug' => false,
642+
'kernel.charset' => 'UTF-8',
642643
'kernel.environment' => 'test',
643644
'kernel.name' => 'kernel',
644645
'kernel.root_dir' => __DIR__,

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"path": "\/hello\/{name}",
3-
"pathRegex": "#^\/hello(?:\/(?P<name>[a-z]+))?$#s",
3+
"pathRegex": "#^\/hello(?:\/(?P<name>[a-z]+))?$#us",
44
"host": "localhost",
5-
"hostRegex": "#^localhost$#si",
5+
"hostRegex": "#^localhost$#usi",
66
"scheme": "http|https",
77
"method": "GET|HEAD",
88
"class": "Symfony\\Component\\Routing\\Route",

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
- Path: /hello/{name}
2-
- Path Regex: #^/hello(?:/(?P<name>[a-z]+))?$#s
2+
- Path Regex: #^/hello(?:/(?P<name>[a-z]+))?$#us
33
- Host: localhost
4-
- Host Regex: #^localhost$#si
4+
- Host Regex: #^localhost$#usi
55
- Scheme: http|https
66
- Method: GET|HEAD
77
- Class: Symfony\Component\Routing\Route

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
+--------------+---------------------------------------------------------+
44
| Route Name | |
55
| Path | /hello/{name} |
6-
| Path Regex | #^/hello(?:/(?P<name>[a-z]+))?$#s |
6+
| Path Regex | #^/hello(?:/(?P<name>[a-z]+))?$#us |
77
| Host | localhost |
8-
| Host Regex | #^localhost$#si |
8+
| Host Regex | #^localhost$#usi |
99
| Scheme | http|https |
1010
| Method | GET|HEAD |
1111
| Requirements | name: [a-z]+ |

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_1.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<route class="Symfony\Component\Routing\Route">
3-
<path regex="#^/hello(?:/(?P&lt;name&gt;[a-z]+))?$#s">/hello/{name}</path>
4-
<host regex="#^localhost$#si">localhost</host>
3+
<path regex="#^/hello(?:/(?P&lt;name&gt;[a-z]+))?$#us">/hello/{name}</path>
4+
<host regex="#^localhost$#usi">localhost</host>
55
<scheme>http</scheme>
66
<scheme>https</scheme>
77
<method>GET</method>

src/Symfony/Bundle/FrameworkBundle/Tests/Fixtures/Descriptor/route_2.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"path": "\/name\/add",
3-
"pathRegex": "#^\/name\/add$#s",
3+
"pathRegex": "#^\/name\/add$#us",
44
"host": "localhost",
5-
"hostRegex": "#^localhost$#si",
5+
"hostRegex": "#^localhost$#usi",
66
"scheme": "http|https",
77
"method": "PUT|POST",
88
"class": "Symfony\\Component\\Routing\\Route",

0 commit comments

Comments
 (0)
0