Sha256: d20775d88e7b6b4ad0836d1354fdee1ed82f14c1dbfa31997c8c33055f6eab94

Contents?: true

Size: 1.33 KB

Versions: 203

Compression:

Stored size: 1.33 KB

Contents

require 'rack/utils'

module Rack

  # Middleware that applies chunked transfer encoding to response bodies
  # when the response does not include a Content-Length header.
  class Chunked
    include Rack::Utils

    # A body wrapper that emits chunked responses
    class Body
      TERM = "\r\n"
      TAIL = "0#{TERM}#{TERM}"

      include Rack::Utils

      def initialize(body)
        @body = body
      end

      def each
        term = TERM
        @body.each do |chunk|
          size = bytesize(chunk)
          next if size == 0

          chunk = chunk.dup.force_encoding(Encoding::BINARY) if chunk.respond_to?(:force_encoding)
          yield [size.to_s(16), term, chunk, term].join
        end
        yield TAIL
      end

      def close
        @body.close if @body.respond_to?(:close)
      end
    end

    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)
      headers = HeaderHash.new(headers)

      if env['HTTP_VERSION'] == 'HTTP/1.0' ||
         STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
         headers['Content-Length'] ||
         headers['Transfer-Encoding']
        [status, headers, body]
      else
        headers.delete('Content-Length')
        headers['Transfer-Encoding'] = 'chunked'
        [status, headers, Body.new(body)]
      end
    end
  end
end

Version data entries

203 entries across 173 versions & 32 rubygems

Version Path
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
classiccms-0.7.0 vendor/bundle/gems/rack-1.4.1/lib/rack/chunked.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/rack-1.4.5/lib/rack/chunked.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/rack-1.4.5/lib/rack/chunked.rb
challah-1.0.0.beta3 vendor/bundle/gems/rack-1.4.5/lib/rack/chunked.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
challah-1.0.0.beta2 vendor/bundle/gems/rack-1.4.5/lib/rack/chunked.rb
challah-1.0.0.beta vendor/bundle/gems/rack-1.4.3/lib/rack/chunked.rb
challah-1.0.0.beta vendor/bundle/gems/rack-1.4.5/lib/rack/chunked.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
torquebox-console-0.2.5 vendor/bundle/jruby/1.9/gems/rack-1.5.2/lib/rack/chunked.rb
torquebox-console-0.2.5 vendor/bundle/ruby/1.8/gems/rack-1.5.2/lib/rack/chunked.rb
vagrant-actionio-0.0.9 vendor/bundle/gems/rack-1.5.2/lib/rack/chunked.rb