Skip to content

Commit

Permalink
Merge branch 'fix/993'
Browse files Browse the repository at this point in the history
  • Loading branch information
ezimuel committed Feb 14, 2020
2 parents 24534b5 + e8f0f7b commit 6257d0e
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Elasticsearch/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class ClientBuilder
*/
private $sslVerification = null;

/**
* @var bool
*/
private $includePortInHostHeader = false;

public static function create(): ClientBuilder
{
return new static();
Expand Down Expand Up @@ -465,6 +470,17 @@ public function setSSLVerification($value = true): ClientBuilder
return $this;
}

/**
* Include the port in Host header
* @see https://github.com/elastic/elasticsearch-php/issues/993
*/
public function includePortInHostHeader(bool $enable): ClientBuilder
{
$this->includePortInHostHeader = $enable;

return $this;
}

public function build(): Client
{
$this->buildLoggers();
Expand Down Expand Up @@ -505,6 +521,8 @@ public function build(): Client
$this->serializer = new $this->serializer;
}

$this->connectionParams['client']['port_in_header'] = $this->includePortInHostHeader;

if (is_null($this->connectionFactory)) {
if (is_null($this->connectionParams)) {
$this->connectionParams = [];
Expand Down
7 changes: 6 additions & 1 deletion src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,19 @@ public function performRequest(string $method, string $uri, ?array $params = [],
$this->headers = array_merge($this->headers, $options['client']['headers']);
}

$host = $this->host;
if (isset($this->connectionParams['client']['port_in_header']) && $this->connectionParams['client']['port_in_header']) {
$host .= ':' . $this->port;
}

$request = [
'http_method' => $method,
'scheme' => $this->transportSchema,
'uri' => $this->getURI($uri, $params),
'body' => $body,
'headers' => array_merge(
[
'Host' => [$this->host]
'Host' => [$host]
],
$this->headers
)
Expand Down
86 changes: 86 additions & 0 deletions tests/Elasticsearch/Tests/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,90 @@ public function testElasticCloudIdNotOverrideCurlEncoding()
$this->assertNotContains('gzip', $request['request']['client']['curl']);
}
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/993
*/
public function testIncludePortInHostHeader()
{
$host = "localhost";
$url = "$host:1234";
$params = [
'client' => [
'verbose' => true
]
];
$client = ClientBuilder::create()
->setConnectionParams($params)
->setHosts([$url])
->includePortInHostHeader(true)
->build();

$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
$this->assertEquals($url, $request['request']['headers']['Host'][0]);
}
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/993
*/
public function testNotIncludePortInHostHeaderAsDefault()
{
$host = "localhost";
$url = "$host:1234";
$params = [
'client' => [
'verbose' => true
]
];
$client = ClientBuilder::create()
->setConnectionParams($params)
->setHosts([$url])
->build();

$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
$this->assertEquals($host, $request['request']['headers']['Host'][0]);
}
}

/**
* @see https://github.com/elastic/elasticsearch-php/issues/993
*/
public function testNotIncludePortInHostHeader()
{
$host = "localhost";
$url = "$host:1234";
$params = [
'client' => [
'verbose' => true
]
];
$client = ClientBuilder::create()
->setConnectionParams($params)
->setHosts([$url])
->includePortInHostHeader(false)
->build();

$this->assertInstanceOf(Client::class, $client);

try {
$result = $client->info();
} catch (ElasticsearchException $e) {
$request = $client->transport->getLastConnection()->getLastRequestInfo();
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
$this->assertEquals($host, $request['request']['headers']['Host'][0]);
}
}
}

0 comments on commit 6257d0e

Please sign in to comment.