class Rack::Files
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/rack/files.rbs class Rack::Files def get: (Hash env) -> untyped def initialize: (String root, ?Hash headers, ?String default_mime) -> void end
def self.method_added(name)
- Todo: - remove in 3.0
def self.method_added(name) if name == :response_body raise "#{self.class}\#response_body is no longer supported." end super end
def call(env)
def call(env) # HEAD requests drop the response body, including 4xx error messages. @head.call env end
def fail(status, body, headers = {})
def fail(status, body, headers = {}) body += "\n" [ status, { CONTENT_TYPE => "text/plain", CONTENT_LENGTH => body.size.to_s, "X-Cascade" => "pass" }.merge!(headers), [body] ] end
def filesize(path)
def filesize(path) # We check via File::size? whether this file provides size info # via stat (e.g. /proc files often don't), otherwise we have to # figure it out by reading the whole file into memory. ::File.size?(path) || ::File.read(path).bytesize end
def get(env)
Experimental RBS support (using type sampling data from the type_fusion
project).
def get: (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 | Symbol | Symbol | Symbol | String | String | 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_7920_SCRIPT_NAME | String | ORIGINAL_FULLPATH | String | ORIGINAL_SCRIPT_NAME | 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 | | warden | Warden::Proxy | action_dispatch.request.path_parameters | | rack.request.cookie_hash | _share_via_app_session | String | _web_session | String | remember_token | String | rack.request.cookie_string | String | action_dispatch.cookies | ActionDispatch::Cookies::CookieJar | action_dispatch.request.unsigned_session_cookie | session_id | String | warden.user.user.key | String | _csrf_token | String | csrf | String env) -> untyped
This signature was generated using 1 sample from 1 application.
def get(env) request = Rack::Request.new env unless ALLOWED_VERBS.include? request.request_method return fail(405, "Method Not Allowed", { 'Allow' => ALLOW_HEADER }) end path_info = Utils.unescape_path request.path_info return fail(400, "Bad Request") unless Utils.valid_path?(path_info) clean_path_info = Utils.clean_path_info(path_info) path = ::File.join(@root, clean_path_info) available = begin ::File.file?(path) && ::File.readable?(path) rescue SystemCallError # Not sure in what conditions this exception can occur, but this # is a safe way to handle such an error. # :nocov: false # :nocov: end if available serving(request, path) else fail(404, "File not found: #{path_info}") end end
def initialize(root, headers = {}, default_mime = 'text/plain')
Experimental RBS support (using type sampling data from the type_fusion
project).
def initialize: (String root, ? headers, ?String default_mime) -> void
This signature was generated using 2 samples from 1 application.
def initialize(root, headers = {}, default_mime = 'text/plain') @root = (::File.expand_path(root) if root) @headers = headers @default_mime = default_mime @head = Rack::Head.new(lambda { |env| get env }) end
def mime_type(path, default_mime)
def mime_type(path, default_mime) Mime.mime_type(::File.extname(path), default_mime) end
def serving(request, path)
def serving(request, path) if request.options? return [200, { 'Allow' => ALLOW_HEADER, CONTENT_LENGTH => '0' }, []] end last_modified = ::File.mtime(path).httpdate return [304, {}, []] if request.get_header('HTTP_IF_MODIFIED_SINCE') == last_modified headers = { "Last-Modified" => last_modified } mime_type = mime_type path, @default_mime headers[CONTENT_TYPE] = mime_type if mime_type # Set custom headers headers.merge!(@headers) if @headers status = 200 size = filesize path ranges = Rack::Utils.get_byte_ranges(request.get_header('HTTP_RANGE'), size) if ranges.nil? # No ranges: ranges = [0..size - 1] elsif ranges.empty? # Unsatisfiable. Return error, and file size: response = fail(416, "Byte range unsatisfiable") response[1]["Content-Range"] = "bytes */#{size}" return response elsif ranges.size >= 1 # Partial content partial_content = true if ranges.size == 1 range = ranges[0] headers["Content-Range"] = "bytes #{range.begin}-#{range.end}/#{size}" else headers[CONTENT_TYPE] = "multipart/byteranges; boundary=#{MULTIPART_BOUNDARY}" end status = 206 body = BaseIterator.new(path, ranges, mime_type: mime_type, size: size) size = body.bytesize end headers[CONTENT_LENGTH] = size.to_s if request.head? body = [] elsif !partial_content body = Iterator.new(path, ranges, mime_type: mime_type, size: size) end [status, headers, body] end