Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d32ecff

Browse files
committed
bug #24573 [3.3] Fixed pathinfo calculation for requests starting with a question mark. (syzygymsu)
This PR was merged into the 3.3 branch. Discussion ---------- [3.3] Fixed pathinfo calculation for requests starting with a question mark. | Q | A | ------------- | --- | Branch? | 3.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24487 | License | MIT | Doc PR | no Fix of bad merge conflict resolving as mentioned in #24487. Port #21968 to 3.3+ Commits ------- c17a922 Fixed pathinfo calculation for requests starting with a question mark. - fix bad conflict resolving issue - port #21968 to 3.3+
2 parents ff4f4ca + c17a922 commit d32ecff

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1863,6 +1863,9 @@ protected function prepareBaseUrl()
18631863

18641864
// Does the baseUrl have anything in common with the request_uri?
18651865
$requestUri = $this->getRequestUri();
1866+
if ($requestUri !== '' && $requestUri[0] !== '/') {
1867+
$requestUri = '/'.$requestUri;
1868+
}
18661869

18671870
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, $baseUrl)) {
18681871
// full $baseUrl matches
@@ -1935,9 +1938,12 @@ protected function preparePathInfo()
19351938
}
19361939

19371940
// Remove the query string from REQUEST_URI
1938-
if ($pos = strpos($requestUri, '?')) {
1941+
if (false !== $pos = strpos($requestUri, '?')) {
19391942
$requestUri = substr($requestUri, 0, $pos);
19401943
}
1944+
if ($requestUri !== '' && $requestUri[0] !== '/') {
1945+
$requestUri = '/'.$requestUri;
1946+
}
19411947

19421948
$pathInfo = substr($requestUri, strlen($baseUrl));
19431949
if (null !== $baseUrl && (false === $pathInfo || '' === $pathInfo)) {

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,12 @@ public function testGetPathInfo()
12861286
$request->initialize(array(), array(), array(), array(), array(), $server);
12871287

12881288
$this->assertEquals('/path%20test/info', $request->getPathInfo());
1289+
1290+
$server = array();
1291+
$server['REQUEST_URI'] = '?a=b';
1292+
$request->initialize(array(), array(), array(), array(), array(), $server);
1293+
1294+
$this->assertEquals('/', $request->getPathInfo());
12891295
}
12901296

12911297
public function testGetParameterPrecedence()
@@ -2188,6 +2194,61 @@ public function testGetTrustedHeaderName()
21882194
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X_FORWARDED_PORT');
21892195
Request::setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X_FORWARDED_PROTO');
21902196
}
2197+
2198+
public function nonstandardRequestsData()
2199+
{
2200+
return array(
2201+
array('', '', '/', 'http://host:8080/', ''),
2202+
array('/', '', '/', 'http://host:8080/', ''),
2203+
2204+
array('hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2205+
array('/hello/app.php/x', '', '/x', 'http://host:8080/hello/app.php/x', '/hello', '/hello/app.php'),
2206+
2207+
array('', 'a=b', '/', 'http://host:8080/?a=b'),
2208+
array('?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2209+
array('/?a=b', 'a=b', '/', 'http://host:8080/?a=b'),
2210+
2211+
array('x', 'a=b', '/x', 'http://host:8080/x?a=b'),
2212+
array('x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2213+
array('/x?a=b', 'a=b', '/x', 'http://host:8080/x?a=b'),
2214+
2215+
array('hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2216+
array('/hello/x', '', '/x', 'http://host:8080/hello/x', '/hello'),
2217+
2218+
array('hello/app.php/x', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2219+
array('hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2220+
array('/hello/app.php/x?a=b', 'a=b', '/x', 'http://host:8080/hello/app.php/x?a=b', '/hello', '/hello/app.php'),
2221+
);
2222+
}
2223+
2224+
/**
2225+
* @dataProvider nonstandardRequestsData
2226+
*/
2227+
public function testNonstandardRequests($requestUri, $queryString, $expectedPathInfo, $expectedUri, $expectedBasePath = '', $expectedBaseUrl = null)
2228+
{
2229+
if (null === $expectedBaseUrl) {
2230+
$expectedBaseUrl = $expectedBasePath;
2231+
}
2232+
2233+
$server = array(
2234+
'HTTP_HOST' => 'host:8080',
2235+
'SERVER_PORT' => '8080',
2236+
'QUERY_STRING' => $queryString,
2237+
'PHP_SELF' => '/hello/app.php',
2238+
'SCRIPT_FILENAME' => '/some/path/app.php',
2239+
'REQUEST_URI' => $requestUri,
2240+
);
2241+
2242+
$request = new Request(array(), array(), array(), array(), array(), $server);
2243+
2244+
$this->assertEquals($expectedPathInfo, $request->getPathInfo());
2245+
$this->assertEquals($expectedUri, $request->getUri());
2246+
$this->assertEquals($queryString, $request->getQueryString());
2247+
$this->assertEquals(8080, $request->getPort());
2248+
$this->assertEquals('host:8080', $request->getHttpHost());
2249+
$this->assertEquals($expectedBaseUrl, $request->getBaseUrl());
2250+
$this->assertEquals($expectedBasePath, $request->getBasePath());
2251+
}
21912252
}
21922253

21932254
class RequestContentProxy extends Request

0 commit comments

Comments
 (0)