From: GOTOU Yuuzou Date: 2005-03-22T18:45:13+09:00 Subject: Re: can WEBrick bind to port 0, and then tell me what port was allocated? In message <20050321143558.GA569@ensemble.local>, `Sam Roberts ' wrote: > It is very sensitive to error, it is very common to pass nil when you > failed to initialize something correctly, and now it will silently do > something unexpected, and the caller will waste time trying to figure > out why every port is different, and having to ask on ruby-talk, where > you will tell him he probably passed nil... and everybody will waste > time. Hmm, ok. I understand what you mean. I rewrote a patch again using port 0. (please forget my last post;-) > Personally, I would prefer :auto. You don't accidently pass a symbol in > as a port number, doing so is a pretty clear indication to WEBrick, and > to readers of the code that the port is special. :auto seems that it could be used for other parameters. I hesitate to introduce if it isn't useful for elsewhere. How do you think? -- gotoyuzo --- lib/webrick/server.rb 7 Mar 2005 12:32:07 -0000 1.9 +++ lib/webrick/server.rb 22 Mar 2005 09:34:10 -0000 @@ -61,6 +61,9 @@ def initialize(config={}, default=Co warn(":Listen option is deprecated; use GenericServer#listen") end listen(@config[:BindAddress], @config[:Port]) + if @config[:Port] == 0 + @config[:Port] = @listeners[0].addr[1] + end end end --- lib/webrick/utils.rb 28 Sep 2003 17:50:52 -0000 1.3 +++ lib/webrick/utils.rb 22 Mar 2005 09:34:10 -0000 @@ -58,8 +58,9 @@ def create_listeners(address, port, sockets = [] res.each{|ai| begin - logger.debug("TCPServer.new(#{ai[3]}, #{ai[1]})") if logger - sock = TCPServer.new(ai[3], ai[1]) + logger.debug("TCPServer.new(#{ai[3]}, #{port})") if logger + sock = TCPServer.new(ai[3], port) + port = sock.addr[1] if port == 0 Utils::set_close_on_exec(sock) sockets << sock rescue => ex