1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-25 02:22:57 +01:00

Path for requests must always be absolute. Optimizing Url::__toString().

This commit is contained in:
Michael Dowling 2013-01-30 20:51:56 -08:00
parent 0d71baf35b
commit cde72ff57a
3 changed files with 17 additions and 16 deletions

View File

@ -349,7 +349,11 @@ class Request extends AbstractMessage implements RequestInterface
*/
public function getPath()
{
return $this->url->getPath();
if ($path = $this->url->getPath()) {
return '/' . ltrim($path, '/');
} else {
return '/';
}
}
/**
@ -435,10 +439,8 @@ class Request extends AbstractMessage implements RequestInterface
*/
public function getResource()
{
$resource = '/' . ltrim($this->url->getPath(), '/');
$query = (string) $this->url->getQuery();
if (!empty($query)) {
$resource = $this->getPath();
if ($query = (string) $this->url->getQuery()) {
$resource .= '?' . $query;
}

View File

@ -74,21 +74,20 @@ class Url
// Only include the port if it is not the default port of the scheme
if (isset($parts['port'])
&& !(($scheme == 'http' && $parts['port'] == 80)
|| ($scheme == 'https' && $parts['port'] == 443))) {
&& !(($scheme == 'http' && $parts['port'] == 80) || ($scheme == 'https' && $parts['port'] == 443))
) {
$url .= ':' . $parts['port'];
}
}
if (!empty($url)
&& $url[strlen($url) - 1] !== '/'
&& !empty($parts['path'])
&& $parts['path'][0] !== '/'
) {
// Add the path component if present
if (!empty($parts['path'])) {
// Always ensure that the path begins with '/' if set and something is before the path
if ($url && $parts['path'][0] != '/' && substr($url, -1) != '/') {
$url .= '/';
}
$url .= isset($parts['path']) ? $parts['path'] : '';
$url .= $parts['path'];
}
// Add the query string if present
if (isset($parts['query'])) {

View File

@ -334,7 +334,7 @@ class RequestTest extends \Guzzle\Tests\GuzzleTestCase
$this->assertEquals($this->request, $this->request->setPath('/index.html'));
$this->assertEquals('/index.html', $this->request->getPath());
$this->assertEquals($this->request, $this->request->setPath('index.html'));
$this->assertEquals('index.html', $this->request->getPath());
$this->assertEquals('/index.html', $this->request->getPath());
}
/**