Class: Nitro::WebrickAdapter
- Inherits:
-
WEBrick::HTTPServlet::AbstractServlet
- Object
- WEBrick::HTTPServlet::AbstractServlet
- Nitro::WebrickAdapter
- Includes:
- WEBrick
- Defined in:
- lib/nitro/adapter/webrick.rb
Overview
A Webrick Adapter for Nitro. Webrick is a pure Ruby web server included in the default Ruby distribution. The Webrick Adapter is the prefered adapter in development/debug environments. It is also extremely easy to setup.
However, for live/production environments, you should prefer a more performant adapter like FCGI or SCGI.
Instance Method Summary collapse
-
#handle(req, res) ⇒ Object
(also: #do_GET, #do_POST)
Handle the request.
-
#handle_file(req, res) ⇒ Object
Handle a static file.
-
#initialize(webrick, server) ⇒ WebrickAdapter
constructor
A new instance of WebrickAdapter.
-
#rewrite(req) ⇒ Object
Try to rewrite the path to a filename.
-
#unrewrite(req) ⇒ Object
Rewrite back to the original path.
Constructor Details
#initialize(webrick, server) ⇒ WebrickAdapter
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/nitro/adapter/webrick.rb', line 167 def initialize(webrick, server) @server = server @server.[:HandlerTable] = { 'xhtml' => XhtmlFileHandler } # Handles static resources. Useful when running # a stand-alone webrick server. @file_handler = WEBrick::HTTPServlet::FileHandler.new( webrick, server.public_root, server. ) end |
Instance Method Details
#handle(req, res) ⇒ Object Also known as: do_GET, do_POST
Handle the request.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/nitro/adapter/webrick.rb', line 200 def handle(req, res) unless handle_file(req, res) begin path = req.request_uri.path context = Context.new(@server) context.in = StringIO.new(req.body || "") context.headers = {} req.header.each { |h, v| context.headers[h.upcase] = v.first } context.headers.update(req.) # gmosx: make compatible with fastcgi. context.headers['REQUEST_URI'].slice!(/http:\/\/(.*?)\//) context.headers['REQUEST_URI'] = '/' + context.headers['REQUEST_URI'] Cgi.parse_params(context) Cgi.(context) context.render(path) res.status = context.status res.instance_variable_set(:@header, context.response_headers || {}) res.instance_variable_set(:@cookies, context. || {}) res.body = context.out res.chunked = true if context.out.is_a?(IO) and context["SERVER_PROTOCOL"] == "HTTP/1.1" context.close ensure $autoreload_dirty = false Og.manager.put_store if defined?(Og) and Og.respond_to?(:manager) and Og.manager end end end |
#handle_file(req, res) ⇒ Object
Handle a static file. Also handles cached pages.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/nitro/adapter/webrick.rb', line 183 def handle_file(req, res) begin rewrite(req) @file_handler.do_GET(req, res) return true rescue WEBrick::HTTPStatus::PartialContent, WEBrick::HTTPStatus::NotModified => err res.set_error(err) return true rescue Object => ex return false ensure unrewrite(req) end end |
#rewrite(req) ⇒ Object
Try to rewrite the path to a filename.
242 243 244 245 246 247 248 |
# File 'lib/nitro/adapter/webrick.rb', line 242 def rewrite(req) if req.path_info == '/' req.instance_variable_set(:@path_info, '/index.html') elsif req.path_info =~ /^([^.]+)$/ req.instance_variable_set(:@path_info, "#{$1}/index.html") end end |
#unrewrite(req) ⇒ Object
Rewrite back to the original path.
252 253 254 255 256 257 258 |
# File 'lib/nitro/adapter/webrick.rb', line 252 def unrewrite(req) if req.path_info == '/index.html' req.instance_variable_set(:@path_info, '/') elsif req.path_info =~ /^([^.]+)\/index.html$/ req.instance_variable_set(:@path_info, $1) end end |