2009-09-26 12:37:42 -04:00
|
|
|
require 'rack/utils'
|
|
|
|
|
|
|
|
module ActionDispatch
|
2010-09-03 06:48:21 -04:00
|
|
|
class FileHandler
|
2011-05-03 06:32:14 -04:00
|
|
|
def initialize(root, cache_control)
|
2011-04-15 14:09:39 -04:00
|
|
|
@root = root.chomp('/')
|
2011-02-28 21:03:06 -05:00
|
|
|
@compiled_root = /^#{Regexp.escape(root)}/
|
2011-05-03 06:32:14 -04:00
|
|
|
@file_server = ::Rack::File.new(@root, cache_control)
|
2010-09-03 06:48:21 -04:00
|
|
|
end
|
2010-07-29 08:12:25 -04:00
|
|
|
|
2010-09-03 06:48:21 -04:00
|
|
|
def match?(path)
|
|
|
|
path = path.dup
|
|
|
|
|
2011-04-15 14:09:39 -04:00
|
|
|
full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
|
|
|
|
paths = "#{full_path}#{ext}"
|
|
|
|
|
|
|
|
matches = Dir[paths]
|
|
|
|
match = matches.detect { |m| File.file?(m) }
|
|
|
|
if match
|
|
|
|
match.sub!(@compiled_root, '')
|
|
|
|
match
|
2010-09-03 06:48:21 -04:00
|
|
|
end
|
|
|
|
end
|
2010-07-29 08:12:25 -04:00
|
|
|
|
2010-09-03 06:48:21 -04:00
|
|
|
def call(env)
|
|
|
|
@file_server.call(env)
|
2010-07-29 08:12:25 -04:00
|
|
|
end
|
2011-02-28 22:47:09 -05:00
|
|
|
|
|
|
|
def ext
|
|
|
|
@ext ||= begin
|
|
|
|
ext = ::ActionController::Base.page_cache_extension
|
|
|
|
"{,#{ext},/index#{ext}}"
|
|
|
|
end
|
|
|
|
end
|
2010-09-03 06:48:21 -04:00
|
|
|
end
|
2010-07-29 08:12:25 -04:00
|
|
|
|
2010-09-03 06:48:21 -04:00
|
|
|
class Static
|
2011-05-03 06:32:14 -04:00
|
|
|
def initialize(app, path, cache_control=nil)
|
2009-09-26 12:37:42 -04:00
|
|
|
@app = app
|
2011-05-03 06:32:14 -04:00
|
|
|
@file_handler = FileHandler.new(path, cache_control)
|
2009-09-26 12:37:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2011-05-02 20:05:20 -04:00
|
|
|
case env['REQUEST_METHOD']
|
|
|
|
when 'GET', 'HEAD'
|
|
|
|
path = env['PATH_INFO'].chomp('/')
|
2011-04-15 14:09:39 -04:00
|
|
|
if match = @file_handler.match?(path)
|
|
|
|
env["PATH_INFO"] = match
|
|
|
|
return @file_handler.call(env)
|
2009-09-26 12:37:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|