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:



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/http-parser/types.rb', line 95

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.



246
247
248
# File 'lib/http-parser/types.rb', line 246

def data
    self[:data]
end

#errorStandarError

Returns the error that occurred during processing.

Returns:

  • (StandarError)

    Returns the error that occurred.



230
231
232
233
234
235
236
237
238
# File 'lib/http-parser/types.rb', line 230

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



273
274
275
# File 'lib/http-parser/types.rb', line 273

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?



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

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

#flagsInteger

Flags for the parser.

Returns:

  • (Integer)

    Parser flags.



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

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

#http_majorInteger

The parsed HTTP major version number.

Returns:

  • (Integer)

    The HTTP major version number.



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

def http_major
    self[:http_major]
end

#http_methodSymbol

The parsed HTTP Method.

Returns:

  • (Symbol)

    The HTTP Method name.

See Also:



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

def http_method
    METHODS[self[:method]]
end

#http_minorInteger

The parsed HTTP minor version number.

Returns:

  • (Integer)

    The HTTP minor version number.



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

def http_minor
    self[:http_minor]
end

#http_statusInteger

The parsed HTTP response Status Code.

Returns:

  • (Integer)

    The HTTP Status Code.

See Also:



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

def http_status
    self[:status_code]
end

#http_versionString

The parsed HTTP version.

Returns:

  • (String)

    The HTTP version.



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

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:



259
260
261
# File 'lib/http-parser/types.rb', line 259

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.



113
114
115
# File 'lib/http-parser/types.rb', line 113

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

#stop!Object

Halts the parser if called in a callback



266
267
268
# File 'lib/http-parser/types.rb', line 266

def stop!
    throw :return, 1
end

#type:request, ...

The type of the parser.

Returns:

  • (:request, :response, :both)

    The parser type.



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

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.



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

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:



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

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