Class: OpenC3::HttpAccessor

Inherits:
Accessor show all
Defined in:
lib/openc3/accessors/http_accessor.rb

Instance Attribute Summary

Attributes inherited from Accessor

#packet

Instance Method Summary collapse

Methods inherited from Accessor

#args, convert_to_type, read_item, read_items, write_item, write_items

Constructor Details

#initialize(packet, body_accessor = 'FormAccessor', *body_accessor_args) ⇒ HttpAccessor

Returns a new instance of HttpAccessor.



23
24
25
26
27
28
29
30
31
# File 'lib/openc3/accessors/http_accessor.rb', line 23

def initialize(packet, body_accessor = 'FormAccessor', *body_accessor_args)
  super(packet)
  @args << body_accessor
  body_accessor_args.each do |arg|
    @args << arg
  end
  klass = OpenC3.require_class(body_accessor)
  @body_accessor = klass.new(packet, *body_accessor_args)
end

Instance Method Details

#enforce_derived_write_conversion(item) ⇒ Object

If this is true it will enfore that COSMOS DERIVED items must have a write_conversion to be written



174
175
176
177
178
179
180
181
# File 'lib/openc3/accessors/http_accessor.rb', line 174

def enforce_derived_write_conversion(item)
  case item.name
  when 'HTTP_STATUS', 'HTTP_PATH', 'HTTP_METHOD', 'HTTP_PACKET', 'HTTP_ERROR_PACKET', /^HTTP_QUERY_/, /^HTTP_HEADER_/
    return false
  else
    return @body_accessor.enforce_derived_write_conversion(item)
  end
end

#enforce_encodingObject

If this is set it will enforce that buffer data is encoded in a specific encoding



154
155
156
# File 'lib/openc3/accessors/http_accessor.rb', line 154

def enforce_encoding
  return @body_accessor.enforce_encoding
end

#enforce_lengthObject

This affects whether the Packet class enforces the buffer length at all. Set to false to remove any correlation between buffer length and defined sizes of items in COSMOS



161
162
163
# File 'lib/openc3/accessors/http_accessor.rb', line 161

def enforce_length
  return @body_accessor.enforce_length
end

#enforce_short_buffer_allowedObject

This sets the short_buffer_allowed flag in the Packet class which allows packets that have a buffer shorter than the defined size. Note that the buffer is still resized to the defined length



168
169
170
# File 'lib/openc3/accessors/http_accessor.rb', line 168

def enforce_short_buffer_allowed
  return @body_accessor.enforce_short_buffer_allowed
end

#read_item(item, buffer) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openc3/accessors/http_accessor.rb', line 33

def read_item(item, buffer)
  item_name = item.name
  case item_name
  when 'HTTP_STATUS'
    return nil unless @packet.extra
    return @packet.extra['HTTP_STATUS']
  when 'HTTP_PATH'
    return nil unless @packet.extra
    return @packet.extra['HTTP_PATH']
  when 'HTTP_METHOD'
    return nil unless @packet.extra
    return @packet.extra['HTTP_METHOD']
  when 'HTTP_PACKET'
    return nil unless @packet.extra
    return @packet.extra['HTTP_PACKET']
  when 'HTTP_ERROR_PACKET'
    return nil unless @packet.extra
    return @packet.extra['HTTP_ERROR_PACKET']
  when /^HTTP_QUERY_/
    return nil unless @packet.extra
    if item.key
      query_name = item.key
    else
      query_name = item_name[11..-1]
    end
    queries = @packet.extra['HTTP_QUERIES']
    if queries
      return queries[query_name]
    else
      return nil
    end
  when /^HTTP_HEADER_/
    return nil unless @packet.extra
    if item.key
      header_name = item.key
    else
      header_name = item_name[12..-1]
    end
    headers = @packet.extra['HTTP_HEADERS']
    if headers
      return headers[header_name]
    else
      return nil
    end
  else
    return @body_accessor.read_item(item, buffer)
  end
end

#read_items(items, buffer) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/openc3/accessors/http_accessor.rb', line 124

def read_items(items, buffer)
  result = {}
  body_items = []
  items.each do |item|
    if item.name[0..4] == 'HTTP_'
      result[item.name] = read_item(item, buffer)
    else
      body_items << item
    end
  end
  body_result = @body_accessor.read_items(body_items, buffer)
  result.merge!(body_result) # Merge Body accessor read items with HTTP_ items
  return result
end

#write_item(item, value, buffer) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/openc3/accessors/http_accessor.rb', line 82

def write_item(item, value, buffer)
  item_name = item.name
  case item_name
  when 'HTTP_STATUS'
    @packet.extra ||= {}
    @packet.extra['HTTP_STATUS'] = value.to_i
  when 'HTTP_PATH'
    @packet.extra ||= {}
    @packet.extra['HTTP_PATH'] = value.to_s
  when 'HTTP_METHOD'
    @packet.extra ||= {}
    @packet.extra['HTTP_METHOD'] = value.to_s.downcase
  when 'HTTP_PACKET'
    @packet.extra ||= {}
    @packet.extra['HTTP_PACKET'] = value.to_s.upcase
  when 'HTTP_ERROR_PACKET'
    @packet.extra ||= {}
    @packet.extra['HTTP_ERROR_PACKET'] = value.to_s.upcase
  when /^HTTP_QUERY_/
    @packet.extra ||= {}
    if item.key
      query_name = item.key
    else
      query_name = item_name[11..-1]
    end
    queries = @packet.extra['HTTP_QUERIES'] ||= {}
    queries[query_name] = value.to_s
  when /^HTTP_HEADER_/
    @packet.extra ||= {}
    if item.key
      header_name = item.key
    else
      header_name = item_name[12..-1]
    end
    headers = @packet.extra['HTTP_HEADERS'] ||= {}
    headers[header_name] = value.to_s
  else
    @body_accessor.write_item(item, value, buffer)
  end
  return value
end

#write_items(items, values, buffer) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/openc3/accessors/http_accessor.rb', line 139

def write_items(items, values, buffer)
  body_items = []
  items.each_with_index do |item, index|
    if item.name[0..4] == 'HTTP_'
      write_item(item, values[index], buffer)
    else
      body_items << item
    end
  end
  @body_accessor.write_items(body_items, values, buffer)
  return values
end