Class: Rack::File

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/file.rb

Overview

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.

Constant Summary collapse

F =
::File

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ File

Returns a new instance of File.



20
21
22
# File 'lib/rack/file.rb', line 20

def initialize(root)
  @root = root
end

Instance Attribute Details

#pathObject Also known as: to_path

Returns the value of attribute path.



16
17
18
# File 'lib/rack/file.rb', line 16

def path
  @path
end

#rootObject

Returns the value of attribute root.



15
16
17
# File 'lib/rack/file.rb', line 15

def root
  @root
end

Instance Method Details

#_call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rack/file.rb', line 30

def _call(env)
  @path_info = Utils.unescape(env["PATH_INFO"])
  return fail(403, "Forbidden")  if @path_info.include? ".."

  @path = F.join(@root, @path_info)

  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) ⇒ Object



24
25
26
# File 'lib/rack/file.rb', line 24

def call(env)
  dup._call(env)
end

#eachObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rack/file.rb', line 88

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) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rack/file.rb', line 49

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
  ]

  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