Sha256: 3f86f6aff2980dcbb1088158ea2f07dfd0cd2019adb0269e794b38cb67fd70eb

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'digest/md5'
require 'digest/sha1'

module Rack
  module Contrib
    #
    # 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
    #
    class ContentHash
      VERSION = '0.0.1'

      # Initialize the middleware, pass a Hash of algorithms in the second
      # parameter. Ex: .new app, md5: true, sha1: false
      def initialize app, algorithms = {}
        @app = app
        @algo= algorithms
      end

      # Hash the body of your response, and append the headers to the Hash
      def call env
        status, headers, body = @app.call env

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

        [status, headers, body]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-content-hash-0.0.1 lib/rack/contrib/content_hash.rb