lib/contrast/agent/request/request.rb in contrast-agent-7.1.0 vs lib/contrast/agent/request/request.rb in contrast-agent-7.2.0
- old
+ new
@@ -8,10 +8,11 @@
require 'contrast/utils/string_utils'
require 'contrast/utils/hash_digest'
require 'contrast/components/logger'
require 'contrast/components/scope'
require 'contrast/utils/request_utils'
+require 'contrast/utils/duck_utils'
module Contrast
module Agent
# This class is the Contrast representation of the Rack::Request object. It
# provides access to the original Rack::Request object as well as extracts
@@ -22,16 +23,11 @@
include Contrast::Components::Logger::InstanceMethods
include Contrast::Components::Scope::InstanceMethods
extend Forwardable
- INNER_REST_TOKEN = %r{/\d+/}.cs__freeze
- LAST_REST_TOKEN = %r{/\d+$}.cs__freeze
- INNER_NUMBER_MARKER = '/{n}/'
- LAST_NUMBER_MARKER = '/{n}'
- STATIC_SUFFIXES = /\.(?:js|css|jpeg|jpg|gif|png|ico|woff|svg|pdf|eot|ttf|jar)$/i.cs__freeze
- MEDIA_TYPE_MARKERS = %w[image/ text/css text/javascript].cs__freeze
+ EMPTY_PATH = '/'
# @return [Rack::Request] The passed to the Agent RackRequest to be wrapped.
attr_reader :rack_request
# @return [Contrast::Agent::Reporting::ObservedRoute] the route, used for coverage, of this request
attr_accessor :observed_route
@@ -73,15 +69,34 @@
#
# @return uri [String]
def normalized_uri
@_normalized_uri ||= begin
path = rack_request.path_info || rack_request.path.to_s
- path = '/' if path.empty?
+ path = EMPTY_PATH if Contrast::Utils::DuckUtils.empty_duck?(path)
- uri = path.split(Contrast::Utils::ObjectShare::SEMICOLON)[0] # remove ;jsessionid
- uri = uri.split(Contrast::Utils::ObjectShare::QUESTION_MARK)[0] # remove ?query_string=
- uri.gsub(INNER_REST_TOKEN, INNER_NUMBER_MARKER) # replace interior tokens
- uri.gsub(LAST_REST_TOKEN, LAST_NUMBER_MARKER) # replace last token
+ # /foo/bar;jsessionid=123 => /foo/bar
+ uri = path.split(Contrast::Utils::ObjectShare::SEMICOLON)[0]
+ # /foo/bar?query_string=123 => /foo/bar
+ uri = uri.split(Contrast::Utils::ObjectShare::QUESTION_MARK)[0]
+
+ # Replace with tokens:
+ # NUM_ => '/{n}/'
+ # ID_ => '{ID}'
+ #
+ # replace UUIDs: /123e4567-e89b-42d3-a456-556642440000/ => /{ID}/
+ uri.gsub!(UUID_PATTERN, ID_)
+ # replace hash patterns: /6f1ed002ab5595859014ebf0951522d9/ => /{ID}/
+ uri.gsub!(HASH_PATTERN, ID_)
+ # replace windows SID: /S-1-5-21-1843332746-572796286-2118856591-1000/ => /{ID}/
+ uri.gsub!(WIN_PATTERN, ID_)
+ # replace interior number tokens: /123/ => /{n}/
+ uri.gsub!(NUM_PATTERN, NUM_)
+ # replace last number tokens: /123 => /{n}
+ uri.gsub!(END_PATTERN, NUM_[0..-2])
+ uri
+ rescue StandardError => e
+ logger.error('error normalizing uri', error: e, backtrace: e.backtrace)
+ EMPTY_PATH
end
end
# Returns the request file type
#