Class | Merb::Rack::Static |
In: |
merb-core/lib/merb-core/rack/middleware/static.rb
|
Parent: | Merb::Rack::Middleware |
:api: private
# File merb-core/lib/merb-core/rack/middleware/static.rb, line 6 6: def initialize(app,directory) 7: super(app) 8: @static_server = ::Rack::File.new(directory) 9: end
:api: plugin
# File merb-core/lib/merb-core/rack/middleware/static.rb, line 12 12: def call(env) 13: path = if env[Merb::Const::PATH_INFO] 14: env[Merb::Const::PATH_INFO].chomp(Merb::Const::SLASH) 15: else 16: Merb::Const::EMPTY_STRING 17: end 18: cached_path = (path.empty? ? 'index' : path) + '.html' 19: 20: if file_exist?(path) && env[Merb::Const::REQUEST_METHOD] =~ /GET|HEAD/ # Serve the file if it's there and the request method is GET or HEAD 21: serve_static(env) 22: elsif file_exist?(cached_path) && env[Merb::Const::REQUEST_METHOD] =~ /GET|HEAD/ # Serve the page cache if it's there and the request method is GET or HEAD 23: env[Merb::Const::PATH_INFO] = cached_path 24: serve_static(env) 25: elsif path =~ /favicon\.ico/ 26: return [404, { Merb::Const::CONTENT_TYPE => Merb::Const::TEXT_SLASH_HTML }, "404 Not Found."] 27: else 28: @app.call(env) 29: end 30: end
path<String>: | The path to the file relative to the server root. |
Boolean: | True if file exists under the server root and is readable. |
:api: private
# File merb-core/lib/merb-core/rack/middleware/static.rb, line 39 39: def file_exist?(path) 40: full_path = ::File.join(@static_server.root, ::Merb::Parse.unescape(path)) 41: ::File.file?(full_path) && ::File.readable?(full_path) 42: end
env<Hash>: | Environment variables to pass on to the server. |
:api: private
# File merb-core/lib/merb-core/rack/middleware/static.rb, line 48 48: def serve_static(env) 49: env[Merb::Const::PATH_INFO] = ::Merb::Parse.unescape(env[Merb::Const::PATH_INFO]) 50: @static_server.call(env) 51: end