Skip to content
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

Drop standard port (443 for HTTPS, 80 for HTTP) from host header #309

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion src/Elasticsearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public function __construct($handler, $hostDetails, $connectionParams,
$connectionParams['client']['curl'][CURLOPT_USERPWD] = $hostDetails['user'].':'.$hostDetails['pass'];
}

$host = $hostDetails['host'].':'.$hostDetails['port'];
$host = $hostDetails['host'];
if (!$this->isStandardPort($hostDetails['scheme'], $hostDetails['port'])) {
$host .= ':' . $hostDetails['port'];
}
if (isset($hostDetails['path']) === true) {
$host .= $hostDetails['path'];
}
Expand Down Expand Up @@ -468,6 +471,12 @@ protected function throwCurlException($request, $response)
throw $exception;
}

private function isStandardPort($scheme, $port)
{
return ('https' === strtolower($scheme) && 443 === $port)
|| ('http' === strtolower($scheme) && 80 === $port);
}

/**
* Construct a string cURL command
*
Expand Down
93 changes: 93 additions & 0 deletions tests/Elasticsearch/Tests/Connections/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
namespace Elasticsearch\Tests\Connections;


use Elasticsearch\Connections\Connection;
use Elasticsearch\Serializers\SerializerInterface;
use GuzzleHttp\Ring\Future\CompletedFutureArray;
use Psr\Log\LoggerInterface;

class ConnectionTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider hostPartProvider
*
* @param array $hostParts
*/
public function testStandardPortsAreNotSpecifiedOnHosts(array $hostParts, $isStandard)
{
$connection = new Connection(
function (array $request) use ($isStandard, $hostParts) {
$expectedHost = $hostParts['host'];
if (!$isStandard) {
$expectedHost .= ":{$hostParts['port']}";
}

$this->assertSame($expectedHost, $request['headers']['host'][0]);

return new CompletedFutureArray([]);
},
$hostParts,
[],
$this->getMock(SerializerInterface::class),
$this->getMock(LoggerInterface::class),
$this->getMock(LoggerInterface::class)
);

$connection->performRequest('GET', '/');
}

public function hostPartProvider()
{
return [
[
[
'scheme' => 'http',
'host' => 'example.com',
'port' => 80,
],
true,
],
[
[
'scheme' => 'https',
'host' => 'example.com',
'port' => 443,
],
true,
],
[
[
'scheme' => 'http',
'host' => 'example.com',
'port' => 443,
],
false,
],
[
[
'scheme' => 'https',
'host' => 'example.com',
'port' => 80,
],
false,
],
[
[
'scheme' => 'http',
'host' => 'example.com',
'port' => 9200,
],
false,
],
[
[
'scheme' => 'https',
'host' => 'example.com',
'port' => 9200,
],
false,
],
];
}
}