summary refs log tree commit
path: root/lib/rack/etag.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rack/etag.rb')
-rw-r--r--lib/rack/etag.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/rack/etag.rb b/lib/rack/etag.rb
new file mode 100644
index 00000000..06dbc6aa
--- /dev/null
+++ b/lib/rack/etag.rb
@@ -0,0 +1,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