class Rack::Contrib::ContentHash

Add Content-MD5 and Content-SHA1 headers, containing the hashes of the body’s response.

Best installed as your very last middleware piece to ensure the contents doesn’t become invalidated by another middleware piece.

Ex:

use Rack::Contrib::ContentHash, md5: true, sha1: true

Constants

VERSION

Public Class Methods

new(app, algorithms = {}) click to toggle source

Initialize the middleware, pass a Hash of algorithms in the second parameter. Ex: .new app, md5: true, sha1: false

# File lib/rack/contrib/content_hash.rb, line 22
def initialize app, algorithms = {}
  @app = app
  @algo= algorithms
end

Public Instance Methods

call(env) click to toggle source

Hash the body of your response, and append the headers to the Hash

# File lib/rack/contrib/content_hash.rb, line 28
def call env
  status, headers, body = @app.call env

  hashbody = body.join ""
  headers['Content-MD5'] = Digest::MD5.hexdigest(hashbody) if @algo[:md5]
  headers['Content-SHA1'] = Digest::SHA1.hexdigest(hashbody) if @algo[:sha1]

  [status, headers, body]
end