<commit_before>module Rollbar
  module RequestDataExtractor
    ATTACHMENT_CLASSES = %w[
      ActionDispatch::Http::UploadedFile
      Rack::Multipart::UploadedFile
    ].freeze

    def extract_person_data_from_controller(env)
      if env.has_key? 'rollbar.person_data'
        person_data = env['rollbar.person_data'] || {}
      else
        controller = env['action_controller.instance']
        person_data = controller ? controller.try(:rollbar_person_data) : {}
      end
      
      person_data
    end

    def extract_request_data_from_rack(env)
      rack_req = Rack::Request.new(env)
      
      sensitive_params = sensitive_params_list(env)
      request_params = rollbar_filtered_params(sensitive_params, rollbar_request_params(env))
      get_params = rollbar_filtered_params(sensitive_params, rollbar_get_params(rack_req))
      post_params = rollbar_filtered_params(sensitive_params, rollbar_post_params(rack_req))
      cookies = rollbar_filtered_params(sensitive_params, rollbar_request_cookies(rack_req))
      session = rollbar_filtered_params(sensitive_params, env['rack.session.options'])
      route_params = rollbar_filtered_params(sensitive_params, rollbar_route_params(env))
      
      params = request_params.merge(get_params).merge(post_params)
      
      {
        :params => params,
        :url => rollbar_url(env),
        :user_ip => rollbar_user_ip(env),
        :headers => rollbar_headers(env),
        :cookies => cookies,
        :session => session,
        :method => rollbar_request_method(env),
        :route => route_params,
        :request_id => env["action_dispatch.request_id"],
      }
    end

    private

    def rollbar_request_method(env)
      env['REQUEST_METHOD'] || env[:method]
    end

    def rollbar_headers(env)
      env.keys.grep(/^HTTP_/).map do |header|
        name = header.gsub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
        if name == 'Cookie'
           {}
        elsif sensitive_headers_list.include?(name)
          { name => rollbar_scrubbed(env[header]) }
        else
          { name => env[header] }
        end
      end.inject(:merge)
    end

    def rollbar_url(env)
      scheme = env['HTTP_X_FORWARDED_PROTO'] || env['rack.url_scheme']<commit_msg>save rails request_id in the item only if it exists (rails >= 3.2)<commit_after>module Rollbar
  module RequestDataExtractor
    ATTACHMENT_CLASSES = %w[
      ActionDispatch::Http::UploadedFile
      Rack::Multipart::UploadedFile
    ].freeze

    def extract_person_data_from_controller(env)
      if env.has_key? 'rollbar.person_data'
        person_data = env['rollbar.person_data'] || {}
      else
        controller = env['action_controller.instance']
        person_data = controller ? controller.try(:rollbar_person_data) : {}
      end
      
      person_data
    end

    def extract_request_data_from_rack(env)
      rack_req = Rack::Request.new(env)
      
      sensitive_params = sensitive_params_list(env)
      request_params = rollbar_filtered_params(sensitive_params, rollbar_request_params(env))
      get_params = rollbar_filtered_params(sensitive_params, rollbar_get_params(rack_req))
      post_params = rollbar_filtered_params(sensitive_params, rollbar_post_params(rack_req))
      cookies = rollbar_filtered_params(sensitive_params, rollbar_request_cookies(rack_req))
      session = rollbar_filtered_params(sensitive_params, env['rack.session.options'])
      route_params = rollbar_filtered_params(sensitive_params, rollbar_route_params(env))
      
      params = request_params.merge(get_params).merge(post_params)
      
      data = {
        :params => params,
        :url => rollbar_url(env),
        :user_ip => rollbar_user_ip(env),
        :headers => rollbar_headers(env),
        :cookies => cookies,
        :session => session,
        :method => rollbar_request_method(env),
        :route => route_params,
      }
      
      if env["action_dispatch.request_id"]
        data[:request_id] = env["action_dispatch.request_id"]
      end
      
      data
    end

    private

    def rollbar_request_method(env)
      env['REQUEST_METHOD'] || env[:method]
    end

    def rollbar_headers(env)
      env.keys.grep(/^HTTP_/).map do |header|
        name = header.gsub(/^HTTP_/, '').split('_').map(&:capitalize).join('-')
        if name == 'Cookie'
           {}
        elsif sensitive_headers_list.include?(name)
          { name => rollbar_scrubbed(env[header]) }
        else
          { name => env[header] }
        end
      end.inject(:merge)
    end

    def rollbar_url(env)
      scheme = env['HTTP_X_FORWARDED_PROTO'] || env['rack.url_scheme']
{'fraction_non_alphanumeric': 0.10893854748603352, 'fraction_numerical': 0.0004297378599054577, 'mean_word_length': 3.0655021834061134, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2122202', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1420, 'n_words': 219}