Class: HttpParser::Parser

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

Defined Under Namespace

Classes: Callback, DataCallback

Constant Summary collapse

CALLBACKS =
[
    :on_message_begin, :on_url, :on_status, :on_header_field, :on_header_value,
    :on_headers_complete, :on_body, :on_message_complete, :on_chunk_header, :on_chunk_complete
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(callback_obj = nil) {|_self| ... } ⇒ Parser

Initializes the Parser instance.

Yields:

  • (_self)

Yield Parameters:



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/http-parser/parser.rb', line 21

def initialize(callback_obj = nil)
    @settings = ::HttpParser::Settings.new
    @callbacks = {} # so GC doesn't clean them up on java

    if not callback_obj.nil?
        CALLBACKS.each do |callback|
            self.__send__(callback, &callback_obj.method(callback)) if callback_obj.respond_to? callback
        end
    end

    yield self if block_given?
end

Class Method Details

.new_instance(&block) ⇒ Object

Returns a new request/response instance variable



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

def self.new_instance &block
    ::HttpParser::Instance.new &block
end

Instance Method Details

#on_body {|instance, body| ... } ⇒ Object

Registers an ‘on_body` callback.

Yields:

  • (instance, body)

    The given block will be called when the body is recognized in the message body.

Yield Parameters:

  • instance (HttpParser::Instance)

    The state so far of the request / response being processed.

  • body (String)

    The full body or a chunk of the body from a chunked Transfer-Encoded stream.

See Also:



158
159
160
161
162
# File 'lib/http-parser/parser.rb', line 158

def on_body(&block)
    cb = DataCallback.new(&block)
    @callbacks[:on_body] = cb
    @settings[:on_body] = cb
end

#on_chunk_complete {|instance| ... } ⇒ Object

Registers an ‘on_chunk_complete` callback.

Yields:

  • (instance)

    The given block will be called when the current chunk completes.

Yield Parameters:



203
204
205
206
207
# File 'lib/http-parser/parser.rb', line 203

def on_chunk_complete(&block)
    cb = Callback.new(&block)
    @callbacks[:on_message_complete] = cb
    @settings[:on_message_complete] = cb
end

#on_chunk_header {|instance| ... } ⇒ Object

Registers an ‘on_chunk_header` callback.

Yields:

  • (instance)

    The given block will be called when a new chunk header is received.

Yield Parameters:



188
189
190
191
192
# File 'lib/http-parser/parser.rb', line 188

def on_chunk_header(&block)
    cb = Callback.new(&block)
    @callbacks[:on_message_complete] = cb
    @settings[:on_message_complete] = cb
end

#on_header_field {|instance, field| ... } ⇒ Object

Registers an ‘on_header_field` callback.

Yields:

  • (instance, field)

    The given block will be called when a Header name is recognized in the Headers.

Yield Parameters:

  • instance (HttpParser::Instance)

    The state so far of the request / response being processed.

  • field (String)

    A recognized Header name.

See Also:



100
101
102
103
104
# File 'lib/http-parser/parser.rb', line 100

def on_header_field(&block)
    cb = DataCallback.new(&block)
    @callbacks[:on_header_field] = cb
    @settings[:on_header_field] = cb
end

#on_header_value {|instance, value| ... } ⇒ Object

Registers an ‘on_header_value` callback.

Yields:

  • (instance, value)

    The given block will be called when a Header value is recognized in the Headers.

Yield Parameters:

  • instance (HttpParser::Instance)

    The state so far of the request / response being processed.

  • value (String)

    A recognized Header value.

See Also:



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

def on_header_value(&block)
    cb = DataCallback.new(&block)
    @callbacks[:on_header_value] = cb
    @settings[:on_header_value] = cb
end

#on_headers_complete {|instance| ... } ⇒ Object

Registers an ‘on_headers_complete` callback.

Yields:

  • (instance)

    The given block will be called when the Headers stop.

Yield Parameters:



136
137
138
139
140
# File 'lib/http-parser/parser.rb', line 136

def on_headers_complete(&block)
    cb = Callback.new(&block)
    @callbacks[:on_headers_complete] = cb
    @settings[:on_headers_complete] = cb
end

#on_message_begin {|instance| ... } ⇒ Object

Registers an ‘on_message_begin` callback.

Yields:

  • (instance)

    The given block will be called when the HTTP message begins.

Yield Parameters:



43
44
45
46
47
# File 'lib/http-parser/parser.rb', line 43

def on_message_begin(&block)
    cb = Callback.new(&block)
    @callbacks[:on_message_begin] = cb
    @settings[:on_message_begin] = cb
end

#on_message_complete {|instance| ... } ⇒ Object

Registers an ‘on_message_begin` callback.

Yields:

  • (instance)

    The given block will be called when the message completes.

Yield Parameters:



173
174
175
176
177
# File 'lib/http-parser/parser.rb', line 173

def on_message_complete(&block)
    cb = Callback.new(&block)
    @callbacks[:on_message_complete] = cb
    @settings[:on_message_complete] = cb
end

#on_status {|instance| ... } ⇒ Object

Registers an ‘on_status_complete` callback.

Yields:

  • (instance)

    The given block will be called when the status is recognized.

Yield Parameters:



79
80
81
82
83
# File 'lib/http-parser/parser.rb', line 79

def on_status(&block)
    cb = DataCallback.new(&block)
    @callbacks[:on_status] = cb
    @settings[:on_status] = cb
end

#on_url {|instance, url| ... } ⇒ Object

Registers an ‘on_url` callback.

Yields:

  • (instance, url)

    The given block will be called when the Request URI is recognized within the Request-Line.

Yield Parameters:

  • instance (HttpParser::Instance)

    The state so far of the request / response being processed.

  • url (String)

    The recognized Request URI.

See Also:



64
65
66
67
68
# File 'lib/http-parser/parser.rb', line 64

def on_url(&block)
    cb = DataCallback.new(&block)
    @callbacks[:on_url] = cb
    @settings[:on_url] = cb
end

#parse(inst, data) ⇒ Boolean

Parses data.

Parameters:

  • inst (HttpParser::Instance)

    The state so far of the request / response being processed.

  • data (String)

    The data to parse against the instance specified.

Returns:

  • (Boolean)

    Returns true if the data was parsed successfully.



221
222
223
224
# File 'lib/http-parser/parser.rb', line 221

def parse(inst, data)
    ::HttpParser.http_parser_execute(inst, @settings, data, data.length)
    return inst.error?
end