Class: ActionDispatch::Static
- Inherits:
-
Object
- Object
- ActionDispatch::Static
- Defined in:
- lib/action_dispatch/middleware/static.rb
Overview
This middleware will attempt to return the contents of a file’s body from disk in the response. If a file is not found on disk, the request will be delegated to the application stack. This middleware is commonly initialized to serve assets from a server’s ‘public/` directory.
This middleware verifies the path to ensure that only files living in the root directory can be rendered. A request cannot produce a directory traversal using this middleware. Only ‘GET’ and ‘HEAD’ requests will result in a file being returned.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, path, deprecated_cache_control = :not_set, index: 'index', headers: {}) ⇒ Static
constructor
A new instance of Static.
Constructor Details
#initialize(app, path, deprecated_cache_control = :not_set, index: 'index', headers: {}) ⇒ Static
Returns a new instance of Static.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/action_dispatch/middleware/static.rb', line 113 def initialize(app, path, deprecated_cache_control = :not_set, index: 'index', headers: {}) if deprecated_cache_control != :not_set ActiveSupport::Deprecation.warn("The `cache_control` argument is deprecated," \ "replaced by `headers: { 'Cache-Control' => #{deprecated_cache_control} }`, " \ " and will be removed in Rails 5.1.") headers['Cache-Control'.freeze] = deprecated_cache_control end @app = app @file_handler = FileHandler.new(path, index: index, headers: headers) end |
Instance Method Details
#call(env) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/action_dispatch/middleware/static.rb', line 125 def call(env) req = ActionDispatch::Request.new env if req.get? || req.head? path = req.path_info.chomp('/'.freeze) if match = @file_handler.match?(path) req.path_info = match return @file_handler.serve(req) end end @app.call(req.env) end |