Class: HTTP::Response::Parser Private
- Inherits:
-
Object
- Object
- HTTP::Response::Parser
- 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
-
#headers ⇒ HTTP::Headers
readonly
private
The parsed response headers.
-
#http_version ⇒ String?
readonly
private
The parsed HTTP version string.
-
#parser ⇒ LLHttp::Parser
readonly
private
The underlying LLHttp parser.
-
#status_code ⇒ Integer?
readonly
private
The parsed HTTP status code.
Instance Method Summary collapse
-
#add(data) ⇒ Parser
(also: #<<)
private
Feed data into the parser.
-
#add_body(chunk) ⇒ void
private
Append a body chunk to the buffer.
-
#add_header(name, value) ⇒ void
private
Add a parsed header field and value.
-
#finished? ⇒ Boolean
private
Check if the full message has been parsed.
-
#headers? ⇒ Boolean
private
Check if headers have been parsed.
-
#initialize ⇒ Parser
constructor
private
Create a new response parser.
-
#mark_header_finished ⇒ void
private
Mark headers as finished.
-
#mark_message_finished ⇒ void
private
Mark the message as fully parsed.
-
#read(size) ⇒ String?
private
Read up to size bytes from the body buffer.
-
#reset ⇒ void
private
Reset parser to initial state.
-
#reset_for_informational ⇒ void
private
Reset parser state for informational (1xx) responses.
Constructor Details
#initialize ⇒ Parser
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
#headers ⇒ HTTP::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
18 19 20 |
# File 'lib/http/response/parser.rb', line 18 def headers @headers end |
#http_version ⇒ String? (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
28 29 30 |
# File 'lib/http/response/parser.rb', line 28 def http_version @http_version end |
#parser ⇒ LLHttp::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
13 14 15 |
# File 'lib/http/response/parser.rb', line 13 def parser @parser end |
#status_code ⇒ Integer? (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
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
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. 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
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
93 94 95 |
# File 'lib/http/response/parser.rb', line 93 def headers? @header_finished end |
#mark_header_finished ⇒ 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.
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_finished ⇒ 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.
Mark the message as fully parsed
107 108 109 |
# File 'lib/http/response/parser.rb', line 107 def @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
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 |
#reset ⇒ 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.
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_informational ⇒ 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.
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 |