<commit_before>require 'logger'

module Rack
  class Cors
    def initialize(app, opts={})
      @app = app
      @logger = opts[:logger]
      yield self if block_given?
    end

    def allow
      all_resources << (resources = Resources.new)
      yield resources
    end

    def call(env)
      cors_headers = nil
      if env['HTTP_ORIGIN']
        debug(env) do
          [ 'Incoming Headers:',
            "  Origin: #{env['HTTP_ORIGIN']}",
            "  Access-Control-Request-Method: #{env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']}",
            "  Access-Control-Request-Headers: #{env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"
            ].join("\n")
        end
        if env['REQUEST_METHOD'] == 'OPTIONS'
          if headers = process_preflight(env)
            debug(env) do
              "Preflight Headers:\n" +
                  headers.collect{|kv| "  #{kv.join(': ')}"}.join("\n")
            end
            return [200, headers, []]
          end
        else
          cors_headers = process_cors(env)
        end
      end
      status, headers, body = @app.call env
      headers = headers.merge(cors_headers) if cors_headers
      [status, headers, body]
    end

    protected
      def debug(env, message = nil, &block)
        logger = @logger || env['rack.logger'] || begin
          @logger = ::Logger.new(STDOUT).tap {|logger| logger.level = ::Logger::Severity::DEBUG}
        end
        logger.debug(message, &block)
      end

      def all_resources
        @all_resources ||= []
      end

      def process_preflight(env)
        resource = find_resource(env['HTTP_ORIGIN'], env['PATH_INFO'])
        resource && resource.process_preflight(env)
      end

      def process_cors(env)
        resource = find_resource(env['HTTP_ORIGIN'], env['PATH_INFO'])
        resource.to_headers(env) if resource
      end

      def find_resource(origin, path)
        allowed = all_resources.detect {|r| r.allow_origin?(origin)}<commit_msg>set log level to INFO<commit_after>require 'logger'

module Rack
  class Cors
    def initialize(app, opts={})
      @app = app
      @logger = opts[:logger]
      yield self if block_given?
    end

    def allow
      all_resources << (resources = Resources.new)
      yield resources
    end

    def call(env)
      cors_headers = nil
      if env['HTTP_ORIGIN']
        debug(env) do
          [ 'Incoming Headers:',
            "  Origin: #{env['HTTP_ORIGIN']}",
            "  Access-Control-Request-Method: #{env['HTTP_ACCESS_CONTROL_REQUEST_METHOD']}",
            "  Access-Control-Request-Headers: #{env['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"
            ].join("\n")
        end
        if env['REQUEST_METHOD'] == 'OPTIONS'
          if headers = process_preflight(env)
            debug(env) do
              "Preflight Headers:\n" +
                  headers.collect{|kv| "  #{kv.join(': ')}"}.join("\n")
            end
            return [200, headers, []]
          end
        else
          cors_headers = process_cors(env)
        end
      end
      status, headers, body = @app.call env
      headers = headers.merge(cors_headers) if cors_headers
      [status, headers, body]
    end

    protected
      def debug(env, message = nil, &block)
        logger = @logger || env['rack.logger'] || begin
          @logger = ::Logger.new(STDOUT).tap {|logger| logger.level = ::Logger::Severity::INFO}
        end
        logger.debug(message, &block)
      end

      def all_resources
        @all_resources ||= []
      end

      def process_preflight(env)
        resource = find_resource(env['HTTP_ORIGIN'], env['PATH_INFO'])
        resource && resource.process_preflight(env)
      end

      def process_cors(env)
        resource = find_resource(env['HTTP_ORIGIN'], env['PATH_INFO'])
        resource.to_headers(env) if resource
      end

      def find_resource(origin, path)
        allowed = all_resources.detect {|r| r.allow_origin?(origin)}
{'fraction_non_alphanumeric': 0.12325640375348719, 'fraction_numerical': 0.001521683996956632, 'mean_word_length': 2.167871485943775, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1305698', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1164, 'n_words': 257}