<commit_before>module Asset
  # The Router class is a small Rack middleware that matches the asset URLs
  # and serves the content, compressed if you are in production mode.
  class Router

    # Mime types for responses
    MIME = {'js' => 'application/javascript;charset=utf-8', 'css' => 'text/css;charset=utf-8', 'txt' => 'text/plain;charset=utf-8'}

    # Init
    def initialize(app)
      @app = app
    end

    # Call
    def call(env)
      # Setting up request
      @request = Rack::Request.new(env)

      # The routes
      case @request.path_info

      # Match /assets?/:type/path
      when /^(\/assets)?\/(js|css)\/(.+)/
        # Extract type and path
        type, path = $2, $3

        # Extract digest key and remove from path
        path.gsub!("-#{@key = $1}", '') if path =~ /-([a-f0-9]{32})\.(css|js)$/

        # Find the item
        item = ::Asset.manifest.find{|i| i.path == path and i.type == type}

        # Return the content or not found
        item ? found(item) : not_found

      # Bounce favicon requests
      when (::Asset.favicon and /^\/favicon\.ico$/)
        not_found

      # Return a standard robots.txt
      when (::Asset.robots and /^\/robots\.txt$/)
        robots

      else
        # No routes found, pass down the middleware stack
        @app.call(env)
      end
    end

    private

    # Found
    def found(item)
      content = item.content(!!@key)
      [ 200, {'Content-Type' => MIME[item.type],
        'Content-Length' => content.size,
        'Cache-Control' => 'public, must-revalidate, max-age=3600',
        'Expires' => (Time.now.utc + 86400).httpdate,
        'Last-Modified' => item.modified.httpdate
      }, [content]]
    end

    # Not found
    def not_found(path = '@')
      [404, {'Content-Type' => MIME['txt'], 'Content-Length' => 0}, []]
    end

    # Robots
    def robots
      s = %{Sitemap: #{@request.scheme}://#{@request.host}/sitemap.xml}
      [200, {'Content-Type' => MIME['txt'],'Content-Length' => s.size}, [s]]
    end

  end
end
<commit_msg>Added nosniff and longer expiration<commit_after>module Asset
  # The Router class is a small Rack middleware that matches the asset URLs
  # and serves the content, compressed if you are in production mode.
  class Router

    # Mime types for responses
    MIME = {'js' => 'application/javascript;charset=utf-8', 'css' => 'text/css;charset=utf-8', 'txt' => 'text/plain;charset=utf-8'}

    # Init
    def initialize(app)
      @app = app
    end

    # Call
    def call(env)
      # Setting up request
      @request = Rack::Request.new(env)

      # The routes
      case @request.path_info

      # Match /assets?/:type/path
      when /^(\/assets)?\/(js|css)\/(.+)/
        # Extract type and path
        type, path = $2, $3

        # Extract digest key and remove from path
        path.gsub!("-#{@key = $1}", '') if path =~ /-([a-f0-9]{32})\.(css|js)$/

        # Find the item
        item = ::Asset.manifest.find{|i| i.path == path and i.type == type}

        # Return the content or not found
        item ? found(item) : not_found

      # Bounce favicon requests
      when (::Asset.favicon and /^\/favicon\.ico$/)
        not_found

      # Return a standard robots.txt
      when (::Asset.robots and /^\/robots\.txt$/)
        robots

      else
        # No routes found, pass down the middleware stack
        @app.call(env)
      end
    end

    private

    # Found
    def found(item)
      content = item.content(!!@key)
      [ 200, {'Content-Type' => MIME[item.type],
        'X-Content-Type-Options' => 'nosniff',
        'Content-Length' => content.size,
        'Cache-Control' => 'public, must-revalidate, max-age=86400',
        'Expires' => (Time.now.utc + (86400 * 30)).httpdate,
        'Last-Modified' => item.modified.httpdate,
      }, [content]]
    end

    # Not found
    def not_found(path = '@')
      [404, {'Content-Type' => MIME['txt'], 'Content-Length' => 0}, []]
    end

    # Robots
    def robots
      s = %{Sitemap: #{@request.scheme}://#{@request.host}/sitemap.xml}
      [200, {'Content-Type' => MIME['txt'],'Content-Length' => s.size}, [s]]
    end

  end
end

{'fraction_non_alphanumeric': 0.15976900866217517, 'fraction_numerical': 0.01467757459095284, 'mean_word_length': 2.20508866615266, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6571588', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1352, 'n_words': 357}