Skip to content

Commit

Permalink
Report tcp 0 port properly. Fixes #1679
Browse files Browse the repository at this point in the history
Bonus feature: we now log all localhost addresses that we're bound to,
including ipv6 ones.
  • Loading branch information
evanphx authored and nateberkopec committed Sep 11, 2019
1 parent 9c9e902 commit ec44a81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/puma/binder.rb
Expand Up @@ -111,7 +111,16 @@ 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}"

@ios.each do |i|
addr = if i.local_address.ipv6?
"[#{i.local_address.ip_unpack[0]}]:#{i.local_address.ip_unpack[1]}"
else
i.local_address.ip_unpack.join(':')
end

logger.log "* Listening on tcp://#{addr}"
end
end

@listeners << [str, io] if io
Expand Down
20 changes: 20 additions & 0 deletions test/test_binder.rb
Expand Up @@ -95,4 +95,24 @@ 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://127.0.0.1:(\d+)!.match(@events.stdout.string)
port = m[1].to_i

refute_equal 0, port
end

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

assert_match %r!tcp://127.0.0.1:(\d+)!, @events.stdout.string
assert_match %r!tcp://\[::1\]:(\d+)!, @events.stdout.string
end
end

0 comments on commit ec44a81

Please sign in to comment.