10000 bug #29494 [HttpFoundation] Fix request uri when it starts with doubl… · symfony/symfony@e38b5d2 · GitHub
[go: up one dir, main page]

Skip to content

Commit e38b5d2

Browse files
committed
bug #29494 [HttpFoundation] Fix request uri when it starts with double slashes (alquerci)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] Fix request uri when it starts with double slashes | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29478 | License | MIT | Doc PR | ~ When the `REQUEST_URI` starts with a slash no need to `parse_url()`. However to keep the same behaviour regarding the fragment we need to add a logic to remove it. While `parse_url()` handle all cases itself. Commits ------- cf850c1 [HttpFoundation] Fix request uri when it starts with double slashes
2 parents 482f49a + cf850c1 commit e38b5d2

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,15 +1837,23 @@ protected function prepareRequestUri()
18371837
} elseif ($this->server->has('REQUEST_URI')) {
18381838
$requestUri = $this->server->get('REQUEST_URI');
18391839

1840-
// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, only use URL path
1841-
$uriComponents = parse_url($requestUri);
1840+
if ('' !== $requestUri && '/' === $requestUri[0]) {
1841+
// To only use path and query remove the fragment.
1842+
if (false !== $pos = strpos($requestUri, '#')) {
1843+
$requestUri = substr($requestUri, 0, $pos);
1844+
}
1845+
} else {
1846+
// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,
1847+
// only use URL path.
1848+
$uriComponents = parse_url($requestUri);
18421849

1843-
if (isset($uriComponents['path'])) {
1844-
$requestUri = $uriComponents['path'];
1845-
}
1850+
if (isset($uriComponents['path'])) {
1851+
$requestUri = $uriComponents['path'];
1852+
}
18461853

1847-
if (isset($uriComponents['query'])) {
1848-
$requestUri .= '?'.$uriComponents['query'];
1854+
if (isset($uriComponents['query'])) {
1855+
$requestUri .= '?'.$uriComponents['query'];
1856+
}
18491857
}
18501858
} elseif ($this->server->has('ORIG_PATH_INFO')) {
18511859
// IIS 5.0, PHP as CGI

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,55 @@ public function testCreateWithRequestUri()
283283
$this->assertEquals('http://test.com/foo', $request->getUri());
284284
}
285285

286+
/**
287+
* @dataProvider getRequestUriData
288+
*/
289+
public function testGetRequestUri($serverRequestUri, $expected, $message)
290+
{
291+
$request = new Request();
292+
$request->server->add(array(
293+
'REQUEST_URI' => $serverRequestUri,
294+
295+
// For having http://test.com
296+
'SERVER_NAME' => 'test.com',
297+
'SERVER_PORT' => 80,
298+
));
299+
300+
$this->assertSame($expected, $request->getRequestUri(), $message);
301+
$this->assertSame($expected, $request->server->get('REQUEST_URI'), 'Normalize the request URI.');
302+
}
303+
304+
public function getRequestUriData()
305+
{
306+
$message = 'Do not modify the path.';
307+
yield array('/foo', '/foo', $message);
308+
yield array('//bar/foo', '//bar/foo', $message);
309+
yield array('///bar/foo', '///bar/foo', $message);
310+
311+
$message = 'Handle when the scheme, host are on REQUEST_URI.';
312+
yield array('http://test.com/foo?bar=baz', '/foo?bar=baz', $message);
313+
314+
$message = 'Handle when the scheme, host and port are on REQUEST_URI.';
315+
yield array('http://test.com:80/foo', '/foo', $message);
316+
yield array('https://test.com:8080/foo', '/foo', $message);
317+
yield array('https://test.com:443/foo', '/foo', $message);
318+
319+
$message = 'Fragment should not be included in the URI';
320+
yield array('http://test.com/foo#bar', '/foo', $message);
321+
yield array('/foo#bar', '/foo', $message);
322+
}
323+
324+
public function testGetRequestUriWithoutRequiredHeader()
325+
{
326+
$expected = '';
327+
328+
$request = new Request();
329+
330+
$message = 'Fallback to empty URI when headers are missing.';
331+
$this->assertSame($expected, $request->getRequestUri(), $message);
332+
$this->assertSame($expected, $request->server->get('REQUEST_URI'), 'Normalize the request URI.');
333+
}
334+
286335
public function testCreateCheckPrecedence()
287336
{
288337
// server is used by default

0 commit comments

Comments
 (0)
0