Class: HTTP::Response::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/http/response/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

HTTP response parser backed by LLHttp

Defined Under Namespace

Classes: Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new response parser



33
34
35
36
37
# File 'lib/http/response/parser.rb', line 33

def initialize
  @handler = Handler.new(self)
  @parser = LLHttp::Parser.new(@handler, type: :response)
  reset
end

Instance Attribute Details

#headersHTTP::Headers (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed response headers

Returns:



18
19
20
# File 'lib/http/response/parser.rb', line 18

def headers
  @headers
end

#http_versionString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed HTTP version string

Returns:

  • (String, nil)

    the parsed HTTP version



28
29
30
# File 'lib/http/response/parser.rb', line 28

def http_version
  @http_version
end

#parserLLHttp::Parser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The underlying LLHttp parser

Returns:

  • (LLHttp::Parser)

    the underlying parser



13
14
15
# File 'lib/http/response/parser.rb', line 13

def parser
  @parser
end

#status_codeInteger? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed HTTP status code

Returns:

  • (Integer, nil)

    the parsed status code



23
24
25
# File 'lib/http/response/parser.rb', line 23

def status_code
  @status_code
end

Instance Method Details

#add(data) ⇒ Parser Also known as: <<

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Feed data into the parser

Returns:



56
57
58
59
60
61
62
# File 'lib/http/response/parser.rb', line 56

def add(data)
  parser << data

  self
rescue LLHttp::Error => e
  raise IOError, e.message
end

#add_body(chunk) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Append a body chunk to the buffer



121
122
123
124
125
126
127
# File 'lib/http/response/parser.rb', line 121

def add_body(chunk)
  if @chunk
    @chunk << chunk
  else
    @chunk = chunk
  end
end

#add_header(name, value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Add a parsed header field and value



100
101
102
# File 'lib/http/response/parser.rb', line 100

def add_header(name, value)
  @headers.add(name, value)
end

#finished?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the full message has been parsed

Returns:

  • (Boolean)


114
115
116
# File 'lib/http/response/parser.rb', line 114

def finished?
  @message_finished
end

#headers?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if headers have been parsed

Returns:

  • (Boolean)


93
94
95
# File 'lib/http/response/parser.rb', line 93

def headers?
  @header_finished
end

#mark_header_finishedvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Mark headers as finished



84
85
86
87
88
# File 'lib/http/response/parser.rb', line 84

def mark_header_finished
  @header_finished = true
  @status_code = @parser.status_code
  @http_version = "#{@parser.http_major}.#{@parser.http_minor}"
end

#mark_message_finishedvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Mark the message as fully parsed



107
108
109
# File 'lib/http/response/parser.rb', line 107

def mark_message_finished
  @message_finished = true
end

#read(size) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read up to size bytes from the body buffer

Returns:

  • (String, nil)


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/http/response/parser.rb', line 132

def read(size)
  return if @chunk.nil?

  if @chunk.bytesize <= size
    chunk  = @chunk
    @chunk = nil
  else
    chunk = @chunk.byteslice(0, size)
    @chunk[0, size] = ""
  end

  chunk
end

#resetvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset parser to initial state



42
43
44
45
46
47
48
49
50
51
# File 'lib/http/response/parser.rb', line 42

def reset
  @parser.reset
  @handler.reset
  @header_finished = false
  @message_finished = false
  @headers = Headers.new
  @chunk = nil
  @status_code = nil
  @http_version = nil
end

#reset_for_informationalvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Reset parser state for informational (1xx) responses



71
72
73
74
75
76
77
78
79
# File 'lib/http/response/parser.rb', line 71

def reset_for_informational
  @handler.reset
  @header_finished = false
  @message_finished = false
  @headers = Headers.new
  @chunk = nil
  @status_code = nil
  @http_version = nil
end