rack.git  about / heads / tags
a modular Ruby webserver interface
blob b66f467b9dd2ba4891a94127c63f3bbdf7ef08b5 3155 bytes (raw)
$ git show chunk:lib/rack/chunked.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
 
# frozen_string_literal: true

require_relative 'constants'
require_relative 'utils'

module Rack
  # Middleware that applies chunked transfer encoding to response bodies
  # when the response does not include a content-length header.
  #
  # This supports the trailer response header to allow the use of trailing
  # headers in the chunked encoding.  However, using this requires you manually
  # specify a response body that supports a +trailers+ method.  Example:
  #
  #   [200, { 'trailer' => 'expires'}, ["Hello", "World"]]
  #   # error raised
  #
  #   body = ["Hello", "World"]
  #   def body.trailers
  #     { 'expires' => Time.now.to_s }
  #   end
  #   [200, { 'trailer' => 'expires'}, body]
  #   # No exception raised
  class Chunked
    include Rack::Utils

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

      # Store the response body to be chunked.
      def initialize(body)
        @body = body
      end

      # For each element yielded by the response body, yield
      # the element in chunked encoding.
      def each(&block)
        term = TERM
        @body.each do |chunk|
          size = chunk.bytesize
          next if size == 0

          yield [size.to_s(16), term, chunk.b, term].join
        end
        yield TAIL
        yield_trailers(&block)
        yield term
      end

      # Close the response body if the response body supports it.
      def close
        @body.close if @body.respond_to?(:close)
      end

      private

      # Do nothing as this class does not support trailer headers.
      def yield_trailers
      end
    end

    # A body wrapper that emits chunked responses and also supports
    # sending Trailer headers.  Note that the response body provided to
    # initialize must have a +trailers+ method that returns a hash
    # of trailer headers, and the rack response itself should have a
    # Trailer header listing the headers that the +trailers+ method
    # will return.
    class TrailerBody < Body
      private

      # Yield strings for each trailer header.
      def yield_trailers
        @body.trailers.each_pair do |k, v|
          yield "#{k}: #{v}\r\n"
        end
      end
    end

    def initialize(app)
      @app = app
    end

    # Whether the HTTP version supports chunked encoding (only HTTP 1.1 does).
    def chunkable_version?(ver)
      ver == 'HTTP/1.1' # HTTP/2 doesn't, and HTTP/1.2 is unlikely
    end

    # If the rack app returns a response that should have a body,
    # but does not have content-length or transfer-encoding headers,
    # modify the response to use chunked transfer-encoding.
    def call(env)
      status, headers, body = response = @app.call(env)

      if chunkable_version?(env[SERVER_PROTOCOL]) &&
         !STATUS_WITH_NO_ENTITY_BODY.key?(status.to_i) &&
         !headers[CONTENT_LENGTH] &&
         !headers[TRANSFER_ENCODING]

        headers[TRANSFER_ENCODING] = 'chunked'
        if headers['trailer']
          response[2] = TrailerBody.new(body)
        else
          response[2] = Body.new(body)
        end
      end

      response
    end
  end
end

git clone https://yhbt.net/rack.git