From 029179a9f343ff36fc1e37cb9bdfa5db0e9c2408 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 10 Dec 2009 05:19:11 +0000 Subject: * lib/un.rb (httpd): easy WEBrick HTTP server. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@26060 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/un.rb | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/un.rb b/lib/un.rb index 0ce88a5c3..cf817cafc 100644 --- a/lib/un.rb +++ b/lib/un.rb @@ -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 @@ def setup(options = "", *long_options) 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 @@ -282,6 +283,42 @@ def mkmf end 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. # -- cgit