Sha256: 2f04d0618f43a43d27f88298992f6b9fed37b7209a5e2c2ce49c51ab5c8918d3

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

require 'rack/request'

class Dassets::Server

  class Request < Rack::Request

    # The HTTP request method. This is the standard implementation of this
    # method but is respecified here due to libraries that attempt to modify
    # the behavior to respect POST tunnel method specifiers. We always want
    # the real request method.
    def request_method; @env['REQUEST_METHOD']; end
    def path_info;      @env['PATH_INFO'];      end

    # Determine if the request is for an asset file
    # This will be called on every request so speed is an issue
    # - first check if the request is a GET or HEAD (fast)
    # - then check if for a digested asset resource (kinda fast)
    # - then check if source exists for the digested asset (slower)
    def for_asset_file?
      !!((get? || head?) && for_digested_asset? && asset_file.source_cache.exists?)
    end

    def asset_path
      @asset_path ||= path_digest_match.captures.select{ |m| !m.empty? }.join
    end

    def asset_file
      @asset_file ||= Dassets[asset_path]
    end

    private

    def for_digested_asset?
      !path_digest_match.captures.empty?
    end

    def path_digest_match
      @path_digest_match ||= begin
        path_info.match(/\/(.+)-[a-f0-9]{32}(\..+|)$/i) || NullDigestMatch.new
      end
    end

    class NullDigestMatch
      def captures; []; end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dassets-0.5.0 lib/dassets/server/request.rb
dassets-0.4.1 lib/dassets/server/request.rb
dassets-0.4.0 lib/dassets/server/request.rb