Class: Nitro::WebrickAdapter

Inherits:
WEBrick::HTTPServlet::AbstractServlet
  • Object
show all
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

Constructor Details

#initialize(webrick, server) ⇒ WebrickAdapter

Returns a new instance of WebrickAdapter.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nitro/adapter/webrick.rb', line 105

def initialize(webrick, server)
  @server = server
  @server.options[:HandlerTable] = { 'xhtml' => XhtmlFileHandler }

  @handle_static_files = Server.handle_static_files

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

  @file_handler = WEBrick::HTTPServlet::FileHandler.new(
    webrick, 
    server.public_root, 
    server.options
  )
end

Instance Method Details

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

Handle the request.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/nitro/adapter/webrick.rb', line 150

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.meta_vars)

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

      Cgi.parse_params(context)
      Cgi.parse_cookies(context)

      context.render(path)
        
      res.status = context.status
      res.instance_variable_set(:@header, context.response_headers || {})
      res.instance_variable_set(:@cookies, context.response_cookies || {})
      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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/nitro/adapter/webrick.rb', line 123

def handle_file(req, res)
  rewrite(req)

  # gmosx, FIXME: this is a nasty hack that fixes a really
  # *nasty* caching bug. Find a better solution. When hitting
  # the backend server, if the index method takes parameters
  # the dispatcher considers all static files as parameters.
  # If you have output caching enabled for the index page, 
  # all your static files get corrupted.
  
  if (@handle_static_files == false) and (req.path_info =~ /\.html$/)
    return false
  end

  @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

#rewrite(req) ⇒ Object

Try to rewrite the path to a filename.



192
193
194
195
196
197
198
# File 'lib/nitro/adapter/webrick.rb', line 192

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}.html") 
  end
end

#unrewrite(req) ⇒ Object

Rewrite back to the original path.



202
203
204
205
206
207
208
# File 'lib/nitro/adapter/webrick.rb', line 202

def unrewrite(req)
  if req.path_info == '/index.html'
    req.instance_variable_set(:@path_info, '/') 
  elsif req.path_info =~ /^([^.]+)\.html$/    
    req.instance_variable_set(:@path_info, $1) 
  end
end