# code: # * George Moschovitis # # (c) 2004 Navel, all rights reserved. # $Id: webrick.rb 200 2004-12-27 11:24:41Z gmosx $ require "webrick" require "nitro/server/appserver" require "nitro/server/dispatcher" require "nitro/server/render" # Override WEBrick to suit our needs. # Did I mention that WEBrick SUCKS ? # module WEBrick class HTTPRequest # Allow external setting of cookies. # def cookies=(val) @cookies = val end # Allow external setting of query. # def set_query(query_string) @query_string = query_string @query = WEBrick::HTTPUtils::parse_query(query_string) end # Return the referer to this resource. For the initial page in the # clickstream there is no referer, set "/" by default. # def referer return @header["referer"][0] || "/" end alias_method :referrer, :referer # Populate an object from request parameters. # This is a truly dangerous method. # EXCLUDED_PARAMETERS = %w{ oid name } def fill(obj, name = nil) # if an object is passed create an instance. obj = obj.new if obj.is_a?(Class) @query.each do |param, val| begin # gmosx: DO NOT escape by default !!! if not EXCLUDED_PARAMETERS.include?(param) obj.send("__force_#{param}", val) end rescue NameError next end end return obj end alias_method :populate, :fill end end # module module N # = WebrickServer # # The Application Server. Handles dynamic requests in a web application. # Dont keepalive (or use a VERY SMALL keepalive). Typically this server # is used along with a standard http server that handles static # resources like images, css. # # Used temporarily unitl we build our own app server. # class WebrickServer < N::AppServer include WEBrick # the integrated webrick server. attr_accessor :webrick def initialize(name = "WebrickServer") super end # Start the application server. # def start Socket.do_not_reverse_lookup = true # patch for OS X =begin accesslog = WEBrick::BasicLog::new('/dev/null') referer = WEBrick::BasicLog::new('/dev/null') =end accesslog = WEBrick::BasicLog::new('log/access.log') referer = WEBrick::BasicLog::new('log/referer.log') @webrick = WEBrick::HTTPServer.new( :BindAddress => $appsrv_address, :Port => $appsrv_port, :DocumentRoot => $root_dir, :AccessLog => [ [accesslog, AccessLog::COMMON_LOG_FORMAT], [referer, AccessLog::REFERER_LOG_FORMAT] ] ) @webrick.mount('/', N::WebrickDispatcher) trap("INT") { @webrick.shutdown } @webrick.start end # Stop the application server. # def stop @webrick.stop end end # = WebrickDispatcher # class WebrickDispatcher < WEBrick::HTTPServlet::AbstractServlet include N::Dispatcher def initialize(server) # Handles static resources. Useful when running a standalone # webrick server. @file_handler = WEBrick::HTTPServlet::FileHandler.new(server, $root_dir, {:FancyIndexing => true }) end def do_GET(request, response) path = request.request_uri.path # rationalize cookies, WEBrick just SUCKS!! tmp = {} request.cookies.each { |c| tmp[c.name] = c } request.cookies = tmp if path =~ /\./ @file_handler.do_GET(request, response) else process(request, response) end end alias :do_POST :do_GET # Create session # def create_session(request, response) session = nil if cookie = request.cookies[N::Session::COOKIE_NAME] session = $sessions[cookie.value] end unless session $log.debug 'Creating new session.' if $DBG # no session cookie or stale session (garbage collected). session_id = N::Session.calculate_id() cookie = WEBrick::Cookie.new(N::Session::COOKIE_NAME, session_id) response.cookies << cookie session = $sessions[session_id] = N::Session.new(session_id) end return session end end end # module