Sha256: 3f9dcaf7cc1ae8d17aa3121dcc424b10cf7059a935248577a5a00fe56bd3f766

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# typed: false

require_relative 'assets'

module Datadog
  module AppSec
    # AppSec response
    class Response
      attr_reader :status, :headers, :body

      def initialize(status:, headers: {}, body: [])
        @status = status
        @headers = headers
        @body = body
      end

      def to_rack
        [status, headers, body]
      end

      def to_sinatra_response
        ::Sinatra::Response.new(body, status, headers)
      end

      def to_action_dispatch_response
        ::ActionDispatch::Response.new(status, headers, body)
      end

      class << self
        def negotiate(env)
          Response.new(
            status: 403,
            headers: { 'Content-Type' => 'text/html' },
            body: [Datadog::AppSec::Assets.blocked(format: format(env))]
          )
        end

        private

        def format(env)
          format = env['HTTP_ACCEPT'] && env['HTTP_ACCEPT'].split(',').any? do |accept|
            if accept.start_with?('text/html')
              break :html
            elsif accept.start_with?('application/json')
              break :json
            end
          end

          format || :text
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ddtrace-1.8.0 lib/datadog/appsec/response.rb
ddtrace-1.7.0 lib/datadog/appsec/response.rb