<commit_before>require "rack"
require "rack/request"

class SSLEnforcer::Enforcer
  def initialize(app, options = {})
    @options              = { :subdomain => [] }.merge(options)
    @app                  = app
    @subdomains           = @options[:subdomain] || []
    @subdomain_exceptions = @options[:subdomain_exceptions] || []
  end

  def call(env)
    # if the domain is already using SSL don't do anything
    if url_is_ok?(env)
      @app.call(env)
    else
      # if the domain is NOT currently using SSL then we need to redirect the request
      req     = Rack::Request.new(env)
      headers = { "Location" => req.url.gsub(/^http:/, "https:") }

      [301, headers, []]
    end
  end

  private # -----------------------------------------------

  def url_is_ok?(env)
    tld       = get_top_level_domain(env["SERVER_NAME"])
    subdomain = get_subdomain(env["SERVER_NAME"], tld)

    # Hack to deal with heroku redirect issues.
    # http://rack.lighthouseapp.com/projects/22435/tickets/101
    scheme    = "https" if env["SERVER_PORT"] == "443"
    scheme    = env["HTTP_X_FORWARDED_PROTO"] if env["HTTP_X_FORWARDED_PROTO"]

    # If the subdomain is in the list of HTTPS enforced subs, check for HTTPS
    # otherwise, return true
    if @subdomains.index(subdomain).nil?
      return true
    else
      return scheme == "https"
    end
  end

  # return the subdomain regardless of how many levels deep it is
  def get_subdomain(server_name, tld)
    return server_name.gsub /\.?#{tld}$/, ""
  end

  # We will break the server URL into separate parts, reverse them and then
  # reconstruct the TLD (Top Level Domain) from there.  This should allow
  # for support of domains that extend beyond a third level
  # (i.e. - secure.my.domain.com)
  def get_top_level_domain(server_name)
    domain_parts = server_name.split(".").reverse
    return [domain_parts.second, domain_parts.first].join(".")
  end
end<commit_msg>updated logic to include exclusions<commit_after>require "rack"
require "rack/request"

class SSLEnforcer::Enforcer
  def initialize(app, options = {})
    @options    = { :only => [] }.merge(options)
    @app        = app
    @only       = @options[:only] || []
    @exceptions = @options[:except] || []
  end

  def call(env)
    # if the domain is already using SSL don't do anything
    if url_is_ok?(env)
      @app.call(env)
    else
      # if the domain is NOT currently using SSL then we need to redirect the request
      req     = Rack::Request.new(env)
      headers = { "Location" => req.url.gsub(/^http:/, "https:") }

      [301, headers, []]
    end
  end

  private # -----------------------------------------------

  def url_is_ok?(env)
    tld       = get_top_level_domain(env["SERVER_NAME"])
    subdomain = get_subdomain(env["SERVER_NAME"], tld)

    # Hack to deal with heroku redirect issues.
    # http://rack.lighthouseapp.com/projects/22435/tickets/101
    scheme    = "https" if env["SERVER_PORT"] == "443"
    scheme    = env["HTTP_X_FORWARDED_PROTO"] if env["HTTP_X_FORWARDED_PROTO"]

    # If the "only" and or "exceptions" options have not been passed, then
    # we want to force SSL on ALL subdomains
    return false if @only.empty? && @exceptions.empty?

    ## Return the current scheme test restuls if the
    ## subdomain is in the "only" list
    return scheme == "https" if @only.include?(subdomain)

    ## Return true if the subdomain is in in the "except" list
    return true if @exceptions.include?(subdomain)
  end

  # return the subdomain regardless of how many levels deep it is
  def get_subdomain(server_name, tld)
    return server_name.gsub /\.?#{tld}$/, ""
  end

  # We will break the server URL into separate parts, reverse them and then
  # reconstruct the TLD (Top Level Domain) from there.  This should allow
  # for support of domains that extend beyond a third level
  # (i.e. - secure.my.domain.com)
  def get_top_level_domain(server_name)
    domain_parts = server_name.split(".").reverse
    return [domain_parts.second, domain_parts.first].join(".")
  end
end
{'fraction_non_alphanumeric': 0.13357933579335793, 'fraction_numerical': 0.006888068880688807, 'mean_word_length': 2.771799628942486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1049434', 'n_tokens_mistral': 1301, 'n_tokens_neox': 1247, 'n_words': 386}