mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 01:53:58 +01:00
Query strings should be treated an un-encoded by default.
This change updates query strings so that they are treated as un-encoded values by default where the value represents an un-encoded value to send over the wire. A Query object then encodes the value before sending over the wire. This means that even value query string values (e.g., ":") are url encoded. This makes the Query class match PHP's http_build_query function. However, if you want to send requests over the wire using valid query string characters that do not need to be encoded, then you can provide a string to Url::setQuery() and pass true as the second argument to specify that the query string is a raw string that should not be parsed or encoded (unless a call to getQuery() is subsequently made, forcing the query-string to be converted into a Query object).
This commit is contained in:
parent
2ad4f7d6de
commit
1330e3a1fe
@ -10,13 +10,10 @@ class Query extends Collection
|
||||
const RFC1738 = 'RFC1738';
|
||||
|
||||
/** @var callable Encoding function */
|
||||
private $encoding = [__CLASS__, 'rfc3986Encoding'];
|
||||
|
||||
private $encoding = 'rawurlencode';
|
||||
/** @var callable */
|
||||
private $aggregator;
|
||||
|
||||
private static $queryPattern = '/[^a-zA-Z0-9\-\._~!\$\'\(\)\*\+,;%:@\/\?]+|%(?![A-Fa-f0-9]{2})/';
|
||||
|
||||
/**
|
||||
* Parse a query string into a Query object
|
||||
*
|
||||
@ -122,10 +119,10 @@ class Query extends Collection
|
||||
{
|
||||
switch ($type) {
|
||||
case self::RFC3986:
|
||||
$this->encoding = [__CLASS__, 'rfc3986Encoding'];
|
||||
$this->encoding = 'rawurlencode';
|
||||
break;
|
||||
case self::RFC1738:
|
||||
$this->encoding = [__CLASS__, 'rfc1738Encoding'];
|
||||
$this->encoding = 'urlencode';
|
||||
break;
|
||||
case false:
|
||||
$this->encoding = function ($v) { return $v; };
|
||||
@ -204,26 +201,4 @@ class Query extends Collection
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function rfc3986Encoding($str)
|
||||
{
|
||||
static $cb = [__CLASS__, 'rawurlencodeMatch'];
|
||||
return preg_replace_callback(self::$queryPattern, $cb, $str);
|
||||
}
|
||||
|
||||
private static function rfc1738Encoding($str)
|
||||
{
|
||||
static $cb = [__CLASS__, 'urlencodeMatch'];
|
||||
return preg_replace_callback(self::$queryPattern, $cb, $str);
|
||||
}
|
||||
|
||||
private static function rawurlencodeMatch(array $match)
|
||||
{
|
||||
return rawurlencode($match[0]);
|
||||
}
|
||||
|
||||
private static function urlencodeMatch(array $match)
|
||||
{
|
||||
return urlencode($match[0]);
|
||||
}
|
||||
}
|
||||
|
81
src/Url.php
81
src/Url.php
@ -15,11 +15,11 @@ class Url
|
||||
private $password;
|
||||
private $path = '';
|
||||
private $fragment;
|
||||
|
||||
/** @var Query Query part of the URL */
|
||||
private $query;
|
||||
|
||||
private static $defaultPorts = ['http' => 80, 'https' => 443, 'ftp' => 21];
|
||||
private static $pathPattern = '/[^a-zA-Z0-9\-\._~!\$&\'\(\)\*\+,;=%:@\/]+|%(?![A-Fa-f0-9]{2})/';
|
||||
private static $queryPattern = '/[^a-zA-Z0-9\-\._~!\$\'\(\)\*\+,;%:@\/\?=&]+|%(?![A-Fa-f0-9]{2})/';
|
||||
/** @var Query|string Query part of the URL */
|
||||
private $query;
|
||||
|
||||
/**
|
||||
* Factory method to create a new URL from a URL string
|
||||
@ -119,14 +119,14 @@ class Url
|
||||
/**
|
||||
* Create a new URL from URL parts
|
||||
*
|
||||
* @param string $scheme Scheme of the URL
|
||||
* @param string $host Host of the URL
|
||||
* @param string $username Username of the URL
|
||||
* @param string $password Password of the URL
|
||||
* @param int $port Port of the URL
|
||||
* @param string $path Path of the URL
|
||||
* @param string $scheme Scheme of the URL
|
||||
* @param string $host Host of the URL
|
||||
* @param string $username Username of the URL
|
||||
* @param string $password Password of the URL
|
||||
* @param int $port Port of the URL
|
||||
* @param string $path Path of the URL
|
||||
* @param Query|array|string $query Query string of the URL
|
||||
* @param string $fragment Fragment of the URL
|
||||
* @param string $fragment Fragment of the URL
|
||||
*/
|
||||
public function __construct(
|
||||
$scheme,
|
||||
@ -145,9 +145,7 @@ class Url
|
||||
$this->password = $password;
|
||||
$this->fragment = $fragment;
|
||||
|
||||
if (!$query) {
|
||||
$this->query = new Query();
|
||||
} else {
|
||||
if ($query) {
|
||||
$this->setQuery($query);
|
||||
}
|
||||
|
||||
@ -159,7 +157,9 @@ class Url
|
||||
*/
|
||||
public function __clone()
|
||||
{
|
||||
$this->query = clone $this->query;
|
||||
if ($this->query instanceof Query) {
|
||||
$this->query = clone $this->query;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -417,24 +417,47 @@ class Url
|
||||
*/
|
||||
public function getQuery()
|
||||
{
|
||||
// Convert the query string to a query object if not already done.
|
||||
if (!$this->query instanceof Query) {
|
||||
$this->query = $this->query === null
|
||||
? new Query()
|
||||
: Query::fromString($this->query);
|
||||
}
|
||||
|
||||
return $this->query;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the query part of the URL
|
||||
* Set the query part of the URL.
|
||||
*
|
||||
* You may provide a query string as a string and pass $rawString as true
|
||||
* to provide a query string that is not parsed until a call to getQuery()
|
||||
* is made. Setting a raw query string will still encode invalid characters
|
||||
* in a query string.
|
||||
*
|
||||
* @param Query|string|array $query Query string value to set. Can
|
||||
* be a string that will be parsed into a Query object, an array
|
||||
* of key value pairs, or a Query object.
|
||||
* @param bool $rawString Set to true when providing a raw query string.
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function setQuery($query)
|
||||
public function setQuery($query, $rawString = false)
|
||||
{
|
||||
if ($query instanceof Query) {
|
||||
$this->query = $query;
|
||||
} elseif (is_string($query)) {
|
||||
$this->query = Query::fromString($query);
|
||||
if (!$rawString) {
|
||||
$this->query = Query::fromString($query);
|
||||
} else {
|
||||
// Ensure the query does not have illegal characters.
|
||||
$this->query = preg_replace_callback(
|
||||
self::$queryPattern,
|
||||
[__CLASS__, 'encodeMatch'],
|
||||
$query
|
||||
);
|
||||
}
|
||||
|
||||
} elseif (is_array($query)) {
|
||||
$this->query = new Query($query);
|
||||
} else {
|
||||
@ -497,16 +520,7 @@ class Url
|
||||
|
||||
// Passing a URL with a scheme overrides everything
|
||||
if ($parts['scheme']) {
|
||||
return new static(
|
||||
$parts['scheme'],
|
||||
$parts['host'],
|
||||
$parts['user'],
|
||||
$parts['pass'],
|
||||
$parts['port'],
|
||||
$parts['path'],
|
||||
clone $parts['query'],
|
||||
$parts['fragment']
|
||||
);
|
||||
return clone $url;
|
||||
}
|
||||
|
||||
// Setting a host overrides the entire rest of the URL
|
||||
@ -518,7 +532,9 @@ class Url
|
||||
$parts['pass'],
|
||||
$parts['port'],
|
||||
$parts['path'],
|
||||
clone $parts['query'],
|
||||
$parts['query'] instanceof Query
|
||||
? clone $parts['query']
|
||||
: $parts['query'],
|
||||
$parts['fragment']
|
||||
);
|
||||
}
|
||||
@ -526,7 +542,7 @@ class Url
|
||||
if (!$parts['path'] && $parts['path'] !== '0') {
|
||||
// The relative URL has no path, so check if it is just a query
|
||||
$path = $this->path ?: '';
|
||||
$query = count($parts['query']) ? $parts['query'] : $this->query;
|
||||
$query = $parts['query'] ?: $this->query;
|
||||
} else {
|
||||
$query = $parts['query'];
|
||||
if ($parts['path'][0] == '/' || !$this->path) {
|
||||
@ -547,7 +563,7 @@ class Url
|
||||
$this->password,
|
||||
$this->port,
|
||||
$path,
|
||||
clone $query,
|
||||
$query instanceof Query ? clone $query : $query,
|
||||
$parts['fragment']
|
||||
);
|
||||
|
||||
@ -568,9 +584,8 @@ class Url
|
||||
*/
|
||||
public static function encodePath($path)
|
||||
{
|
||||
static $pattern = '/[^a-zA-Z0-9\-\._~!\$&\'\(\)\*\+,;=%:@\/]+|%(?![A-Fa-f0-9]{2})/';
|
||||
static $cb = [__CLASS__, 'encodeMatch'];
|
||||
return preg_replace_callback($pattern, $cb, $path);
|
||||
return preg_replace_callback(self::$pathPattern, $cb, $path);
|
||||
}
|
||||
|
||||
private static function encodeMatch(array $match)
|
||||
|
@ -156,12 +156,6 @@ class QueryTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('foo=bar%20baz', (string) $q);
|
||||
}
|
||||
|
||||
public function testQueryStringsAllowSlash()
|
||||
{
|
||||
$q = Query::fromString('foo=bar/baz&bam=boo boo', Query::RFC3986);
|
||||
$this->assertEquals('foo=bar/baz&bam=boo%20boo', (string) $q);
|
||||
}
|
||||
|
||||
public function testQueryStringsAllowSlashButDoesNotDecodeWhenDisable()
|
||||
{
|
||||
$q = Query::fromString('foo=bar%2Fbaz&bam=boo%20boo', Query::RFC3986);
|
||||
@ -174,17 +168,4 @@ class QueryTest extends \PHPUnit_Framework_TestCase
|
||||
$q = Query::fromString('foo=bar%2Fbaz&bam=boo boo!', false);
|
||||
$this->assertEquals('foo=bar%2Fbaz&bam=boo boo!', (string) $q);
|
||||
}
|
||||
|
||||
public function testQueryDoesNotDoubleEncodeValues()
|
||||
{
|
||||
$q = new Query();
|
||||
$q->set('foo%20baz', 'bar');
|
||||
$this->assertEquals('foo%20baz=bar', (string) $q);
|
||||
}
|
||||
|
||||
public function testQueryIsNormalizedAndProperlyEncodedFromString()
|
||||
{
|
||||
$q = Query::fromString('foo=bar%2Fbaz&bam=boo boo!?');
|
||||
$this->assertEquals('foo=bar/baz&bam=boo%20boo!?', (string) $q);
|
||||
}
|
||||
}
|
||||
|
@ -245,7 +245,7 @@ class UrlTest extends \PHPUnit_Framework_TestCase
|
||||
$url->setScheme('https');
|
||||
$this->assertEquals('https', $url->getScheme());
|
||||
$url->setQuery('a=123');
|
||||
$this->assertEquals('a=123', $url->getQuery());
|
||||
$this->assertEquals('a=123', (string) $url->getQuery());
|
||||
$this->assertEquals(
|
||||
'https://b:a@example.com:8080/foo/bar?a=123#abc',
|
||||
(string) $url
|
||||
@ -256,6 +256,12 @@ class UrlTest extends \PHPUnit_Framework_TestCase
|
||||
'https://b:a@example.com:8080/foo/bar?b=boo#abc',
|
||||
(string) $url
|
||||
);
|
||||
|
||||
$url->setQuery('a%20=bar!', true);
|
||||
$this->assertEquals(
|
||||
'https://b:a@example.com:8080/foo/bar?a%20=bar!#abc',
|
||||
(string) $url
|
||||
);
|
||||
}
|
||||
|
||||
public function testSetQueryAcceptsArray()
|
||||
@ -274,6 +280,17 @@ class UrlTest extends \PHPUnit_Framework_TestCase
|
||||
$url->setQuery(false);
|
||||
}
|
||||
|
||||
public function testDefersParsingAndEncodingQueryUntilNecessary()
|
||||
{
|
||||
$url = Url::fromString('http://www.test.com');
|
||||
// Note that invalid characters are encoded.
|
||||
$url->setQuery('foo#bar/', true);
|
||||
$this->assertEquals('http://www.test.com?foo%23bar/', (string) $url);
|
||||
$this->assertInternalType('string', $this->readAttribute($url, 'query'));
|
||||
$this->assertEquals('foo%23bar%2F', (string) $url->getQuery());
|
||||
$this->assertInstanceOf('GuzzleHttp\Query', $this->readAttribute($url, 'query'));
|
||||
}
|
||||
|
||||
public function urlProvider()
|
||||
{
|
||||
return array(
|
||||
|
Loading…
x
Reference in New Issue
Block a user