Class: N::WebrickAdapter

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Defined in:
lib/nitro/adapters/webrick.rb

Overview

A Webrick Adapter for Nitro.

Instance Method Summary collapse

Constructor Details

#initialize(server, conf) ⇒ WebrickAdapter

REQUEST_MUTEX = Mutex.new



78
79
80
81
82
83
84
85
86
87
# File 'lib/nitro/adapters/webrick.rb', line 78

def initialize(server, conf)
@conf = conf

# Handles static resources. Useful when running 
# a stand-alone webrick server.

  @file_handler = WEBrick::HTTPServlet::FileHandler.new(
		server, 
		conf.dispatcher.root, conf.webrick_options || {})
end

Instance Method Details

#handle(req, res) ⇒ Object Also known as: do_GET, do_POST



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nitro/adapters/webrick.rb', line 89

def handle(req, res)
	path = req.request_uri.path

   if path =~ /\./
		@file_handler.do_GET(req, res)
	else
		# gmosx, TODO: move this into a filter.
		Og.db.get_connection if defined?(Og)

		# REQUEST_MUTEX.lock

		context = Context.new(@conf)
		
		context.params = req.query 
		context.headers = {}
		req.header.each { |h, v| context.headers[h.upcase] = v.first }
		context.headers.update(req.meta_vars)
		context.cookies = {}
		req.cookies.each { |c| context.cookies[c.name] = c.value }
	
		context.render(path)

		res.status = context.status
		res.header = context.response_headers || {}
		res.cookies = context.response_cookies || {}
		res.body = context.out
	end
ensure
	# REQUEST_MUTEX.unlock if REQUEST_MUTEX.locked?
	Og.db.put_connection if defined?(Og)
end