forked from rack/rack-contrib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Rack::ETag now supports SHA-1 and SHA-2 in addition to MD5"
Let's keep ETag simple. See the following for discussion: http://github.com/jamesarosen/rack-contrib/commit/5f2aa98
- Loading branch information
Showing
2 changed files
with
9 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,20 @@ | ||
require 'digest/md5' | ||
|
||
module Rack | ||
# Automatically sets the ETag header on all String bodies | ||
# if none is set. | ||
# | ||
# By default, uses an MD5 hash to generate the ETag. | ||
# | ||
# @param [#call] app the underlying Rack application. Required. | ||
# @param [Symbol] digest the digest to use. Optional. Options are [:md5, :sha1, :sha256, :sha384, :sha512]. | ||
class ETag | ||
def initialize(app, digest = :md5) | ||
def initialize(app) | ||
@app = app | ||
load_digest(digest) | ||
@digest_method = self.method("#{digest}_hash") | ||
end | ||
|
||
def call(env) | ||
status, headers, body = @app.call(env) | ||
|
||
if !headers.has_key?('ETag') && body.is_a?(String) | ||
headers['ETag'] = %("#{@digest_method.call(body)}") | ||
headers['ETag'] = %("#{Digest::MD5.hexdigest(body)}") | ||
end | ||
|
||
[status, headers, body] | ||
end | ||
|
||
private | ||
|
||
def load_digest(digest) | ||
case digest | ||
when :md5 | ||
require 'digest/md5' | ||
when :sha1 | ||
require 'digest/sha1' | ||
when :sha256, :sha384, :sha512 | ||
require 'digest/sha2' | ||
else | ||
raise ArgumentError.new("Digest #{digest} is not supported.") | ||
end | ||
end | ||
|
||
def md5_hash(body) | ||
Digest::MD5.hexdigest(body) | ||
end | ||
|
||
def sha1_hash(body) | ||
Digest::SHA1.hexdigest(body) | ||
end | ||
|
||
def sha256_hash(body) | ||
Digest::SHA2.hexdigest(body) | ||
end | ||
|
||
def sha384_hash(body) | ||
(Digest::SHA2.new(384) << body).to_s | ||
end | ||
|
||
def sha512_hash(body) | ||
(Digest::SHA2.new(512) << body).to_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters