Skip to content

Commit acdaec6

Browse files
committed
RequestFactory: simplified (WIP)
1 parent 88784b1 commit acdaec6

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

src/Http/RequestFactory.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ class RequestFactory extends Nette\Object
2121
/** @internal */
2222
const NONCHARS = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
2323

24-
/** @var array */
25-
public $urlFilters = array(
26-
'path' => array('#/{2,}#' => '/'), // '%20' => ''
27-
'url' => array(), // '#[.,)]\z#' => ''
28-
);
29-
3024
/** @var bool */
3125
private $binary = FALSE;
3226

@@ -81,26 +75,17 @@ public function createHttpRequest()
8175
}
8276

8377
// path & query
84-
if (isset($_SERVER['REQUEST_URI'])) { // Apache, IIS 6.0
85-
$requestUrl = $_SERVER['REQUEST_URI'];
86-
87-
} elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 (PHP as CGI ?)
88-
$requestUrl = $_SERVER['ORIG_PATH_INFO'];
89-
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
90-
$requestUrl .= '?' . $_SERVER['QUERY_STRING'];
91-
}
92-
} else {
78+
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
79+
if (preg_match(self::NONCHARS, $requestUrl) || preg_last_error()) {
9380
$requestUrl = '';
9481
}
9582

96-
$requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
9783
$tmp = explode('?', $requestUrl, 2);
98-
$url->setPath(Strings::replace($tmp[0], $this->urlFilters['path']));
84+
$url->setPath($tmp[0]);
9985
$url->setQuery(isset($tmp[1]) ? $tmp[1] : '');
10086

10187
// normalized url
10288
$url->canonicalize();
103-
$url->setPath(Strings::fixEncoding($url->getPath()));
10489

10590
// detect script path
10691
if (isset($_SERVER['SCRIPT_NAME'])) {

tests/Http/Request.request.phpt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@ require __DIR__ . '/../bootstrap.php';
1515
$_SERVER = array(
1616
'HTTPS' => 'On',
1717
'HTTP_HOST' => 'nette.org:8080',
18-
'QUERY_STRING' => 'x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be)',
18+
'QUERY_STRING' => 'x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be',
1919
'REMOTE_ADDR' => '192.168.188.66',
2020
'REQUEST_METHOD' => 'GET',
21-
'REQUEST_URI' => '/file.php?x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be)',
21+
'REQUEST_URI' => '/file.php?x param=val.&pa%%72am=val2&param3=v%20a%26l%3Du%2Be',
2222
'SCRIPT_FILENAME' => '/public_html/www/file.php',
2323
'SCRIPT_NAME' => '/file.php',
2424
);
2525

2626
test(function() {
2727
$factory = new Http\RequestFactory;
28-
$factory->urlFilters['path'] = array('#%20#' => '');
29-
$factory->urlFilters['url'] = array('#[.,)]\z#' => '');
28+
// $factory->urlFilters['path'] = array('#%20#' => '');
29+
// $factory->urlFilters['url'] = array('#[.,)]\z#' => '');
3030
$request = $factory->createHttpRequest();
3131

3232
Assert::same( 'GET', $request->getMethod() );
@@ -54,8 +54,6 @@ test(function() {
5454

5555
test(function() {
5656
$factory = new Http\RequestFactory;
57-
$factory->urlFilters['path'] = array();
58-
$factory->urlFilters['url'] = array();
5957
$request = $factory->createHttpRequest();
6058

6159
Assert::same( 'https', $request->getUrl()->scheme );
@@ -64,11 +62,11 @@ test(function() {
6462
Assert::same( 'nette.org', $request->getUrl()->host );
6563
Assert::same( 8080, $request->getUrl()->port );
6664
Assert::same( '/file.php', $request->getUrl()->path );
67-
Assert::same( 'x param=val.&pa%ram=val2&param3=v a%26l%3Du%2Be)', $request->getUrl()->query );
65+
Assert::same( 'x param=val.&pa%ram=val2&param3=v a%26l%3Du%2Be', $request->getUrl()->query );
6866
Assert::same( '', $request->getUrl()->fragment );
6967
Assert::same( 'val.', $request->getQuery('x_param') );
7068
Assert::same( 'val2', $request->getQuery('pa%ram') );
71-
Assert::same( 'v a&l=u+e)', $request->getQuery('param3') );
69+
Assert::same( 'v a&l=u+e', $request->getQuery('param3') );
7270
if (!function_exists('apache_request_headers')) {
7371
Assert::same( 'nette.org:8080', $request->headers['host'] );
7472
}

0 commit comments

Comments
 (0)