Module: Tilia::Http::Message

Includes:
MessageInterface
Included in:
Request, Response
Defined in:
lib/tilia/http/message.rb

Overview

This is the abstract base class for both the Request and Response objects.

This object contains a few simple methods that are shared by both.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



41
42
43
# File 'lib/tilia/http/message.rb', line 41

def body
  @body
end

#http_versionObject

Returns the value of attribute http_version.



122
123
124
# File 'lib/tilia/http/message.rb', line 122

def http_version
  @http_version
end

Instance Method Details

#add_header(name, value) ⇒ void

This method returns an undefined value.

Adds a HTTP header.

This method will not overwrite any existing HTTP header, but instead add another value. Individual values can be retrieved with getHeadersAsArray.

Parameters:

  • name (String)
  • value (String)


92
93
94
95
96
97
98
99
100
101
# File 'lib/tilia/http/message.rb', line 92

def add_header(name, value)
  l_name = name.downcase
  value = [value] unless value.is_a?(Array)

  if @headers.key?(l_name)
    @headers[l_name][1].concat value
  else
    @headers[l_name] = [name, value]
  end
end

#add_headers(headers) ⇒ void

This method returns an undefined value.

Adds a new set of HTTP headers.

Any existing headers will not be overwritten.

Parameters:

  • headers (Hash)


104
105
106
107
108
# File 'lib/tilia/http/message.rb', line 104

def add_headers(headers)
  headers.each do |name, value|
    add_header(name, value)
  end
end

#body_as_streamIO, StringIO

Returns the body as a readable stream resource.

Note that the stream may not be rewindable, and therefore may only be read once.

Returns:

  • (IO, StringIO)


11
12
13
14
15
16
17
18
19
20
# File 'lib/tilia/http/message.rb', line 11

def body_as_stream
  body = @body
  if body.is_a?(String) || body.nil?
    stream = StringIO.new
    stream.write body
    stream.rewind
    return stream
  end
  body
end

#body_as_stringString

Returns the body as a string.

Note that because the underlying data may be based on a stream, this method could only work correctly the first time.

Returns:

  • (String)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tilia/http/message.rb', line 23

def body_as_string
  body = @body
  if body.is_a?(String)
    body
  elsif body.nil?
    ''
  else
    content_length = header('Content-Length')

    if content_length.present?
      body.read(content_length.to_i)
    else
      body.read
    end
  end
end

#header(name) ⇒ String?

Returns a specific HTTP header, based on it’s name.

The name must be treated as case-insensitive. If the header does not exist, this method must return null.

If a header appeared more than once in a HTTP request, this method will concatenate all the values with a comma.

Note that this not make sense for all headers. Some, such as Set-Cookie cannot be logically combined with a comma. In those cases you should use header_as_array.

Parameters:

  • name (String)

Returns:

  • (String, nil)


61
62
63
64
65
66
67
# File 'lib/tilia/http/message.rb', line 61

def header(name)
  name = name.downcase

  return @headers[name][1].join(',') if @headers.key?(name)

  nil
end

#header?(name) ⇒ Boolean

Will return true or false, depending on if a HTTP header exists.

Parameters:

  • name (String)

Returns:

  • (Boolean)


56
57
58
# File 'lib/tilia/http/message.rb', line 56

def header?(name)
  @headers.key? name.downcase
end

#header_as_array(name) ⇒ Array<String>

Returns a HTTP header as an array.

For every time the HTTP header appeared in the request or response, an item will appear in the array.

If the header did not exists, this method will return an empty array.

Parameters:

  • name (String)

Returns:

  • (Array<String>)


70
71
72
73
74
75
76
# File 'lib/tilia/http/message.rb', line 70

def header_as_array(name)
  name = name.downcase

  return @headers[name][1] if @headers.key?(name)

  []
end

#headersHash

Returns all the HTTP headers as an array.

Every header is returned as an array, with one or more values.

Returns:

  • (Hash)


47
48
49
50
51
52
53
# File 'lib/tilia/http/message.rb', line 47

def headers
  result = {}
  @headers.values.each do |header_info|
    result[header_info[0]] = header_info[1]
  end
  result
end

#initialize(*args) ⇒ Object

Initializes the instance vars of Message



125
126
127
128
129
130
131
# File 'lib/tilia/http/message.rb', line 125

def initialize(*args)
  @body = nil
  @headers = {}
  @http_version = '1.1'

  super
end

#initialize_copy(_original) ⇒ Object

creates a deep copy of the headers hash when cloning



134
135
136
# File 'lib/tilia/http/message.rb', line 134

def initialize_copy(_original)
  @headers = @headers.deep_dup
end

#remove_header(name) ⇒ Object

Removes a HTTP header.

The specified header name must be treated as case-insenstive. This method should return true if the header was successfully deleted, and false if the header did not exist.

Parameters:

  • name (String)

Returns:

  • bool



111
112
113
114
115
116
# File 'lib/tilia/http/message.rb', line 111

def remove_header(name)
  name = name.downcase
  return false unless @headers.key?(name)
  @headers.delete name
  true
end

#update_header(name, value) ⇒ void

This method returns an undefined value.

Updates a HTTP header.

The case-sensitity of the name value must be retained as-is.

If the header already existed, it will be overwritten.

Parameters:

  • name (String)
  • value (String, Array<String>)


79
80
81
82
# File 'lib/tilia/http/message.rb', line 79

def update_header(name, value)
  value = [value] unless value.is_a?(Array)
  @headers[name.downcase] = [name, value]
end

#update_headers(headers) ⇒ void

This method returns an undefined value.

Sets a new set of HTTP headers.

The headers array should contain headernames for keys, and their value should be specified as either a string or an array.

Any header that already existed will be overwritten.

Parameters:

  • headers (Hash)


85
86
87
88
89
# File 'lib/tilia/http/message.rb', line 85

def update_headers(headers)
  headers.each do |name, value|
    update_header(name, value)
  end
end