Skip to content

Commit 1fdcb66

Browse files
yaauieestolfo
authored andcommitted
use URI::Generic to avoid edge-cases with ports
Resolves: #625
1 parent 0ac2e07 commit 1fdcb66

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

elasticsearch-transport/lib/elasticsearch/transport/client.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,16 @@ def __parse_host(host)
194194
host_parts = case host
195195
when String
196196
if host =~ /^[a-z]+\:\/\//
197-
uri = URI.parse(host)
198-
199-
# Handle when port is not specified
200-
port = uri.port unless uri.port == uri.default_port
197+
# Construct a new `URI::Generic` directly from the array returned by URI::split.
198+
# This avoids `URI::HTTP` and `URI::HTTPS`, which supply default ports.
199+
uri = URI::Generic.new(*URI.split(host))
201200

202201
{ :scheme => uri.scheme,
203202
:user => uri.user,
204203
:password => uri.password,
205204
:host => uri.host,
206205
:path => uri.path,
207-
:port => port }
206+
:port => uri.port }
208207
else
209208
host, port = host.split(':')
210209
{ :host => host,

elasticsearch-transport/spec/elasticsearch/transport/client_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@
314314
end
315315
end
316316

317+
context 'when there is one host with a protocol and the default http port explicitly provided' do
318+
let(:host) do
319+
['http://myhost:80']
320+
end
321+
322+
it 'respects the explicit port' do
323+
expect(hosts[0][:port]).to be(80)
324+
end
325+
end
326+
327+
context 'when there is one host with a protocol and the default https port explicitly provided' do
328+
let(:host) do
329+
['https://myhost:443']
330+
end
331+
332+
it 'respects the explicit port' do
333+
expect(hosts[0][:port]).to be(443)
334+
end
335+
end
336+
317337
context 'when there is one host with a scheme, protocol and no port' do
318338

319339
let(:host) do

0 commit comments

Comments
 (0)