Skip to content

Commit

Permalink
Do not output an ETag header if response body is blank or when sendin…
Browse files Browse the repository at this point in the history
…g files with send_file(... :xsendfile => true) [#1578 state:committed]

Signed-off-by: David Heinemeier Hansson <david@loudthinking.com>
  • Loading branch information
FooBarWidget authored and dhh committed Dec 16, 2008
1 parent 7c09050 commit 9e2b4a1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*2.3.0 [Edge]*

* Fixed that send_file shouldn't set an etag #1578 [Hongli Lai]

* Allow users to opt out of the spoofing checks in Request#remote_ip. Useful for sites whose traffic regularly triggers false positives. [Darren Boyd]

* Deprecated formatted_polymorphic_url. [Jeremy Kemper]
Expand Down
6 changes: 5 additions & 1 deletion actionpack/lib/action_controller/response.rb
Expand Up @@ -115,7 +115,11 @@ def etag?
end

def etag=(etag)
headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
if etag.blank?
headers.delete('ETag')
else
headers['ETag'] = %("#{Digest::MD5.hexdigest(ActiveSupport::Cache.expand_cache_key(etag))}")
end
end

def redirect(url, status)
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -208,6 +208,10 @@ def heading
def greeting
# let's just rely on the template
end

def blank_response
render :text => ' '
end

def layout_test
render :action => "hello_world"
Expand Down Expand Up @@ -1380,6 +1384,11 @@ def setup
@request.host = "www.nextangle.com"
@expected_bang_etag = etag_for(expand_key([:foo, 123]))
end

def test_render_blank_body_shouldnt_set_etag
get :blank_response
assert !@response.etag?
end

def test_render_200_should_set_etag
get :render_hello_world_from_variable
Expand Down
1 change: 1 addition & 0 deletions actionpack/test/controller/send_file_test.rb
Expand Up @@ -69,6 +69,7 @@ def test_x_sendfile_header

assert_equal @controller.file_path, response.headers['X-Sendfile']
assert response.body.blank?
assert !response.etag?
end

def test_data
Expand Down

0 comments on commit 9e2b4a1

Please sign in to comment.