<commit_before>module Rack
  class Parser

    POST_BODY  = 'rack.input'.freeze
    FORM_INPUT = 'rack.request.form_input'.freeze
    FORM_HASH  = 'rack.request.form_hash'.freeze

    JSON_PARSER   = proc { |data| JSON.parse data }
    ERROR_HANDLER = proc { |err, type| [400, {}, ['']] }

    attr_reader :parsers, :handlers, :logger

    def initialize(app, options = {})
      @app      = app
      @parsers  = options.delete(:parsers)  || { %r{json} => JSON_PARSER }
      @handlers = options.delete(:handlers) || {}
      @logger   = options.delete(:logger)
    end

    def call(env)
      dup._call(env)
    end

    def _call(env)
      type   = Rack::Request.new(env).media_type
      parser = parsers.detect { |content_type, _| type.match(content_type) } if type
      return @app.call(env) unless parser
      body = env[POST_BODY].read ; env[POST_BODY].rewind
      return @app.call(env) unless body && !body.empty?
      begin
        parsed = parser.last.call body
        env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY]
      rescue StandardError => e
        warn! e, type
        handler   = handlers.detect { |content_type, _|  type.match(content_type) }
        handler ||= ['default', ERROR_HANDLER]
        return handler.last.call(e, type)
      end
      @app.call env
    end

    # Private: send a warning out to the logger
    #
    # error - Exception object
    # type  - String of the Content-Type
    #
    def warn!(error, type)
      return unless logger
      message = "[Rack::Parser] Error on %s : %s" % [type, error.to_s]
      logger.warn message
    end
  end
end
<commit_msg>remove the need to shadow call<commit_after>module Rack
  class Parser

    POST_BODY  = 'rack.input'.freeze
    FORM_INPUT = 'rack.request.form_input'.freeze
    FORM_HASH  = 'rack.request.form_hash'.freeze

    JSON_PARSER   = proc { |data| JSON.parse data }
    ERROR_HANDLER = proc { |err, type| [400, {}, ['']] }

    attr_reader :parsers, :handlers, :logger

    def initialize(app, options = {})
      @app      = app
      @parsers  = options.delete(:parsers)  || { %r{json} => JSON_PARSER }
      @handlers = options.delete(:handlers) || {}
      @logger   = options.delete(:logger)
    end

    def call(env)
      type   = Rack::Request.new(env).media_type
      parser = parsers.detect { |content_type, _| type.match(content_type) } if type
      return @app.call(env) unless parser
      body = env[POST_BODY].read ; env[POST_BODY].rewind
      return @app.call(env) unless body && !body.empty?
      begin
        parsed = parser.last.call body
        env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY]
      rescue StandardError => e
        warn! e, type
        handler   = handlers.detect { |content_type, _|  type.match(content_type) }
        handler ||= ['default', ERROR_HANDLER]
        return handler.last.call(e, type)
      end
      @app.call env
    end

    # Private: send a warning out to the logger
    #
    # error - Exception object
    # type  - String of the Content-Type
    #
    def warn!(error, type)
      return unless logger
      message = "[Rack::Parser] Error on %s : %s" % [type, error.to_s]
      logger.warn message
    end
  end
end

{'fraction_non_alphanumeric': 0.12259466170080695, 'fraction_numerical': 0.00186219739292365, 'mean_word_length': 2.488095238095238, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1955604', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1060, 'n_words': 257}