Sha256: 0870652ab90dcb4b63c008815ce6d6dfe590e8bff86e54e721debff9940bda38

Contents?: true

Size: 1.23 KB

Versions: 17

Compression:

Stored size: 1.23 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
          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

17 entries across 15 versions & 3 rubygems

Version Path
candlepin-api-0.4.0 bundle/ruby/1.9.1/gems/rack-1.3.5/lib/rack/chunked.rb
candlepin-api-0.4.0 bundle/ruby/gems/rack-1.3.5/lib/rack/chunked.rb
candlepin-api-0.4.0 bundle/ruby/1.8/gems/rack-1.3.5/lib/rack/chunked.rb
rack-1.3.10 lib/rack/chunked.rb
rack-1.3.9 lib/rack/chunked.rb
rack-1.3.8 lib/rack/chunked.rb
rack-1.3.7 lib/rack/chunked.rb
rack-1.3.6 lib/rack/chunked.rb
rack-1.3.5 lib/rack/chunked.rb
rack-1.3.4 lib/rack/chunked.rb
rack-1.3.3 lib/rack/chunked.rb
rack-1.3.2 lib/rack/chunked.rb
rack-1.3.1 lib/rack/chunked.rb
search_biomodel-1.0.0 search_biomodel/ruby/1.8/gems/rack-1.3.0/lib/rack/chunked.rb
rack-1.3.0 lib/rack/chunked.rb
rack-1.3.0.beta2 lib/rack/chunked.rb
rack-1.3.0.beta lib/rack/chunked.rb