class ActionDispatch::FileHandler
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/action_dispatch/middleware/static.rbs class ActionDispatch::FileHandler def attempt: (Hash env) -> nil def clean_path: (String path_info) -> String def compressible?: (String content_type) -> true def each_candidate_filepath: (String path_info) -> nil def each_precompressed_filepath: (String filepath) -> nil def file_readable?: (String path) -> false def find_file: (String path_info, accept_encoding: Array[Array, String, Float]) -> nil def try_files: (String filepath, String content_type, accept_encoding: Array[Array, String, Float]) -> nil def try_precompressed_files: (String filepath, Hash headers, accept_encoding: Array[Array, String, Float]) -> nil end
additional response headers.index: "index"
to change the default path
/index.html, and optional
Pass the root
directory to search for matching files, an optional
If no matching file is found, this endpoint responds 404 Not Found
.
endpoint returns that file with a Content-Encoding: br
header.
and gzip (.gz) files are supported. If path
.br exists, this
Precompressed versions of these files are checked first. Brotli (.br)
conventions: path
, path
.html, path
/index.html.
URL paths are matched with static files according to expected
This endpoint serves static files from disk using Rack::File
.
def attempt(env)
Experimental RBS support (using type sampling data from the type_fusion
project).
def attempt: (rack.version | Integer | rack.errors | IO | rack.multithread | TrueClass | rack.multiprocess | FalseClass | rack.run_once | FalseClass | rack.url_scheme | String | SCRIPT_NAME | String | QUERY_STRING | String | SERVER_SOFTWARE | String | GATEWAY_INTERFACE | String | REQUEST_METHOD | String | REQUEST_PATH | String | REQUEST_URI | String | SERVER_PROTOCOL | String | HTTP_HOST | String | HTTP_SEC_FETCH_SITE | String | HTTP_ACCEPT_ENCODING | String | HTTP_COOKIE | String | HTTP_CONNECTION | String | HTTP_SEC_FETCH_MODE | String | HTTP_ACCEPT | String | HTTP_USER_AGENT | String | HTTP_REFERER | String | HTTP_SEC_FETCH_DEST | String | HTTP_ACCEPT_LANGUAGE | String | puma.request_body_wait | Float | SERVER_NAME | String | SERVER_PORT | String | PATH_INFO | String | REMOTE_ADDR | String | HTTP_VERSION | String | puma.socket | TCPSocket | rack.hijack? | TrueClass | rack.hijack | Puma::Client | rack.input | Puma::NullIO | rack.after_reply | | puma.config | Puma::Configuration | action_dispatch.parameter_filter | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | Symbol | action_dispatch.redirect_filter | | action_dispatch.secret_key_base | String | action_dispatch.show_exceptions | TrueClass | action_dispatch.show_detailed_exceptions | TrueClass | action_dispatch.log_rescued_responses | TrueClass | action_dispatch.logger | ActiveSupport::Logger | action_dispatch.backtrace_cleaner | Rails::BacktraceCleaner | action_dispatch.key_generator | ActiveSupport::CachingKeyGenerator | action_dispatch.http_auth_salt | String | action_dispatch.signed_cookie_salt | String | action_dispatch.encrypted_cookie_salt | String | action_dispatch.encrypted_signed_cookie_salt | String | action_dispatch.authenticated_encrypted_cookie_salt | String | action_dispatch.use_authenticated_cookie_encryption | TrueClass | action_dispatch.encrypted_cookie_cipher | NilClass | action_dispatch.signed_cookie_digest | NilClass | action_dispatch.cookies_serializer | Symbol | action_dispatch.cookies_digest | NilClass | action_dispatch.cookies_rotations | ActiveSupport::Messages::RotationConfiguration | action_dispatch.cookies_same_site_protection | Proc | action_dispatch.use_cookies_with_metadata | TrueClass | action_dispatch.content_security_policy | NilClass | action_dispatch.content_security_policy_report_only | FalseClass | action_dispatch.content_security_policy_nonce_generator | NilClass | action_dispatch.content_security_policy_nonce_directives | NilClass | action_dispatch.permissions_policy | NilClass | action_dispatch.routes | ActionDispatch::Routing::RouteSet | ROUTES_14080_SCRIPT_NAME | String | ORIGINAL_FULLPATH | String | ORIGINAL_SCRIPT_NAME | String | action_dispatch.authorized_host | String | action_dispatch.request_id | String | action_dispatch.remote_ip | ActionDispatch::RemoteIp::GetIp | rack.session | ActionDispatch::Request::Session | rack.session.options | ActionDispatch::Request::Session::Options | rack.tempfiles | env) -> nil
This signature was generated using 1 sample from 1 application.
def attempt(env) request = Rack::Request.new env if request.get? || request.head? if found = find_file(request.path_info, accept_encoding: request.accept_encoding) serve request, *found end end end
def call(env)
def call(env) attempt(env) || @file_server.call(env) end
def clean_path(path_info)
Experimental RBS support (using type sampling data from the type_fusion
project).
def clean_path: (String path_info) -> String
This signature was generated using 1 sample from 1 application.
def clean_path(path_info) path = ::Rack::Utils.unescape_path path_info.chomp("/") if ::Rack::Utils.valid_path? path ::Rack::Utils.clean_path_info path end end
def compressible?(content_type)
Experimental RBS support (using type sampling data from the type_fusion
project).
def compressible?: (String content_type) -> true
This signature was generated using 10 samples from 2 applications.
def compressible?(content_type) @compressible_content_types.match?(content_type) end
def each_candidate_filepath(path_info)
Experimental RBS support (using type sampling data from the type_fusion
project).
def each_candidate_filepath: (String path_info) -> nil
This signature was generated using 2 samples from 1 application.
def each_candidate_filepath(path_info) return unless path = clean_path(path_info) ext = ::File.extname(path) content_type = ::Rack::Mime.mime_type(ext, nil) yield path, content_type || "text/plain" # Tack on .html and /index.html only for paths that don't have # an explicit, resolvable file extension. No need to check # for foo.js.html and foo.js/index.html. unless content_type default_ext = ::ActionController::Base.default_static_extension if ext != default_ext default_content_type = ::Rack::Mime.mime_type(default_ext, "text/plain") yield "#{path}#{default_ext}", default_content_type yield "#{path}/#{@index}#{default_ext}", default_content_type end end nil end
def each_precompressed_filepath(filepath)
Experimental RBS support (using type sampling data from the type_fusion
project).
def each_precompressed_filepath: (String filepath) -> nil
This signature was generated using 4 samples from 1 application.
def each_precompressed_filepath(filepath) @precompressed.each do |content_encoding| precompressed_ext = PRECOMPRESSED.fetch(content_encoding) yield content_encoding, "#{filepath}#{precompressed_ext}" end nil end
def file_readable?(path)
Experimental RBS support (using type sampling data from the type_fusion
project).
def file_readable?: (String path) -> false
This signature was generated using 11 samples from 2 applications.
def file_readable?(path) file_path = File.join(@root, path.b) File.file?(file_path) && File.readable?(file_path) end
def find_file(path_info, accept_encoding:)
Experimental RBS support (using type sampling data from the type_fusion
project).
def find_file: (String path_info, accept_encoding: Array | String | Float | Array | String | Float) -> nil
This signature was generated using 1 sample from 1 application.
If a matching file is found, the path and necessary response headers
in that order, including .br and .gzip compressed extensions.
Checks for +path+, +path+.html, and +path+/index.html files,
+public/+ directory (see Static#call).
Used by the +Static+ class to negotiate a servable file in the
Match a URI path to a static file to be served.
def find_file(path_info, accept_encoding:) each_candidate_filepath(path_info) do |filepath, content_type| if response = try_files(filepath, content_type, accept_encoding: accept_encoding) return response end end end
def initialize(root, index: "index", headers: {}, precompressed: %i[ br gzip ], compressible_content_types: /\A(?:text\/|application\/javascript)/)
def initialize(root, index: "index", headers: {}, precompressed: %i[ br gzip ], compressible_content_types: /\A(?:text\/|application\/javascript)/) @root = root.chomp("/").b @index = index @precompressed = Array(precompressed).map(&:to_s) | %w[ identity ] @compressible_content_types = compressible_content_types @file_server = ::Rack::File.new(@root, headers) end
def serve(request, filepath, content_headers)
def serve(request, filepath, content_headers) original, request.path_info = request.path_info, ::Rack::Utils.escape_path(filepath).b @file_server.call(request.env).tap do |status, headers, body| # Omit Content-Encoding/Type/etc headers for 304 Not Modified if status != 304 headers.update(content_headers) end end ensure request.path_info = original end
def try_files(filepath, content_type, accept_encoding:)
Experimental RBS support (using type sampling data from the type_fusion
project).
def try_files: (String filepath, String content_type, accept_encoding: Array | String | Float) -> nil
This signature was generated using 4 samples from 2 applications.
def try_files(filepath, content_type, accept_encoding:) headers = { "Content-Type" => content_type } if compressible? content_type try_precompressed_files filepath, headers, accept_encoding: accept_encoding elsif file_readable? filepath [ filepath, headers ] end end
def try_precompressed_files(filepath, headers, accept_encoding:)
Experimental RBS support (using type sampling data from the type_fusion
project).
def try_precompressed_files: (String filepath, Content-Type | String headers, accept_encoding: Array | String | Float) -> nil
This signature was generated using 5 samples from 1 application.
def try_precompressed_files(filepath, headers, accept_encoding:) each_precompressed_filepath(filepath) do |content_encoding, precompressed_filepath| if file_readable? precompressed_filepath # Identity encoding is default, so we skip Accept-Encoding # negotiation and needn't set Content-Encoding. # # Vary header is expected when we've found other available # encodings that Accept-Encoding ruled out. if content_encoding == "identity" return precompressed_filepath, headers else headers["Vary"] = "Accept-Encoding" if accept_encoding.any? { |enc, _| /\b#{content_encoding}\b/i.match?(enc) } headers["Content-Encoding"] = content_encoding return precompressed_filepath, headers end end end end end