added Request::getSchemeAndHttpHost() and Request::getUserInfo() (closes #4312, refs #3416, refs #3056)
This commit is contained in:
parent
cd08db8e2a
commit
df8d94e33c
@ -4,6 +4,7 @@ CHANGELOG
|
||||
2.1.0
|
||||
-----
|
||||
|
||||
* added Request::getSchemeAndHttpHost() and Request::getUserInfo()
|
||||
* added a fluent interface to the Response class
|
||||
* added Request::isProxyTrusted()
|
||||
* added JsonResponse
|
||||
|
@ -694,6 +694,23 @@ class Request
|
||||
return $this->server->get('PHP_AUTH_PW');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the user info.
|
||||
*
|
||||
* @return string A user name and, optionally, scheme-specific information about how to gain authorization to access the server
|
||||
*/
|
||||
public function getUserInfo()
|
||||
{
|
||||
$userinfo = $this->getUser();
|
||||
|
||||
$pass = $this->getPassword();
|
||||
if ('' != $pass) {
|
||||
$userinfo .= ":$pass";
|
||||
}
|
||||
|
||||
return $userinfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTTP host being requested.
|
||||
*
|
||||
@ -731,6 +748,16 @@ class Request
|
||||
return $this->requestUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the scheme and HTTP host.
|
||||
*
|
||||
* @return string The schem and HTTP host
|
||||
*/
|
||||
public function getSchemeAndHttpHost()
|
||||
{
|
||||
return $this->getScheme().'://'.(('' != $auth = $this->getUserInfo()) ? $auth.'@' : '').$this->getHttpHost();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a normalized URI for the Request.
|
||||
*
|
||||
@ -747,20 +774,7 @@ class Request
|
||||
$qs = '?'.$qs;
|
||||
}
|
||||
|
||||
$auth = '';
|
||||
if ($user = $this->getUser()) {
|
||||
$auth = $user;
|
||||
}
|
||||
|
||||
if ($pass = $this->getPassword()) {
|
||||
$auth .= ":$pass";
|
||||
}
|
||||
|
||||
if ('' !== $auth) {
|
||||
$auth .= '@';
|
||||
}
|
||||
|
||||
return $this->getScheme().'://'.$auth.$this->getHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
|
||||
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -774,7 +788,7 @@ class Request
|
||||
*/
|
||||
public function getUriForPath($path)
|
||||
{
|
||||
return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
|
||||
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1274,7 +1288,7 @@ class Request
|
||||
} elseif ($this->server->has('REQUEST_URI')) {
|
||||
$requestUri = $this->server->get('REQUEST_URI');
|
||||
// HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
|
||||
$schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
|
||||
$schemeAndHttpHost = $this->getSchemeAndHttpHost();
|
||||
if (strpos($requestUri, $schemeAndHttpHost) === 0) {
|
||||
$requestUri = substr($requestUri, strlen($schemeAndHttpHost));
|
||||
}
|
||||
|
@ -336,7 +336,17 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
'http://hostname:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string',
|
||||
$request->getUri()
|
||||
);
|
||||
}
|
||||
|
||||
// with user info
|
||||
|
||||
$server['PHP_AUTH_USER'] = 'fabien';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://fabien@hostname:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
|
||||
|
||||
$server['PHP_AUTH_PW'] = 'symfony';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://fabien:symfony@hostname:8080/ba%20se/index_dev.php/foo%20bar/in+fo?query=string', $request->getUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\HttpFoundation\Request::getUriForPath
|
||||
@ -436,6 +446,61 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
$this->assertEquals('http://servername/some/path', $request->getUriForPath('/some/path'), '->getUriForPath() with rewrite, default port without HOST_HEADER');
|
||||
$this->assertEquals('servername', $request->getHttpHost());
|
||||
|
||||
// with user info
|
||||
|
||||
$server['PHP_AUTH_USER'] = 'fabien';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://fabien@servername/some/path', $request->getUriForPath('/some/path'));
|
||||
|
||||
$server['PHP_AUTH_PW'] = 'symfony';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://fabien:symfony@servername/some/path', $request->getUriForPath('/some/path'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\HttpFoundation\Request::getUserInfo
|
||||
*/
|
||||
public function testGetUserInfo()
|
||||
{
|
||||
$request = new Request();
|
||||
|
||||
$server['PHP_AUTH_USER'] = 'fabien';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('fabien', $request->getUserInfo());
|
||||
|
||||
$server['PHP_AUTH_USER'] = '0';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('0', $request->getUserInfo());
|
||||
|
||||
$server['PHP_AUTH_PW'] = '0';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('0:0', $request->getUserInfo());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Symfony\Component\HttpFoundation\Request::getSchemeAndHttpHost
|
||||
*/
|
||||
public function testGetSchemeAndHttpHost()
|
||||
{
|
||||
$request = new Request();
|
||||
|
||||
$server['SERVER_NAME'] = 'servername';
|
||||
$server['SERVER_PORT'] = '90';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://servername:90', $request->getSchemeAndHttpHost());
|
||||
|
||||
$server['PHP_AUTH_USER'] = 'fabien';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://fabien@servername:90', $request->getSchemeAndHttpHost());
|
||||
|
||||
$server['PHP_AUTH_USER'] = '0';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://0@servername:90', $request->getSchemeAndHttpHost());
|
||||
|
||||
$server['PHP_AUTH_PW'] = '0';
|
||||
$request->initialize(array(), array(), array(), array(), array(), $server);
|
||||
$this->assertEquals('http://0:0@servername:90', $request->getSchemeAndHttpHost());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user