Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf850c1

Browse filesBrowse files
committed
[HttpFoundation] Fix request uri when it starts with double slashes
1 parent 7fa13be commit cf850c1
Copy full SHA for cf850c1

File tree

Expand file treeCollapse file tree

2 files changed

+64
-7
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+64
-7
lines changed

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+15-7Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+49Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.