Skip to content

Commit c1a040c

Browse files
committed
Backported 1303 in 8.x
1 parent 55407ab commit c1a040c

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

src/Traits/EndpointTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Elastic\Elasticsearch\Client;
1818
use Elastic\Elasticsearch\Exception\ContentTypeException;
1919
use Elastic\Elasticsearch\Exception\MissingParameterException;
20+
use Elastic\Elasticsearch\Utility;
2021
use Elastic\Transport\Serializer\JsonSerializer;
2122
use Elastic\Transport\Serializer\NDJsonSerializer;
2223
use Http\Discovery\Psr17FactoryDiscovery;
@@ -79,7 +80,7 @@ private function convertValue($value): string
7980
*/
8081
protected function encode($value): string
8182
{
82-
return urlencode($this->convertValue($value));
83+
return Utility::urlencode($this->convertValue($value));
8384
}
8485

8586
/**

src/Utility.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch;
16+
17+
class Utility
18+
{
19+
const ENV_URL_PLUS_AS_SPACE = 'ELASTIC_CLIENT_URL_PLUS_AS_SPACE';
20+
21+
/**
22+
* Get the ENV variable with a thread safe fallback criteria
23+
* @see https://github.com/elastic/elasticsearch-php/issues/1237
24+
*
25+
* @return string | false
26+
*/
27+
public static function getEnv(string $env)
28+
{
29+
return $_SERVER[$env] ?? $_ENV[$env] ?? getenv($env);
30+
}
31+
32+
/**
33+
* Encode a string in URL using urlencode() or rawurlencode()
34+
* according to ENV_URL_PLUS_AS_SPACE.
35+
* If ENV_URL_PLUS_AS_SPACE is not defined or true use urlencode(),
36+
* otherwise use rawurlencode()
37+
*
38+
* @see https://github.com/elastic/elasticsearch-php/issues/1278
39+
* @deprecated will be replaced by PHP function rawurlencode()
40+
*/
41+
public static function urlencode(string $url): string
42+
{
43+
$plusAsSpace = self::getEnv(self::ENV_URL_PLUS_AS_SPACE);
44+
return $plusAsSpace === false || $plusAsSpace === 'true'
45+
? urlencode($url)
46+
: rawurlencode($url);
47+
}
48+
}

tests/UtilityTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Elasticsearch PHP Client
4+
*
5+
* @link https://github.com/elastic/elasticsearch-php
6+
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
9+
* Licensed to Elasticsearch B.V under one or more agreements.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
11+
* See the LICENSE file in the project root for more information.
12+
*/
13+
declare(strict_types = 1);
14+
15+
namespace Elastic\Elasticsearch\Tests;
16+
17+
use Elastic\Elasticsearch\Utility;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class UtilityTest extends TestCase
21+
{
22+
public function tearDown(): void
23+
{
24+
unset($_SERVER[Utility::ENV_URL_PLUS_AS_SPACE]);
25+
unset($_ENV[Utility::ENV_URL_PLUS_AS_SPACE]);
26+
putenv(Utility::ENV_URL_PLUS_AS_SPACE);
27+
}
28+
29+
public function testGetEnvWithDollarServer()
30+
{
31+
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
32+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
33+
}
34+
35+
public function testGetEnvWithDollarEnv()
36+
{
37+
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
38+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
39+
}
40+
41+
public function testGetEnvWithPutEnv()
42+
{
43+
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
44+
$this->assertEquals('true', Utility::getEnv(Utility::ENV_URL_PLUS_AS_SPACE));
45+
}
46+
47+
public function testUrlWithPlusAsDefault()
48+
{
49+
$url = Utility::urlencode('bar baz');
50+
$this->assertEquals('bar+baz', $url);
51+
}
52+
53+
public function testUrlWithPlusWithDollarServer()
54+
{
55+
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
56+
$url = Utility::urlencode('bar baz');
57+
$this->assertEquals('bar+baz', $url);
58+
}
59+
60+
public function testUrlWithPlusWithDollarEnv()
61+
{
62+
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'true';
63+
$url = Utility::urlencode('bar baz');
64+
$this->assertEquals('bar+baz', $url);
65+
}
66+
67+
public function testUrlWithPlusWithPutEnv()
68+
{
69+
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=true');
70+
$url = Utility::urlencode('bar baz');
71+
$this->assertEquals('bar+baz', $url);
72+
}
73+
74+
public function testUrlWith2BWithDollarServer()
75+
{
76+
$_SERVER[Utility::ENV_URL_PLUS_AS_SPACE] = 'false';
77+
$url = Utility::urlencode('bar baz');
78+
$this->assertEquals('bar%20baz', $url);
79+
}
80+
81+
public function testUrlWith2BWithDollarEnv()
82+
{
83+
$_ENV[Utility::ENV_URL_PLUS_AS_SPACE] = 'false';
84+
$url = Utility::urlencode('bar baz');
85+
$this->assertEquals('bar%20baz', $url);
86+
}
87+
88+
public function testUrlWith2BWithPutEnv()
89+
{
90+
putenv(Utility::ENV_URL_PLUS_AS_SPACE . '=false');
91+
$url = Utility::urlencode('bar baz');
92+
$this->assertEquals('bar%20baz', $url);
93+
}
94+
}

0 commit comments

Comments
 (0)