Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[HttpFoundation] fix pathinfo

  • Loading branch information...
commit 79fff620548612bec5cdd302d75f9626ed3f74b2 1 parent aec3c8a
@Tobion authored
View
10 src/Symfony/Component/HttpFoundation/ApacheRequest.php
@@ -33,19 +33,11 @@ protected function prepareBaseUrl()
{
$baseUrl = $this->server->get('SCRIPT_NAME');
- if (false === strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
+ if (0 !== strpos($this->server->get('REQUEST_URI'), $baseUrl)) {
// assume mod_rewrite
return rtrim(dirname($baseUrl), '/\\');
}
return $baseUrl;
}
-
- /**
- * {@inheritdoc}
- */
- protected function preparePathInfo()
- {
- return $this->server->get('PATH_INFO') ?: substr($this->prepareRequestUri(), strlen($this->prepareBaseUrl())) ?: '/';
- }
}
View
83 src/Symfony/Component/HttpFoundation/Request.php
@@ -726,8 +726,6 @@ public function getScriptName()
/**
* Returns the path being requested relative to the executed script.
*
- * The path info always starts with a /.
- *
* Suppose this request is instantiated from /mysite on localhost:
*
* * http://localhost/mysite returns an empty string
@@ -735,6 +733,8 @@ public function getScriptName()
* * http://localhost/mysite/enco%20ded returns '/enco%20ded'
* * http://localhost/mysite/about?var=1 returns '/about'
*
+ *
+ *
* @return string The raw path (i.e. not urldecoded)
*
* @api
@@ -1451,46 +1451,48 @@ public function splitHttpAcceptHeader($header)
}
/*
- * The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
+ * The following methods starting with `prepare*` are derived from code of the Zend Framework
*
* Code subject to the new BSD license (http://framework.zend.com/license/new-bsd).
*
* Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
*/
+ /**
+ * Detects the base path of the request based on the server variables.
+ *
+ * @return string The request URI
+ */
protected function prepareRequestUri()
{
- $requestUri = '';
-
- if ($this->headers->has('X_ORIGINAL_URL') && false !== stripos(PHP_OS, 'WIN')) {
- // IIS with Microsoft Rewrite Module
+ if ($this->headers->has('X_ORIGINAL_URL')) {
+ // IIS 7.0 or later with Microsoft Rewrite Module
$requestUri = $this->headers->get('X_ORIGINAL_URL');
- } elseif ($this->headers->has('X_REWRITE_URL') && false !== stripos(PHP_OS, 'WIN')) {
+ } elseif ($this->headers->has('X_REWRITE_URL')) {
// IIS with ISAPI_Rewrite
$requestUri = $this->headers->get('X_REWRITE_URL');
- } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') {
+ } elseif ('1' === $this->server->get('IIS_WasUrlRewritten') && '' !== $this->server->get('UNENCODED_URL', '')) {
// IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
$requestUri = $this->server->get('UNENCODED_URL');
} elseif ($this->server->has('REQUEST_URI')) {
$requestUri = $this->server->get('REQUEST_URI');
- // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
- $schemeAndHttpHost = $this->getSchemeAndHttpHost();
- if (strpos($requestUri, $schemeAndHttpHost) === 0) {
- $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
- }
+ // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path and query string
+ $requestUri = preg_replace('#^[^/:]++://[^/]++#', '', $requestUri);
} elseif ($this->server->has('ORIG_PATH_INFO')) {
// IIS 5.0, PHP as CGI
$requestUri = $this->server->get('ORIG_PATH_INFO');
- if ('' != $this->server->get('QUERY_STRING')) {
+ if ('' !== $this->server->get('QUERY_STRING', '')) {
$requestUri .= '?'.$this->server->get('QUERY_STRING');
}
+ } else {
+ $requestUri = '/';
}
return $requestUri;
}
/**
- * Prepares the base URL.
+ * Detects the base url of the request based on the server variables.
*
* @return string
*/
@@ -1556,59 +1558,50 @@ protected function prepareBaseUrl()
}
/**
- * Prepares the base path.
+ * Autodetects the base path of the request based on the server variables.
*
- * @return string base path
+ * @return string The base path
*/
protected function prepareBasePath()
{
$filename = basename($this->server->get('SCRIPT_FILENAME'));
$baseUrl = $this->getBaseUrl();
- if (empty($baseUrl)) {
+
+ if ('' === $baseUrl) {
return '';
}
+ // basename() matches the script filename; return the directory
if (basename($baseUrl) === $filename) {
- $basePath = dirname($baseUrl);
- } else {
- $basePath = $baseUrl;
+ return str_replace('\\', '/', dirname($baseUrl));
}
- if ('\\' === DIRECTORY_SEPARATOR) {
- $basePath = str_replace('\\', '/', $basePath);
- }
-
- return rtrim($basePath, '/');
+ // Base path is identical to base URL
+ return $baseUrl;
}
/**
- * Prepares the path info.
+ * Detects the path info of the request based on the server variables.
*
* @return string path info
*/
protected function preparePathInfo()
{
- $baseUrl = $this->getBaseUrl();
-
- if (null === ($requestUri = $this->getRequestUri())) {
- return '/';
- }
-
- $pathInfo = '/';
-
- // Remove the query string from REQUEST_URI
- if ($pos = strpos($requestUri, '?')) {
- $requestUri = substr($requestUri, 0, $pos);
+ // TODO cannot use PATH_INFO as-is because it is decoded
+ // see http://www.ietf.org/rfc/rfc3875
+ if ('' !== $pathinfo = $this->server->get('PATH_INFO', '')) {
+ return $pathinfo;
}
- if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) {
- // If substr() returns false then PATH_INFO is set to an empty string
- return '/';
- } elseif (null === $baseUrl) {
- return $requestUri;
+ $path = $this->getRequestUri();
+ // Remove the query string from the request URI
+ if (false !== $pos = strpos($path, '?')) {
+ $path = substr($path, 0, $pos);
}
- return (string) $pathInfo;
+ // substr returns false when $path is not longer than the base url,
+ // set path info to an empty string then
+ return (string) substr($path, strlen($this->getBaseUrl()));
}
/**
View
26 src/Symfony/Component/HttpFoundation/Tests/ApacheRequestTest.php
@@ -23,9 +23,9 @@ public function testUriMethods($server, $expectedRequestUri, $expectedBaseUrl, $
$request = new ApacheRequest();
$request->server->replace($server);
- $this->assertEquals($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
- $this->assertEquals($expectedBaseUrl, $request->getBaseUrl(), '->getBaseUrl() is correct');
- $this->assertEquals($expectedPathInfo, $request->getPathInfo(), '->getPathInfo() is correct');
+ $this->assertSame($expectedRequestUri, $request->getRequestUri(), '->getRequestUri() is correct');
+ $this->assertSame($expectedBaseUrl, $request->getBaseUrl(), '->getBaseUrl() is correct');
+ $this->assertSame($expectedPathInfo, $request->getPathInfo(), '->getPathInfo() is correct');
}
public function provideServerVars()
@@ -76,7 +76,7 @@ public function provideServerVars()
),
'/app_dev.php',
'/app_dev.php',
- '/',
+ '',
),
array(
array(
@@ -87,6 +87,24 @@ public function provideServerVars()
'',
'/',
),
+ array(
+ array(
+ 'REQUEST_URI' => '/symfony/app.php/',
+ 'SCRIPT_NAME' => '/symfony/app.php',
+ ),
+ '/symfony/app.php/',
+ '/symfony/app.php',
+ '/',
+ ),
+ array(
+ array(
+ 'REQUEST_URI' => '/symfony?foo=/symfony/index.php',
+ 'SCRIPT_NAME' => '/symfony/index.php',
+ ),
+ '/symfony?foo=/symfony/index.php',
+ '/symfony',
+ '',
+ ),
);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.