Skip to content

Commit

Permalink
merged branch fabpot/request-methods (PR #4679)
Browse files Browse the repository at this point in the history
Commits
-------

df8d94e added Request::getSchemeAndHttpHost() and Request::getUserInfo() (closes #4312, refs #3416, refs #3056)

Discussion
----------

added Request::getSchemeAndHttpHost() and Request::getUserInfo()

see #4312

---------------------------------------------------------------------------

by travisbot at 2012-06-28T15:22:03Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730172) (merged 598bd56f into 0d27570).

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:22:35Z

Why not just `getSchemeAndHost`? That sounds long enough, and is fairly explicit given the context.

---------------------------------------------------------------------------

by fabpot at 2012-06-28T15:25:34Z

@Seldaek because (and that's probably unfortunate) we have both `getHost()` and `getHttpHost()`. The former does not include the port whereas the latter does.

---------------------------------------------------------------------------

by Seldaek at 2012-06-28T15:26:42Z

Ok makes sense.

---------------------------------------------------------------------------

by travisbot at 2012-06-28T16:11:16Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/1730630) (merged df8d94e into 884a826).
  • Loading branch information
fabpot committed Jun 28, 2012
2 parents 7e71229 + c07d21d commit d653bbd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
46 changes: 30 additions & 16 deletions Request.php
Expand Up @@ -700,6 +700,23 @@ public function getPassword()
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.
*
Expand Down Expand Up @@ -737,6 +754,16 @@ public function getRequestUri()
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.
*
Expand All @@ -753,20 +780,7 @@ public function getUri()
$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;
}

/**
Expand All @@ -780,7 +794,7 @@ public function getUri()
*/
public function getUriForPath($path)
{
return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
}

/**
Expand Down Expand Up @@ -1280,7 +1294,7 @@ protected function prepareRequestUri()
} 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));
}
Expand Down
67 changes: 66 additions & 1 deletion Tests/RequestTest.php
Expand Up @@ -336,7 +336,17 @@ public function testGetUri()
'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
Expand Down Expand Up @@ -436,6 +446,61 @@ public function testGetUriForPath()

$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());
}

/**
Expand Down

0 comments on commit d653bbd

Please sign in to comment.