diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-12-19 16:49:06 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-12-19 16:49:06 -0600 |
commit | a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7 (patch) | |
tree | 21d5f53af84aafef5a7eff9e6c94d07b752ccc4d /actionpack | |
parent | cbee72d21630f8949c4575cd1fd5a6f2e13d7827 (diff) | |
download | rails-a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7.tar.gz rails-a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7.tar.bz2 rails-a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7.zip |
Process CGI 'cookie' header into 'Set-Cookie' for all responses.
This mostly affects response.headers['cookie'] for test requests. Use response.cookies instead.
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/rack_process.rb | 19 | ||||
-rw-r--r-- | actionpack/lib/action_controller/response.rb | 51 | ||||
-rw-r--r-- | actionpack/lib/action_controller/test_process.rb | 7 | ||||
-rw-r--r-- | actionpack/test/controller/cookie_test.rb | 16 |
4 files changed, 49 insertions, 44 deletions
diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index e7d00409ca..be189b421d 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -121,7 +121,6 @@ module ActionController #:nodoc: convert_language! convert_expires! set_status! - set_cookies! end private @@ -147,23 +146,5 @@ module ActionController #:nodoc: def set_status! self.status ||= "200 OK" end - - def set_cookies! - # Convert 'cookie' header to 'Set-Cookie' headers. - # Because Set-Cookie header can appear more the once in the response body, - # we store it in a line break separated string that will be translated to - # multiple Set-Cookie header by the handler. - if cookie = headers.delete('cookie') - cookies = [] - - case cookie - when Array then cookie.each { |c| cookies << c.to_s } - when Hash then cookie.each { |_, c| cookies << c.to_s } - else cookies << cookie.to_s - end - - headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].flatten.compact - end - end end end diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb index e1bf5bb04d..f89861e5a5 100644 --- a/actionpack/lib/action_controller/response.rb +++ b/actionpack/lib/action_controller/response.rb @@ -107,11 +107,11 @@ module ActionController # :nodoc: def etag headers['ETag'] end - + def etag? headers.include?('ETag') end - + def etag=(etag) if etag.blank? headers.delete('ETag') @@ -140,22 +140,23 @@ module ActionController # :nodoc: handle_conditional_get! set_content_length! convert_content_type! + set_cookies! end private - def handle_conditional_get! - if etag? || last_modified? - set_conditional_cache_control! - elsif nonempty_ok_response? - self.etag = body - - if request && request.etag_matches?(etag) - self.status = '304 Not Modified' - self.body = '' - end - - set_conditional_cache_control! - end + def handle_conditional_get! + if etag? || last_modified? + set_conditional_cache_control! + elsif nonempty_ok_response? + self.etag = body + + if request && request.etag_matches?(etag) + self.status = '304 Not Modified' + self.body = '' + end + + set_conditional_cache_control! + end end def nonempty_ok_response? @@ -180,7 +181,7 @@ module ActionController # :nodoc: self.headers["type"] = content_type end end - + # Don't set the Content-Length for block-based bodies as that would mean reading it all into memory. Not nice # for, say, a 2GB streaming file. def set_content_length! @@ -188,5 +189,23 @@ module ActionController # :nodoc: self.headers["Content-Length"] ||= body.size end end + + def set_cookies! + # Convert 'cookie' header to 'Set-Cookie' headers. + # Because Set-Cookie header can appear more the once in the response body, + # we store it in a line break separated string that will be translated to + # multiple Set-Cookie header by the handler. + if cookie = headers.delete('cookie') + cookies = [] + + case cookie + when Array then cookie.each { |c| cookies << c.to_s } + when Hash then cookie.each { |_, c| cookies << c.to_s } + else cookies << cookie.to_s + end + + headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].flatten.compact + end + end end end diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index d3c66dd5f2..1597f637fc 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -264,7 +264,12 @@ module ActionController #:nodoc: # # assert_equal ['AuthorOfNewPage'], r.cookies['author'].value def cookies - headers['cookie'].inject({}) { |hash, cookie| hash[cookie.name] = cookie; hash } + cookies = {} + Array(headers['Set-Cookie']).each do |cookie| + key, value = cookie.split(";").first.split("=") + cookies[key] = [value].compact + end + cookies end # Returns binary content (downloadable file), converted to a String diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb index 67f31ba36b..cfe5726253 100644 --- a/actionpack/test/controller/cookie_test.rb +++ b/actionpack/test/controller/cookie_test.rb @@ -51,33 +51,33 @@ class CookieTest < Test::Unit::TestCase def test_setting_cookie get :authenticate - assert_equal "user_name=david; path=/", @response.headers["cookie"].to_s + assert_equal ["user_name=david; path=/"], @response.headers["Set-Cookie"] assert_equal({"user_name" => ["david"]}, @response.cookies) end def test_setting_cookie_for_fourteen_days get :authenticate_for_fourteen_days - assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"].to_s + assert_equal ["user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT"], @response.headers["Set-Cookie"] assert_equal({"user_name" => ["david"]}, @response.cookies) end def test_setting_cookie_for_fourteen_days_with_symbols get :authenticate_for_fourteen_days_with_symbols - assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"].to_s + assert_equal ["user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT"], @response.headers["Set-Cookie"] assert_equal({"user_name" => ["david"]}, @response.cookies) end def test_setting_cookie_with_http_only get :authenticate_with_http_only - assert_equal "user_name=david; path=/; HttpOnly", @response.headers["cookie"].to_s + assert_equal ["user_name=david; path=/; HttpOnly"], @response.headers["Set-Cookie"] assert_equal({"user_name" => ["david"]}, @response.cookies) end def test_multiple_cookies get :set_multiple_cookies assert_equal 2, @response.cookies.size - assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["cookie"][0].to_s - assert_equal "login=XJ-122; path=/", @response.headers["cookie"][1].to_s + assert_equal "user_name=david; path=/; expires=Mon, 10 Oct 2005 05:00:00 GMT", @response.headers["Set-Cookie"][0] + assert_equal "login=XJ-122; path=/", @response.headers["Set-Cookie"][1] assert_equal({"login" => ["XJ-122"], "user_name" => ["david"]}, @response.cookies) end @@ -87,7 +87,7 @@ class CookieTest < Test::Unit::TestCase def test_expiring_cookie get :logout - assert_equal "user_name=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT", @response.headers["cookie"].to_s + assert_equal ["user_name=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"], @response.headers["Set-Cookie"] assert_equal({"user_name" => []}, @response.cookies) end @@ -109,7 +109,7 @@ class CookieTest < Test::Unit::TestCase def test_delete_cookie_with_path get :delete_cookie_with_path - assert_equal "user_name=; path=/beaten; expires=Thu, 01 Jan 1970 00:00:00 GMT", @response.headers["cookie"].to_s + assert_equal ["user_name=; path=/beaten; expires=Thu, 01 Jan 1970 00:00:00 GMT"], @response.headers["Set-Cookie"] end def test_cookie_to_s_simple_values |