Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 4 additions & 6 deletions src/Elasticsearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@

use Elasticsearch\Common\Exceptions\UnexpectedValueException;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\Transport;
use Exception;
use GuzzleHttp\Ring\Future\FutureArrayInterface;
use Elasticsearch\Utility;

abstract class AbstractEndpoint
{
Expand Down Expand Up @@ -127,7 +125,7 @@ public function setIndex($index)
$index = implode(",", $index);
}

$this->index = urlencode($index);
$this->index = Utility::urlencode($index);

return $this;
}
Expand Down Expand Up @@ -155,7 +153,7 @@ public function setType(?string $type)
$type = implode(",", $type);
}

$this->type = urlencode($type);
$this->type = Utility::urlencode($type);

return $this;
}
Expand All @@ -175,7 +173,7 @@ public function setId($docID)
$docID = (string) $docID;
}

$this->id = urlencode($docID);
$this->id = Utility::urlencode($docID);

return $this;
}
Expand Down
47 changes: 47 additions & 0 deletions src/Elasticsearch/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);

namespace Elasticsearch;

class Utility
{
const ENV_URL_PLUS_AS_SPACE = 'ES_URL_PLUS_AS_SPACE';

/**
* Get the ENV variable with a thread safe fallback criteria
* @see https://github.com/elastic/elasticsearch-php/issues/1237
*
* @return string | false
*/
public static function getEnv(string $env)
{
return $_SERVER[$env] ?? $_ENV[$env] ?? getenv($env);
}

/**
* Encode a string in URL using urlencode() or rawurlencode()
* according to env variable ES_URL_PLUS_AS_SPACE.
* If ES_URL_PLUS_AS_SPACE is true use urlencode(), otherwise rawurlencode()
*
* @see https://github.com/elastic/elasticsearch-php/issues/1278
*/
public static function urlencode(string $url): string
{
return self::getEnv(self::ENV_URL_PLUS_AS_SPACE) === 'true'
? rawurlencode($url)
: urlencode($url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
class StickyRoundRobinSelectorTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector
*/
protected $roundRobin;

public function setUp(): void
{
$this->roundRobin = new Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use Elasticsearch\Common\Exceptions\Serializer\JsonErrorException;
use Elasticsearch\Serializers\SmartSerializer;
use Mockery as m;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -29,6 +28,11 @@
*/
class SmartSerializerTest extends TestCase
{
/**
* @var SmartSerializer
*/
protected $serializer;

public function setUp(): void
{
$this->serializer = new SmartSerializer();
Expand Down
21 changes: 21 additions & 0 deletions tests/Elasticsearch/Tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@

class TransportTest extends TestCase
{
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var LoggerInterface
*/
protected $trace;
/**
* @var SerializerInterface
*/
protected $serializer;
/**
* @var AbstractConnectionPool
*/
protected $connectionPool;
/**
* @var Connection
*/
protected $connection;

public function setUp(): void
{
$this->logger = $this->createMock(LoggerInterface::class);
Expand Down
77 changes: 77 additions & 0 deletions tests/Elasticsearch/Tests/UtilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Elasticsearch PHP client
*
* @link https://github.com/elastic/elasticsearch-php/
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
*
* Licensed to Elasticsearch B.V under one or more agreements.
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
* the GNU Lesser General Public License, Version 2.1, at your option.
* See the LICENSE file in the project root for more information.
*/


declare(strict_types = 1);

namespace Elasticsearch\Tests;

use Elasticsearch\Utility;
use PHPUnit\Framework\TestCase;

class UtilityTest extends TestCase
{
public function tearDown(): void
{
unset($_SERVER[Utility::ENV_URL_PLUS_AS_SPACE]);
unset($_ENV[Utility::ENV_URL_PLUS_AS_SPACE]);
putenv(Utility::ENV_URL_PLUS_AS_SPACE);
}

public function testGetEnvWithDollarServer()
{
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
}

public function testGetEnvWithDollarEnv()
{
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
}

public function testGetEnvWithPutEnv()
{
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
}

public function testUrlencodeWithDefault()
{
$url = Utility::urlencode('bar baz');
$this->assertEquals('bar+baz', $url);
}

public function testUrlencodeWithDollarServer()
{
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
$url = Utility::urlencode('bar baz');
$this->assertEquals('bar%20baz', $url);
}

public function testUrlencodeWithDollarEnv()
{
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
$url = Utility::urlencode('bar baz');
$this->assertEquals('bar%20baz', $url);
}

public function testUrlencodeWithPutEnv()
{
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
$url = Utility::urlencode('bar baz');
$this->assertEquals('bar%20baz', $url);
}
}