lib/rack/file.rb in rack-1.4.1 vs lib/rack/file.rb in rack-1.4.2
- old
+ new
@@ -19,13 +19,20 @@
attr_accessor :path
attr_accessor :cache_control
alias :to_path :path
- def initialize(root, cache_control = nil)
+ def initialize(root, headers={})
@root = root
- @cache_control = cache_control
+ # Allow a cache_control string for backwards compatibility
+ if headers.instance_of? String
+ warn \
+ "Rack::File headers parameter replaces cache_control after Rack 1.5."
+ @headers = { 'Cache-Control' => headers }
+ else
+ @headers = headers
+ end
end
def call(env)
dup._call(env)
end
@@ -76,12 +83,14 @@
"Last-Modified" => last_modified,
"Content-Type" => Mime.mime_type(F.extname(@path), 'text/plain')
},
env["REQUEST_METHOD"] == "HEAD" ? [] : self
]
- response[1].merge! 'Cache-Control' => @cache_control if @cache_control
+ # Set custom 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))
@@ -99,10 +108,10 @@
return response
else
# Partial content:
@range = ranges[0]
response[0] = 206
- response[1]["Content-Range"] = "bytes #{@range.begin}-#{@range.end}/#{size}"
+ 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