Skip to content

Commit

Permalink
RequestFactory: optimized script path detection performance
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Dec 21, 2014
1 parent e9dd988 commit fa5188e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,17 @@ 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));
}
}
$url->setScriptPath($i === $max && strlen($path) === strlen($script)
? $path
: 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'));
Expand Down
10 changes: 10 additions & 0 deletions tests/Http/RequestFactory.scriptPath.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ 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() );
});

0 comments on commit fa5188e

Please sign in to comment.