1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
# Copyright (c) 2004, 2005, 2006 Tanaka Akira. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
# webapp/webrick-servlet.rb provides an adaptor for a webapp and WEBrick.
#
# webapp/webrick-servlet.rb registers a handler for .webrick extension
# using WEBrick::HTTPServlet::FileHandler.add_handler.
# So WEBrick based HTTP server which require webapp/webrick-servlet.rb
# can run a web application using webapp which filename has .webrick
# extension.
#
# Such HTTP server can be defined as follows.
#
# require 'webapp/webrick-servlet'
# httpd = WEBrick::HTTPServer.new(:Port => 10080, :DocumentRoot => Dir.getwd)
# trap(:INT){ httpd.shutdown }
# httpd.start
#
# Apart from that, webapp/webrick-servlet.rb provides
# WebApp::WEBrickServletHandler.load_servlet which load WEBrick servlet
# using webapp.
#
# WebApp::WEBrickServletHandler.load_servlet can be used for constructing
# a web site with a single web application as follows.
#
# require 'webapp/webrick-servlet'
# servlet = ARGV.shift
# httpd = WEBrick::HTTPServer.new(:Port => 10080)
# trap(:INT){ httpd.shutdown }
# httpd.mount("/", WebApp::WEBrickServletHandler.load_servlet(servlet))
# httpd.start
#
# When above server accept a request to http://host:10080/foo/bar,
# the servlet takes /foo/bar as a path_info.
#
# Note that the servlet loading mechanism may be used without webapp
# because the mechanism itself is not webapp dependent.
require 'webrick'
class WebApp
class WEBrickServletHandler
LoadedServlets = {}
def WEBrickServletHandler.get_instance(config, name)
unless LoadedServlets[name]
LoadedServlets[name] = load_servlet(name)
end
LoadedServlets[name]
end
# load a WEBrick servlet written using webapp.
# WEBrickServletHandler.load_servlet returns a WEBrick servlet
# generated by WEBrick::HTTPServlet::ProcHandler.
def WEBrickServletHandler.load_servlet(path)
begin
Thread.current[:webrick_load_servlet] = true
load path, true
unless Thread.current[:webrick_load_servlet].respond_to? :call
raise "WEBrick servlet is not registered: #{path}"
end
procedure = Thread.current[:webrick_load_servlet]
return WEBrick::HTTPServlet::ProcHandler.new(procedure)
ensure
Thread.current[:webrick_load_servlet] = nil
end
end
end
end
WEBrick::HTTPServlet::FileHandler.add_handler('webrick',
WebApp::WEBrickServletHandler)
if $0 == __FILE__
# usage: [-p port] [docroot|servlet]
require 'optparse'
port = 10080
ARGV.options {|q|
q.def_option('--help', 'show this message') {puts q; exit(0)}
q.def_option('--port=portnum', 'specify server port (default: 10080)') {|num| port = num.to_i }
q.parse!
}
docroot = ARGV.shift || Dir.getwd
httpd = WEBrick::HTTPServer.new(:Port => port)
trap(:INT){ httpd.shutdown }
if File.directory? docroot
httpd.mount("/", WEBrick::HTTPServlet::FileHandler, docroot, WEBrick::Config::HTTP[:DocumentRootOptions])
httpd.start
else
httpd.mount("/", WebApp::WEBrickServletHandler.load_servlet(docroot))
httpd.start
end
end
|