Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails / rack / static.rb
1 module Rails
2 module Rack
3 class Static
4 FILE_METHODS = %w(GET HEAD).freeze
5
6 def initialize(app)
7 @app = app
8 @file_server = ::Rack::File.new(File.join(RAILS_ROOT, "public"))
9 end
10
11 def call(env)
12 path = env['PATH_INFO'].chomp('/')
13 method = env['REQUEST_METHOD']
14 cached_path = (path.empty? ? 'index' : path) + ::ActionController::Base.page_cache_extension
15
16 if FILE_METHODS.include?(method)
17 if file_exist?(path)
18 return @file_server.call(env)
19 elsif file_exist?(cached_path)
20 env['PATH_INFO'] = cached_path
21 return @file_server.call(env)
22 end
23 end
24
25 @app.call(env)
26 end
27
28 private
29 def file_exist?(path)
30 full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
31 File.file?(full_path) && File.readable?(full_path)
32 end
33 end
34 end
35 end