<commit_before>require 'addressable/uri'

module Rack
  class CanonicalHost
    class Redirect
      HTML_TEMPLATE = <<-HTML.gsub(/^\s+/, '')
        <!DOCTYPE html>
        <html lang="en-US">
          <head><title>301 Moved Permanently</title></head>
          <body>
            <h1>Moved Permanently</h1>
            <p>The document has moved <a href="%s">here</a>.</p>
          </body>
        </html>
      HTML

      def initialize(env, host, options={})
        @env = env
        @host = host
        @force_ssl = options[:force_ssl]
        @ignore = Array(options[:ignore])
        @if = Array(options[:if])
      end

      def canonical?
        (known? && ssl?) || ignored? || !conditions_match?
      end

      def response
        headers = { 'Location' => new_url, 'Content-Type' => 'text/html' }
        [301, headers, [HTML_TEMPLATE % new_url]]
      end

    private

      def known?
        @host.nil? || request_uri.host == @host
      end

      def ssl?
        !@force_ssl || request_uri.scheme == "https"
      end

      def ignored?
        @ignore && @ignore.include?(request_uri.host)
      end

      def conditions_match?
        return true unless @if.size > 0
        @if.include?( request_uri.host ) || any_regexp_match?( @if, request_uri.host )
      end
      private :conditions_match?

      def any_regexp_match?( regexp_array, string )
        regexp_array.any?{ |r| string[r] }
      end
      private :any_regexp_match?

      def new_url
        request_uri.tap { |uri|
          uri.host = @host if @host
          uri.scheme = "https" if @force_ssl
        }.to_s
      end

      def request_uri
        Addressable::URI.parse(Rack::Request.new(@env).url)
      end
    end
  end
end<commit_msg>Cache the request_uri construction for subsequent calls.<commit_after>require 'addressable/uri'

module Rack
  class CanonicalHost
    class Redirect
      HTML_TEMPLATE = <<-HTML.gsub(/^\s+/, '')
        <!DOCTYPE html>
        <html lang="en-US">
          <head><title>301 Moved Permanently</title></head>
          <body>
            <h1>Moved Permanently</h1>
            <p>The document has moved <a href="%s">here</a>.</p>
          </body>
        </html>
      HTML

      def initialize(env, host, options={})
        @env = env
        @host = host
        @force_ssl = options[:force_ssl]
        @ignore = Array(options[:ignore])
        @if = Array(options[:if])
      end

      def canonical?
        (known? && ssl?) || ignored? || !conditions_match?
      end

      def response
        headers = { 'Location' => new_url, 'Content-Type' => 'text/html' }
        [301, headers, [HTML_TEMPLATE % new_url]]
      end

    private

      def known?
        @host.nil? || request_uri.host == @host
      end

      def ssl?
        !@force_ssl || request_uri.scheme == "https"
      end

      def ignored?
        @ignore && @ignore.include?(request_uri.host)
      end

      def conditions_match?
        return true unless @if.size > 0
        @if.include?( request_uri.host ) || any_regexp_match?( @if, request_uri.host )
      end
      private :conditions_match?

      def any_regexp_match?( regexp_array, string )
        regexp_array.any?{ |r| string[r] }
      end
      private :any_regexp_match?

      def new_url
        request_uri.tap { |uri|
          uri.host = @host if @host
          uri.scheme = "https" if @force_ssl
        }.to_s
      end

      def request_uri
        @request_uri ||= Addressable::URI.parse(Rack::Request.new(@env).url)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.12858750703432753, 'fraction_numerical': 0.005064715813168261, 'mean_word_length': 1.9575707154742097, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 39, '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': '2537823', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1126, 'n_words': 260}