Skip to content

Commit 2cd9d93

Browse files
committed
Fix for #993, added ClientBuilder::includePortInHostHeader()
1 parent 24534b5 commit 2cd9d93

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 19 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,18 @@ public function setSSLVerification($value = true): ClientBuilder
465470
return $this;
466471
}
467472

473+
/**
474+
* Include the port in Host header
475+
*
476+
* @see https://github.com/elastic/elasticsearch-php/issues/993
477+
*/
478+
public function includePortInHostHeader(bool $enable): ClientBuilder
479+
{
480+
$this->includePortInHostHeader = $enable;
481+
482+
return $this;
483+
}
484+
468485
public function build(): Client
469486
{
470487
$this->buildLoggers();
@@ -505,6 +522,8 @@ public function build(): Client
505522
$this->serializer = new $this->serializer;
506523
}
507524

525+
$this->connectionParams['client']['port_in_header'] = $this->includePortInHostHeader;
526+
508527
if (is_null($this->connectionFactory)) {
509528
if (is_null($this->connectionParams)) {
510529
$this->connectionParams = [];

src/Elasticsearch/Connections/Connection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public function __construct(
168168
}
169169
$port = $hostDetails['port'];
170170

171+
if (isset($connectionParams['client']['port_in_header']) && $connectionParams['client']['port_in_header']) {
172+
if (!in_array((int) $port, [80,443])) {
173+
$host .= ":$port";
174+
}
175+
}
171176
$this->host = $host;
172177
$this->path = $path;
173178
$this->port = $port;
@@ -198,6 +203,7 @@ public function performRequest(string $method, string $uri, ?array $params = [],
198203
$this->headers = array_merge($this->headers, $options['client']['headers']);
199204
}
200205

206+
201207
$request = [
202208
'http_method' => $method,
203209
'scheme' => $this->transportSchema,

tests/Elasticsearch/Tests/ClientBuilderTest.php

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

0 commit comments

Comments
 (0)