Skip to content

Commit

Permalink
[HttpFoundation] Fix prepare request uri when it starts with double s…
Browse files Browse the repository at this point in the history
…lashes - KISS
  • Loading branch information
alquerci committed Dec 8, 2018
1 parent 113a577 commit bfe7d4c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1836,17 +1836,17 @@ protected function prepareRequestUri()
$this->server->remove('IIS_WasUrlRewritten');
} elseif ($this->server->has('REQUEST_URI')) {
$requestUri = $this->server->get('REQUEST_URI');

// The REQUEST_URI starts with double slashes.
if (0 === strpos($requestUri, '//')) {
// We assume that the scheme and host are not provided (e.g. //path/too).
// Then patch the call to `parse_url()`.
$requestUri = "http://example.com$requestUri";
$uriComponents = array();

// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path,
// only use URL path.
if ('' !== $requestUri && '/' !== $requestUri[0]) {
$uriComponents = parse_url($requestUri);
} elseif (false !== $pos = strpos($requestUri, '#')) {
// Remove the fragment.
$requestUri = substr($requestUri, 0, $pos);
}

// HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, only use URL path
$uriComponents = parse_url($requestUri);

if (isset($uriComponents['path'])) {
$requestUri = $uriComponents['path'];
}
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -282,9 +282,19 @@ public function testCreateWithRequestUri()
$request->server->set('REQUEST_URI', 'http://test.com/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());

$request = Request::create('http://test.com/foo#bar');
$request->server->set('REQUEST_URI', '/foo#bar');
$this->assertEquals('http://test.com/foo', $request->getUri());

// When the path starts with 2 slashes
$request = Request::create('http://foo//bar/foo');
$request->server->set('REQUEST_URI', '//bar/foo');
$this->assertEquals('//bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with double slashes.');

// When the path starts with more than 2 slashes
$request = Request::create('http://foo///bar/foo');
$request->server->set('REQUEST_URI', '///bar/foo');
$this->assertEquals('///bar/foo', $request->getPathInfo(), 'When the REQUEST_URI starts with more than 2 slashes.');
}

public function testCreateCheckPrecedence()
Expand Down

0 comments on commit bfe7d4c

Please sign in to comment.