<commit_before>require "rack/cloudflare/version"

module Rack
  module Cloudflare
    class CacheControl
      def initialize(app)
        @app = app
      end

      def call(env)
        status, headers, body = @app.call(env)
        if status.to_s == '404'
          [status, headers.merge('Cache-Control' => 'no-cache'), body]
        else
          [status, headers, body]
        end
      end
    end

    class XForwardedFor
      CLOUDFLARE_IP_RANGES = %w(
        172.16.17.32/21
        192.168.3.11/20
        172.16.31.10/22
        172.16.17.32/22
        192.168.127.12/22
        192.168.127.12/18
        172.16.17.32/18
        172.16.58.3/20
        172.16.17.32/20
        172.16.31.10/22
        172.16.31.10/17
        192.168.3.11/15
        192.168.3.11/12
      ).map do |range|
        IPAddr.new(range)
      end

      def initialize(app)
        @app = app
      end

      def call(env)
        fwd = (env['HTTP_X_FORWARDED_FOR'] || '').split(/, /)
        if env['HTTP_CF_CONNECTING_IP'] && fwd.last && CLOUDFLARE_IP_RANGES.any? { |range| range.include?(fwd.last) }
          env['HTTP_REMOTE_ADDR_BEFORE_CF'] = env['REMOTE_ADDR']
          env['HTTP_X_FORWARDED_FOR_BEFORE_CF'] = env['HTTP_X_FORWARDED_FOR']
          env['REMOTE_ADDR'] = env['HTTP_CF_CONNECTING_IP']
          env['HTTP_X_FORWARDED_FOR'] = env['HTTP_CF_CONNECTING_IP']
        end
        @app.call(env)
      end
    end
  end
end
<commit_msg>Add IPv6 addresses.<commit_after>require "rack/cloudflare/version"

module Rack
  module Cloudflare
    class CacheControl
      def initialize(app)
        @app = app
      end

      def call(env)
        status, headers, body = @app.call(env)
        if status.to_s == '404'
          [status, headers.merge('Cache-Control' => 'no-cache'), body]
        else
          [status, headers, body]
        end
      end
    end

    class XForwardedFor
      CLOUDFLARE_IP_RANGES = %w(
        172.16.17.32/21
        192.168.3.11/20
        172.16.31.10/22
        172.16.17.32/22
        192.168.127.12/22
        192.168.127.12/18
        172.16.17.32/18
        172.16.58.3/20
        172.16.17.32/20
        172.16.31.10/22
        172.16.31.10/17
        192.168.3.11/15
        192.168.3.11/12
        2400:cb00::/32
        2606:4700::/32
        2803:f800::/32
        2405:b500::/32
        2405:8100::/32
      ).map do |range|
        IPAddr.new(range)
      end

      def initialize(app)
        @app = app
      end

      def call(env)
        fwd = (env['HTTP_X_FORWARDED_FOR'] || '').split(/, /)
        if env['HTTP_CF_CONNECTING_IP'] && fwd.last && CLOUDFLARE_IP_RANGES.any? { |range| range.include?(fwd.last) }
          env['HTTP_REMOTE_ADDR_BEFORE_CF'] = env['REMOTE_ADDR']
          env['HTTP_X_FORWARDED_FOR_BEFORE_CF'] = env['HTTP_X_FORWARDED_FOR']
          env['REMOTE_ADDR'] = env['HTTP_CF_CONNECTING_IP']
          env['HTTP_X_FORWARDED_FOR'] = env['HTTP_CF_CONNECTING_IP']
        end
        @app.call(env)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.13260219341974078, 'fraction_numerical': 0.11465603190428714, 'mean_word_length': 1.7818853974121995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 26, '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': '2188775', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1131, 'n_words': 177}