<commit_before>module Troy
  class Server
    attr_reader :root, :request

    def initialize(root)
      @root = Pathname.new(root)
    end

    def call(env)
      @request = Rack::Request.new(env)
      process
    end

    def render(status, content_type, path)
      last_modified = path.mtime.httpdate
      return [304, {}, []] if request.env["HTTP_IF_MODIFIED_SINCE"] == last_modified

      headers = {
        "Content-Type" => content_type,
        "Last-Modified" => last_modified
      }

      content = request.head? ? [] : [path.read]

      [status, headers, content]
    end

    def normalized_path
      path = request.path.gsub(%r[/$], "")
      path << "?#{request.query_string}" unless request.query_string.empty?
      path
    end

    def process
      path = request.path[%r[^/(.*?)/?$], 1]
      path = "index" if path == ""
      path = root.join(path)

      if request.path != "/" && request.path.end_with?("/")
        [301, {
          "Content-Type" => "text/html",
          "Location" => normalized_path
        }, []]
      elsif (_path = Pathname.new("#{path}.html")).file?
        render(200, "text/html", _path)
      elsif (_path = Pathname.new("#{path}.xml")).file?
        render(200, "text/xml", _path)
      elsif path.file? && path.extname !~ /\.(html?|xml)$/
        render(200, Rack::Mime.mime_type(path.extname, "text/plain"), path)
      else
        render(404, "text/html", root.join("404.html"))
      end
    rescue Exception => error
      render(500, "text/html", root.join("500.html"))
    end
  end
end<commit_msg>Small refactoring on the redirect part.<commit_after>module Troy
  class Server
    attr_reader :root, :request

    def initialize(root)
      @root = Pathname.new(root)
    end

    def call(env)
      @request = Rack::Request.new(env)
      process
    end

    def render(status, content_type, path)
      last_modified = path.mtime.httpdate
      return [304, {}, []] if request.env["HTTP_IF_MODIFIED_SINCE"] == last_modified

      headers = {
        "Content-Type" => content_type,
        "Last-Modified" => last_modified
      }

      content = request.head? ? [] : [path.read]

      [status, headers, content]
    end

    def normalized_path
      path = request.path.gsub(%r[/$], "")
      path << "?#{request.query_string}" unless request.query_string.empty?
      path
    end

    def redirect(path)
      [301, {"Content-Type" => "text/html", "Location" => path}, []]
    end

    def process
      path = request.path[%r[^/(.*?)/?$], 1]
      path = "index" if path == ""
      path = root.join(path)

      if request.path != "/" && request.path.end_with?("/")
        redirect normalized_path
      elsif (_path = Pathname.new("#{path}.html")).file?
        render(200, "text/html", _path)
      elsif (_path = Pathname.new("#{path}.xml")).file?
        render(200, "text/xml", _path)
      elsif path.file? && path.extname !~ /\.(html?|xml)$/
        render(200, Rack::Mime.mime_type(path.extname, "text/plain"), path)
      else
        render(404, "text/html", root.join("404.html"))
      end
    rescue Exception => error
      render(500, "text/html", root.join("500.html"))
    end
  end
end
{'fraction_non_alphanumeric': 0.16572681704260653, 'fraction_numerical': 0.017543859649122806, 'mean_word_length': 2.43702906350915, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '2051618', 'n_tokens_mistral': 1106, 'n_tokens_neox': 1044, 'n_words': 217}