Class | CGI::Cookie |
In: |
lib/cgi.rb
|
Parent: | DelegateClass(Array) |
Class representing an HTTP cookie.
In addition to its specific fields and methods, a Cookie instance is a delegator to the array of its values.
See RFC 2965.
cookie1 = CGI::Cookie::new("name", "value1", "value2", ...) cookie1 = CGI::Cookie::new("name" => "name", "value" => "value") cookie1 = CGI::Cookie::new('name' => 'name', 'value' => ['value1', 'value2', ...], 'path' => 'path', # optional 'domain' => 'domain', # optional 'expires' => Time.now, # optional 'secure' => true # optional ) cgi.out("cookie" => [cookie1, cookie2]) { "string" } name = cookie1.name values = cookie1.value path = cookie1.path domain = cookie1.domain expires = cookie1.expires secure = cookie1.secure cookie1.name = 'name' cookie1.value = ['value1', 'value2', ...] cookie1.path = 'path' cookie1.domain = 'domain' cookie1.expires = Time.now + 30 cookie1.secure = true
domain | [RW] | |
expires | [RW] | |
name | [RW] | |
path | [RW] | |
secure | [R] | |
value | [R] |
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.
# File lib/cgi.rb, line 794 794: def initialize(name = "", *value) 795: if name.kind_of?(String) 796: @name = name 797: @value = value 798: %r|^(.*/)|.match(ENV["SCRIPT_NAME"]) 799: @path = ($1 or "") 800: @secure = false 801: return super(@value) 802: end 803: 804: options = name 805: unless options.has_key?("name") 806: raise ArgumentError, "`name' required" 807: end 808: 809: @name = options["name"] 810: @value = Array(options["value"]) 811: # simple support for IE 812: if options["path"] 813: @path = options["path"] 814: else 815: %r|^(.*/)|.match(ENV["SCRIPT_NAME"]) 816: @path = ($1 or "") 817: end 818: @domain = options["domain"] 819: @expires = options["expires"] 820: @secure = options["secure"] == true ? true : false 821: 822: super(@value) 823: 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, ... }
# File lib/cgi.rb, line 875 875: def Cookie::parse(raw_cookie) 876: cookies = Hash.new([]) 877: return cookies unless raw_cookie 878: 879: raw_cookie.split(/[;,]\s?/).each do |pairs| 880: name, values = pairs.split('=',2) 881: next unless name and values 882: name = CGI::unescape(name) 883: values ||= "" 884: values = values.split('&').collect{|v| CGI::unescape(v) } 885: if cookies.has_key?(name) 886: values = cookies[name].value + values 887: end 888: cookies[name] = Cookie::new(name, *values) 889: end 890: 891: cookies 892: end
Convert the Cookie to its string representation.
# File lib/cgi.rb, line 841 841: def to_s 842: buf = "" 843: buf += @name + '=' 844: 845: buf += @value.map { |v| CGI::escape(v) }.join("&") 846: 847: if @domain 848: buf += '; domain=' + @domain 849: end 850: 851: if @path 852: buf += '; path=' + @path 853: end 854: 855: if @expires 856: buf += '; expires=' + CGI::rfc1123_date(@expires) 857: end 858: 859: if @secure == true 860: buf += '; secure' 861: end 862: 863: buf 864: end