rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 5d8f01e015f814301f8289c0ee6a990752682a6e 1095 bytes (raw)
$ git show v0.97.0:lib/rainbows/response/range.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
# -*- encoding: binary -*-
# :enddoc:
module Rainbows::Response::Range
  HTTP_RANGE = 'HTTP_RANGE'
  Content_Range = 'Content-Range'.freeze
  Content_Length = 'Content-Length'.freeze

  # This does not support multipart responses (does anybody actually
  # use those?) +headers+ is always a Rack::Utils::HeaderHash
  def make_range!(env, status, headers)
    if 200 == status.to_i &&
        (clen = headers[Content_Length]) &&
        /\Abytes=(\d+-\d*|\d*-\d+)\z/ =~ env[HTTP_RANGE]
      a, b = $1.split(/-/)
      clen = clen.to_i
      if b.nil? # bytes=M-
        offset = a.to_i
        count = clen - offset
      elsif a.empty? # bytes=-N
        offset = clen - b.to_i
        count = clen - offset
      else  # bytes=M-N
        offset = a.to_i
        count = b.to_i + 1 - offset
      end
      raise Rainbows::Response416 if count <= 0 || offset >= clen
      count = clen if count > clen
      headers[Content_Length] = count.to_s
      headers[Content_Range] = "bytes #{offset}-#{offset+count-1}/#{clen}"
      [ status, offset, count ]
    end
    # nil if no status
  end
end

git clone https://yhbt.net/rainbows.git