Skip to content

Commit 6257d0e

Browse files
committed
Merge branch 'fix/993'
2 parents 24534b5 + e8f0f7b commit 6257d0e

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ class ClientBuilder
128128
*/
129129
private $sslVerification = null;
130130

131+
/**
132+
* @var bool
133+
*/
134+
private $includePortInHostHeader = false;
135+
131136
public static function create(): ClientBuilder
132137
{
133138
return new static();
@@ -465,6 +470,17 @@ public function setSSLVerification($value = true): ClientBuilder
465470
return $this;
466471
}
467472

473+
/**
474+
* Include the port in Host header
475+
* @see https://github.com/elastic/elasticsearch-php/issues/993
476+
*/
477+
public function includePortInHostHeader(bool $enable): ClientBuilder
478+
{
479+
$this->includePortInHostHeader = $enable;
480+
481+
return $this;
482+
}
483+
468484
public function build(): Client
469485
{
470486
$this->buildLoggers();
@@ -505,6 +521,8 @@ public function build(): Client
505521
$this->serializer = new $this->serializer;
506522
}
507523

524+
$this->connectionParams['client']['port_in_header'] = $this->includePortInHostHeader;
525+
508526
if (is_null($this->connectionFactory)) {
509527
if (is_null($this->connectionParams)) {
510528
$this->connectionParams = [];

src/Elasticsearch/Connections/Connection.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,19 @@ public function performRequest(string $method, string $uri, ?array $params = [],
198198
$this->headers = array_merge($this->headers, $options['client']['headers']);
199199
}
200200

201+
$host = $this->host;
202+
if (isset($this->connectionParams['client']['port_in_header']) && $this->connectionParams['client']['port_in_header']) {
203+
$host .= ':' . $this->port;
204+
}
205+
201206
$request = [
202207
'http_method' => $method,
203208
'scheme' => $this->transportSchema,
204209
'uri' => $this->getURI($uri, $params),
205210
'body' => $body,
206211
'headers' => array_merge(
207212
[
208-
'Host' => [$this->host]
213+
'Host' => [$host]
209214
],
210215
$this->headers
211216
)

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,90 @@ public function testElasticCloudIdNotOverrideCurlEncoding()
7070
$this->assertNotContains('gzip', $request['request']['client']['curl']);
7171
}
7272
}
73+
74+
/**
75+
* @see https://github.com/elastic/elasticsearch-php/issues/993
76+
*/
77+
public function testIncludePortInHostHeader()
78+
{
79+
$host = "localhost";
80+
$url = "$host:1234";
81+
$params = [
82+
'client' => [
83+
'verbose' => true
84+
]
85+
];
86+
$client = ClientBuilder::create()
87+
->setConnectionParams($params)
88+
->setHosts([$url])
89+
->includePortInHostHeader(true)
90+
->build();
91+
92+
$this->assertInstanceOf(Client::class, $client);
93+
94+
try {
95+
$result = $client->info();
96+
} catch (ElasticsearchException $e) {
97+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
98+
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
99+
$this->assertEquals($url, $request['request']['headers']['Host'][0]);
100+
}
101+
}
102+
103+
/**
104+
* @see https://github.com/elastic/elasticsearch-php/issues/993
105+
*/
106+
public function testNotIncludePortInHostHeaderAsDefault()
107+
{
108+
$host = "localhost";
109+
$url = "$host:1234";
110+
$params = [
111+
'client' => [
112+
'verbose' => true
113+
]
114+
];
115+
$client = ClientBuilder::create()
116+
->setConnectionParams($params)
117+
->setHosts([$url])
118+
->build();
119+
120+
$this->assertInstanceOf(Client::class, $client);
121+
122+
try {
123+
$result = $client->info();
124+
} catch (ElasticsearchException $e) {
125+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
126+
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
127+
$this->assertEquals($host, $request['request']['headers']['Host'][0]);
128+
}
129+
}
130+
131+
/**
132+
* @see https://github.com/elastic/elasticsearch-php/issues/993
133+
*/
134+
public function testNotIncludePortInHostHeader()
135+
{
136+
$host = "localhost";
137+
$url = "$host:1234";
138+
$params = [
139+
'client' => [
140+
'verbose' => true
141+
]
142+
];
143+
$client = ClientBuilder::create()
144+
->setConnectionParams($params)
145+
->setHosts([$url])
146+
->includePortInHostHeader(false)
147+
->build();
148+
149+
$this->assertInstanceOf(Client::class, $client);
150+
151+
try {
152+
$result = $client->info();
153+
} catch (ElasticsearchException $e) {
154+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
155+
$this->assertTrue(isset($request['request']['headers']['Host'][0]));
156+
$this->assertEquals($host, $request['request']['headers']['Host'][0]);
157+
}
158+
}
73159
}

0 commit comments

Comments
 (0)