Module: Datadog::AppSec::Utils::HTTP::Body

Defined in:
lib/datadog/appsec/utils/http/body.rb

Overview

Module for handling HTTP body parsing

Class Method Summary collapse

Class Method Details

.parse(body, media_type:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/datadog/appsec/utils/http/body.rb', line 14

def self.parse(body, media_type:)
  return if body.nil?

  body.rewind if body.respond_to?(:rewind) # steep:ignore NoMethod
  # @type var content: ::String?
  content = body.respond_to?(:read) ? body.read : body # steep:ignore NoMethod, IncompatibleAssignment
  body.rewind if body.respond_to?(:rewind) # steep:ignore NoMethod

  return if content.nil? || content.empty?

  if media_type.subtype == 'json' || media_type.subtype.end_with?('+json')
    JSON.parse(content)
  elsif media_type.subtype == 'x-www-form-urlencoded'
    URLEncoded.parse(content)
  end
rescue => e
  AppSec.telemetry.report(e, description: 'AppSec: Failed to parse body')

  nil
end