Class: Merb::Rack::Application
- Defined in:
- lib/merb-core/rack/application.rb
Instance Method Summary collapse
-
#call(env) ⇒ Object
Parameters env<Hash>:: Environment variables to pass on to the application.
-
#file_exist?(path) ⇒ Boolean
Parameters path<String>:: The path to the file relative to the server root.
-
#initialize(options = {}) ⇒ Application
constructor
Parameters options<Hash>:: Options for creating a new application.
-
#serve_static(env) ⇒ Object
Parameters env<Hash>:: Environment variables to pass on to the server.
-
#strip_path_prefix(env) ⇒ Object
Parameters env<Hash>:: Environment variables to pass on to the server.
Constructor Details
#initialize(options = {}) ⇒ Application
Parameters
- options<Hash>
-
Options for creating a new application. Currently ignored.
9 10 11 12 13 14 |
# File 'lib/merb-core/rack/application.rb', line 9 def initialize(={}) @static_server = ::Rack::File.new Merb.dir_for(:public) if prefix = ::Merb::Config[:path_prefix] @path_prefix = /^#{Regexp.escape(prefix)}/ end end |
Instance Method Details
#call(env) ⇒ Object
Parameters
- env<Hash>
-
Environment variables to pass on to the application.
Returns
- Array
-
A 3 element tuple consisting of response status, headers and body.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/merb-core/rack/application.rb', line 22 def call(env) strip_path_prefix(env) if @path_prefix # Strip out the path_prefix if one was set path = env['PATH_INFO'] ? env['PATH_INFO'].chomp('/') : "" cached_path = (path.empty? ? 'index' : path) + '.html' Merb.logger.info "Request: #{path}" if file_exist?(path) && env['REQUEST_METHOD'] =~ /GET|HEAD/ # Serve the file if it's there and the request method is GET or HEAD serve_static(env) elsif file_exist?(cached_path) && env['REQUEST_METHOD'] =~ /GET|HEAD/ # Serve the page cache if it's there and the request method is GET or HEAD env['PATH_INFO'] = cached_path serve_static(env) else # No static file, let Merb handle it if path =~ /favicon\.ico/ return [404, {"Content-Type"=>"text/html"}, "404 Not Found."] end begin controller = ::Merb::Dispatcher.handle(env) rescue Object => e return [500, {"Content-Type"=>"text/html"}, e. + "<br/>" + e.backtrace.join("<br/>")] end Merb.logger.info "\n\n" Merb.logger.flush [controller.status, controller.headers, controller.body] end end |
#file_exist?(path) ⇒ Boolean
Parameters
- path<String>
-
The path to the file relative to the server root.
Returns
- Boolean
-
True if file exists under the server root and is readable.
52 53 54 55 |
# File 'lib/merb-core/rack/application.rb', line 52 def file_exist?(path) full_path = ::File.join(@static_server.root, ::Merb::Request.unescape(path)) ::File.file?(full_path) && ::File.readable?(full_path) end |
#serve_static(env) ⇒ Object
Parameters
- env<Hash>
-
Environment variables to pass on to the server.
59 60 61 62 |
# File 'lib/merb-core/rack/application.rb', line 59 def serve_static(env) env["PATH_INFO"] = ::Merb::Request.unescape(env["PATH_INFO"]) @static_server.call(env) end |
#strip_path_prefix(env) ⇒ Object
Parameters
- env<Hash>
-
Environment variables to pass on to the server.
66 67 68 69 70 71 72 73 |
# File 'lib/merb-core/rack/application.rb', line 66 def strip_path_prefix(env) ['PATH_INFO', 'REQUEST_URI'].each do |path_key| if env[path_key] =~ @path_prefix env[path_key].sub!(@path_prefix, '') env[path_key] = '/' if env[path_key].empty? end end end |