Permalink
Browse files

Process CGI 'cookie' header into 'Set-Cookie' for all responses.

This mostly affects response.headers['cookie'] for test requests. Use response.cookies instead.
  • Loading branch information...
1 parent cbee72d commit a14bbd7a8574c3b485d4b71e0950b2b9ff4e90d7 @josh josh committed Dec 19, 2008
@@ -121,7 +121,6 @@ def prepare!
convert_language!
convert_expires!
set_status!
- set_cookies!
end
private
@@ -147,23 +146,5 @@ def set_content_length!
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
@@ -107,11 +107,11 @@ def last_modified=(utc_time)
def etag
headers['ETag']
end
-
+
def etag?
headers.include?('ETag')
end
-
+
def etag=(etag)
if etag.blank?
headers.delete('ETag')
@@ -140,22 +140,23 @@ def prepare!
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,13 +181,31 @@ def convert_content_type!
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!
unless body.respond_to?(:call) || (status && status.to_s[0..2] == '304')
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
@@ -264,7 +264,12 @@ def has_template_object?(name=nil)
#
# 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
@@ -51,33 +51,33 @@ def setup
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 @@ def test_setting_test_cookie
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 @@ def test_cookiejar_accessor_with_array_value
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

0 comments on commit a14bbd7

Please sign in to comment.