diff options
author | raggi <jftucker@gmail.com> | 2010-10-03 21:50:06 -0300 |
---|---|---|
committer | raggi <jftucker@gmail.com> | 2010-10-03 21:50:06 -0300 |
commit | 94555f91770cbd22551c5ff58f943c9b51b4a849 (patch) | |
tree | 8fccf82545c2d157c279a0f4b8e43a3a426ab3a8 | |
parent | 8859e5c35cac9631ed55bfab904bf7028db3260c (diff) | |
download | rack-94555f91770cbd22551c5ff58f943c9b51b4a849.tar.gz |
minor style changes for merge
-rw-r--r-- | lib/rack/file.rb | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/lib/rack/file.rb b/lib/rack/file.rb index 6524e199..fc79fa2f 100644 --- a/lib/rack/file.rb +++ b/lib/rack/file.rb @@ -33,13 +33,15 @@ module Rack @path = F.join(@root, @path_info) - begin - if F.file?(@path) && F.readable?(@path) - serving(env) - else - raise Errno::EPERM - end + 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 @@ -51,17 +53,21 @@ module Rack # figure it out by reading the whole file into memory. size = F.size?(@path) || Utils.bytesize(F.read(@path)) - response = [200, { + response = [ + 200, + { "Last-Modified" => F.mtime(@path).httpdate, "Content-Type" => Mime.mime_type(F.extname(@path), 'text/plain') - }, self] + }, + self + ] - ranges = File.byte_ranges(env, size) + ranges = self.class.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) + @range = 0..size-1 elsif ranges.empty? # Unsatisfiable. Return error, and file size: response = fail(416, "Byte range unsatisfiable") @@ -76,7 +82,7 @@ module Rack end response[1]["Content-Length"] = size.to_s - return response + response end def each @@ -96,12 +102,12 @@ module Rack # Parses the "Range:" header, if present, into an array of Range objects. # Returns nil if the header is missing or syntactically invalid. # Returns an empty array if none of the ranges are satisfiable. - def File.byte_ranges(env, size) + def self.byte_ranges(env, size) # See <http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35> http_range = env['HTTP_RANGE'] return nil unless http_range ranges = [] - for range_spec in http_range.split(/,\s*/) do + http_range.split(/,\s*/).each do |range_spec| matches = range_spec.match(/bytes=(\d*)-(\d*)/) return nil unless matches r0,r1 = matches[1], matches[2] @@ -112,8 +118,8 @@ module Rack r1 = size - 1 else r0 = r0.to_i - if r1.empty? then - r1 = size-1 + if r1.empty? + r1 = size - 1 else r1 = r1.to_i return nil if r1 < r0 # backwards range is syntactically invalid @@ -122,18 +128,22 @@ module Rack end ranges << (r0..r1) if r0 <= r1 end - return ranges + ranges end private def fail(status, body) body += "\n" - [status, - {"Content-Type" => "text/plain", - "Content-Length" => body.size.to_s, - "X-Cascade" => "pass"}, - [body]] + [ + status, + { + "Content-Type" => "text/plain", + "Content-Length" => body.size.to_s, + "X-Cascade" => "pass" + }, + [body] + ] end end |