edge badge

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.

Methods
C
N
Class Public methods
new(app, path, cache_control = nil, index: 'index')
# File actionpack/lib/action_dispatch/middleware/static.rb, line 111
def initialize(app, path, cache_control = nil, index: 'index')
  @app = app
  @file_handler = FileHandler.new(path, cache_control, index: index)
end
Instance Public methods
call(env)
# File actionpack/lib/action_dispatch/middleware/static.rb, line 116
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