Class: Rack::File
- Inherits:
-
Object
- Object
- Rack::File
- Defined in:
- rack/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
- SEPS =
Regexp.union(*[::File::SEPARATOR, ::File::ALT_SEPARATOR].compact)
- ALLOWED_VERBS =
%w[GET HEAD]
- F =
::File
Instance Attribute Summary (collapse)
-
- (void) cache_control
Returns the value of attribute cache_control.
-
- (void) path
(also: #to_path)
Returns the value of attribute path.
-
- (void) root
Returns the value of attribute root.
Instance Method Summary (collapse)
- - (void) _call(env)
- - (void) call(env)
- - (void) each
-
- (File) initialize(root, headers = {}, default_mime = 'text/plain')
constructor
A new instance of File.
- - (void) serving(env)
Constructor Details
- (File) initialize(root, headers = {}, default_mime = 'text/plain')
Returns a new instance of File
24 25 26 27 28 |
# File 'rack/rack/file.rb', line 24 def initialize(root, headers={}, default_mime = 'text/plain') @root = root @headers = headers @default_mime = default_mime end |
Instance Attribute Details
- (void) cache_control
Returns the value of attribute cache_control
20 21 22 |
# File 'rack/rack/file.rb', line 20 def cache_control @cache_control end |
- (void) path Also known as: to_path
Returns the value of attribute path
19 20 21 |
# File 'rack/rack/file.rb', line 19 def path @path end |
- (void) root
Returns the value of attribute root
18 19 20 |
# File 'rack/rack/file.rb', line 18 def root @root end |
Instance Method Details
- (void) _call(env)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'rack/rack/file.rb', line 36 def _call(env) unless ALLOWED_VERBS.include? env["REQUEST_METHOD"] return fail(405, "Method Not Allowed") end path_info = Utils.unescape(env["PATH_INFO"]) parts = path_info.split SEPS clean = [] parts.each do |part| next if part.empty? || part == '.' part == '..' ? clean.pop : clean << part end @path = F.join(@root, *clean) 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 |
- (void) call(env)
30 31 32 |
# File 'rack/rack/file.rb', line 30 def call(env) dup._call(env) end |
- (void) each
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'rack/rack/file.rb', line 108 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 |
- (void) serving(env)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'rack/rack/file.rb', line 66 def serving(env) last_modified = F.mtime(@path).httpdate return [304, {}, []] if env['HTTP_IF_MODIFIED_SINCE'] == last_modified headers = { "Last-Modified" => last_modified } mime = Mime.mime_type(F.extname(@path), @default_mime) headers["Content-Type"] = mime if mime # Set custom headers @headers.each { |field, content| headers[field] = content } if @headers response = [ 200, headers, env["REQUEST_METHOD"] == "HEAD" ? [] : self ] # 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)) 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 |