Sha256: 9c1162a04e623dda0409672615fa49420a4943fd55836465de2a6d9eac9ed130

Contents?: true

Size: 1.58 KB

Versions: 98

Compression:

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

    # pre-HTTP/1.0 (informally "HTTP/0.9") HTTP requests did not have
    # a version (nor response headers)
    def chunkable_version?(ver)
      case ver
      when "HTTP/1.0", nil, "HTTP/0.9"
        false
      else
        true
      end
    end

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

      if ! chunkable_version?(env['HTTP_VERSION']) ||
         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

98 entries across 90 versions & 23 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/rack-1.6.6/lib/rack/chunked.rb