Permalink
Browse files

[Routing] Request methods always return a raw path, fix the matcher t…

…o decode only once

sq
  • Loading branch information...
1 parent 3535cfa commit 31fa46b81a4536ade62aa2ec92447d2afa131628 @vicb vicb committed Mar 19, 2012
Showing with 51 additions and 12 deletions.
  1. +19 −9 Request.php
  2. +1 −1 RequestMatcher.php
  3. +8 −0 Tests/RequestMatcherTest.php
  4. +23 −2 Tests/RequestTest.php
View
@@ -16,6 +16,14 @@
/**
* Request represents an HTTP request.
*
+ * The methods dealing with URL accept / return a raw path (% encoded):
+ * * getBasePath
+ * * getBaseUrl
+ * * getPathInfo
+ * * getRequestUri
+ * * getUri
+ * * getUriForPath
+ *
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
@@ -568,9 +576,10 @@ public function getScriptName()
*
* * http://localhost/mysite returns an empty string
* * http://localhost/mysite/about returns '/about'
+ * * htpp://localhost/mysite/enco%20ded returns '/enco%20ded'
* * http://localhost/mysite/about?var=1 returns '/about'
*
- * @return string
+ * @return string The raw path (i.e. not urldecoded)
*
* @api
*/
@@ -588,11 +597,12 @@ public function getPathInfo()
*
* Suppose that an index.php file instantiates this request object:
*
- * * http://localhost/index.php returns an empty string
- * * http://localhost/index.php/page returns an empty string
- * * http://localhost/web/index.php return '/web'
+ * * http://localhost/index.php returns an empty string
+ * * http://localhost/index.php/page returns an empty string
+ * * http://localhost/web/index.php returns '/web'
+ * * http://localhost/we%20b/index.php returns '/we%20b'
*
- * @return string
+ * @return string The raw path (i.e. not urldecoded)
*
* @api
*/
@@ -613,7 +623,7 @@ public function getBasePath()
* This is similar to getBasePath(), except that it also includes the
* script filename (e.g. index.php) if one exists.
*
- * @return string
+ * @return string The raw url (i.e. not urldecoded)
*
* @api
*/
@@ -698,7 +708,7 @@ public function getHttpHost()
/**
* Returns the requested URI.
*
- * @return string
+ * @return string The raw URI (i.e. not urldecoded)
*
* @api
*/
@@ -1317,7 +1327,7 @@ protected function prepareBaseUrl()
}
$basename = basename($baseUrl);
- if (empty($basename) || !strpos(urldecode($truncatedRequestUri), $basename)) {
+ if (empty($basename) || !strpos(rawurldecode($truncatedRequestUri), $basename)) {
// no match whatsoever; set it blank
return '';
}
@@ -1385,7 +1395,7 @@ protected function preparePathInfo()
return $requestUri;
}
- return rawurldecode((string) $pathInfo);
+ return (string) $pathInfo;
}
/**
View
@@ -127,7 +127,7 @@ public function matches(Request $request)
if (null !== $this->path) {
$path = str_replace('#', '\\#', $this->path);
- if (!preg_match('#'.$path.'#', $request->getPathInfo())) {
+ if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
return false;
}
}
@@ -150,6 +150,14 @@ public function testPathWithLocaleIsNotSupported()
$this->assertFalse($matcher->matches($request));
}
+ public function testPathWithEncodedCharacters()
+ {
+ $matcher = new RequestMatcher();
+ $request = Request::create('/admin/fo%20o');
+ $matcher->matchPath('^/admin/fo o*$');
+ $this->assertTrue($matcher->matches($request));
+ }
+
public function testAttributes()
{
$matcher = new RequestMatcher();
View
@@ -315,6 +315,27 @@ public function testGetUri()
$request->initialize(array(), array(), array(), array(), array(), $server);
$this->assertEquals('http://servername/path/info?query=string', $request->getUri(), '->getUri() with rewrite, default port without HOST_HEADER');
+
+ // With encoded characters
+
+ $server = array(
+ 'HTTP_HOST' => 'hostname:8080',
+ 'SERVER_NAME' => 'servername',
+ 'SERVER_PORT' => '8080',
+ 'QUERY_STRING' => 'query=string',
+ 'REQUEST_URI' => '/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
+ 'SCRIPT_NAME' => '/ba se/index_dev.php',
+ 'PATH_TRANSLATED' => 'redirect:/index.php/foo bar/in+fo',
+ 'PHP_SELF' => '/ba se/index_dev.php/path/info',
+ 'SCRIPT_FILENAME' => '/some/where/ba se/index_dev.php',
+ );
+
+ $request->initialize(array(), array(), array(), array(), array(), $server);
+
+ $this->assertEquals(
+ 'http://hostname:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
+ $request->getUri()
+ );
}
/**
@@ -984,14 +1005,14 @@ public function getBaseUrlData()
'/home',
),
array(
- '/foo%20bar/app.php/home%2Fbaz',
+ '/foo%20bar/app.php/home%3Dbaz',
array(
'SCRIPT_FILENAME' => '/home/John Doe/public_html/foo bar/app.php',
'SCRIPT_NAME' => '/foo bar/app.php',
'PHP_SELF' => '/foo bar/app.php',
),
'/foo%20bar/app.php',
- '/home%2Fbaz',
+ '/home%3Dbaz',
),
array(
'/foo/bar+baz',

0 comments on commit 31fa46b

Please sign in to comment.