Class: Rack::File

Inherits:
Object show all
Defined in:
lib/gems/rack-0.9.1/lib/rack/file.rb

Overview

Rack::File serves files below the root given, according to the path info of the Rack request.

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.



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

def initialize(root)
  @root = root
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 13

def path
  @path
end

#rootObject

Returns the value of attribute root.



12
13
14
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 12

def root
  @root
end

Instance Method Details

#_call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 25

def _call(env)
  @path_info = Utils.unescape(env["PATH_INFO"])
  return forbidden  if @path_info.include? ".."

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

  begin
    if F.file?(@path) && F.readable?(@path)
      serving
    else
      raise Errno::EPERM
    end
  rescue SystemCallError
    not_found
  end
end

#call(env) ⇒ Object



19
20
21
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 19

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

#eachObject



77
78
79
80
81
82
83
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 77

def each
  F.open(@path, "rb") { |file|
    while part = file.read(8192)
      yield part
    end
  }
end

#forbiddenObject



42
43
44
45
46
47
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 42

def forbidden
  body = "Forbidden\n"
  [403, {"Content-Type" => "text/plain",
         "Content-Length" => body.size.to_s},
   [body]]
end

#not_foundObject



70
71
72
73
74
75
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 70

def not_found
  body = "File not found: #{@path_info}\n"
  [404, {"Content-Type" => "text/plain",
     "Content-Length" => body.size.to_s},
   [body]]
end

#servingObject

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. And while
we're at it we also use this as body then.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gems/rack-0.9.1/lib/rack/file.rb', line 55

def serving
  if size = F.size?(@path)
    body = self
  else
    body = [F.read(@path)]
    size = body.first.size
  end

  [200, {
    "Last-Modified"  => F.mtime(@path).httpdate,
    "Content-Type"   => Mime.mime_type(F.extname(@path), 'text/plain'),
    "Content-Length" => size.to_s
  }, body]
end