Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Uri implements UriInterface, \JsonSerializable
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
*/
private const CHAR_SUB_DELIMS = '!\$&\'\(\)\*\+,;=';
private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26'];
private const QUERY_SEPARATORS_REPLACEMENT = ['=' => '%3D', '&' => '%26', '+' => '%2B'];

/** @var string Uri scheme. */
private $scheme = '';
Expand Down Expand Up @@ -661,7 +661,8 @@ private static function getFilteredQueryString(UriInterface $uri, array $keys):

private static function generateQueryString(string $key, ?string $value): string
{
// Query string separators ("=", "&") within the key or value need to be encoded
// Query string separators ("=", "&") and literal plus signs ("+") within the
// key or value need to be encoded
// (while preventing double-encoding) before setting the query string. All other
// chars that need percent-encoding will be encoded by withQuery().
$queryString = strtr($key, self::QUERY_SEPARATORS_REPLACEMENT);
Expand Down
11 changes: 11 additions & 0 deletions tests/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ public function testWithQueryValueHandlesEncoding(): void
self::assertSame('E%3Dmc%5e2=ein%26stein', $uri->getQuery(), 'Encoded key/value do not get double-encoded');
}

public function testWithQueryValueEncodesPlusSign(): void
{
$uri = new Uri();
$uri = Uri::withQueryValue($uri, 'a+b', 'c+d');
self::assertSame('a%2Bb=c%2Bd', $uri->getQuery(), 'Plus signs in key and value get encoded to %2B');

$uri = new Uri();
$uri = Uri::withQueryValue($uri, 'query', 'a+b c');
self::assertSame('query=a%2Bb%20c', $uri->getQuery(), 'Plus sign is encoded distinctly from space');
}

public function testWithoutQueryValueHandlesEncoding(): void
{
// It also tests that the case of the percent-encoding does not matter,
Expand Down