Sha256: abd9f7f9539e8a34ce00d1a8322f32664aaa6b66e3673d5d1f4e69ccfcdd1d9e

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'em/streamer'

# similar to EventMachine::FileStreamer, but for any IO object
module EventMachine
  class IOStreamer
    include Deferrable
    CHUNK_SIZE = 16384

    # @param [EventMachine::Connection] connection
    # @param [IO] io Data source
    # @param [Integer] Data size
    #
    # @option opts [Boolean] :http_chunks (false) Use HTTP 1.1 style chunked-encoding semantics.
    def initialize(connection, io, opts = {})
      @connection = connection
      @io = io
      @http_chunks = opts[:http_chunks]

      @buff = String.new
      @io.binmode if @io.respond_to?(:binmode)
      stream_one_chunk
    end

  private

    # Used internally to stream one chunk at a time over multiple reactor ticks
    # @private
    def stream_one_chunk
      loop do
        if @io.eof?
          @connection.send_data "0\r\n\r\n" if @http_chunks
          succeed
          break
        end

        if @connection.respond_to?(:get_outbound_data_size) && (@connection.get_outbound_data_size > FileStreamer::BackpressureLevel)
          EventMachine::next_tick { stream_one_chunk }
          break
        end

        if @io.read(CHUNK_SIZE, @buff)
          @connection.send_data("#{@buff.length.to_s(16)}\r\n") if @http_chunks
          @connection.send_data(@buff)
          @connection.send_data("\r\n") if @http_chunks
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
em-http-request-samesite-0.1.1 lib/em/io_streamer.rb
em-http-request-samesite-1.1.7 lib/em/io_streamer.rb
em-http-request-1.1.7 lib/em/io_streamer.rb
em-http-request-1.1.6 lib/em/io_streamer.rb