diff --git a/vendor/rails/actionpack/lib/action_controller/base.rb b/vendor/rails/actionpack/lib/action_controller/base.rb
index a84d876e..4c6ecd73 100755
--- a/vendor/rails/actionpack/lib/action_controller/base.rb
+++ b/vendor/rails/actionpack/lib/action_controller/base.rb
@@ -7,6 +7,7 @@ require 'action_controller/url_rewriter'
require 'action_controller/status_codes'
require 'drb'
require 'set'
+require 'md5'
module ActionController #:nodoc:
class ActionControllerError < StandardError #:nodoc:
@@ -599,6 +600,12 @@ module ActionController #:nodoc:
# _Deprecation_ _notice_: This used to have the signatures
# render_partial(partial_path = default_template_name, object = nil, local_assigns = {}) and
# render_partial_collection(partial_name, collection, partial_spacer_template = nil, local_assigns = {}).
+ # == Automatic etagging
+ #
+ # Rendering will automatically insert the etag header on 200 OK responses. The etag is calculated using MD5 of the
+ # response body. If a request comes in that has a matching etag, the response will be changed to a 304 Not Modified
+ # and the response body will be set to an empty string.
+ #
#
# === Rendering a template
#
@@ -822,6 +829,16 @@ module ActionController #:nodoc:
else
response.body = text
end
+ if response.headers['Status'] == "200 OK" && response.body.size > 0
+ response.headers['Etag'] = "\"#{MD5.new(text).to_s}\""
+
+ if request.headers['HTTP_IF_NONE_MATCH'] == response.headers['Etag']
+ response.headers['Status'] = "304 Not Modified"
+ response.body = ''
+ end
+ end
+
+ response.body
end
def render_javascript(javascript, status = nil, append_response = true) #:nodoc:
diff --git a/vendor/rails/actionpack/lib/action_controller/request.rb b/vendor/rails/actionpack/lib/action_controller/request.rb
index b1cb1252..348cac8c 100755
--- a/vendor/rails/actionpack/lib/action_controller/request.rb
+++ b/vendor/rails/actionpack/lib/action_controller/request.rb
@@ -48,6 +48,10 @@ module ActionController
# REQUEST_METHOD header directly. Thus, for head, both get? and head? will return true.
def head?
@env['REQUEST_METHOD'].downcase.to_sym == :head
+ end
+
+ def headers
+ @env
end
# Determine whether the body of a HTTP call is URL-encoded (default)
diff --git a/vendor/rails/actionpack/test/controller/render_test.rb b/vendor/rails/actionpack/test/controller/render_test.rb
index d364dd6c..22683e51 100644
--- a/vendor/rails/actionpack/test/controller/render_test.rb
+++ b/vendor/rails/actionpack/test/controller/render_test.rb
@@ -69,6 +69,10 @@ class TestController < ActionController::Base
render "test/hello"
end
+ def heading
+ head :ok
+ end
+
def greeting
# let's just rely on the template
end
@@ -286,8 +290,50 @@ class RenderTest < Test::Unit::TestCase
assert_equal "Goodbye, Local David", @response.body
end
+ def test_render_200_should_set_etag
+ get :render_hello_world_from_variable
+ assert_equal etag_for("hello david"), @response.headers['Etag']
+ end
+
+ def test_render_against_etag_request_should_304_when_match
+ @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello david")
+ get :render_hello_world_from_variable
+ assert_equal "304 Not Modified", @response.headers['Status']
+ assert @response.body.empty?
+ end
+
+ def test_render_against_etag_request_should_200_when_no_match
+ @request.headers["HTTP_IF_NONE_MATCH"] = etag_for("hello somewhere else")
+ get :render_hello_world_from_variable
+ assert_equal "200 OK", @response.headers['Status']
+ assert !@response.body.empty?
+ end
+
+ def test_render_with_etag
+ get :render_hello_world_from_variable
+ expected_etag = "\"#{MD5.new("hello david").to_s}\""
+ assert_equal expected_etag, @response.headers['Etag']
+
+ @request.headers["HTTP_IF_NONE_MATCH"] = expected_etag
+ get :render_hello_world_from_variable
+ assert_equal "304 Not Modified", @response.headers['Status']
+
+ @request.headers["HTTP_IF_NONE_MATCH"] = "\"diftag\""
+ get :render_hello_world_from_variable
+ assert_equal "200 OK", @response.headers['Status']
+ end
+
+ def render_with_404_shouldnt_have_etag
+ get :render_custom_code
+ assert_nil @response.headers['Etag']
+ end
+
protected
def assert_deprecated_render(&block)
assert_deprecated(/render/, &block)
end
+
+ def etag_for(text)
+ "\"#{MD5.new(text).to_s}\""
+ end
end