Class: RaptorIO::Protocol::HTTP::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/raptor-io/protocol/http/message.rb

Overview

HTTP message, holds shared attributes of Request and Response.

Author:

Direct Known Subclasses

Request, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Note:

All options will be sent through the class setters whenever possible to allow for normalization.

Returns a new instance of Message.

Parameters:

  • options (Hash) (defaults to: {})

    Message options.

Options Hash (options):

  • :url (String)

    The URL of the remote resource.

  • :headers (Hash)

    HTTP headers.

  • :body (String)

    Body.

  • :version (String) — default: 1.1

    HTTP version.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/raptor-io/protocol/http/message.rb', line 30

def initialize( options = {} )
  options.each do |k, v|
    begin
      send( "#{k}=", v )
    rescue NoMethodError
      instance_variable_set( "@#{k}".to_sym, v )
    end
  end

  @headers  = Headers.new( @headers )
  @version ||= '1.1'
end

Instance Attribute Details

#bodyString

Returns Request/Response body.

Returns:



18
19
20
# File 'lib/raptor-io/protocol/http/message.rb', line 18

def body
  @body
end

#headersHeaders<String, String> (readonly)

Returns HTTP headers as a Hash-like object.

Returns:



15
16
17
# File 'lib/raptor-io/protocol/http/message.rb', line 15

def headers
  @headers
end

#versionString (readonly)

Returns HTTP version.

Returns:



12
13
14
# File 'lib/raptor-io/protocol/http/message.rb', line 12

def version
  @version
end

Instance Method Details

#http_1_0?Boolean

Returns ‘true` when #version is `1.0`, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` when #version is `1.0`, `false` otherwise.



60
61
62
# File 'lib/raptor-io/protocol/http/message.rb', line 60

def http_1_0?
  version == '1.0'
end

#http_1_1?Boolean

Returns ‘true` when #version is `1.1`, `false` otherwise.

Returns:

  • (Boolean)

    ‘true` when #version is `1.1`, `false` otherwise.



54
55
56
# File 'lib/raptor-io/protocol/http/message.rb', line 54

def http_1_1?
  version == '1.1'
end

#keep_alive?Bool

Returns ‘true` if the connections should be reused, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the connections should be reused, `false` otherwise.



45
46
47
48
49
50
# File 'lib/raptor-io/protocol/http/message.rb', line 45

def keep_alive?
  connection = headers['Connection'].to_s.downcase

  return connection == 'keep-alive' if version.to_f < 1.1
  connection != 'close'
end