In Files

Parent

Class/Module Index [+]

Quicksearch

Rack::File

Rack::File serves files below the root directory given, according to the path info of the Rack request. e.g. when Rack::File.new(“/etc”) is used, you can access ‘passwd’ file as localhost:9292/passwd

Handlers can detect if bodies are a Rack::File, and use mechanisms like sendfile on the path.

Constants

F
SEPS

Attributes

cache_control[RW]
path[RW]
root[RW]
to_path[RW]

Public Class Methods

new(root, cache_control = nil) click to toggle source
# File lib/rack/file.rb, line 23
def initialize(root, cache_control = nil)
  @root = root
  @cache_control = cache_control
end

Public Instance Methods

_call(env) click to toggle source
# File lib/rack/file.rb, line 34
def _call(env)
  @path_info = Utils.unescape(env["PATH_INFO"])
  parts = @path_info.split SEPS

  return fail(403, "Forbidden")  if parts.include? ".."

  @path = F.join(@root, *parts)

  available = begin
    F.file?(@path) && F.readable?(@path)
  rescue SystemCallError
    false
  end

  if available
    serving(env)
  else
    fail(404, "File not found: #{@path_info}")
  end
end
call(env) click to toggle source
# File lib/rack/file.rb, line 28
def call(env)
  dup._call(env)
end
each() click to toggle source
# File lib/rack/file.rb, line 95
def each
  F.open(@path, "rb") do |file|
    file.seek(@range.begin)
    remaining_len = @range.end-@range.begin+1
    while remaining_len > 0
      part = file.read([8192, remaining_len].min)
      break unless part
      remaining_len -= part.length

      yield part
    end
  end
end
serving(env) click to toggle source
# File lib/rack/file.rb, line 55
def serving(env)
  # NOTE:
  #   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.
  size = F.size?(@path) || Utils.bytesize(F.read(@path))

  response = [
    200,
    {
      "Last-Modified"  => F.mtime(@path).httpdate,
      "Content-Type"   => Mime.mime_type(F.extname(@path), 'text/plain')
    },
    self
  ]
  response[1].merge! 'Cache-Control' => @cache_control if @cache_control

  ranges = Rack::Utils.byte_ranges(env, size)
  if ranges.nil? || ranges.length > 1
    # No ranges, or multiple ranges (which we don't support):
    # TODO: Support multiple byte-ranges
    response[0] = 200
    @range = 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
  else
    # Partial content:
    @range = ranges[0]
    response[0] = 206
    response[1]["Content-Range"]  = "bytes #{@range.begin}-#{@range.end}/#{size}"
    size = @range.end - @range.begin + 1
  end

  response[1]["Content-Length"] = size.to_s
  response
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.