summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn W Higgins <wishdev@gmail.com>2020-07-26 07:25:37 (GMT)
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2020-09-24 12:31:55 (GMT)
commit4715a24dd277515077af441c1d31aeb2431917f5 (patch)
treea0f0311426a9ef96d59366a7fa18333ed81e3bda
parentc06eab13290757fc326bb2a6e3ac25cd53e00894 (diff)
[ruby/webrick] Ensure server port numbers are numeric and ensure they are stored as integers
https://github.com/ruby/webrick/commit/86ed621e11
-rw-r--r--lib/webrick/server.rb3
-rw-r--r--test/webrick/test_server.rb28
2 files changed, 31 insertions, 0 deletions
diff --git a/lib/webrick/server.rb b/lib/webrick/server.rb
index 4a6e74c..fd6b7a6 100644
--- a/lib/webrick/server.rb
+++ b/lib/webrick/server.rb
@@ -102,6 +102,9 @@ module WEBrick
@listeners = []
@shutdown_pipe = nil
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 @@ class TestWEBrickServer < Test::Unit::TestCase
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