Skip to content

Commit f781367

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

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/Http/RequestFactory.php

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

116-
$path = strtolower($url->getPath()) . '/';
117-
$script = strtolower($script) . '/';
116+
$path = $url->getPath();
118117
$max = min(strlen($path), strlen($script));
119118
for ($i = 0; $i < $max; $i++) {
120-
if ($path[$i] !== $script[$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));
124121
}
125122
}
123+
if ($i === $max && strlen($path) === strlen($script)) {
124+
$url->setScriptPath($path);
125+
} else {
126+
$url->setScriptPath(substr($path, 0, strrpos($path, '/', $i - $max - 1) + 1));
127+
}
126128

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

tests/Http/RequestFactory.scriptPath.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,45 @@ 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+
});
68+
69+
70+
test(function() use ($factory) {
71+
$_SERVER = array(
72+
'REQUEST_URI' => '/test//',
73+
'SCRIPT_NAME' => '/test/index.php',
74+
);
75+
76+
Assert::same( '/test/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
77+
});
78+
79+
80+
// http://forum.nette.org/cs/5932-lepsi-detekce-requesturi-a-scriptpath
81+
test(function() use ($factory) {
82+
$_SERVER = array(
83+
'REQUEST_URI' => '/sign/in/',
84+
'SCRIPT_NAME' => '/sign/in/',
85+
);
86+
87+
Assert::same( '/sign/in/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
88+
});
89+
90+
91+
// http://forum.nette.org/cs/9139-spatny-urlscript-scriptpath
92+
test(function() use ($factory) {
93+
$_SERVER = array(
94+
'REQUEST_URI' => '/configuration/',
95+
'SCRIPT_NAME' => '/configuration/www/index.php',
96+
);
97+
98+
Assert::same( '/configuration/', $factory->createHttpRequest()->getUrl()->getScriptPath() );
99+
});

0 commit comments

Comments
 (0)