aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/cgi_ext/cookie.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-05-15 00:08:05 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-05-15 00:08:05 +0000
commitf8273e430916f8c7b0d21ad14aab90e427f8c0a6 (patch)
tree4dfec8ec427d1c1d3b2fc98184790a95f5ff7919 /actionpack/lib/action_controller/cgi_ext/cookie.rb
parent7fb5d44df77c377a02797d0dbdddbf6817d55ce8 (diff)
downloadrails-f8273e430916f8c7b0d21ad14aab90e427f8c0a6.tar.gz
rails-f8273e430916f8c7b0d21ad14aab90e427f8c0a6.tar.bz2
rails-f8273e430916f8c7b0d21ad14aab90e427f8c0a6.zip
Shine some sunlight on the CGI extensions. Remove unused CGI#session.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6733 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/cgi_ext/cookie.rb')
-rw-r--r--actionpack/lib/action_controller/cgi_ext/cookie.rb123
1 files changed, 123 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/cgi_ext/cookie.rb b/actionpack/lib/action_controller/cgi_ext/cookie.rb
new file mode 100644
index 0000000000..68ac7062f8
--- /dev/null
+++ b/actionpack/lib/action_controller/cgi_ext/cookie.rb
@@ -0,0 +1,123 @@
+CGI.module_eval { remove_const "Cookie" }
+
+# TODO: document how this differs from stdlib CGI::Cookie
+class CGI #:nodoc:
+ class Cookie < DelegateClass(Array)
+ # Create a new CGI::Cookie object.
+ #
+ # The contents of the cookie can be specified as a +name+ and one
+ # or more +value+ arguments. Alternatively, the contents can
+ # be specified as a single hash argument. The possible keywords of
+ # this hash are as follows:
+ #
+ # name:: the name of the cookie. Required.
+ # value:: the cookie's value or list of values.
+ # path:: the path for which this cookie applies. Defaults to the
+ # base directory of the CGI script.
+ # domain:: the domain for which this cookie applies.
+ # expires:: the time at which this cookie expires, as a +Time+ object.
+ # secure:: whether this cookie is a secure cookie or not (default to
+ # false). Secure cookies are only transmitted to HTTPS
+ # servers.
+ #
+ # These keywords correspond to attributes of the cookie object.
+ def initialize(name = '', *value)
+ if name.kind_of?(String)
+ @name = name
+ @value = Array(value)
+ @domain = nil
+ @expires = nil
+ @secure = false
+ @path = nil
+ else
+ @name = name['name']
+ @value = Array(name['value'])
+ @domain = name['domain']
+ @expires = name['expires']
+ @secure = name['secure'] || false
+ @path = name['path']
+ end
+
+ unless @name
+ raise ArgumentError, "`name' required"
+ end
+
+ # simple support for IE
+ unless @path
+ %r|^(.*/)|.match(ENV['SCRIPT_NAME'])
+ @path = ($1 or '')
+ end
+
+ super(@value)
+ end
+
+ def __setobj__(obj)
+ @_dc_obj = obj
+ end
+
+ attr_accessor("name", "value", "path", "domain", "expires")
+ attr_reader("secure")
+
+ # Set whether the Cookie is a secure cookie or not.
+ #
+ # +val+ must be a boolean.
+ def secure=(val)
+ @secure = val if val == true or val == false
+ @secure
+ end
+
+ # Convert the Cookie to its string representation.
+ def to_s
+ buf = ""
+ buf << @name << '='
+
+ if @value.kind_of?(String)
+ buf << CGI::escape(@value)
+ else
+ buf << @value.collect{|v| CGI::escape(v) }.join("&")
+ end
+
+ if @domain
+ buf << '; domain=' << @domain
+ end
+
+ if @path
+ buf << '; path=' << @path
+ end
+
+ if @expires
+ buf << '; expires=' << CGI::rfc1123_date(@expires)
+ end
+
+ if @secure == true
+ buf << '; secure'
+ end
+
+ buf
+ end
+
+ # Parse a raw cookie string into a hash of cookie-name=>Cookie
+ # pairs.
+ #
+ # cookies = CGI::Cookie::parse("raw_cookie_string")
+ # # { "name1" => cookie1, "name2" => cookie2, ... }
+ #
+ def self.parse(raw_cookie)
+ cookies = Hash.new([])
+
+ if raw_cookie
+ raw_cookie.split(/; ?/).each do |pairs|
+ name, values = pairs.split('=',2)
+ next unless name and values
+ name = CGI::unescape(name)
+ values = values.split('&').collect!{|v| CGI::unescape(v) }
+ unless cookies.has_key?(name)
+ cookies[name] = new(name, *values)
+ end
+ end
+ end
+
+ cookies
+ end
+ end # class Cookie
+end