class Rack::SteadyETag

Based on Rack::Etag github.com/rack/rack/blob/master/lib/rack/etag.rb

Automatically sets the ETag header on all String bodies.

The ETag header is skipped if ETag or Last-Modified headers are sent or if a sendfile body (body.responds_to :to_path) is given (since such cases should be handled by apache/nginx).

Constants

DEFAULT_CACHE_CONTROL
IGNORE_PATTERNS

Public Class Methods

new(app, no_digest_cache_control: nil, digest_cache_control: DEFAULT_CACHE_CONTROL, ignore_patterns: IGNORE_PATTERNS.dup) click to toggle source
# File lib/rack/steady_etag.rb, line 27
def initialize(app, no_digest_cache_control: nil, digest_cache_control: DEFAULT_CACHE_CONTROL, ignore_patterns: IGNORE_PATTERNS.dup)
  @app = app
  @digest_cache_control = digest_cache_control
  @no_digest_cache_control = no_digest_cache_control
  @ignore_patterns = ignore_patterns
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/steady_etag.rb, line 34
def call(env)
  status, headers, body = @app.call(env)
  headers = Utils::HeaderHash[headers]
  session = env[RACK_SESSION]

  if etag_status?(status) && etag_body?(body) && !skip_caching?(headers)
    original_body = body
    digest, new_body = digest_body(body, headers, session)
    body = Rack::BodyProxy.new(new_body) do
      original_body.close if original_body.respond_to?(:close)
    end
    headers[ETAG] = %(W/"#{digest}") if digest
  end

  [status, headers, body]
end

Private Instance Methods

cache_control_private?(headers) click to toggle source
# File lib/rack/steady_etag.rb, line 73
def cache_control_private?(headers)
  headers[CACHE_CONTROL] && headers[CACHE_CONTROL] =~ /\bprivate\b/
end
digest_body(body, headers, session) click to toggle source
# File lib/rack/steady_etag.rb, line 77
def digest_body(body, headers, session)
  parts = []
  digest = nil

  body.each do |part|
    parts << part

    if part.present?
      set_cache_control_with_digest(headers)

      if cache_control_private?(headers)
        part = strip_ignore_patterns(part)
      end

      unless digest
        digest = Digest::SHA256.new

        if session && (session_id = session['session_id'])
          digest << session_id.to_s
        end
      end

      digest << part
    end
  end

  if digest
    digest = digest.hexdigest.byteslice(0,32)
  else
    set_cache_control_without_digest(headers)
  end

  [digest, parts]
end
etag_body?(body) click to toggle source
# File lib/rack/steady_etag.rb, line 65
def etag_body?(body)
  !body.respond_to?(:to_path)
end
etag_status?(status) click to toggle source
# File lib/rack/steady_etag.rb, line 61
def etag_status?(status)
  status == 200 || status == 201
end
set_cache_control_with_digest(headers) click to toggle source
# File lib/rack/steady_etag.rb, line 53
def set_cache_control_with_digest(headers)
  headers[CACHE_CONTROL] ||= @digest_cache_control if @digest_cache_control
end
set_cache_control_without_digest(headers) click to toggle source
# File lib/rack/steady_etag.rb, line 57
def set_cache_control_without_digest(headers)
  headers[CACHE_CONTROL] ||= @no_digest_cache_control if @no_digest_cache_control
end
skip_caching?(headers) click to toggle source
# File lib/rack/steady_etag.rb, line 69
def skip_caching?(headers)
  headers.key?(ETAG) || headers.key?('Last-Modified')
end
strip_ignore_patterns(html) click to toggle source
# File lib/rack/steady_etag.rb, line 112
def strip_ignore_patterns(html)
  @ignore_patterns.each do |ignore_pattern|
    if ignore_pattern.respond_to?(:call)
      html = ignore_pattern.call(html)
    else
      html = html.gsub(ignore_pattern, '')
    end
  end
  html
end