class TCellAgent::MetaData

Attributes

content_type[RW]
csrf_exception_name[RW]
database_result_sizes[RW]
flattened_body_dict[R]
flattened_get_dict[R]
flattened_headers_dict[R]
flattened_path_parameters[R]
flattened_post_dict[R]
location[RW]
method[RW]
path[RW]
raw_request_body[RW]
remote_address[RW]
request_content_bytes_len[RW]
response_code[RW]
response_content_bytes_len[RW]
response_headers[RW]
reverse_proxy_header_value[RW]
route_id[RW]
session_id[RW]
sql_exceptions[RW]
transaction_id[RW]
user_agent[RW]
user_id[RW]

Public Class Methods

for_appfirewall(request, response_content_length, response_code, response_headers) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 15
def for_appfirewall(request, response_content_length, response_code, response_headers)
  meta_data = TCellAgent::MetaData.for_patches(request)

  tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
  meta_data.csrf_exception_name = tcell_context.csrf_exception_name
  meta_data.user_agent = tcell_context.user_agent
  meta_data.path_parameters = tcell_context.path_parameters
  meta_data.sql_exceptions = tcell_context.sql_exceptions
  meta_data.database_result_sizes = tcell_context.database_result_sizes

  meta_data.response_content_bytes_len = response_content_length

  meta_data.response_code = response_code
  meta_data.response_headers = response_headers

  meta_data
end
for_patches(request) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 33
def for_patches(request)
  tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
  # use uri stored in tcell_context because
  # rails modifies original request.url
  # to always return /404 (or whatever error code
  # it encountered)
  meta_data = MetaData.new(
    tcell_context.request_method,
    tcell_context.remote_address,
    tcell_context.route_id,
    tcell_context.session_id,
    tcell_context.user_id,
    tcell_context.transaction_id,
    tcell_context.uri,
    tcell_context.reverse_proxy_header_value
  )
  meta_data.path = tcell_context.path

  meta_data.set_parameter_dicts(request)

  meta_data
end
new(method, remote_address, route_id, session_id, user_id, transaction_id, location, reverse_proxy_header_value) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 84
def initialize(method,
               remote_address,
               route_id,
               session_id,
               user_id,
               transaction_id,
               location,
               reverse_proxy_header_value)
  @method = method
  @remote_address = remote_address
  @route_id = route_id
  @session_id = session_id
  @user_id = user_id
  @transaction_id = transaction_id
  @location = location
  @reverse_proxy_header_value = reverse_proxy_header_value

  @flattened_get_dict = {}
  @flattened_cookie_dict = {}
  @flattened_path_parameters = {}

  # POST/body dict can contain files, so always flatten it
  # to get rid of those and to make this model serializable
  @flattened_post_dict = {}
  @flattened_body_dict = {}

  @request_content_bytes_len = 0
  @response_content_bytes_len = 0
  @user_agent = nil
end

Public Instance Methods

charset() click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 143
def charset
  Rack::MediaType.params(@content_type)['charset'] || Encoding.default_external
rescue StandardError
  Encoding.default_external
end
get_dict=(value) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 115
def get_dict=(value)
  @flattened_get_dict = TCellAgent::Utils::Params.flatten(value)
end
get_raw_post_data(request) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 149
def get_raw_post_data(request)
  content_length = request.content_length.to_i if request.content_length
  if !content_length.nil? && content_length > TCELL_MAX_BODY_LENGTH || request.content_type.nil?
    return nil
  end

  raw_post_data = nil
  # Positions strio to the beginning of input, resetting lineno to zero.
  # rails 4.1 seems to read the stringIO directly and so body.gets is empty
  # this is called

  body = request.body
  body.rewind if body.respond_to?(:rewind)
  raw_post_data = body.read(request.content_length.to_i) if request.content_length
  body.rewind if body.respond_to?(:rewind)

  raw_post_data.force_encoding(charset) unless raw_post_data.nil?
end
headers_dict=(value) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 123
def headers_dict=(value)
  headers_dict = value.select do |header_key, _v|
    (header_key != 'HTTP_COOKIE' && header_key.start_with?('HTTP_')) ||
      %w[CONTENT_TYPE CONTENT_LENGTH].include?(header_key)
  end

  headers_dict = headers_dict.each_with_object({}) do |(k, v), memo|
    memo[k.sub(/^HTTP_/, '').tr('_', '-').downcase] = v
  end
  @flattened_headers_dict = TCellAgent::Utils::Params.flatten(headers_dict)
end
path_parameters=(value) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 139
def path_parameters=(value)
  @flattened_path_parameters = TCellAgent::Utils::Params.flatten(value)
end
post_dict=(value) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 135
def post_dict=(value)
  @flattened_post_dict = TCellAgent::Utils::Params.flatten(value)
end
set_parameter_dicts(request) click to toggle source
# File lib/tcell_agent/tcell_context.rb, line 168
def set_parameter_dicts(request)
  @flattened_body_dict = {} # deprecated
  @content_type = request.content_type || ''
  @raw_request_body = get_raw_post_data(request) unless @content_type.start_with?('application/octet-stream',
                                                                                  'multipart/form-data')
  @request_content_bytes_len = (request.content_length || 0).to_i

  self.get_dict = request.GET
  self.cookie_dict = request.cookies

  self.post_dict = if @content_type.start_with?('application/json', 'application/xml') ||
                      (@content_type.start_with?('multipart/form-data') && @request_content_bytes_len == 0)
                     {}
                   else
                     begin
                       request.POST
                     rescue EOFError
                       {}
                     end
                   end

  self.headers_dict = request.env
end