Class: FFI::HTTP::Parser::Instance

Inherits:
Struct
  • Object
show all
Defined in:
lib/ffi/http/parser/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr = nil) {|_self| ... } ⇒ Instance

Initializes the Parser instance.

Parameters:

  • ptr (FFI::Pointer) (defaults to: nil)

    Optional pointer to an existing http_parser struct.

Yields:

  • (_self)

Yield Parameters:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ffi/http/parser/instance.rb', line 44

def initialize(ptr=nil)
  if ptr then super(ptr)
  else
    super()

    self.type = :both
  end

  @settings = Settings.new

  yield self if block_given?

  Parser.http_parser_init(self,type) unless ptr
end

Instance Attribute Details

#type:request, ...

The type of the parser.

Returns:

  • (:request, :response, :both)

    The parser type.



36
37
38
# File 'lib/ffi/http/parser/instance.rb', line 36

def type
  @type
end

Instance Method Details

#<<(data) ⇒ Instance

Parses data.

Parameters:

  • data (String)

    The data to parse.

Returns:



231
232
233
234
# File 'lib/ffi/http/parser/instance.rb', line 231

def <<(data)
  parse(data)
  return self
end

#dataFFI::Pointer

Additional data attached to the parser.

Returns:

  • (FFI::Pointer)

    Pointer to the additional data.



350
351
352
# File 'lib/ffi/http/parser/instance.rb', line 350

def data
  self[:data]
end

#flagsInteger

Flags for the parser.

Returns:

  • (Integer)

    Parser flags.



274
275
276
# File 'lib/ffi/http/parser/instance.rb', line 274

def flags
  (self[:type_flags] & 0xfc) >> 2
end

#http_majorInteger

The parsed HTTP major version number.

Returns:

  • (Integer)

    The HTTP major version number.



284
285
286
# File 'lib/ffi/http/parser/instance.rb', line 284

def http_major
  self[:http_major]
end

#http_methodSymbol

The parsed HTTP Method.

Returns:

  • (Symbol)

    The HTTP Method name.

See Also:



328
329
330
# File 'lib/ffi/http/parser/instance.rb', line 328

def http_method
  METHODS[self[:method]]
end

#http_minorInteger

The parsed HTTP minor version number.

Returns:

  • (Integer)

    The HTTP minor version number.



294
295
296
# File 'lib/ffi/http/parser/instance.rb', line 294

def http_minor
  self[:http_minor]
end

#http_statusInteger

The parsed HTTP response Status Code.

Returns:

  • (Integer)

    The HTTP Status Code.

See Also:



316
317
318
# File 'lib/ffi/http/parser/instance.rb', line 316

def http_status
  self[:status_code]
end

#http_versionString

The parsed HTTP version.

Returns:

  • (String)

    The HTTP version.



304
305
306
# File 'lib/ffi/http/parser/instance.rb', line 304

def http_version
  "%d.%d" % [self[:http_major], self[:http_minor]]
end

#keep_alive?Boolean

Determines whether the Connection: keep-alive header has been parsed.

Returns:

  • (Boolean)

    Specifies whether the Connection should be kept alive.

See Also:



363
364
365
# File 'lib/ffi/http/parser/instance.rb', line 363

def keep_alive?
  Parser.http_should_keep_alive(self) > 0
end

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

Registers an on_body callback.

Yields:

  • (body)

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

Yield Parameters:

  • body (String)

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

See Also:



194
195
196
# File 'lib/ffi/http/parser/instance.rb', line 194

def on_body(&block)
  @settings[:on_body] = wrap_data_callback(block)
end

#on_fragment {|fragment| ... } ⇒ Object

Registers an on_fragment callback.

Yields:

  • (fragment)

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

Yield Parameters:

  • fragment (String)

    The recognized URI fragment.

See Also:



113
114
115
# File 'lib/ffi/http/parser/instance.rb', line 113

def on_fragment(&block)
  @settings[:on_fragment] = wrap_data_callback(block)
end

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

Registers an on_header_field callback.

Yields:

  • (field)

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

Yield Parameters:

  • field (String)

    A recognized Header name.

See Also:



145
146
147
# File 'lib/ffi/http/parser/instance.rb', line 145

def on_header_field(&block)
  @settings[:on_header_field] = wrap_data_callback(block)
end

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

Registers an on_header_value callback.

Yields:

  • (value)

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

Yield Parameters:

  • value (String)

    A recognized Header value.

See Also:



161
162
163
# File 'lib/ffi/http/parser/instance.rb', line 161

def on_header_value(&block)
  @settings[:on_header_value] = wrap_data_callback(block)
end

#on_headers_complete { ... } ⇒ Object

Registers an on_headers_complete callback.

Yields:

  • [] The given block will be called when the Headers stop.



171
172
173
174
175
176
177
178
179
# File 'lib/ffi/http/parser/instance.rb', line 171

def on_headers_complete(&block)
  @settings[:on_headers_complete] = proc { |parser|
    case block.call()
    when :error then -1
    when :stop  then  1
    else              0
    end
  }
end

#on_message_begin { ... } ⇒ Object

Registers an on_message_begin callback.

Yields:

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



65
66
67
# File 'lib/ffi/http/parser/instance.rb', line 65

def on_message_begin(&block)
  @settings[:on_message_begin] = wrap_callback(block)
end

#on_message_complete { ... } ⇒ Object

Registers an on_message_begin callback.

Yields:

  • [] The given block will be called when the message completes.



204
205
206
# File 'lib/ffi/http/parser/instance.rb', line 204

def on_message_complete(&block)
  @settings[:on_message_complete] = wrap_callback(block)
end

#on_path {|path| ... } ⇒ Object

Registers an on_path callback.

Yields:

  • (path)

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

Yield Parameters:

  • path (String)

    The recognized URI path.

See Also:



81
82
83
# File 'lib/ffi/http/parser/instance.rb', line 81

def on_path(&block)
  @settings[:on_path] = wrap_data_callback(block)
end

#on_query_string {|query| ... } ⇒ Object

Registers an on_query_string callback.

Yields:

  • (query)

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

Yield Parameters:

  • query (String)

    The recognized URI query-string.

See Also:



97
98
99
# File 'lib/ffi/http/parser/instance.rb', line 97

def on_query_string(&block)
  @settings[:on_query_string] = wrap_data_callback(block)
end

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

Registers an on_url callback.

Yields:

  • (url)

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

Yield Parameters:

  • url (String)

    The recognized Request URI.

See Also:



129
130
131
# File 'lib/ffi/http/parser/instance.rb', line 129

def on_url(&block)
  @settings[:on_url] = wrap_data_callback(block)
end

#parse(data) ⇒ Integer

Parses data.

Parameters:

  • data (String)

    The data to parse.

Returns:

  • (Integer)

    The number of bytes parsed. 0 will be returned if the parser encountered an error.



218
219
220
# File 'lib/ffi/http/parser/instance.rb', line 218

def parse(data)
  Parser.http_parser_execute(self,@settings,data,data.length)
end

#reset!(new_type = self.type) ⇒ Object Also known as: reset

Resets the parser.

Parameters:

  • new_type (:request, :response, :both) (defaults to: self.type)

    The new type for the parser.



242
243
244
# File 'lib/ffi/http/parser/instance.rb', line 242

def reset!(new_type=self.type)
  Parser.http_parser_init(self,new_type)
end

#upgrade?Boolean

Determines whether the Upgrade header has been parsed.

Returns:

  • (Boolean)

    Specifies whether the Upgrade header has been seen.

See Also:



340
341
342
# File 'lib/ffi/http/parser/instance.rb', line 340

def upgrade?
  self[:upgrade] == 1
end

#wrap_callback(callback) ⇒ Proc (protected)

Wraps a callback, so if it returns :error, -1 will be returned. 0 will be returned by default.

Parameters:

  • callback (Proc)

    The callback to wrap.

Returns:

  • (Proc)

    The wrapped callback.



379
380
381
# File 'lib/ffi/http/parser/instance.rb', line 379

def wrap_callback(callback)
  proc { |parser| (callback.call() == :error) ? -1 : 0 }
end

#wrap_data_callback(callback) ⇒ Proc (protected)

Wraps a data callback, so if it returns :error, -1 will be returned. 0 will be returned by default.

Parameters:

  • callback (Proc)

    The callback to wrap.

Returns:

  • (Proc)

    The wrapped callback.



393
394
395
396
397
398
399
# File 'lib/ffi/http/parser/instance.rb', line 393

def wrap_data_callback(callback)
  proc { |parser,buffer,length|
    data = buffer.get_bytes(0,length)

    (callback.call(data) == :error) ? -1 : 0
  }
end