A
download HTTPServer.rb
Language: Ruby
LOC: 62
Project Info
IOWA
Server: RubyForge
Type: cvs
...Forge\i\iowa\iowa\iowa\src\
   Application.rb
   ApplicationStats.rb
   Association.rb
   BindingsParser.rb
   Client.rb
   Component.rb
   ComponentProxy.rb
   config.rb
   Context.rb
   DbPool.rb
   DynamicElements.rb
   Element.rb
   Form.rb
   HTTPServer.rb
   ISAAC.rb
   KeyValueCoding.rb
   LRUCache.rb
   PageStore.rb
   read_multipart.rb
   Request.rb
   SciTE.properties
   Session.rb
   SessionStats.rb
   SessionStore.rb
   Tag.rb
   TemplateParser.rb
   WEBrickServlet.rb

require 'iowa'
require 'webrick'
require 'iowa/WEBrickServlet'

module Iowa
	class HTTPServer < WEBrick::HTTPServer
	
	URIRegex = Regexp.new('(/*(?:[^/]*/*)*?)(\w+\-\w+\-\w+\.\w+\.[\w\.]+)*$')

		def initialize(options)
			@mapfile = options.has_key?(:MapFile) ? options[:MapFile] : nil
			@mapfile_mtime = 0
			@mappings = {}
			Iowa.app.initialLoad
			@iowa_servlet = Iowa::WEBrickServlet
			super
		end

		def load_map_file
			if File.stat(@mapfile).mtime != @mapfile_mtime
				@mapfile_mtime = File.stat(@mapfile).mtime
				@mappings = {}
				begin
					File.open(@mapfile,'r') do |fh|
						fh.each do |line|
							m = /^(.*?):/.match(line)
							@mappings[m[1]] = true unless line =~ /^\s*#/
						end
					end
				rescue Exception
					@mappings = {}
				end
			end
		end

		def in_mapfile?(path)
			load_map_file if @mapfile.to_s != ''
			m = URIRegex.match path
			urlRoot = m[1]
			urlRoot.chop! if m[2] unless urlRoot == '/'
			@mappings.has_key? urlRoot
		end

		def service(req,res)
			if req.unparsed_uri == "*"
				if req.request_method == "OPTIONS"
					do_OPTIONS(req, res)
					raise HTTPStatus::OK
				end
				raise HTTPStatus::NotFound, "`#{req.unparsed_uri}' not found."
			end

			servlet = options = script_name = path_info = nil
			if in_mapfile?(req.path)
				servlet = @iowa_servlet
				script_name = 'Iowa'
				path_info = req.path
			else
				servlet, options, script_name, path_info = search_servlet(req.path)
			end
			raise HTTPStatus::NotFound, "`#{req.path}' not found." unless servlet
			req.script_name = script_name
			req.path_info = path_info
			si = servlet.get_instance(self, *options)
			@logger.debug(format("%s is invoked.", si.class.name))
			si.service(req, res)
		end

	end
end

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us