ruby-changes:63065
From: John <ko1@a...>
Date: Thu, 24 Sep 2020 21:42:22 +0900 (JST)
Subject: [ruby-changes:63065] 4715a24dd2 (master): [ruby/webrick] Ensure server port numbers are numeric and ensure they are stored as integers
https://git.ruby-lang.org/ruby.git/commit/?id=4715a24dd2 From 4715a24dd277515077af441c1d31aeb2431917f5 Mon Sep 17 00:00:00 2001 From: John W Higgins <wishdev@g...> Date: Sun, 26 Jul 2020 00:25:37 -0700 Subject: [ruby/webrick] Ensure server port numbers are numeric and ensure they are stored as integers https://github.com/ruby/webrick/commit/86ed621e11 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 https://github.com/ruby/ruby/blob/trunk/lib/webrick/server.rb#L102 @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 https://github.com/ruby/ruby/blob/trunk/test/webrick/test_server.rb#L172 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 -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/