Class: HttpParser::Instance

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

Overview

Effectively this represents a request instance

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Instance.

Yields:

  • (_self)

Yield Parameters:



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/http-parser/types.rb', line 106

def initialize(ptr = nil)
    if ptr then super(ptr)
    else
        super()
        self.type = :both
    end

    yield self if block_given?

    ::HttpParser.http_parser_init(self, self.type) unless ptr
end

Instance Method Details

#dataFFI::Pointer

Additional data attached to the parser.

Returns:

  • (FFI::Pointer)

    Pointer to the additional data.



257
258
259
# File 'lib/http-parser/types.rb', line 257

def data
    self[:data]
end

#errorStandarError

Returns the error that occurred during processing.

Returns:

  • (StandarError)

    Returns the error that occurred.



241
242
243
244
245
246
247
248
249
# File 'lib/http-parser/types.rb', line 241

def error
    error = (self[:error_upgrade] & 0b1111111)
    return nil if error == 0

    err = ::HttpParser.err_name(error)[4..-1] # HPE_ is at the start of all these errors
    klass = ERRORS[err.to_sym]
    err = "#{::HttpParser.err_desc(error)} (#{err})"
    return klass.nil? ? Error::UNKNOWN.new(err) : klass.new(err)
end

#error!Object

Indicates an error has occurred when called in a callback



294
295
296
# File 'lib/http-parser/types.rb', line 294

def error!
    throw :return, -1
end

#error?Boolean

Determines whether an error occurred during processing.

Returns:

  • (Boolean)

    Did a parsing error occur with the request?



230
231
232
233
# File 'lib/http-parser/types.rb', line 230

def error?
    error = (self[:error_upgrade] & 0b1111111)
    return error != 0
end

#final_chunk?Boolean

Determines if a chunked response has completed

Returns:

  • (Boolean)

    Specifies whether the chunked response has completed



280
281
282
# File 'lib/http-parser/types.rb', line 280

def final_chunk?
    ::HttpParser.http_body_is_final(self) > 0
end

#flagsInteger

Flags for the parser.

Returns:

  • (Integer)

    Parser flags.



154
155
156
# File 'lib/http-parser/types.rb', line 154

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

#http_majorInteger

The parsed HTTP major version number.

Returns:

  • (Integer)

    The HTTP major version number.



164
165
166
# File 'lib/http-parser/types.rb', line 164

def http_major
    self[:http_major]
end

#http_methodSymbol

The parsed HTTP Method.

Returns:

  • (Symbol)

    The HTTP Method name.

See Also:



208
209
210
# File 'lib/http-parser/types.rb', line 208

def http_method
    METHODS[self[:method]]
end

#http_minorInteger

The parsed HTTP minor version number.

Returns:

  • (Integer)

    The HTTP minor version number.



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

def http_minor
    self[:http_minor]
end

#http_statusInteger

The parsed HTTP response Status Code.

Returns:

  • (Integer)

    The HTTP Status Code.

See Also:



196
197
198
# File 'lib/http-parser/types.rb', line 196

def http_status
    self[:status_code]
end

#http_versionString

The parsed HTTP version.

Returns:

  • (String)

    The HTTP version.



184
185
186
# File 'lib/http-parser/types.rb', line 184

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:



270
271
272
# File 'lib/http-parser/types.rb', line 270

def keep_alive?
    ::HttpParser.http_should_keep_alive(self) > 0
end

#reset!(new_type = type) ⇒ Object

Resets the parser.

Parameters:

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

    The new type for the parser.



124
125
126
# File 'lib/http-parser/types.rb', line 124

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

#stop!Object

Halts the parser if called in a callback



287
288
289
# File 'lib/http-parser/types.rb', line 287

def stop!
    throw :return, 1
end

#type:request, ...

The type of the parser.

Returns:

  • (:request, :response, :both)

    The parser type.



134
135
136
# File 'lib/http-parser/types.rb', line 134

def type
    TYPES[self[:type_flags] & 0x3]
end

#type=(new_type) ⇒ Object

Sets the type of the parser.

Parameters:

  • new_type (:request, :response, :both)

    The new parser type.



144
145
146
# File 'lib/http-parser/types.rb', line 144

def type=(new_type)
    self[:type_flags] = (flags | TYPES[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:



220
221
222
# File 'lib/http-parser/types.rb', line 220

def upgrade?
    (self[:error_upgrade] & 0b10000000) > 0
end