<commit_before>require 'active_support/core_ext/hash/conversions'
require 'action_dispatch/http/request'
require 'active_support/core_ext/hash/indifferent_access'

module ActionDispatch
  class XmlParamsParser
    def initialize(app)
      @app = app
    end

    def call(env)
      if params = parse_formatted_parameters(env)
        env["action_dispatch.request.request_parameters"] = params
      end

      @app.call(env)
    end

    private
      def parse_formatted_parameters(env)
        request = Request.new(env)

        return false if request.content_length.zero?

        mime_type = content_type_from_legacy_post_data_format_header(env) ||
          request.content_mime_type

        if mime_type == Mime::XML
          data = request.deep_munge(Hash.from_xml(request.body.read) || {})
          data.with_indifferent_access
        else
          false
        end
      rescue Exception => e # XML code block errors
        logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"

        raise ActionDispatch::ParamsParser::ParseError.new(e.message, e)
      end

      def content_type_from_legacy_post_data_format_header(env)
        if env['HTTP_X_POST_DATA_FORMAT'].to_s.downcase == 'xml'
          Mime::XML
        end
      end

      def logger(env)
        env['action_dispatch.logger'] || ActiveSupport::Logger.new($stderr)
      end
  end
end<commit_msg>Ensure compatibility with Rails 4.1+<commit_after>require 'active_support/core_ext/hash/conversions'
require 'action_dispatch/http/request'
require 'active_support/core_ext/hash/indifferent_access'

module ActionDispatch
  class XmlParamsParser
    def initialize(app)
      @app = app
    end

    def call(env)
      if params = parse_formatted_parameters(env)
        env["action_dispatch.request.request_parameters"] = params
      end

      @app.call(env)
    end

    private
      def parse_formatted_parameters(env)
        request = Request.new(env)

        return false if request.content_length.zero?

        mime_type = content_type_from_legacy_post_data_format_header(env) ||
          request.content_mime_type

        if mime_type == Mime::XML
          if defined?(::ActionDispatch::Request::Utils)
            # Rails 4.1
            data = Request::Utils.deep_munge(Hash.from_xml(request.body.read) || {})
          else
            # Rails 4.0
            data = request.deep_munge(Hash.from_xml(request.body.read) || {})
          end

          data.with_indifferent_access
        else
          false
        end
      rescue Exception => e # XML code block errors
        logger(env).debug "Error occurred while parsing request parameters.\nContents:\n\n#{request.raw_post}"

        raise ActionDispatch::ParamsParser::ParseError.new(e.message, e)
      end

      def content_type_from_legacy_post_data_format_header(env)
        if env['HTTP_X_POST_DATA_FORMAT'].to_s.downcase == 'xml'
          Mime::XML
        end
      end

      def logger(env)
        env['action_dispatch.logger'] || ActiveSupport::Logger.new($stderr)
      end
  end
end
{'fraction_non_alphanumeric': 0.09003215434083602, 'fraction_numerical': 0.0019292604501607716, 'mean_word_length': 2.721291866028708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '7544801', 'n_tokens_mistral': 934, 'n_tokens_neox': 909, 'n_words': 171}