Skip to content

Commit

Permalink
Report tcp 0 port properly. Fixes #1679
Browse files Browse the repository at this point in the history
Prefer ip_port

Fix everything
  • Loading branch information
evanphx authored and nateberkopec committed Sep 11, 2019
1 parent 8a58dd3 commit 3827f00
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/puma/binder.rb
Expand Up @@ -111,10 +111,11 @@ def parse(binds, logger)
bak = params.fetch('backlog', 1024).to_i

io = add_tcp_listener uri.host, uri.port, opt, bak
logger.log "* Listening on #{str}"

logger.log "* Listening on tcp://#{uri.host}:#{io.local_address.ip_port}"
end

@listeners << [str, io] if io
add_to_listeners(str, io)
when "unix"
path = "#{uri.host}#{uri.path}".gsub("%20", " ")

Expand Down Expand Up @@ -149,7 +150,7 @@ def parse(binds, logger)
logger.log "* Listening on #{str}"
end

@listeners << [str, io]
add_to_listeners(str, io)
when "ssl"
params = Util.parse_query uri.query
require 'puma/minissl'
Expand Down Expand Up @@ -222,7 +223,7 @@ def parse(binds, logger)
logger.log "* Listening on #{str}"
end

@listeners << [str, io] if io
add_to_listeners(str,io)
else
logger.error "Invalid URI: #{str}"
end
Expand Down Expand Up @@ -273,10 +274,9 @@ def loopback_addresses
#
def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024)
if host == "localhost"
loopback_addresses.each do |addr|
return loopback_addresses.map do |addr|
add_tcp_listener addr, port, optimize_for_latency, backlog
end
return
end.last
end

host = host[1..-2] if host and host[0..0] == '['
Expand Down Expand Up @@ -312,10 +312,9 @@ def add_ssl_listener(host, port, ctx,
MiniSSL.check

if host == "localhost"
loopback_addresses.each do |addr|
return loopback_addresses.map do |addr|
add_ssl_listener addr, port, ctx, optimize_for_latency, backlog
end
return
end.last
end

host = host[1..-2] if host[0..0] == '['
Expand Down Expand Up @@ -413,5 +412,10 @@ def inherit_unix_listener(path, fd)
s
end

def add_to_listeners(str, io)
# TODO: It's possible that it's "OK" to add loopback addresses to the listeners
# array, but we're not sure. See https://github.com/puma/puma/pull/1786
@listeners << [str, io] if io && !io.local_address.ipv4_loopback? && !io.local_address.ipv6_loopback?
end
end
end
12 changes: 12 additions & 0 deletions test/test_binder.rb
Expand Up @@ -95,4 +95,16 @@ def test_binder_parses_tlsv1_1_enabled

refute ssl_context_for_binder(@binder).no_tlsv1_1
end

def test_correct_zero_port
@events = Puma::Events.strings
@binder = Puma::Binder.new(@events)
@binder.parse(["tcp://localhost:0"], @events)

m = %r!tcp://localhost:(\d+)!.match(@events.stdout.string)

port = m[1].to_i

refute_equal 0, port
end
end

0 comments on commit 3827f00

Please sign in to comment.