Module: Contrast::Api::Decorators::HttpRequest

Includes:
Components::Interface
Included in:
Contrast::Api::Dtm::HttpRequest
Defined in:
lib/contrast/api/decorators/http_request.rb

Overview

Used to decorate the Contrast::Api::Dtm::HttpRequest protobuf model so it can own some of the data massaging required for Request dtm. Only works as an extension of that class.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

OMITTED_BODY =
'{{body-omitted-by-contrast}}'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



22
23
24
# File 'lib/contrast/api/decorators/http_request.rb', line 22

def self.included klass
  klass.extend(ClassMethods)
end

Instance Method Details

#append_addresses(request) ⇒ Object

Append the sender and receiver to self.

Parameters:



29
30
31
32
# File 'lib/contrast/api/decorators/http_request.rb', line 29

def append_addresses request
  self.sender = Contrast::Api::Dtm::Address.build_sender(request)
  self.receiver = Contrast::Api::Dtm::Address.build_receiver
end

#append_body(request) ⇒ Object

Append the body to self, using the preferred request_body_binary field.

Parameters:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/contrast/api/decorators/http_request.rb', line 77

def append_body request
  self.document_type = Contrast::Utils::StringUtils.force_utf8(request.document_type)

  self.request_body_binary = if omit_body?(request)
                               OMITTED_BODY
                             else
                               Contrast::Utils::StringUtils.force_utf8(request.body)
                             end
  return if request.file_names.empty?

  request.file_names.each do |name, filename|
    pair = Contrast::Api::Dtm::SimplePair.new
    pair.key = Contrast::Utils::StringUtils.force_utf8(name)
    pair.value = Contrast::Utils::StringUtils.force_utf8(filename)
    multipart_headers << pair
  end
end

#append_connection(request) ⇒ Object

Append the connection to self.

Parameters:



37
38
39
40
41
42
# File 'lib/contrast/api/decorators/http_request.rb', line 37

def append_connection request
  self.protocol = Contrast::Utils::StringUtils.force_utf8(request.scheme)
  self.version = '1.1' # currently not in rack request; hard-coding
  self.method = Contrast::Utils::StringUtils.force_utf8(request.request_method)
  self.raw = Contrast::Utils::StringUtils.force_utf8(request.rack_request.path_info)
end

#append_headers(request) ⇒ Object

Append the normalized headers to self, using the preferred request_headers field.

Parameters:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/contrast/api/decorators/http_request.rb', line 58

def append_headers request
  request.headers.each do |k, v|
    next unless k && v
    next if v.is_a?(Hash)

    key = Contrast::Utils::StringUtils.force_utf8(k)
    val = Contrast::Utils::StringUtils.force_utf8(v)
    request_headers[key] = val
  end

  request.cookies.each do |k, v|
    append_pair(normalized_cookies, k, v)
  end
end

#append_pair(map, key, value) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/contrast/api/decorators/http_request.rb', line 102

def append_pair map, key, value
  return unless key && value
  return if value.is_a?(Hash)

  safe_key = Contrast::Utils::StringUtils.force_utf8(key)
  map[safe_key] ||= Contrast::Api::Dtm::Pair.new
  map[safe_key].key = safe_key
  map[safe_key].values << Contrast::Utils::StringUtils.force_utf8(value)
end

#append_params(request) ⇒ Object

Append the parameters to self, using the preferred normalized_request_params field.

Parameters:



48
49
50
51
52
# File 'lib/contrast/api/decorators/http_request.rb', line 48

def append_params request
  request.parameters.each do |k, v|
    append_pair(normalized_request_params, k, v)
  end
end

#omit_body?(request) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/contrast/api/decorators/http_request.rb', line 95

def omit_body? request
  return true if AGENT.omit_body?
  return false if request.document_type != :NORMAL

  request.content_type&.include?('multipart/form-data')
end