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.

Examples of use

  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

Methods

new   parse   secure=   to_s  

Attributes

domain  [RW] 
expires  [RW] 
name  [RW] 
path  [RW] 
secure  [R] 
value  [RW] 

Public Class methods

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.

[Source]

     # File lib/cgi.rb, line 793
793:     def initialize(name = "", *value)
794:       options = if name.kind_of?(String)
795:                   { "name" => name, "value" => value }
796:                 else
797:                   name
798:                 end
799:       unless options.has_key?("name")
800:         raise ArgumentError, "`name' required"
801:       end
802: 
803:       @name = options["name"]
804:       @value = Array(options["value"])
805:       # simple support for IE
806:       if options["path"]
807:         @path = options["path"]
808:       else
809:         %r|^(.*/)|.match(ENV["SCRIPT_NAME"])
810:         @path = ($1 or "")
811:       end
812:       @domain = options["domain"]
813:       @expires = options["expires"]
814:       @secure = options["secure"] == true ? true : false
815: 
816:       super(@value)
817:     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, ... }

[Source]

     # File lib/cgi.rb, line 869
869:   def Cookie::parse(raw_cookie)
870:     cookies = Hash.new([])
871:     return cookies unless raw_cookie
872: 
873:     raw_cookie.split(/[;,]\s?/).each do |pairs|
874:       name, values = pairs.split('=',2)
875:       next unless name and values
876:       name = CGI::unescape(name)
877:       values ||= ""
878:       values = values.split('&').collect{|v| CGI::unescape(v) }
879:       if cookies.has_key?(name)
880:         values = cookies[name].value + values
881:       end
882:       cookies[name] = Cookie::new({ "name" => name, "value" => values })
883:     end
884: 
885:     cookies
886:   end

Public Instance methods

Set whether the Cookie is a secure cookie or not.

val must be a boolean.

[Source]

     # File lib/cgi.rb, line 825
825:     def secure=(val)
826:       @secure = val if val == true or val == false
827:       @secure
828:     end

Convert the Cookie to its string representation.

[Source]

     # File lib/cgi.rb, line 831
831:     def to_s
832:       buf = ""
833:       buf += @name + '='
834: 
835:       if @value.kind_of?(String)
836:         buf += CGI::escape(@value)
837:       else
838:         buf += @value.collect{|v| CGI::escape(v) }.join("&")
839:       end
840: 
841:       if @domain
842:         buf += '; domain=' + @domain
843:       end
844: 
845:       if @path
846:         buf += '; path=' + @path
847:       end
848: 
849:       if @expires
850:         buf += '; expires=' + CGI::rfc1123_date(@expires)
851:       end
852: 
853:       if @secure == true
854:         buf += '; secure'
855:       end
856: 
857:       buf
858:     end

[Validate]