From 9212136e6a99eaafac0f52ff418d3a935469cd42 Mon Sep 17 00:00:00 2001 From: Ry Biesemeyer Date: Mon, 6 May 2019 21:06:46 +0000 Subject: [PATCH] use URI::Generic to avoid edge-cases with ports Resolves: https://github.com/elastic/elasticsearch-ruby/issues/625 --- lib/elasticsearch/transport/client.rb | 9 ++++----- spec/elasticsearch/transport/client_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/elasticsearch/transport/client.rb b/lib/elasticsearch/transport/client.rb index 30c7be7..4d84b38 100644 --- a/lib/elasticsearch/transport/client.rb +++ b/lib/elasticsearch/transport/client.rb @@ -194,17 +194,16 @@ def __parse_host(host) host_parts = case host when String if host =~ /^[a-z]+\:\/\// - uri = URI.parse(host) - - # Handle when port is not specified - port = uri.port unless uri.port == uri.default_port + # Construct a new `URI::Generic` directly from the array returned by URI::split. + # This avoids `URI::HTTP` and `URI::HTTPS`, which supply default ports. + uri = URI::Generic.new(*URI.split(host)) { :scheme => uri.scheme, :user => uri.user, :password => uri.password, :host => uri.host, :path => uri.path, - :port => port } + :port => uri.port } else host, port = host.split(':') { :host => host, diff --git a/spec/elasticsearch/transport/client_spec.rb b/spec/elasticsearch/transport/client_spec.rb index 160a822..0854fec 100644 --- a/spec/elasticsearch/transport/client_spec.rb +++ b/spec/elasticsearch/transport/client_spec.rb @@ -314,6 +314,26 @@ end end + context 'when there is one host with a protocol and the default http port explicitly provided' do + let(:host) do + ['http://myhost:80'] + end + + it 'respects the explicit port' do + expect(hosts[0][:port]).to be(80) + end + end + + context 'when there is one host with a protocol and the default https port explicitly provided' do + let(:host) do + ['https://myhost:443'] + end + + it 'respects the explicit port' do + expect(hosts[0][:port]).to be(443) + end + end + context 'when there is one host with a scheme, protocol and no port' do let(:host) do