Skip to content

Commit 5b27c17

Browse files
committed
RequestFactory: optimized script path detection performance, thx @JanTvrdik [Closes #35]
1 parent 06cc6f4 commit 5b27c17

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/Http/RequestFactory.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,21 @@ public function createHttpRequest()
8181
}
8282

8383
// path & query
84-
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
84+
$requestUrl = isset($_SERVER['REQUEST_URI'][0]) && $_SERVER['REQUEST_URI'][0] === '/' ? $_SERVER['REQUEST_URI'] : '/';
8585
$requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
8686
$tmp = explode('?', $requestUrl, 2);
8787
$path = Strings::fixEncoding(Strings::replace($tmp[0], $this->urlFilters['path']));
8888
$url->setPath($path);
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 = $i ? 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'));

tests/Http/RequestFactory.scriptPath.phpt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,43 @@ test(function() use ($factory) {
103103

104104
Assert::same( '/blog/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
105105
});
106+
107+
108+
test(function() use ($factory) {
109+
$_SERVER = array(
110+
'REQUEST_URI' => '/',
111+
'SCRIPT_NAME' => 'c:\\index.php',
112+
);
113+
114+
Assert::same( '/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
115+
});
116+
117+
118+
test(function() use ($factory) {
119+
$_SERVER = array(
120+
'REQUEST_URI' => '',
121+
'SCRIPT_NAME' => 'c:\\index.php',
122+
);
123+
124+
Assert::same( '/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
125+
});
126+
127+
128+
test(function() use ($factory) {
129+
$_SERVER = array(
130+
'REQUEST_URI' => 'c',
131+
'SCRIPT_NAME' => 'c:\\index.php',
132+
);
133+
134+
Assert::same( '/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
135+
});
136+
137+
138+
test(function() use ($factory) {
139+
$_SERVER = array(
140+
'REQUEST_URI' => 'c',
141+
'SCRIPT_NAME' => '',
142+
);
143+
144+
Assert::same( '/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
145+
});

0 commit comments

Comments
 (0)