Skip to content

Fix for #993, added ClientBuilder::includePortInHostHeader() #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 14, 2020
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
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]);
}
}
}