From 86ed621e1137c6f986447aad02cf4053b2708e23 Mon Sep 17 00:00:00 2001 From: John W Higgins Date: Sun, 26 Jul 2020 00:25:37 -0700 Subject: [PATCH] Ensure server port numbers are numeric and ensure they are stored as integers --- lib/webrick/server.rb | 3 +++ test/webrick/test_server.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb index 23cde61..01976fe 100644 --- a/lib/webrick/server.rb +++ b/lib/webrick/server.rb @@ -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 diff --git a/test/webrick/test_server.rb b/test/webrick/test_server.rb index 9a3a8f1..aa40a72 100644 --- a/test/webrick/test_server.rb +++ b/test/webrick/test_server.rb @@ -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