f07c6beb5e4db5ae9984935d63933b54ec4ed644
[feedcatcher.git] / vendor / rails / railties / lib / rails / rack / static.rb
1 require 'rack/utils'
2
3 module Rails
4 module Rack
5 class Static
6 FILE_METHODS = %w(GET HEAD).freeze
7
8 def initialize(app)
9 @app = app
10 @file_server = ::Rack::File.new(File.join(RAILS_ROOT, "public"))
11 end
12
13 def call(env)
14 path = env['PATH_INFO'].chomp('/')
15 method = env['REQUEST_METHOD']
16
17 if FILE_METHODS.include?(method)
18 if file_exist?(path)
19 return @file_server.call(env)
20 else
21 cached_path = directory_exist?(path) ? "#{path}/index" : path
22 cached_path += ::ActionController::Base.page_cache_extension
23
24 if file_exist?(cached_path)
25 env['PATH_INFO'] = cached_path
26 return @file_server.call(env)
27 end
28 end
29 end
30
31 @app.call(env)
32 end
33
34 private
35 def file_exist?(path)
36 full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
37 File.file?(full_path) && File.readable?(full_path)
38 end
39
40 def directory_exist?(path)
41 full_path = File.join(@file_server.root, ::Rack::Utils.unescape(path))
42 File.directory?(full_path) && File.readable?(full_path)
43 end
44 end
45 end
46 end