Sha256: f42e28969f55a342bfb0946372ecee040583844bcdde56de6d4b6382ce8a8694

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

require 'rack/request'

module Dassets; end
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'].sub(dassets_base_url, '')
    end

    def dassets_base_url
      Dassets.config.base_url.to_s
    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.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

5 entries across 5 versions & 1 rubygems

Version Path
dassets-0.12.0 lib/dassets/server/request.rb
dassets-0.11.0 lib/dassets/server/request.rb
dassets-0.10.1 lib/dassets/server/request.rb
dassets-0.10.0 lib/dassets/server/request.rb
dassets-0.9.0 lib/dassets/server/request.rb