set_range(r, e = nil)
public
Sets the HTTP Range:
header. Accepts either a Range object as a single
argument, or a beginning index and a length from that index. Example:
req.range = (0..1023)
req.set_range 0, 1023
Show source
def set_range(r, e = nil)
unless r
@header.delete 'range'
return r
end
r = (r...r+e) if e
case r
when Numeric
n = r.to_i
rangestr = (n > 0 ? "0-#{n-1}" : "-#{-n}")
when Range
first = r.first
last = r.last
last -= 1 if r.exclude_end?
if last == -1
rangestr = (first > 0 ? "#{first}-" : "-#{-first}")
else
raise HTTPHeaderSyntaxError, 'range.first is negative' if first < 0
raise HTTPHeaderSyntaxError, 'range.last is negative' if last < 0
raise HTTPHeaderSyntaxError, 'must be .first < .last' if first > last
rangestr = "#{first}-#{last}"
end
else
raise TypeError, 'Range/Integer is required'
end
@header['range'] = ["bytes=#{rangestr}"]
r
end