class IntervalResponse::Multi

Constants

ALPHABET

Public Class Methods

Public Instance Methods

content_length() click to toggle source
# File lib/interval_response/multi.rb, line 31
def content_length
  # The Content-Length of a multipart response includes the length
  # of all the ranges of the resource, but also the lengths of the
  # multipart part headers - which we need to precompute. To do it
  # we need to run through all of our ranges and output some strings,
  # and if a lot of ranges are involved this can get expensive. So
  # memoize the envelope size (it never changes between calls)
  @envelope_size ||= compute_envelope_size
end
each() { |part_header, entire_header_range| ... } click to toggle source
# File lib/interval_response/multi.rb, line 15
def each
  # serve the part of the interval map
  @http_ranges.each_with_index do |http_range, range_i|
    part_header = part_header(range_i, http_range)
    entire_header_range = 0..(part_header.bytesize - 1)
    yield(part_header, entire_header_range)
    @interval_sequence.each_in_range(http_range) do |segment, range_in_segment|
      yield(segment, range_in_segment)
    end
  end
end
headers() click to toggle source
# File lib/interval_response/multi.rb, line 41
def headers
  {
    'Accept-Ranges' => 'bytes',
    'Content-Length' => content_length.to_s,
    'Content-Type' => "multipart/byte-ranges; boundary=#{@boundary}",
    'ETag' => etag,
  }
end
multiple_ranges?() click to toggle source
# File lib/interval_response/multi.rb, line 54
def multiple_ranges?
  true
end
satisfied_with_first_interval?() click to toggle source
# File lib/interval_response/multi.rb, line 50
def satisfied_with_first_interval?
  @interval_sequence.first_interval_only?(*@http_ranges)
end
status_code() click to toggle source
# File lib/interval_response/multi.rb, line 27
def status_code
  206
end

Private Instance Methods

compute_envelope_size() click to toggle source
# File lib/interval_response/multi.rb, line 60
def compute_envelope_size
  @http_ranges.each_with_index.inject(0) do |size_sum, (http_range, part_index)|
    header_bytes = part_header(part_index, http_range)
    range_size = http_range.end - http_range.begin + 1
    size_sum + header_bytes.bytesize + range_size
  end
end
part_header(part_index, http_r) click to toggle source
# File lib/interval_response/multi.rb, line 68
def part_header(part_index, http_r)
  [
    part_index > 0 ? "\r\n" : "", # Parts follwing the first have to be delimited "at the top"
    "--%s\r\n" % @boundary,
    "Content-Type: binary/octet-stream\r\n",
    "Content-Range: bytes %d-%d/%d\r\n" % [http_r.begin, http_r.end, @interval_sequence.size],
    "\r\n",
  ].join
end