diff --git a/src/Elasticsearch/ClientBuilder.php b/src/Elasticsearch/ClientBuilder.php index 5a161b7f9..1cf1d80ff 100644 --- a/src/Elasticsearch/ClientBuilder.php +++ b/src/Elasticsearch/ClientBuilder.php @@ -638,7 +638,7 @@ private function normalizeExtendedHost(array $host) $host['scheme'] = 'http'; } if (isset($host['port']) === false) { - $host['port'] = '9200'; + $host['port'] = 9200; } return $host; } diff --git a/src/Elasticsearch/Connections/Connection.php b/src/Elasticsearch/Connections/Connection.php index 63a2cfe84..c5cd2903f 100644 --- a/src/Elasticsearch/Connections/Connection.php +++ b/src/Elasticsearch/Connections/Connection.php @@ -58,6 +58,11 @@ class Connection implements ConnectionInterface * @var string|null */ protected $path; + + /** + * @var int + */ + protected $port; /** * @var LoggerInterface @@ -121,18 +126,23 @@ public function __construct( $connectionParams['client']['curl'][CURLOPT_USERPWD] = $hostDetails['user'].':'.$hostDetails['pass']; } + $connectionParams['client']['curl'][CURLOPT_PORT] = $hostDetails['port']; + if (isset($connectionParams['client']['headers']) === true) { $this->headers = $connectionParams['client']['headers']; unset($connectionParams['client']['headers']); } - $host = $hostDetails['host'].':'.$hostDetails['port']; + $host = $hostDetails['host']; $path = null; if (isset($hostDetails['path']) === true) { $path = $hostDetails['path']; } + $port = $hostDetails['port']; + $this->host = $host; $this->path = $path; + $this->port = $port; $this->log = $log; $this->trace = $trace; $this->connectionParams = $connectionParams; @@ -528,6 +538,14 @@ public function getPath() { return $this->path; } + + /** + * @return int + */ + public function getPort() + { + return $this->port; + } /** * @param array $request diff --git a/src/Elasticsearch/Connections/ConnectionInterface.php b/src/Elasticsearch/Connections/ConnectionInterface.php index de819a5f0..a5082cdca 100644 --- a/src/Elasticsearch/Connections/ConnectionInterface.php +++ b/src/Elasticsearch/Connections/ConnectionInterface.php @@ -50,6 +50,13 @@ public function getTransportSchema(); */ public function getHost(); + /** + * Get the port for this connection + * + * @return int + */ + public function getPort(); + /** * Get the username:password string for this connection, null if not set * diff --git a/tests/Elasticsearch/Tests/ClientTest.php b/tests/Elasticsearch/Tests/ClientTest.php index 245e2608d..4260b62fe 100644 --- a/tests/Elasticsearch/Tests/ClientTest.php +++ b/tests/Elasticsearch/Tests/ClientTest.php @@ -276,7 +276,8 @@ public function testInlineHosts() 'localhost:9200' ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("localhost:9200", $host->getHost()); + $this->assertSame("localhost", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -284,21 +285,24 @@ public function testInlineHosts() 'http://localhost:9200' ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("localhost:9200", $host->getHost()); + $this->assertSame("localhost", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); $client = Elasticsearch\ClientBuilder::create()->setHosts([ 'http://foo.com:9200' ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); $client = Elasticsearch\ClientBuilder::create()->setHosts([ 'https://foo.com:9200' ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("https", $host->getTransportSchema()); @@ -306,7 +310,8 @@ public function testInlineHosts() 'https://user:pass@foo.com:9200' ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("https", $host->getTransportSchema()); $this->assertSame("user:pass", $host->getUserPass()); } @@ -321,7 +326,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("localhost:9200", $host->getHost()); + $this->assertSame("localhost", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -333,7 +339,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -345,7 +352,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("https", $host->getTransportSchema()); @@ -356,7 +364,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -366,7 +375,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -378,7 +388,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9500", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9500, $host->getPort()); $this->assertSame("https", $host->getTransportSchema()); @@ -401,7 +412,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("the_foo.com:9200", $host->getHost()); + $this->assertSame("the_foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); @@ -414,7 +426,8 @@ public function testExtendedHosts() ] ])->build(); $host = $client->transport->getConnection(); - $this->assertSame("foo.com:9200", $host->getHost()); + $this->assertSame("foo.com", $host->getHost()); + $this->assertSame(9200, $host->getPort()); $this->assertSame("http", $host->getTransportSchema()); $this->assertSame("user:abc#$@?%!abc", $host->getUserPass()); }