Class Rack::File
In: lib/rack/file.rb
Parent: Object

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.

Methods

_call   call   each   new  

Constants

F = ::File

Attributes

path  [RW] 
root  [RW] 

Public Class methods

[Source]

    # File lib/rack/file.rb, line 12
12:     def initialize(root)
13:       @root = root
14:     end

Public Instance methods

[Source]

    # File lib/rack/file.rb, line 22
22:     def _call(env)
23:       if env["PATH_INFO"].include? ".."
24:         return [403, {"Content-Type" => "text/plain"}, ["Forbidden\n"]]
25:       end
26: 
27:       @path = F.join(@root, Utils.unescape(env["PATH_INFO"]))
28:       ext = F.extname(@path)[1..-1]
29: 
30:       if F.file?(@path) && F.readable?(@path)
31:         [200, {
32:            "Last-Modified"  => F.mtime(@path).rfc822,
33:            "Content-Type"   => MIME_TYPES[ext] || "text/plain",
34:            "Content-Length" => F.size(@path).to_s
35:          }, self]
36:       else
37:         return [404, {"Content-Type" => "text/plain"},
38:                 ["File not found: #{env["PATH_INFO"]}\n"]]
39:       end
40:     end

[Source]

    # File lib/rack/file.rb, line 16
16:     def call(env)
17:       dup._call(env)
18:     end

[Source]

    # File lib/rack/file.rb, line 42
42:     def each
43:       F.open(@path, "rb") { |file|
44:         while part = file.read(8192)
45:           yield part
46:         end
47:       }
48:     end

[Validate]