In Files

Parent

Class Index [+]

Quicksearch

Rack::ETag

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).

On initialization, you can pass two parameters: a Cache-Control directive used when Etag is absent and a directive when it is present. The first defaults to nil, while the second defaults to “max-age=0, private, must-revalidate“

Constants

DEFAULT_CACHE_CONTROL

Public Class Methods

new(app, no_cache_control = nil, cache_control = DEFAULT_CACHE_CONTROL) click to toggle source
    # File lib/rack/etag.rb, line 16
16:     def initialize(app, no_cache_control = nil, cache_control = DEFAULT_CACHE_CONTROL)
17:       @app = app
18:       @cache_control = cache_control
19:       @no_cache_control = no_cache_control
20:     end

Public Instance Methods

call(env) click to toggle source
    # File lib/rack/etag.rb, line 22
22:     def call(env)
23:       status, headers, body = @app.call(env)
24: 
25:       if etag_status?(status) && etag_body?(body) && !skip_caching?(headers)
26:         digest, body = digest_body(body)
27:         headers['ETag'] = %("#{digest}") if digest
28:       end
29: 
30:       unless headers['Cache-Control']
31:         headers['Cache-Control'] =
32:           (digest ? @cache_control : @no_cache_control) || []
33:       end
34: 
35:       [status, headers, body]
36:     end

Private Instance Methods

digest_body(body) click to toggle source
    # File lib/rack/etag.rb, line 53
53:       def digest_body(body)
54:         parts = []
55:         body.each { |part| parts << part }
56:         string_body = parts.join
57:         digest = Digest::MD5.hexdigest(string_body) unless string_body.empty?
58:         [digest, parts]
59:       end
etag_body?(body) click to toggle source
    # File lib/rack/etag.rb, line 44
44:       def etag_body?(body)
45:         !body.respond_to?(:to_path)
46:       end
etag_status?(status) click to toggle source
    # File lib/rack/etag.rb, line 40
40:       def etag_status?(status)
41:         status == 200 || status == 201
42:       end
skip_caching?(headers) click to toggle source
    # File lib/rack/etag.rb, line 48
48:       def skip_caching?(headers)
49:         headers['Cache-Control'] == 'no-cache' ||
50:           headers.key?('ETag') || headers.key?('Last-Modified')
51:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.