diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-12-10 05:19:11 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2009-12-10 05:19:11 +0000 |
commit | 029179a9f343ff36fc1e37cb9bdfa5db0e9c2408 (patch) | |
tree | 90b712a43d706e0a73fa7bbe700162bde68f500b | |
parent | 6e62845f1a02e55589b204b8f571ed27beef9e17 (diff) | |
download | ruby-029179a9f343ff36fc1e37cb9bdfa5db0e9c2408.tar.gz ruby-029179a9f343ff36fc1e37cb9bdfa5db0e9c2408.tar.xz ruby-029179a9f343ff36fc1e37cb9bdfa5db0e9c2408.zip |
* 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
-rw-r--r-- | lib/un.rb | 39 |
1 files changed, 38 insertions, 1 deletions
@@ -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 @@ -283,6 +284,42 @@ def mkmf 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] |