Permalink
Browse files

Fix parsing multiple ranges

Fix parsing miltiple ranges in HTTP_RANGE header according to w3 rfc2616 (according to last example in sec14.35.1 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.1 ) (according to BNF rules in http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.1 )
  • Loading branch information...
1 parent 1824547 commit 251e6e0fbaf404ac3ae897c24c08b5392413881e @funny-falcon committed Nov 9, 2012
Showing with 6 additions and 6 deletions.
  1. +6 −6 lib/rack/utils.rb
View
@@ -316,16 +316,16 @@ def rfc2822(time)
def 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
+ return nil unless http_range && http_range =~ /bytes=([^;]+)/
ranges = []
- 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]
+ $1.split(/,\s*/).each do |range_spec|
+ return nil unless range_spec =~ /(\d*)-(\d*)/
+ r0,r1 = $1, $2
if r0.empty?
return nil if r1.empty?
# suffix-byte-range-spec, represents trailing suffix of file
- r0 = [size - r1.to_i, 0].max
+ r0 = size - r1.to_i
+ r0 = 0 if r0 < 0
r1 = size - 1
else
r0 = r0.to_i

0 comments on commit 251e6e0

Please sign in to comment.