From f49fd2ab7be1f60f147887be570ff1fd46a6dfc6 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sat, 27 Dec 2014 01:21:03 +0100 Subject: [PATCH] RequestFactory: optimized script path detection performance, thx @JanTvrdik [Closes #35] --- src/Http/RequestFactory.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Http/RequestFactory.php b/src/Http/RequestFactory.php index f4878618..eca0ede9 100644 --- a/src/Http/RequestFactory.php +++ b/src/Http/RequestFactory.php @@ -89,16 +89,13 @@ public function createHttpRequest() $url->setQuery(isset($tmp[1]) ? $tmp[1] : ''); // detect script path - $path .= '/'; - $script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] . '/' : '/'; - $max = min(strlen($path), strlen($script)); - for ($i = 0; $i < $max; $i++) { - if ($path[$i] !== $script[$i]) { - break; - } elseif ($path[$i] === '/') { - $url->setScriptPath(substr($url->getPath(), 0, $i + 1)); - } + $script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '/'; + if ($path !== $script) { + $max = min(strlen($path), strlen($script)); + for ($i = 0; $i < $max && $path[$i] === $script[$i]; $i++); + $path = substr($path, 0, strrpos($path, '/', $i - $max - 1) + 1); } + $url->setScriptPath($path); // GET, POST, COOKIE $useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));