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 14
14:     def initialize(root)
15:       @root = root
16:     end

Public Instance methods

[Source]

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

[Source]

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

[Source]

    # File lib/rack/file.rb, line 47
47:     def each
48:       F.open(@path, "rb") { |file|
49:         while part = file.read(8192)
50:           yield part
51:         end
52:       }
53:     end

[Validate]