From f781367391ebb3ae314179928b309b738d67a7bb Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sun, 21 Dec 2014 19:35:31 +0100 Subject: [PATCH 1/2] RequestFactory: optimized script path detection performance --- src/Http/RequestFactory.php | 12 ++++--- tests/Http/RequestFactory.scriptPath.phpt | 42 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Http/RequestFactory.php b/src/Http/RequestFactory.php index 6ada3696..161524be 100644 --- a/src/Http/RequestFactory.php +++ b/src/Http/RequestFactory.php @@ -113,16 +113,18 @@ public function createHttpRequest() $script = '/'; } - $path = strtolower($url->getPath()) . '/'; - $script = strtolower($script) . '/'; + $path = $url->getPath(); $max = min(strlen($path), strlen($script)); for ($i = 0; $i < $max; $i++) { - if ($path[$i] !== $script[$i]) { + if ($path[$i] !== $script[$i] && strcasecmp($path[$i], $script[$i])) { break; - } elseif ($path[$i] === '/') { - $url->setScriptPath(substr($url->getPath(), 0, $i + 1)); } } + if ($i === $max && strlen($path) === strlen($script)) { + $url->setScriptPath($path); + } else { + $url->setScriptPath(substr($path, 0, strrpos($path, '/', $i - $max - 1) + 1)); + } // GET, POST, COOKIE $useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags')); diff --git a/tests/Http/RequestFactory.scriptPath.phpt b/tests/Http/RequestFactory.scriptPath.phpt index 2a43a0a0..201911cd 100644 --- a/tests/Http/RequestFactory.scriptPath.phpt +++ b/tests/Http/RequestFactory.scriptPath.phpt @@ -55,3 +55,45 @@ test(function() use ($factory) { Assert::same( '/www/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); }); + + +test(function() use ($factory) { + $_SERVER = array( + 'REQUEST_URI' => '/test/in', + 'SCRIPT_NAME' => '/test/index.php', + ); + + Assert::same( '/test/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); +}); + + +test(function() use ($factory) { + $_SERVER = array( + 'REQUEST_URI' => '/test//', + 'SCRIPT_NAME' => '/test/index.php', + ); + + Assert::same( '/test/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); +}); + + +// http://forum.nette.org/cs/5932-lepsi-detekce-requesturi-a-scriptpath +test(function() use ($factory) { + $_SERVER = array( + 'REQUEST_URI' => '/sign/in/', + 'SCRIPT_NAME' => '/sign/in/', + ); + + Assert::same( '/sign/in/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); +}); + + +// http://forum.nette.org/cs/9139-spatny-urlscript-scriptpath +test(function() use ($factory) { + $_SERVER = array( + 'REQUEST_URI' => '/configuration/', + 'SCRIPT_NAME' => '/configuration/www/index.php', + ); + + Assert::same( '/configuration/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); +}); From c5aa73ee5732a8f9a1f65f7c3b6c413215ad645c Mon Sep 17 00:00:00 2001 From: Jan Tvrdik Date: Sun, 21 Dec 2014 20:12:50 +0100 Subject: [PATCH 2/2] RequestFactory: script path detection is case-sensitive (BC break) --- src/Http/RequestFactory.php | 6 ++---- tests/Http/RequestFactory.scriptPath.phpt | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Http/RequestFactory.php b/src/Http/RequestFactory.php index 161524be..785659ad 100644 --- a/src/Http/RequestFactory.php +++ b/src/Http/RequestFactory.php @@ -115,10 +115,8 @@ public function createHttpRequest() $path = $url->getPath(); $max = min(strlen($path), strlen($script)); - for ($i = 0; $i < $max; $i++) { - if ($path[$i] !== $script[$i] && strcasecmp($path[$i], $script[$i])) { - break; - } + for ($i = 0; $i < $max && $path[$i] === $script[$i]; $i++) { + // nothing } if ($i === $max && strlen($path) === strlen($script)) { $url->setScriptPath($path); diff --git a/tests/Http/RequestFactory.scriptPath.phpt b/tests/Http/RequestFactory.scriptPath.phpt index 201911cd..c17b75a8 100644 --- a/tests/Http/RequestFactory.scriptPath.phpt +++ b/tests/Http/RequestFactory.scriptPath.phpt @@ -16,7 +16,7 @@ $factory = new RequestFactory; test(function() use ($factory) { $_SERVER = array( 'REQUEST_URI' => '/projects/modules-usage/www/', - 'SCRIPT_FILENAME' => 'W:/projects/Modules-Usage/www/index.php', + 'SCRIPT_FILENAME' => 'W:/projects/modules-usage/www/index.php', 'SCRIPT_NAME' => '/projects/modules-usage/www/index.php', ); @@ -27,8 +27,8 @@ test(function() use ($factory) { test(function() use ($factory) { $_SERVER = array( 'REQUEST_URI' => '/projects/modules-usage/www/default/add-item', - 'SCRIPT_FILENAME' => 'W:/projects/Modules-Usage/www/index.php', - 'SCRIPT_NAME' => '/projects/Modules-Usage/www/index.php', + 'SCRIPT_FILENAME' => 'W:/projects/modules-usage/www/index.php', + 'SCRIPT_NAME' => '/projects/modules-usage/www/index.php', ); Assert::same( '/projects/modules-usage/www/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); @@ -97,3 +97,13 @@ test(function() use ($factory) { Assert::same( '/configuration/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); }); + + +test(function() use ($factory) { + $_SERVER = array( + 'REQUEST_URI' => '/blog/WWW/', + 'SCRIPT_NAME' => '/blog/www/index.php', + ); + + Assert::same( '/blog/', $factory->createHttpRequest()->getUrl()->getScriptPath() ); +});