From a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 19 Dec 2008 16:49:06 -0600 Subject: Process CGI 'cookie' header into 'Set-Cookie' for all responses. This mostly affects response.headers['cookie'] for test requests. Use response.cookies instead. --- actionpack/lib/action_controller/rack_process.rb | 19 --------- actionpack/lib/action_controller/response.rb | 51 ++++++++++++++++-------- actionpack/lib/action_controller/test_process.rb | 7 +++- 3 files changed, 41 insertions(+), 36 deletions(-) (limited to 'actionpack/lib') 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 -- cgit v1.2.3