~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/cgi/cookie.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Class representing an HTTP cookie.
2
 
#
3
 
# In addition to its specific fields and methods, a Cookie instance
4
 
# is a delegator to the array of its values.
5
 
#
6
 
# See RFC 2965.
7
 
#
8
 
# == Examples of use
9
 
#   cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
10
 
#   cookie1 = CGI::Cookie::new("name" => "name", "value" => "value")
11
 
#   cookie1 = CGI::Cookie::new('name'    => 'name',
12
 
#                              'value'   => ['value1', 'value2', ...],
13
 
#                              'path'    => 'path',   # optional
14
 
#                              'domain'  => 'domain', # optional
15
 
#                              'expires' => Time.now, # optional
16
 
#                              'secure'  => true      # optional
17
 
#                             )
18
 
#
19
 
#   cgi.out("cookie" => [cookie1, cookie2]) { "string" }
20
 
#
21
 
#   name    = cookie1.name
22
 
#   values  = cookie1.value
23
 
#   path    = cookie1.path
24
 
#   domain  = cookie1.domain
25
 
#   expires = cookie1.expires
26
 
#   secure  = cookie1.secure
27
 
#
28
 
#   cookie1.name    = 'name'
29
 
#   cookie1.value   = ['value1', 'value2', ...]
30
 
#   cookie1.path    = 'path'
31
 
#   cookie1.domain  = 'domain'
32
 
#   cookie1.expires = Time.now + 30
33
 
#   cookie1.secure  = true
34
1
class CGI
35
2
  @@accept_charset="UTF-8" unless defined?(@@accept_charset)
 
3
  # Class representing an HTTP cookie.
 
4
  #
 
5
  # In addition to its specific fields and methods, a Cookie instance
 
6
  # is a delegator to the array of its values.
 
7
  #
 
8
  # See RFC 2965.
 
9
  #
 
10
  # == Examples of use
 
11
  #   cookie1 = CGI::Cookie::new("name", "value1", "value2", ...)
 
12
  #   cookie1 = CGI::Cookie::new("name" => "name", "value" => "value")
 
13
  #   cookie1 = CGI::Cookie::new('name'    => 'name',
 
14
  #                              'value'   => ['value1', 'value2', ...],
 
15
  #                              'path'    => 'path',   # optional
 
16
  #                              'domain'  => 'domain', # optional
 
17
  #                              'expires' => Time.now, # optional
 
18
  #                              'secure'  => true      # optional
 
19
  #                             )
 
20
  #
 
21
  #   cgi.out("cookie" => [cookie1, cookie2]) { "string" }
 
22
  #
 
23
  #   name    = cookie1.name
 
24
  #   values  = cookie1.value
 
25
  #   path    = cookie1.path
 
26
  #   domain  = cookie1.domain
 
27
  #   expires = cookie1.expires
 
28
  #   secure  = cookie1.secure
 
29
  #
 
30
  #   cookie1.name    = 'name'
 
31
  #   cookie1.value   = ['value1', 'value2', ...]
 
32
  #   cookie1.path    = 'path'
 
33
  #   cookie1.domain  = 'domain'
 
34
  #   cookie1.expires = Time.now + 30
 
35
  #   cookie1.secure  = true
36
36
  class Cookie < Array
37
37
 
38
38
    # Create a new CGI::Cookie object.
39
39
    #
40
 
    # The contents of the cookie can be specified as a +name+ and one
41
 
    # or more +value+ arguments.  Alternatively, the contents can
42
 
    # be specified as a single hash argument.  The possible keywords of
43
 
    # this hash are as follows:
44
 
    #
45
 
    # name:: the name of the cookie.  Required.
46
 
    # value:: the cookie's value or list of values.
47
 
    # path:: the path for which this cookie applies.  Defaults to the
48
 
    #        base directory of the CGI script.
49
 
    # domain:: the domain for which this cookie applies.
50
 
    # expires:: the time at which this cookie expires, as a +Time+ object.
51
 
    # secure:: whether this cookie is a secure cookie or not (default to
52
 
    #          false).  Secure cookies are only transmitted to HTTPS
53
 
    #          servers.
54
 
    #
55
 
    # These keywords correspond to attributes of the cookie object.
 
40
    # :call-seq:
 
41
    #   Cookie.new(name_string,*value)
 
42
    #   Cookie.new(options_hash)
 
43
    #
 
44
    # +name_string+::
 
45
    #   The name of the cookie; in this form, there is no #domain or
 
46
    #   #expiration.  The #path is gleaned from the +SCRIPT_NAME+ environment
 
47
    #   variable, and #secure is false.
 
48
    # <tt>*value</tt>::
 
49
    #   value or list of values of the cookie
 
50
    # +options_hash+::
 
51
    #   A Hash of options to initialize this Cookie.  Possible options are:
 
52
    #
 
53
    #   name:: the name of the cookie.  Required.
 
54
    #   value:: the cookie's value or list of values.
 
55
    #   path:: the path for which this cookie applies.  Defaults to the
 
56
    #          the value of the +SCRIPT_NAME+ environment variable.
 
57
    #   domain:: the domain for which this cookie applies.
 
58
    #   expires:: the time at which this cookie expires, as a +Time+ object.
 
59
    #   secure:: whether this cookie is a secure cookie or not (default to
 
60
    #            false).  Secure cookies are only transmitted to HTTPS
 
61
    #            servers.
 
62
    #
 
63
    #   These keywords correspond to attributes of the cookie object.
56
64
    def initialize(name = "", *value)
57
65
      @domain = nil
58
66
      @expires = nil
85
93
      super(value)
86
94
    end
87
95
 
88
 
    attr_accessor("name", "path", "domain", "expires")
 
96
    # Name of this cookie, as a +String+
 
97
    attr_accessor :name
 
98
    # Path for which this cookie applies, as a +String+
 
99
    attr_accessor :path
 
100
    # Domain for which this cookie applies, as a +String+
 
101
    attr_accessor :domain
 
102
    # Time at which this cookie expires, as a +Time+
 
103
    attr_accessor :expires
 
104
    # True if this cookie is secure; false otherwise
89
105
    attr_reader("secure")
90
106
 
 
107
    # Returns the value or list of values for this cookie.
91
108
    def value
92
109
      self
93
110
    end
94
111
 
 
112
    # Replaces the value of this cookie with a new value or list of values.
95
113
    def value=(val)
96
114
      replace(Array(val))
97
115
    end
117
135
 
118
136
  end # class Cookie
119
137
 
120
 
 
121
138
  # Parse a raw cookie string into a hash of cookie-name=>Cookie
122
139
  # pairs.
123
140
  #