Skip to content

Commit

Permalink
Removing complicated logic that does a reasonable amount of unnecessa…
Browse files Browse the repository at this point in the history
…ry work. Favouring $_SERVER['PATH_INFO'] over more complicated, logic. This may cause issues with IIS 5.x, but its a very old release now.

Updating tests to match values that come out of IIS7.
  • Loading branch information
markstory committed Feb 19, 2011
1 parent 52467a7 commit db00915
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 62 deletions.
70 changes: 18 additions & 52 deletions cake/libs/cake_request.php
Expand Up @@ -187,49 +187,37 @@ protected function _processGet() {
}

/**
* Returns the REQUEST_URI from the server environment, or, failing that,
* constructs a new one, using the PHP_SELF constant and other variables.
* Get the request uri. Looks in PATH_INFO first, as this is the exact value we need prepared
* by PHP. Following that, REQUEST_URI, PHP_SELF, HTTP_X_REWRITE_URL and argv are checked in that order.
* Each of these server variables have the base path, and query strings stripped off
*
* @return string URI
* @return string URI The CakePHP request path that is being accessed.
*/
protected function _uri() {
foreach (array('HTTP_X_REWRITE_URL', 'REQUEST_URI', 'argv') as $var) {
$pathInfo = env('PATH_INFO');
if (!empty($pathInfo)) {
return $pathInfo;
}
foreach (array('PHP_SELF', 'REQUEST_URI', 'HTTP_X_REWRITE_URL', 'argv') as $var) {
if ($uri = env($var)) {
if ($var == 'argv') {
$uri = $uri[0];
$uri = $url[0];
}
break;
}
}
$base = $this->base;

$base = trim(Configure::read('App.baseUrl'), '/');

if ($base) {
$uri = preg_replace('/^(?:\/)?(?:' . preg_quote($base, '/') . ')?(?:url=)?/', '', $uri);
}
if (PHP_SAPI == 'isapi') {
$uri = preg_replace('/^(?:\/)?(?:\/)?(?:\?)?(?:url=)?/', '', $uri);
}
if (!empty($uri)) {
if (key($_GET) && strpos(key($_GET), '?') !== false) {
unset($_GET[key($_GET)]);
}
$uri = explode('?', $uri, 2);

if (isset($uri[1])) {
parse_str($uri[1], $_GET);
}
$uri = $uri[0];
} else {
$uri = env('QUERY_STRING');
if (strpos($uri, $base) === 0) {
$uri = substr($uri, strlen($base));
}
if (is_string($uri) && strpos($uri, 'index.php') !== false) {
list(, $uri) = explode('index.php', $uri, 2);
if (strpos($uri, '?') !== false) {
$uri = parse_url($uri, PHP_URL_PATH);
}
if (empty($uri) || $uri == '/' || $uri == '//') {
return '';
return '/';
}
return str_replace('//', '/', '/' . $uri);
return $uri;
}

/**
Expand All @@ -239,29 +227,7 @@ protected function _uri() {
*/
protected function _url() {
if (empty($_GET[self::$urlKey])) {
$uri = $this->_uri();
$base = $this->base;

$url = null;
$tmpUri = preg_replace('/^(?:\?)?(?:\/)?/', '', $uri);
$baseDir = trim(dirname($base) . '/', '/');

if ($tmpUri === '/' || $tmpUri == $baseDir || $tmpUri == $base) {
$url = '/';
} else {
$elements = array();
if ($base && strpos($uri, $base) !== false) {
$elements = explode($base, $uri);
} elseif (preg_match('/^[\/\?\/|\/\?|\?\/]/', $uri)) {
$elements = array(1 => preg_replace('/^[\/\?\/|\/\?|\?\/]/', '', $uri));
}

if (!empty($elements[1])) {
$url = $elements[1];
} else {
$url = '/';
}
}
$url = $this->_uri();
} else {
$url = $_GET[self::$urlKey];
}
Expand Down
22 changes: 12 additions & 10 deletions cake/tests/cases/libs/cake_request.test.php
Expand Up @@ -998,7 +998,7 @@ public static function environmentGenerator() {
array(
'App' => array(
'base' => false,
'baseUrl' => '/index.php?',
'baseUrl' => '/index.php',
'dir' => 'app',
'webroot' => 'webroot'
),
Expand All @@ -1017,13 +1017,13 @@ public static function environmentGenerator() {
),
),
array(
'base' => '/index.php?',
'base' => '/index.php',
'webroot' => '/app/webroot/',
'url' => ''
),
),
array(
'IIS - No rewrite with path',
'IIS - No rewrite with path, no PHP_SELF',
array(
'App' => array(
'base' => false,
Expand All @@ -1034,6 +1034,7 @@ public static function environmentGenerator() {
'SERVER' => array(
'QUERY_STRING' => '/posts/add',
'REQUEST_URI' => '/index.php?/posts/add',
'PHP_SELF' => '',
'URL' => '/index.php?/posts/add',
'argv' => array('/posts/add'),
'argc' => 1
Expand All @@ -1050,7 +1051,7 @@ public static function environmentGenerator() {
array(
'App' => array(
'base' => false,
'baseUrl' => '/site/index.php?',
'baseUrl' => '/site/index.php',
'dir' => 'app',
'webroot' => 'webroot',
),
Expand All @@ -1069,7 +1070,7 @@ public static function environmentGenerator() {
),
array(
'url' => '',
'base' => '/site/index.php?',
'base' => '/site/index.php',
'webroot' => '/site/app/webroot/'
),
),
Expand All @@ -1078,7 +1079,7 @@ public static function environmentGenerator() {
array(
'App' => array(
'base' => false,
'baseUrl' => '/site/index.php?',
'baseUrl' => '/site/index.php',
'dir' => 'app',
'webroot' => 'webroot'
),
Expand All @@ -1087,18 +1088,18 @@ public static function environmentGenerator() {
'SCRIPT_NAME' => '/site/index.php',
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
'QUERY_STRING' => '/posts/add',
'REQUEST_URI' => '/site/index.php?/posts/add',
'URL' => '/site/index.php?/posts/add',
'REQUEST_URI' => '/site/index.php/posts/add',
'URL' => '/site/index.php/posts/add',
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
'PHP_SELF' => '/site/index.php',
'PHP_SELF' => '/site/index.php/posts/add',
'argv' => array('/posts/add'),
'argc' => 1
),
),
array(
'url' => 'posts/add',
'base' => '/site/index.php?',
'base' => '/site/index.php',
'webroot' => '/site/app/webroot/'
)
),
Expand Down Expand Up @@ -1211,6 +1212,7 @@ public static function environmentGenerator() {
'dir' => 'app',
'webroot' => 'webroot'
),
'GET' => array('a' => 'b', 'c' => 'd'),
'SERVER' => array(
'SERVER_NAME' => 'localhost',
'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
Expand Down

0 comments on commit db00915

Please sign in to comment.