Class: Racknga::Middleware::Range — racknga - Ranguba

Class: Racknga::Middleware::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/racknga/middleware/range.rb

Overview

This is a middleware that provides HTTP range request (partial request) support. For example, HTTP range request is used for playing a video on the way.

Usage:

require "racknga"
use Racknga::Middleware::Range
run YourApplication

Defined Under Namespace

Classes: RangeStream

Instance Method Summary (collapse)

Constructor Details

- (Range) initialize(application)

A new instance of Range



32
33
34
# File 'lib/racknga/middleware/range.rb', line 32

def initialize(application)
  @application = application
end

Instance Method Details

- (Object) call(environment)

For Rack.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/racknga/middleware/range.rb', line 37

def call(environment)
  status, headers, body = @application.call(environment)
  return [status, headers, body] if status != 200

  headers = Rack::Utils::HeaderHash.new(headers)
  headers["Accept-Ranges"] = "bytes"
  request = Rack::Request.new(environment)
  range = request.env["HTTP_RANGE"]
  if range and /\Abytes=(\d*)-(\d*)\z/ =~ range
    first_byte, last_byte = $1, $2
    status, headers, body = apply_range(status, headers, body, request,
                                        first_byte, last_byte)
  end
  [status, headers.to_hash, body]
end