merged branch stealth35/http_found_auth (PR #2029)

Commits
-------

aecfd0a [HttpFoundation] Support user and password in url

Discussion
----------

[HttpFoundation] Support user and password in url

Allow `http://user:password@test.com` for url request and `basic auth`

Added Methods for `Request`
  - `getUser`
  - `getPassword `

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

by stealth35 at 2011/08/26 07:52:57 -0700

@seldaek @stof thanks guys it's done

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

by stof at 2011/08/28 11:52:11 -0700

btw, you should rebase your branch on top of the upstream master branch as it conflicts

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

by stealth35 at 2011/08/28 13:15:55 -0700

@stof done

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

by stof at 2011/09/04 02:09:11 -0700

@stealth35 you made an error when rebasing: you merged the previous version of the branch instead of forcing the push, thus duplicating all commits in the PR (rebasing rewrites the history so it creates new commits). Could you clean your branch ?

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

by stealth35 at 2011/09/05 02:00:17 -0700

@stof 👍

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

by stealth35 at 2011/09/06 08:21:00 -0700

should be 2.0 ?

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

by fabpot at 2011/09/11 23:52:30 -0700

@stealth35: Can you remove the `@api` tags? We will review what is included into the public API later on. thanks.

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

by stealth35 at 2011/09/12 04:02:01 -0700

@fabpot done
This commit is contained in:
Fabien Potencier 2011-09-13 08:47:11 +02:00
commit 51403530b9
2 changed files with 61 additions and 1 deletions

View File

@ -213,6 +213,14 @@ class Request
$defaults['HTTP_HOST'] = $defaults['HTTP_HOST'].':'.$components['port'];
}
if (isset($components['user'])) {
$defaults['PHP_AUTH_USER'] = $components['user'];
}
if (isset($components['pass'])) {
$defaults['PHP_AUTH_PW'] = $components['pass'];
}
if (!isset($components['path'])) {
$components['path'] = '';
}
@ -571,6 +579,26 @@ class Request
return $this->headers->get('X-Forwarded-Port') ?: $this->server->get('SERVER_PORT');
}
/**
* Returns the user.
*
* @return string|null
*/
public function getUser()
{
return $this->server->get('PHP_AUTH_USER');
}
/**
* Returns the password.
*
* @return string|null
*/
public function getPassword()
{
return $this->server->get('PHP_AUTH_PW');
}
/**
* Returns the HTTP host being requested.
*
@ -624,7 +652,20 @@ class Request
$qs = '?'.$qs;
}
return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$this->getPathInfo().$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;
}
/**

View File

@ -128,6 +128,25 @@ class RequestTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('test.com:90', $request->getHttpHost());
$this->assertFalse($request->isSecure());
$request = Request::create('http://test:test@test.com');
$this->assertEquals('http://test:test@test.com/', $request->getUri());
$this->assertEquals('/', $request->getPathInfo());
$this->assertEquals('', $request->getQueryString());
$this->assertEquals(80, $request->getPort());
$this->assertEquals('test.com', $request->getHttpHost());
$this->assertEquals('test', $request->getUser());
$this->assertEquals('test', $request->getPassword());
$this->assertFalse($request->isSecure());
$request = Request::create('http://testnopass@test.com');
$this->assertEquals('http://testnopass@test.com/', $request->getUri());
$this->assertEquals('/', $request->getPathInfo());
$this->assertEquals('', $request->getQueryString());
$this->assertEquals(80, $request->getPort());
$this->assertEquals('test.com', $request->getHttpHost());
$this->assertEquals('testnopass', $request->getUser());
$this->assertNull($request->getPassword());
$this->assertFalse($request->isSecure());
}
/**