Class: Butler::Asset
- Inherits:
-
Object
- Object
- Butler::Asset
- Defined in:
- lib/butler/asset.rb
Overview
Butler::Asset
Sends a single requested file out to the user The code has been largely taken from Rack::File but adjusted to support sending custuom HTTP headers
Usage:
Butler::Asset.new(root_directory, headers: {
'Cache-Control' => 'public, max-age=31536000',
'Some custom header' => 'Content for some custom header'
})
Constant Summary collapse
- SEPS =
Regexp.union(*[File::SEPARATOR, File::ALT_SEPARATOR].compact)
- ALLOWED_VERBS =
%w[GET HEAD]
- F =
File
Instance Attribute Summary collapse
-
#path ⇒ Object
(also: #to_path)
Returns the value of attribute path.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #_call(env) ⇒ Object
- #call(env) ⇒ Object
- #each ⇒ Object
-
#initialize(root, options = {}) ⇒ Asset
constructor
A new instance of Asset.
- #serving(env) ⇒ Object
Constructor Details
Instance Attribute Details
#path ⇒ Object Also known as: to_path
Returns the value of attribute path.
23 24 25 |
# File 'lib/butler/asset.rb', line 23 def path @path end |
#root ⇒ Object
Returns the value of attribute root.
23 24 25 |
# File 'lib/butler/asset.rb', line 23 def root @root end |
Instance Method Details
#_call(env) ⇒ Object
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 65 66 67 68 69 70 |
# File 'lib/butler/asset.rb', line 37 def _call(env) unless ALLOWED_VERBS.include? env["REQUEST_METHOD"] return fail(405, "Method Not Allowed") end path_info = Rack::Utils.unescape(env["PATH_INFO"]) parts = path_info.split SEPS parts.inject(0) do |depth, part| case part when '', '.' depth when '..' return fail(404, "Not Found") if depth - 1 < 0 depth - 1 else depth + 1 end end @path = F.join(@root, *parts) 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
31 32 33 |
# File 'lib/butler/asset.rb', line 31 def call(env) dup._call(env) end |
#each ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/butler/asset.rb', line 117 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
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 107 108 109 110 111 112 113 114 115 |
# File 'lib/butler/asset.rb', line 72 def serving(env) last_modified = F.mtime(@path).httpdate return [304, {}, []] if env['HTTP_IF_MODIFIED_SINCE'] == last_modified response = [ 200, { "Last-Modified" => last_modified, "Content-Type" => Rack::Mime.mime_type(F.extname(@path), 'text/plain') }, env["REQUEST_METHOD"] == "HEAD" ? [] : self ] # Set headers @headers.each { |field, content| response[1][field] = content } if @headers # 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 |