Skip to content

Commit

Permalink
Ensure server port numbers are numeric and ensure they are stored as …
Browse files Browse the repository at this point in the history
…integers
  • Loading branch information
wishdev authored and jeremyevans committed Jul 26, 2020
1 parent 1daacc1 commit 86ed621
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/webrick/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def initialize(config={}, default=Config::General)
@listeners = []
@shutdown_pipe = @config[:ShutdownPipe]
unless @config[:DoNotListen]
raise ArgumentError, "Port must an integer" unless @config[:Port].to_s == @config[:Port].to_i.to_s

@config[:Port] = @config[:Port].to_i
if @config[:Listen]
warn(":Listen option is deprecated; use GenericServer#listen", uplevel: 1)
end
Expand Down
28 changes: 28 additions & 0 deletions test/webrick/test_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,32 @@ def test_shutdown_pipe
pipe.last.puts('')
assert_join_threads([server_thread])
end

def test_port_numbers
config = {
:BindAddress => '0.0.0.0',
:Logger => WEBrick::Log.new([], WEBrick::BasicLog::WARN),
}

ports = [0, "0"]

ports.each do |port|
config[:Port]= port
server = WEBrick::GenericServer.new(config)
server_thread = Thread.start { server.start }
client_thread = Thread.start {
sleep 0.1 until server.status == :Running || !server_thread.status
server_port = server.listeners[0].addr[1]
server.stop
assert_equal server.config[:Port], server_port
sleep 0.1 until server.status == :Stop || !server_thread.status
}
assert_join_threads([client_thread, server_thread])
end

assert_raise(ArgumentError) do
config[:Port]= "FOO"
WEBrick::GenericServer.new(config)
end
end
end

0 comments on commit 86ed621

Please sign in to comment.