Skip to content

Commit f49fd2a

Browse files
committed
RequestFactory: optimized script path detection performance, thx @JanTvrdik [Closes #35]
1 parent 6e592e5 commit f49fd2a

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/Http/RequestFactory.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,13 @@ public function createHttpRequest()
8989
$url->setQuery(isset($tmp[1]) ? $tmp[1] : '');
9090

9191
// detect script path
92-
$path .= '/';
93-
$script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] . '/' : '/';
94-
$max = min(strlen($path), strlen($script));
95-
for ($i = 0; $i < $max; $i++) {
96-
if ($path[$i] !== $script[$i]) {
97-
break;
98-
} elseif ($path[$i] === '/') {
99-
$url->setScriptPath(substr($url->getPath(), 0, $i + 1));
100-
}
92+
$script = isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : '/';
93+
if ($path !== $script) {
94+
$max = min(strlen($path), strlen($script));
95+
for ($i = 0; $i < $max && $path[$i] === $script[$i]; $i++);
96+
$path = substr($path, 0, strrpos($path, '/', $i - $max - 1) + 1);
10197
}
98+
$url->setScriptPath($path);
10299

103100
// GET, POST, COOKIE
104101
$useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));

0 commit comments

Comments
 (0)