Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/1-2-stable/actionpack/lib/action_controller/cgi_ext/cookie_performance_fix.rb

Revision 3039, 3.5 kB (checked in by bitsweat, 3 years ago)

Handle cookie parsing irregularity for certain Nokia phones. Closes #2530.

Line 
1 CGI.module_eval { remove_const "Cookie" }
2
3 class CGI #:nodoc:
4   # This is a cookie class that fixes the performance problems with the default one that ships with 1.8.1 and below.
5   # It replaces the inheritance on SimpleDelegator with DelegateClass(Array) following the suggestion from Matz on
6   # http://groups.google.com/groups?th=e3a4e68ba042f842&seekm=c3sioe%241qvm%241%40news.cybercity.dk#link14
7   class Cookie < DelegateClass(Array)
8     # Create a new CGI::Cookie object.
9     #
10     # The contents of the cookie can be specified as a +name+ and one
11     # or more +value+ arguments.  Alternatively, the contents can
12     # be specified as a single hash argument.  The possible keywords of
13     # this hash are as follows:
14     #
15     # name:: the name of the cookie.  Required.
16     # value:: the cookie's value or list of values.
17     # path:: the path for which this cookie applies.  Defaults to the
18     #        base directory of the CGI script.
19     # domain:: the domain for which this cookie applies.
20     # expires:: the time at which this cookie expires, as a +Time+ object.
21     # secure:: whether this cookie is a secure cookie or not (default to
22     #          false).  Secure cookies are only transmitted to HTTPS
23     #          servers.
24     #
25     # These keywords correspond to attributes of the cookie object.
26     def initialize(name = '', *value)
27       if name.kind_of?(String)
28         @name = name
29         @value = Array(value)
30         @domain = nil
31         @expires = nil
32         @secure = false
33         @path = nil
34       else
35         @name = name['name']
36         @value = Array(name['value'])
37         @domain = name['domain']
38         @expires = name['expires']
39         @secure = name['secure'] || false
40         @path = name['path']
41       end
42      
43       unless @name
44         raise ArgumentError, "`name' required"
45       end
46
47       # simple support for IE
48       unless @path
49         %r|^(.*/)|.match(ENV['SCRIPT_NAME'])
50         @path = ($1 or '')
51       end
52
53       super(@value)
54     end
55
56     def __setobj__(obj)
57       @_dc_obj = obj
58     end
59
60     attr_accessor("name", "value", "path", "domain", "expires")
61     attr_reader("secure")
62
63     # Set whether the Cookie is a secure cookie or not.
64     #
65     # +val+ must be a boolean.
66     def secure=(val)
67       @secure = val if val == true or val == false
68       @secure
69     end
70
71     # Convert the Cookie to its string representation.
72     def to_s
73       buf = ""
74       buf << @name << '='
75
76       if @value.kind_of?(String)
77         buf << CGI::escape(@value)
78       else
79         buf << @value.collect{|v| CGI::escape(v) }.join("&")
80       end
81
82       if @domain
83         buf << '; domain=' << @domain
84       end
85
86       if @path
87         buf << '; path=' << @path
88       end
89
90       if @expires
91         buf << '; expires=' << CGI::rfc1123_date(@expires)
92       end
93
94       if @secure == true
95         buf << '; secure'
96       end
97
98       buf
99     end
100
101     # Parse a raw cookie string into a hash of cookie-name=>Cookie
102     # pairs.
103     #
104     #   cookies = CGI::Cookie::parse("raw_cookie_string")
105     #     # { "name1" => cookie1, "name2" => cookie2, ... }
106     #
107     def self.parse(raw_cookie)
108       cookies = Hash.new([])
109
110       if raw_cookie
111         raw_cookie.split(/; ?/).each do |pairs|
112           name, values = pairs.split('=',2)
113           next unless name and values
114           name = CGI::unescape(name)
115           values = values.split('&').collect!{|v| CGI::unescape(v) }
116           unless cookies.has_key?(name)
117             cookies[name] = new(name, *values)
118           end
119         end
120       end
121
122       cookies
123     end
124   end # class Cookie
125 end
Note: See TracBrowser for help on using the browser.