Object
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“
# 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
# 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
# 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
# File lib/rack/etag.rb, line 44 44: def etag_body?(body) 45: !body.respond_to?(:to_path) 46: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.