File: streamer.rb

package info (click to toggle)
ruby-webmock 3.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,196 kB
  • sloc: ruby: 12,905; makefile: 6
file content (44 lines) | stat: -rw-r--r-- 932 bytes parent folder | download
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
# frozen_string_literal: true

module HTTP
  class Response
    class Streamer
      def initialize(str, encoding: Encoding::BINARY)
        @io = StringIO.new str
        @encoding = encoding
      end

      def readpartial(size = nil, outbuf = nil)
        unless size
          if defined?(HTTP::Connection::BUFFER_SIZE)
            size = HTTP::Connection::BUFFER_SIZE
          elsif defined?(HTTP::Client::BUFFER_SIZE)
            size = HTTP::Client::BUFFER_SIZE
          end
        end

        chunk = @io.read(size, outbuf)

        # HTTP.rb 6.0+ expects EOFError at end-of-stream instead of nil
        if chunk.nil?
          raise EOFError if HTTP::VERSION >= "6.0.0"
          return nil
        end

        chunk.force_encoding(@encoding)
      end

      def close
        @io.close
      end

      def finished_request?
        @io.eof?
      end

      def sequence_id
        -1
      end
    end
  end
end