rack.git  about / heads / tags
a modular Ruby webserver interface
blob 06dbc6aa5f8b75357982df77484219b574ccbde6 499 bytes (raw)
$ git show rack.io:lib/rack/etag.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
require 'digest/md5'

module Rack
  # Automatically sets the ETag header on all String bodies
  class ETag
    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)

      if !headers.has_key?('ETag')
        parts = []
        body.each { |part| parts << part.to_s }
        headers['ETag'] = %("#{Digest::MD5.hexdigest(parts.join(""))}")
        [status, headers, parts]
      else
        [status, headers, body]
      end
    end
  end
end

git clone https://yhbt.net/rack.git