<commit_before># The Httpbl middleware 

class HttpBL
  autoload :Resolv, 'resolv'
  
  def initialize(app, options = {})
    @app = app
    @options = {:blocked_search_engines => [],
                :age_threshold => 10,
                :threat_level_threshold => 5,
                :deny_types => [1, 2, 4, 8, 16, 32, 64, 128]
                # 8..128 aren't used as of 3/2009, but might be used in the future
                }.merge(options)
    raise "Missing :api_key for Http:BL middleware" unless @options[:api_key]
  end
  
  def call(env)
    dup._call(env)
  end
  
  def _call(env)
    @resolver = Resolv::DNS.new
    query = @options[:api_key] + '.' + Rack::Request.new(env).ip.split('.').reverse.join('.') + '.dnsbl.httpbl.org'
    @bl_response = (@resolver.getaddress(query).to_s rescue nil )
    if @bl_response and blocked?(@bl_response)
      [403, {"Content-Type" => "text/html"}, "<h1>403 Forbidden</h1> Request IP is listed as suspicious by <a href='http://projecthoneypot.org'>Project Honeypot</a>"]
    else
      @app.call(env)
    end
  end
  
  def blocked?(response)
    response = response.split('.').collect!(&:to_i)
    if response[0] == 127 
      if response[3] == 0
        @blocked = true if @options[:blocked_search_engines].include? response[2]
      else 
        @age = true if response[1] < @options[:age_threshold]
        @threat = true if response[2] > @options[:threat_level_threshold]
        @options[:deny_types].each do |key|<commit_msg>Ok, the real 0.1.0, hopefully<commit_after># The Httpbl middleware 

class HttpBL
  autoload :Resolv, 'resolv'
  
  def initialize(app, options = {})
    @app = app
    @options = {:blocked_search_engines => [],
                :age_threshold => 10,
                :threat_level_threshold => 3,
                :deny_types => [1, 2, 4, 8, 16, 32, 64, 128]
                # 8..128 aren't used as of 3/2009, but might be used in the future
                }.merge(options)
    raise "Missing :api_key for Http:BL middleware" unless @options[:api_key]
  end
  
  def call(env)
    dup._call(env)
  end
  
  def _call(env)
    @resolver = Resolv::DNS.new
    query = @options[:api_key] + '.' + Rack::Request.new(env).ip.split('.').reverse.join('.') + '.dnsbl.httpbl.org'
    @bl_response = (@resolver.getaddress(query).to_s rescue nil )
    if @bl_response and blocked?(@bl_response)
      [403, {"Content-Type" => "text/html"}, "<h1>403 Forbidden</h1> Request IP is listed as suspicious by <a href='http://projecthoneypot.org'>Project Honeypot</a>"]
    else
      @app.call(env)
    end
  end
  
  def blocked?(response)
    response = response.split('.').collect!(&:to_i)
    if response[0] == 127 
      if response[3] == 0
        @blocked = true if @options[:blocked_search_engines].include? response[2]
      else 
        @age = true if response[1] < @options[:age_threshold]
        @threat = true if response[2] > @options[:threat_level_threshold]
        @options[:deny_types].each do |key|
{'fraction_non_alphanumeric': 0.1488933601609658, 'fraction_numerical': 0.029175050301810865, 'mean_word_length': 2.752201257861635, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3569944', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1007, 'n_words': 243}