Class: Raw::WebrickHandler

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
Includes:
AdapterHandlerMixin, WEBrick
Defined in:
lib/raw/adapter/webrick.rb

Overview

The Webrick Handler, handles an HTTP request. – TODO: add some way to prevent the display of template files if the public dir is used as the template dir. ++

Instance Method Summary collapse

Methods included from AdapterHandlerMixin

#handle_context, #rewrite, #unrewrite

Constructor Details

#initialize(webrick, app) ⇒ WebrickHandler

Returns a new instance of WebrickHandler.



79
80
81
82
83
84
85
86
87
88
# File 'lib/raw/adapter/webrick.rb', line 79

def initialize(webrick, app)
  @application = app

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

  @file_handler = WEBrick::HTTPServlet::FileHandler.new(
    webrick, app.public_dir, app.options
  )
end

Instance Method Details

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

Handle the request.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/raw/adapter/webrick.rb', line 106

def handle(req, res)
  # Perform default rewriting rules.
  
  rewrite(req)

  # First, try to serve a static file from disk.

  return if handle_file(req, res)

  # No static file found, attempt to dynamically generate 
  # a response.
  
  context = Context.new(@application)

  context.in = StringIO.new(req.body || "")

  context.headers = {}
  req.header.each { |h, v| context.headers[h.upcase] = v.first }
  context.headers.update(req.meta_vars)

  # gmosx: make compatible with fastcgi.
    
  context.headers["REQUEST_URI"].slice!(/http:\/\/(.*?)\//)
  context.headers["REQUEST_URI"] = '/' + context.headers['REQUEST_URI']

  res.body = handle_context(context)
  
  res.status = context.status
  res.instance_variable_set(:@header, context.response_headers || {})
  res.instance_variable_set(:@cookies, context.response_cookies || {})
    
  context.close
end

#handle_file(req, res) ⇒ Object

Handle a static file. Also handles cached pages. Typically not used in production applications.



93
94
95
96
97
98
99
100
101
102
# File 'lib/raw/adapter/webrick.rb', line 93

def handle_file(req, res)
  return false unless @application.handle_static_files    
  @file_handler.do_GET(req, res)
  return true
rescue WEBrick::HTTPStatus::PartialContent, WEBrick::HTTPStatus::NotModified => err
  res.set_error(err)
  return true
rescue WEBrick::HTTPStatus::NotFound => ex
  return false
end