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
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/chunked.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/chunked.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/rack-1.5.5/lib/rack/chunked.rb
classiccms-0.7.5 vendor/bundle/gems/rack-1.4.1/lib/rack/chunked.rb
classiccms-0.7.4 vendor/bundle/gems/rack-1.4.1/lib/rack/chunked.rb
classiccms-0.7.3 vendor/bundle/gems/rack-1.4.1/lib/rack/chunked.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/chunked.rb
rack-1.4.7 lib/rack/chunked.rb
rack-1.5.5 lib/rack/chunked.rb
rack-1.4.6 lib/rack/chunked.rb
rack-1.5.4 lib/rack/chunked.rb
rack-1.5.3 lib/rack/chunked.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rack-1.4.5/lib/rack/chunked.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/chunked.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/rack-1.5.2/lib/rack/chunked.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/chunked.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/chunked.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/chunked.rb
scout_realtime-1.0.5 lib/vendor/rack-1.5.2/lib/rack/chunked.rb
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/rack-1.5.2/lib/rack/chunked.rb