Class: Http2::ResponseReader

Inherits:
Object
  • Object
show all
Defined in:
lib/http2/response_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args:, http2:, sock:, request:) ⇒ ResponseReader

Returns a new instance of ResponseReader.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/http2/response_reader.rb', line 4

def initialize(args:, http2:, sock:, request:)
  @mode = "headers"
  @transfer_encoding = nil
  @request = request
  @response = Http2::Response.new(debug: http2.debug, request: request)
  @rec_count = 0
  @args = args
  @debug = http2.debug
  @http2 = http2
  @sock = sock
  @nl = @http2.nl
  @conn = @http2.connection

  read_headers
  read_body if @length == nil || @length.positive?
  finish
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



2
3
4
# File 'lib/http2/response_reader.rb', line 2

def response
  @response
end

Instance Method Details

#finishObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/http2/response_reader.rb', line 56

def finish
  # Check if we should reconnect based on keep-alive-max.
  @conn.close if !@conn.closed? && (@keepalive_max == 1 || @connection == "close")

  # Validate that the response is as it should be.
  puts "Http2: Validating response." if @debug

  raise "No status-code was received from the server. Headers: '#{@response.headers}' Body: '#{@response.body}'." unless @response.code

  @response.validate!
  check_and_decode
  @http2.autostate_register(@response) if @http2.args[:autostate]
  handle_errors

  if (response = check_and_follow_redirect)
    @response = response
  end
end

#read_bodyObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/http2/response_reader.rb', line 40

def read_body
  loop do
    if @length
      line = @conn.read(@length)
      raise "Expected to get #{@length} of bytes but got #{line.bytesize}" if @length != line.bytesize
    else
      line = @conn.gets
    end

    check_line_read(line)
    stat = parse_body(line)
    break if stat == :break
    next if stat == :next
  end
end

#read_headersObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/http2/response_reader.rb', line 22

def read_headers
  loop do
    line = @conn.gets
    check_line_read(line)

    if line == "\n" || line == "\r\n" || line == @nl
      puts "Http2: Changing mode to body!" if @debug
      raise "No headers was given at all? Possibly corrupt state after last request?" if @response.headers.empty?

      @mode = "body"
      @http2.on_content_call(@args, @nl)
      break
    end

    parse_header(line)
  end
end