1 # -*- encoding: binary -*-
3 # This code is based on the original Rails handler in Mongrel
4 # Copyright (c) 2005 Zed A. Shaw
5 # Copyright (c) 2009 Eric Wong
6 # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
9 # Static file handler for Rails < 2.3. This handler is only provided
10 # as a convenience for developers. Performance-minded deployments should
11 # use nginx (or similar) for serving static files.
13 # This supports page caching directly and will try to resolve a
14 # request in the following order:
16 # * If the requested exact PATH_INFO exists as a file then serve it.
17 # * If it exists at PATH_INFO+rest_operator+".html" exists
20 # This means that if you are using page caching it will actually work
21 # with Unicorn and you should see a decent speed boost (but not as
22 # fast as if you use a static server like nginx).
23 class Unicorn::App::OldRails::Static < Struct.new(:app, :root, :file_server)
24 FILE_METHODS = { 'GET' => true, 'HEAD' => true }
26 # avoid allocating new strings for hash lookups
27 REQUEST_METHOD = 'REQUEST_METHOD'
28 REQUEST_URI = 'REQUEST_URI'
29 PATH_INFO = 'PATH_INFO'
33 self.root = "#{::RAILS_ROOT}/public"
34 self.file_server = ::Rack::File.new(root)
38 # short circuit this ASAP if serving non-file methods
39 FILE_METHODS.include?(env[REQUEST_METHOD]) or return app.call(env)
41 # first try the path as-is
42 path_info = env[PATH_INFO].chomp("/")
43 if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}")
44 # File exists as-is so serve it up
45 env[PATH_INFO] = path_info
46 return file_server.call(env)
49 # then try the cached version:
50 path_info << ActionController::Base.page_cache_extension
52 if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}")
53 env[PATH_INFO] = path_info
54 return file_server.call(env)
57 app.call(env) # call OldRails
59 end if defined?(Unicorn::App::OldRails)