Class: FFI::HTTP::Parser::Instance
- Inherits:
-
Struct
- Object
- Struct
- FFI::HTTP::Parser::Instance
- Defined in:
- lib/ffi/http/parser/instance.rb
Instance Attribute Summary collapse
-
#type ⇒ :request, ...
The type of the parser.
Instance Method Summary collapse
-
#<<(data) ⇒ Instance
Parses data.
-
#data ⇒ FFI::Pointer
Additional data attached to the parser.
-
#flags ⇒ Integer
Flags for the parser.
-
#http_major ⇒ Integer
The parsed HTTP major version number.
-
#http_method ⇒ Symbol
The parsed HTTP Method.
-
#http_minor ⇒ Integer
The parsed HTTP minor version number.
-
#http_status ⇒ Integer
The parsed HTTP response Status Code.
-
#http_version ⇒ String
The parsed HTTP version.
-
#initialize(ptr = nil) {|_self| ... } ⇒ Instance
constructor
Initializes the Parser instance.
-
#keep_alive? ⇒ Boolean
Determines whether the
Connection: keep-alive
header has been parsed. -
#on_body {|body| ... } ⇒ Object
Registers an
on_body
callback. -
#on_fragment {|fragment| ... } ⇒ Object
Registers an
on_fragment
callback. -
#on_header_field {|field| ... } ⇒ Object
Registers an
on_header_field
callback. -
#on_header_value {|value| ... } ⇒ Object
Registers an
on_header_value
callback. -
#on_headers_complete { ... } ⇒ Object
Registers an
on_headers_complete
callback. -
#on_message_begin { ... } ⇒ Object
Registers an
on_message_begin
callback. -
#on_message_complete { ... } ⇒ Object
Registers an
on_message_begin
callback. -
#on_path {|path| ... } ⇒ Object
Registers an
on_path
callback. -
#on_query_string {|query| ... } ⇒ Object
Registers an
on_query_string
callback. -
#on_url {|url| ... } ⇒ Object
Registers an
on_url
callback. -
#parse(data) ⇒ Integer
Parses data.
-
#reset!(new_type = self.type) ⇒ Object
(also: #reset)
Resets the parser.
-
#upgrade? ⇒ Boolean
Determines whether the
Upgrade
header has been parsed. -
#wrap_callback(callback) ⇒ Proc
protected
Wraps a callback, so if it returns
:error
,-1
will be returned. -
#wrap_data_callback(callback) ⇒ Proc
protected
Wraps a data callback, so if it returns
:error
,-1
will be returned.
Constructor Details
#initialize(ptr = nil) {|_self| ... } ⇒ Instance
Initializes the Parser instance.
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.
36 37 38 |
# File 'lib/ffi/http/parser/instance.rb', line 36 def type @type end |
Instance Method Details
#<<(data) ⇒ Instance
Parses data.
231 232 233 234 |
# File 'lib/ffi/http/parser/instance.rb', line 231 def <<(data) parse(data) return self end |
#data ⇒ FFI::Pointer
Additional data attached to the parser.
350 351 352 |
# File 'lib/ffi/http/parser/instance.rb', line 350 def data self[:data] end |
#flags ⇒ Integer
Flags for the parser.
274 275 276 |
# File 'lib/ffi/http/parser/instance.rb', line 274 def flags (self[:type_flags] & 0xfc) >> 2 end |
#http_major ⇒ Integer
The parsed HTTP major version number.
284 285 286 |
# File 'lib/ffi/http/parser/instance.rb', line 284 def http_major self[:http_major] end |
#http_method ⇒ Symbol
The parsed HTTP Method.
328 329 330 |
# File 'lib/ffi/http/parser/instance.rb', line 328 def http_method METHODS[self[:method]] end |
#http_minor ⇒ Integer
The parsed HTTP minor version number.
294 295 296 |
# File 'lib/ffi/http/parser/instance.rb', line 294 def http_minor self[:http_minor] end |
#http_status ⇒ Integer
The parsed HTTP response Status Code.
316 317 318 |
# File 'lib/ffi/http/parser/instance.rb', line 316 def http_status self[:status_code] end |
#http_version ⇒ String
The parsed 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.
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.
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.
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.
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.
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.
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.
65 66 67 |
# File 'lib/ffi/http/parser/instance.rb', line 65 def (&block) @settings[:on_message_begin] = wrap_callback(block) end |
#on_message_complete { ... } ⇒ Object
Registers an on_message_begin
callback.
204 205 206 |
# File 'lib/ffi/http/parser/instance.rb', line 204 def (&block) @settings[:on_message_complete] = wrap_callback(block) end |
#on_path {|path| ... } ⇒ Object
Registers an on_path
callback.
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.
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.
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.
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.
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.
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.
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.
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 |