Skip to content

Commit 1c1ff1f

Browse files
committed
RequestFactory: optimized script path detection performance
1 parent e9dd988 commit 1c1ff1f

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/Http/RequestFactory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ public function createHttpRequest()
113113
$script = '/';
114114
}
115115

116-
$path = strtolower($url->getPath()) . '/';
117-
$script = strtolower($script) . '/';
118-
$max = min(strlen($path), strlen($script));
119-
for ($i = 0; $i < $max; $i++) {
120-
if ($path[$i] !== $script[$i]) {
116+
$path = $url->getPath();
117+
$max = min(strlen($path), strlen($script)) - 1;
118+
for ($i = 0, $j = 0; $i <= $max; $i++) {
119+
if ($path[$i] !== $script[$i] && strcasecmp($path[$i], $script[$i])) {
121120
break;
122-
} elseif ($path[$i] === '/') {
123-
$url->setScriptPath(substr($url->getPath(), 0, $i + 1));
121+
} elseif ($path[$i] === '/' || ($i === $max && strlen($path) === strlen($script))) {
122+
$j = $i;
124123
}
125124
}
125+
$url->setScriptPath(substr($path, 0, $j + 1));
126126

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

tests/Http/RequestFactory.scriptPath.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@ test(function() use ($factory) {
5555

5656
Assert::same( '/www/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
5757
});
58+
59+
60+
test(function() use ($factory) {
61+
$_SERVER = array(
62+
'REQUEST_URI' => '/test/in',
63+
'SCRIPT_NAME' => '/test/index.php',
64+
);
65+
66+
Assert::same( '/test/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
67+
});

0 commit comments

Comments
 (0)