class StackifyRubyAPM::ResponseManipulator

an abstraction for manipulating the HTML we capture in the middleware

Constants

BRKT
CHARSET_RE
GT
HEAD_END
HEAD_START
RUM_SCRIPT_VARIABLE
SCAN_LIMIT

examine in order to look for a RUM insertion point.

X_UA_COMPATIBLE_RE

Attributes

Rack_flagger[R]
env[R]
jsfile_to_inject[R]
rack_body[R]
rack_headers[R]
rack_response[R]
rack_status[R]

Public Class Methods

new(env, rack_response, config) click to toggle source
# File lib/stackify_apm/response_manipulator.rb, line 26
def initialize(env, rack_response, config)
  @env = env
  @rack_response = rack_response

  @rack_status = rack_response[0]
  @rack_headers = rack_response[1]
  @rack_body = rack_response[2]
  @rack_flagger = nil
  @config = config
end

Public Instance Methods

adjust_pagehtml_response() click to toggle source

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity

# File lib/stackify_apm/response_manipulator.rb, line 43
def adjust_pagehtml_response
  response = @rack_body
  source = gather_source(response)
  close_old_response(response)
  return nil unless source

  client_id = @config.client_id
  device_id = @config.device_id
  client_rundomain = @config.client_run_domain
  inject_flag = false

  if client_id && device_id
    if StackifyRubyAPM.check_isdomain(client_rundomain)
      inject_flag = true
    else
      info 'RUM Injection Error: Client RUM Domain is invalid.'
    end
  else
    info 'RUM Injection Error: No Device ID and/or Client ID found.'
  end

  return unless inject_flag

  # Only scan the first 50k (roughly) then give up.
  insertion_index = find_end_of_head_open(source[0..SCAN_LIMIT])

  if insertion_index && inject_flag
    source = source[0...insertion_index] <<
             StackifyRubyAPM.inject_rum_script <<
             source[insertion_index..-1]
  end
  source
end
close_old_response(response) click to toggle source
# File lib/stackify_apm/response_manipulator.rb, line 90
def close_old_response(response)
  response.close if response.respond_to?(:close)
end
find_end_of_head_open(beginning_of_source) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity

# File lib/stackify_apm/response_manipulator.rb, line 79
def find_end_of_head_open(beginning_of_source)
  head_open = beginning_of_source.index(HEAD_END)
  beginning_of_source.index(GT, head_open) - 6 if head_open
end
gather_source(response) click to toggle source
# File lib/stackify_apm/response_manipulator.rb, line 84
def gather_source(response)
  source = nil
  response.each { |fragment| source ? (source << fragment.to_s) : (source = fragment.to_s) }
  source
end
rebuild_rack_response() click to toggle source
# File lib/stackify_apm/response_manipulator.rb, line 37
def rebuild_rack_response
  [rack_status, rack_headers, rack_body]
end