def run_cli_server
require 'webrick'
config = {}
case ARGV[0]
when nil
config = {
:Port => TCPServer.open(0) {|s| s.addr[1] }
}
when /:(\d+)\z/
config = {
:ServerName => $`,
:BindAddress => $`,
:Port => $1.to_i
}
when /\A\d+\z/
config = {
:Port => ARGV[0].to_i
}
else
raise ArgumentError, "unexpected port: #{ARGV[0].inspect}"
end
servlet = WEBrick::HTTPServlet::ProcHandler.new(run_webrick)
Thread.current[:webrick_load_servlet] = nil
top_uri = "http://"
top_uri << (config[:BindAddress] || config[:ServerName] || WEBrick::Utils.getservername)
top_uri << ":#{config[:Port]}" if config[:Port] != 80
top_uri << '/'
puts top_uri
httpd = WEBrick::HTTPServer.new(config)
trap(:INT){ httpd.shutdown }
httpd.mount("/", servlet)
httpd.start
exit 0
end