Class: LLHttp::Parser
- Inherits:
-
Object
- Object
- LLHttp::Parser
- Defined in:
- lib/llhttp/parser.rb
Overview
- public
-
Wraps an llhttp context for parsing http requests and responses.
class Delegate < LLHttp::Delegate
def on_message_begin
...
end
...
end
parser = LLHttp::Parser.new(Delegate.new, type: :request)
parser << "GET / HTTP/1.1\r\n\r\n"
parser.finish
...
Introspection
* `LLHttp::Parser#content_length` returns the content length of the current request.
* `LLHttp::Parser#method_name` returns the method name of the current response.
* `LLHttp::Parser#status_code` returns the status code of the current response.
* `LLHttp::Parser#http_major` returns the major http version of the current request/response.
* `LLHttp::Parser#http_minor` returns the minor http version of the current request/response.
* `LLHttp::Parser#keep_alive?` returns `true` if there might be more messages.
Finishing
Call `LLHttp::Parser#finish` when processing is complete for the current request or response.
Constant Summary collapse
- LLHTTP_TYPES =
{both: 0, request: 1, response: 2}.freeze
- CALLBACKS =
%i[ on_message_begin on_headers_complete on_message_complete on_chunk_header on_chunk_complete on_url_complete on_status_complete on_header_field_complete on_header_value_complete ].freeze
- CALLBACKS_WITH_DATA =
%i[ on_url on_status on_header_field on_header_value on_body ].freeze
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
- public
-
The parser type; one of: ‘:both`, `:request`, or `:response`.
Class Method Summary collapse
Instance Method Summary collapse
-
#content_length ⇒ Object
- public
-
Get the content length of the current request.
-
#finish ⇒ Object
- public
-
Tells the parser we are finished.
-
#http_major ⇒ Object
- public
-
Get the major http version of the current request/response.
-
#http_minor ⇒ Object
- public
-
Get the minor http version of the current request/response.
-
#initialize(delegate, type: :both) ⇒ Parser
constructor
A new instance of Parser.
-
#keep_alive? ⇒ Boolean
- public
-
Returns ‘true` if there might be more messages.
-
#method_name ⇒ Object
- public
-
Get the method of the current response.
-
#parse(data) ⇒ Object
(also: #<<)
- public
-
Parse the given data.
-
#reset ⇒ Object
- public
-
Get ready to parse the next request/response.
-
#status_code ⇒ Object
- public
-
Get the status code of the current response.
Constructor Details
#initialize(delegate, type: :both) ⇒ Parser
Returns a new instance of Parser.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/llhttp/parser.rb', line 60 def initialize(delegate, type: :both) @type, @delegate = type.to_sym, delegate @callbacks = Callbacks.new (CALLBACKS + CALLBACKS_WITH_DATA).each do |callback| if delegate.respond_to?(callback) @callbacks[callback] = method(callback).to_proc end end @pointer = LLHttp.rb_llhttp_init(LLHTTP_TYPES.fetch(@type), @callbacks) ObjectSpace.define_finalizer(self, self.class.free(@pointer)) end |
Instance Attribute Details
#type ⇒ Object (readonly)
- public
-
The parser type; one of: ‘:both`, `:request`, or `:response`.
58 59 60 |
# File 'lib/llhttp/parser.rb', line 58 def type @type end |
Class Method Details
.free(pointer) ⇒ Object
160 161 162 |
# File 'lib/llhttp/parser.rb', line 160 def self.free(pointer) proc { LLHttp.rb_llhttp_free(pointer) } end |
Instance Method Details
#content_length ⇒ Object
- public
-
Get the content length of the current request.
86 87 88 |
# File 'lib/llhttp/parser.rb', line 86 def content_length LLHttp.rb_llhttp_content_length(@pointer) end |
#finish ⇒ Object
- public
-
Tells the parser we are finished.
122 123 124 |
# File 'lib/llhttp/parser.rb', line 122 def finish LLHttp.llhttp_finish(@pointer) end |
#http_major ⇒ Object
- public
-
Get the major http version of the current request/response.
104 105 106 |
# File 'lib/llhttp/parser.rb', line 104 def http_major LLHttp.rb_llhttp_http_major(@pointer) end |
#http_minor ⇒ Object
- public
-
Get the minor http version of the current request/response.
110 111 112 |
# File 'lib/llhttp/parser.rb', line 110 def http_minor LLHttp.rb_llhttp_http_minor(@pointer) end |
#keep_alive? ⇒ Boolean
- public
-
Returns ‘true` if there might be more messages.
116 117 118 |
# File 'lib/llhttp/parser.rb', line 116 def keep_alive? LLHttp.llhttp_should_keep_alive(@pointer) == 1 end |
#method_name ⇒ Object
- public
-
Get the method of the current response.
92 93 94 |
# File 'lib/llhttp/parser.rb', line 92 def method_name LLHttp.rb_llhttp_method_name(@pointer) end |
#parse(data) ⇒ Object Also known as: <<
- public
-
Parse the given data.
78 79 80 81 |
# File 'lib/llhttp/parser.rb', line 78 def parse(data) errno = LLHttp.llhttp_execute(@pointer, data, data.length) raise build_error(errno) if errno > 0 end |
#reset ⇒ Object
- public
-
Get ready to parse the next request/response.
128 129 130 |
# File 'lib/llhttp/parser.rb', line 128 def reset LLHttp.llhttp_reset(@pointer) end |
#status_code ⇒ Object
- public
-
Get the status code of the current response.
98 99 100 |
# File 'lib/llhttp/parser.rb', line 98 def status_code LLHttp.rb_llhttp_status_code(@pointer) end |