Skip to content

Commit

Permalink
Rely on set and delete cookie logic from rack.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed May 17, 2010
1 parent 8c5e165 commit 941b653
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
35 changes: 2 additions & 33 deletions actionpack/lib/action_dispatch/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def location=(url)
def to_a
assign_default_content_type_and_charset!
handle_conditional_get!
self["Set-Cookie"] = @cookie.join("\n") unless @cookie.blank?
self["Set-Cookie"] = self["Set-Cookie"].join("\n") if self["Set-Cookie"].respond_to?(:join)
self["ETag"] = @_etag if @_etag
super
end
Expand Down Expand Up @@ -170,7 +170,7 @@ def write(str)
# assert_equal 'AuthorOfNewPage', r.cookies['author']
def cookies
cookies = {}
if header = @cookie
if header = self["Set-Cookie"]
header = header.split("\n") if header.respond_to?(:to_str)
header.each do |cookie|
if pair = cookie.split(';').first
Expand All @@ -182,37 +182,6 @@ def cookies
cookies
end

def set_cookie(key, value)
case value
when Hash
domain = "; domain=" + value[:domain] if value[:domain]
path = "; path=" + value[:path] if value[:path]
# According to RFC 2109, we need dashes here.
# N.B.: cgi.rb uses spaces...
expires = "; expires=" + value[:expires].clone.gmtime.
strftime("%a, %d-%b-%Y %H:%M:%S GMT") if value[:expires]
secure = "; secure" if value[:secure]
httponly = "; HttpOnly" if value[:httponly]
value = value[:value]
end
value = [value] unless Array === value
cookie = Rack::Utils.escape(key) + "=" +
value.map { |v| Rack::Utils.escape v }.join("&") +
"#{domain}#{path}#{expires}#{secure}#{httponly}"

@cookie << cookie
end

def delete_cookie(key, value={})
@cookie.reject! { |cookie|
cookie =~ /\A#{Rack::Utils.escape(key)}=/
}

set_cookie(key,
{:value => '', :path => nil, :domain => nil,
:expires => Time.at(0) }.merge(value))
end

private
def assign_default_content_type_and_charset!
return if headers[CONTENT_TYPE].present?
Expand Down
4 changes: 4 additions & 0 deletions actionpack/test/dispatch/response_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def setup
status, headers, body = @response.to_a
assert_equal "user_name=david; path=/\nlogin=foo%26bar; path=/; expires=Mon, 10-Oct-2005 05:00:00 GMT", headers["Set-Cookie"]
assert_equal({"login" => "foo&bar", "user_name" => "david"}, @response.cookies)

@response.delete_cookie("login")
status, headers, body = @response.to_a
assert_equal({"user_name" => "david", "login" => nil}, @response.cookies)
end

test "read cache control" do
Expand Down

0 comments on commit 941b653

Please sign in to comment.