ruby-changes:14237
From: nobu <ko1@a...>
Date: Thu, 10 Dec 2009 14:19:21 +0900 (JST)
Subject: [ruby-changes:14237] Ruby:r26060 (trunk): * lib/un.rb (httpd): easy WEBrick HTTP server.
nobu 2009-12-10 14:19:11 +0900 (Thu, 10 Dec 2009) New Revision: 26060 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=26060 Log: * lib/un.rb (httpd): easy WEBrick HTTP server. Modified files: trunk/lib/un.rb Index: lib/un.rb =================================================================== --- lib/un.rb (revision 26059) +++ lib/un.rb (revision 26060) @@ -21,6 +21,7 @@ # ruby -run -e touch -- [OPTION] FILE # ruby -run -e wait_writable -- [OPTION] FILE # ruby -run -e mkmf -- [OPTION] EXTNAME [OPTION] +# ruby -run -e httpd -- [OPTION] DocumentRoot # ruby -run -e help [COMMAND] require "fileutils" @@ -43,7 +44,7 @@ end long_options.each do |s| opt_name = s[/\A(?:--)?([^\s=]+)/, 1].intern - o.on(s.sub(/\A(?!--)/, '--')) do |val| + o.on(s.gsub(/([a-z])([A-Z])/){$1+"-"+$2.downcase}.sub(/\A(?!--)/, '--')) do |val| opt_hash[opt_name] = val end end @@ -283,6 +284,42 @@ end ## +# Run WEBrick HTTP server. +# +# ruby -run -e httpd -- [OPTION] DocumentRoot +# +# --bind-address=ADDR address to bind +# --port=NUM listening port number +# --max-clients=MAX max number of simultaneous clients +# --temp-dir=DIR temporary directory +# --do-not-reverse-lookup disable reverse lookup +# --request-timeout=SECOND request timeout in seconds +# --http-version=VERSION HTTP version +# -v verbose +# + +def httpd + setup("", "BindAddress=ADDR", "Port=PORT", "MaxClients=NUM", "TempDir=DIR", + "DoNotReverseLookup", "RequestTimeout=SECOND", "HTTPVersion=VERSION") do + |argv, options| + require 'webrick' + opt = options[:RequestTimeout] and options[:RequestTimeout] = opt.to_i + unless argv.empty? + options[:DocumentRoot] = argv.shift + end + s = WEBrick::HTTPServer.new(options) + shut = proc {s.shutdown} + Signal.trap("TERM", shut) + Signal.trap("QUIT", shut) + if STDIN.tty? + Signal.trap("HUP", shut) + Signal.trap("INT", shut) + end + s.start + end +end + +## # Display help message. # # ruby -run -e help [COMMAND] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/