Froze rails gems
[depot.git] / vendor / rails / actionpack / lib / action_controller / cgi_ext / cookie.rb
1 CGI.module_eval { remove_const "Cookie" }
2
3 # TODO: document how this differs from stdlib CGI::Cookie
4 class CGI #:nodoc:
5 class Cookie < DelegateClass(Array)
6 attr_accessor :name, :value, :path, :domain, :expires
7 attr_reader :secure, :http_only
8
9 # Creates a new CGI::Cookie object.
10 #
11 # The contents of the cookie can be specified as a +name+ and one
12 # or more +value+ arguments. Alternatively, the contents can
13 # be specified as a single hash argument. The possible keywords of
14 # this hash are as follows:
15 #
16 # * <tt>:name</tt> - The name of the cookie. Required.
17 # * <tt>:value</tt> - The cookie's value or list of values.
18 # * <tt>:path</tt> - The path for which this cookie applies. Defaults to the
19 # base directory of the CGI script.
20 # * <tt>:domain</tt> - The domain for which this cookie applies.
21 # * <tt>:expires</tt> - The time at which this cookie expires, as a Time object.
22 # * <tt>:secure</tt> - Whether this cookie is a secure cookie or not (defaults to
23 # +false+). Secure cookies are only transmitted to HTTPS servers.
24 # * <tt>:http_only</tt> - Whether this cookie can be accessed by client side scripts (e.g. document.cookie) or only over HTTP.
25 # More details in http://msdn2.microsoft.com/en-us/library/system.web.httpcookie.httponly.aspx. Defaults to +false+.
26 #
27 # These keywords correspond to attributes of the cookie object.
28 def initialize(name = '', *value)
29 if name.kind_of?(String)
30 @name = name
31 @value = Array(value)
32 @domain = nil
33 @expires = nil
34 @secure = false
35 @http_only = false
36 @path = nil
37 else
38 @name = name['name']
39 @value = (name['value'].kind_of?(String) ? [name['value']] : Array(name['value'])).delete_if(&:blank?)
40 @domain = name['domain']
41 @expires = name['expires']
42 @secure = name['secure'] || false
43 @http_only = name['http_only'] || false
44 @path = name['path']
45 end
46
47 raise ArgumentError, "`name' required" unless @name
48
49 # simple support for IE
50 unless @path
51 %r|^(.*/)|.match(ENV['SCRIPT_NAME'])
52 @path = ($1 or '')
53 end
54
55 super(@value)
56 end
57
58 # Sets whether the Cookie is a secure cookie or not.
59 def secure=(val)
60 @secure = val == true
61 end
62
63 # Sets whether the Cookie is an HTTP only cookie or not.
64 def http_only=(val)
65 @http_only = val == true
66 end
67
68 # Converts the Cookie to its string representation.
69 def to_s
70 buf = ''
71 buf << @name << '='
72 buf << (@value.kind_of?(String) ? CGI::escape(@value) : @value.collect{|v| CGI::escape(v) }.join("&"))
73 buf << '; domain=' << @domain if @domain
74 buf << '; path=' << @path if @path
75 buf << '; expires=' << CGI::rfc1123_date(@expires) if @expires
76 buf << '; secure' if @secure
77 buf << '; HttpOnly' if @http_only
78 buf
79 end
80
81 # FIXME: work around broken 1.8.7 DelegateClass#respond_to?
82 def respond_to?(method, include_private = false)
83 return true if super(method)
84 return __getobj__.respond_to?(method, include_private)
85 end
86
87 # Parses a raw cookie string into a hash of <tt>cookie-name => cookie-object</tt>
88 # pairs.
89 #
90 # cookies = CGI::Cookie::parse("raw_cookie_string")
91 # # => { "name1" => cookie1, "name2" => cookie2, ... }
92 #
93 def self.parse(raw_cookie)
94 cookies = Hash.new([])
95
96 if raw_cookie
97 raw_cookie.split(/;\s?/).each do |pairs|
98 name, value = pairs.split('=',2)
99 next unless name and value
100 name = CGI::unescape(name)
101 unless cookies.has_key?(name)
102 cookies[name] = new(name, CGI::unescape(value))
103 end
104 end
105 end
106
107 cookies
108 end
109 end # class Cookie
110 end