Class: LLHttp::Parser

Inherits:
Object
  • Object
show all
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.

Resetting

Call `LLHttp::Parser#reset` to reset the parser for the next request or response.

Constant Summary collapse

LLHTTP_TYPES =
{both: 0, request: 1, response: 2}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, type: :both) ⇒ Parser

Returns a new instance of Parser.



44
45
46
47
48
# File 'lib/llhttp/parser.rb', line 44

def initialize(delegate, type: :both)
  @type, @delegate = type.to_sym, delegate

  llhttp_init(LLHTTP_TYPES.fetch(@type))
end

Instance Attribute Details

#typeObject (readonly)

public

The parser type; one of: ‘both`, `request`, or `response`.



42
43
44
# File 'lib/llhttp/parser.rb', line 42

def type
  @type
end