<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package CGI::Cookie;

use strict;
use warnings;

use if $] &gt;= 5.019, 'deprecate';

# See the bottom of this file for the POD documentation.  Search for the
# string '=head'.

# You can run this file through either pod2man or pod2html to produce pretty
# documentation in manual or html file format (these utilities are part of the
# Perl 5 distribution).

# Copyright 1995-1999, Lincoln D. Stein.  All rights reserved.
# It may be used and modified freely, but I do request that this copyright
# notice remain attached to the file.  You may modify this module as you
# wish, but if you redistribute a modified version, please attach a note
# listing the modifications you have made.

our $VERSION='1.31';

use CGI::Util qw(rearrange unescape escape);
use overload '""' =&gt; \&amp;as_string, 'cmp' =&gt; \&amp;compare, 'fallback' =&gt; 1;

my $PERLEX = 0;
# Turn on special checking for ActiveState's PerlEx
$PERLEX++ if defined($ENV{'GATEWAY_INTERFACE'}) &amp;&amp; $ENV{'GATEWAY_INTERFACE'} =~ /^CGI-PerlEx/;

# Turn on special checking for mod_perl
# PerlEx::DBI tries to fool DBI by setting MOD_PERL
my $MOD_PERL = 0;
if (exists $ENV{MOD_PERL} &amp;&amp; ! $PERLEX) {
  if (exists $ENV{MOD_PERL_API_VERSION} &amp;&amp; $ENV{MOD_PERL_API_VERSION} == 2) {
      $MOD_PERL = 2;
      require Apache2::RequestUtil;
      require APR::Table;
  } else {
    $MOD_PERL = 1;
    require Apache;
  }
}

# fetch a list of cookies from the environment and
# return as a hash.  the cookies are parsed as normal
# escaped URL data.
sub fetch {
    my $class = shift;
    my $raw_cookie = get_raw_cookie(@_) or return;
    return $class-&gt;parse($raw_cookie);
}

# Fetch a list of cookies from the environment or the incoming headers and
# return as a hash. The cookie values are not unescaped or altered in any way.
 sub raw_fetch {
   my $class = shift;
   my $raw_cookie = get_raw_cookie(@_) or return;
   my %results;
   my($key,$value);
   
   my @pairs = split("[;,] ?",$raw_cookie);
  for my $pair ( @pairs ) {
    $pair =~ s/^\s+|\s+$//g;    # trim leading trailing whitespace
    my ( $key, $value ) = split "=", $pair;

    $value = defined $value ? $value : '';
    $results{$key} = $value;
  }
  return wantarray ? %results : \%results;
}

sub get_raw_cookie {
  my $r = shift;
  $r ||= eval { $MOD_PERL == 2                    ? 
                  Apache2::RequestUtil-&gt;request() :
                  Apache-&gt;request } if $MOD_PERL;

  return $r-&gt;headers_in-&gt;{'Cookie'} if $r;

  die "Run $r-&gt;subprocess_env; before calling fetch()" 
    if $MOD_PERL and !exists $ENV{REQUEST_METHOD};
    
  return $ENV{HTTP_COOKIE} || $ENV{COOKIE};
}


sub parse {
  my ($self,$raw_cookie) = @_;
  return wantarray ? () : {} unless $raw_cookie;

  my %results;

  my @pairs = split("[;,] ?",$raw_cookie);
  for (@pairs) {
    s/^\s+//;
    s/\s+$//;

    my($key,$value) = split("=",$_,2);

    # Some foreign cookies are not in name=value format, so ignore
    # them.
    next if !defined($value);
    my @values = ();
    if ($value ne '') {
      @values = map unescape($_),split(/[&amp;;]/,$value.'&amp;dmy');
      pop @values;
    }
    $key = unescape($key);
    # A bug in Netscape can cause several cookies with same name to
    # appear.  The FIRST one in HTTP_COOKIE is the most recent version.
    $results{$key} ||= $self-&gt;new(-name=&gt;$key,-value=&gt;\@values);
  }
  return wantarray ? %results : \%results;
}

sub new {
  my ( $class, @params ) = @_;
  $class = ref( $class ) || $class;
  # Ignore mod_perl request object--compatibility with Apache::Cookie.
  shift if ref $params[0]
        &amp;&amp; eval { $params[0]-&gt;isa('Apache::Request::Req') || $params[0]-&gt;isa('Apache') };
  my ( $name, $value, $path, $domain, $secure, $expires, $max_age, $httponly )
   = rearrange(
    [
      'NAME', [ 'VALUE', 'VALUES' ],
      'PATH',   'DOMAIN',
      'SECURE', 'EXPIRES',
      'MAX-AGE','HTTPONLY'
    ],
    @params
   );
  return undef unless defined $name and defined $value;
  my $self = {};
  bless $self, $class;
  $self-&gt;name( $name );
  $self-&gt;value( $value );
  $path ||= "/";
  $self-&gt;path( $path )         if defined $path;
  $self-&gt;domain( $domain )     if defined $domain;
  $self-&gt;secure( $secure )     if defined $secure;
  $self-&gt;expires( $expires )   if defined $expires;
  $self-&gt;max_age($expires)     if defined $max_age;
  $self-&gt;httponly( $httponly ) if defined $httponly;
  return $self;
}

sub as_string {
    my $self = shift;
    return "" unless $self-&gt;name;

    no warnings; # some things may be undefined, that's OK.

    my $name  = escape( $self-&gt;name );
    my $value = join "&amp;", map { escape($_) } $self-&gt;value;
    my @cookie = ( "$name=$value" );

    push @cookie,"domain=".$self-&gt;domain   if $self-&gt;domain;
    push @cookie,"path=".$self-&gt;path       if $self-&gt;path;
    push @cookie,"expires=".$self-&gt;expires if $self-&gt;expires;
    push @cookie,"max-age=".$self-&gt;max_age if $self-&gt;max_age;
    push @cookie,"secure"                  if $self-&gt;secure;
    push @cookie,"HttpOnly"                if $self-&gt;httponly;

    return join "; ", @cookie;
}

sub compare {
    my ( $self, $value ) = @_;
    return "$self" cmp $value;
}

sub bake {
  my ($self, $r) = @_;

  $r ||= eval {
      $MOD_PERL == 2
          ? Apache2::RequestUtil-&gt;request()
          : Apache-&gt;request
  } if $MOD_PERL;
  if ($r) {
      $r-&gt;headers_out-&gt;add('Set-Cookie' =&gt; $self-&gt;as_string);
  } else {
      require CGI;
      print CGI::header(-cookie =&gt; $self);
  }

}

# accessors
sub name {
    my ( $self, $name ) = @_;
    $self-&gt;{'name'} = $name if defined $name;
    return $self-&gt;{'name'};
}

sub value {
  my ( $self, $value ) = @_;
  if ( defined $value ) {
    my @values
     = ref $value eq 'ARRAY' ? @$value
     : ref $value eq 'HASH'  ? %$value
     :                         ( $value );
    $self-&gt;{'value'} = [@values];
  }
  return wantarray ? @{ $self-&gt;{'value'} } : $self-&gt;{'value'}-&gt;[0];
}

sub domain {
    my ( $self, $domain ) = @_;
    $self-&gt;{'domain'} = lc $domain if defined $domain;
    return $self-&gt;{'domain'};
}

sub secure {
    my ( $self, $secure ) = @_;
    $self-&gt;{'secure'} = $secure if defined $secure;
    return $self-&gt;{'secure'};
}

sub expires {
    my ( $self, $expires ) = @_;
    $self-&gt;{'expires'} = CGI::Util::expires($expires,'cookie') if defined $expires;
    return $self-&gt;{'expires'};
}

sub max_age {
    my ( $self, $max_age ) = @_;
    $self-&gt;{'max-age'} = CGI::Util::expire_calc($max_age)-time() if defined $max_age;
    return $self-&gt;{'max-age'};
}

sub path {
    my ( $self, $path ) = @_;
    $self-&gt;{'path'} = $path if defined $path;
    return $self-&gt;{'path'};
}


sub httponly { # HttpOnly
    my ( $self, $httponly ) = @_;
    $self-&gt;{'httponly'} = $httponly if defined $httponly;
    return $self-&gt;{'httponly'};
}

1;

=head1 NAME

CGI::Cookie - Interface to HTTP Cookies

=head1 SYNOPSIS

    use CGI qw/:standard/;
    use CGI::Cookie;

    # Create new cookies and send them
    $cookie1 = CGI::Cookie-&gt;new(-name=&gt;'ID',-value=&gt;123456);
    $cookie2 = CGI::Cookie-&gt;new(-name=&gt;'preferences',
                               -value=&gt;{ font =&gt; Helvetica,
                                         size =&gt; 12 } 
                               );
    print header(-cookie=&gt;[$cookie1,$cookie2]);

    # fetch existing cookies
    %cookies = CGI::Cookie-&gt;fetch;
    $id = $cookies{'ID'}-&gt;value;

    # create cookies returned from an external source
    %cookies = CGI::Cookie-&gt;parse($ENV{COOKIE});

=head1 DESCRIPTION

CGI::Cookie is an interface to HTTP/1.1 cookies, an
innovation that allows Web servers to store persistent information on
the browser's side of the connection.  Although CGI::Cookie is
intended to be used in conjunction with CGI.pm (and is in fact used by
it internally), you can use this module independently.

For full information on cookies see 

	http://tools.ietf.org/html/rfc2109
	http://tools.ietf.org/html/rfc2965
	http://tools.ietf.org/html/draft-ietf-httpstate-cookie

=head1 USING CGI::Cookie

CGI::Cookie is object oriented.  Each cookie object has a name and a
value.  The name is any scalar value.  The value is any scalar or
array value (associative arrays are also allowed).  Cookies also have
several optional attributes, including:

=over 4

=item B&lt;1. expiration date&gt;

The expiration date tells the browser how long to hang on to the
cookie.  If the cookie specifies an expiration date in the future, the
browser will store the cookie information in a disk file and return it
to the server every time the user reconnects (until the expiration
date is reached).  If the cookie species an expiration date in the
past, the browser will remove the cookie from the disk file.  If the
expiration date is not specified, the cookie will persist only until
the user quits the browser.

=item B&lt;2. domain&gt;

This is a partial or complete domain name for which the cookie is 
valid.  The browser will return the cookie to any host that matches
the partial domain name.  For example, if you specify a domain name
of ".capricorn.com", then the browser will return the cookie to
Web servers running on any of the machines "www.capricorn.com", 
"ftp.capricorn.com", "feckless.capricorn.com", etc.  Domain names
must contain at least two periods to prevent attempts to match
on top level domains like ".edu".  If no domain is specified, then
the browser will only return the cookie to servers on the host the
cookie originated from.

=item B&lt;3. path&gt;

If you provide a cookie path attribute, the browser will check it
against your script's URL before returning the cookie.  For example,
if you specify the path "/cgi-bin", then the cookie will be returned
to each of the scripts "/cgi-bin/tally.pl", "/cgi-bin/order.pl", and
"/cgi-bin/customer_service/complain.pl", but not to the script
"/cgi-private/site_admin.pl".  By default, the path is set to "/", so
that all scripts at your site will receive the cookie.

=item B&lt;4. secure flag&gt;

If the "secure" attribute is set, the cookie will only be sent to your
script if the CGI request is occurring on a secure channel, such as SSL.

=item B&lt;5. httponly flag&gt;

If the "httponly" attribute is set, the cookie will only be accessible
through HTTP Requests. This cookie will be inaccessible via JavaScript
(to prevent XSS attacks).

This feature is supported by nearly all modern browsers.

See these URLs for more information:

	http://msdn.microsoft.com/en-us/library/ms533046.aspx
    http://www.browserscope.org/?category=security&amp;v=top

=back

=head2 Creating New Cookies

	my $c = CGI::Cookie-&gt;new(-name    =&gt;  'foo',
                             -value   =&gt;  'bar',
                             -expires =&gt;  '+3M',
                             -domain  =&gt;  '.capricorn.com',
                             -path    =&gt;  '/cgi-bin/database',
                             -secure  =&gt;  1
	                    );

Create cookies from scratch with the B&lt;new&gt; method.  The B&lt;-name&gt; and
B&lt;-value&gt; parameters are required.  The name must be a scalar value.
The value can be a scalar, an array reference, or a hash reference.
(At some point in the future cookies will support one of the Perl
object serialization protocols for full generality).

B&lt;-expires&gt; accepts any of the relative or absolute date formats
recognized by CGI.pm, for example "+3M" for three months in the
future.  See CGI.pm's documentation for details.

B&lt;-max-age&gt; accepts the same data formats as B&lt;&lt; -expires &gt;&gt;, but sets a
relative value instead of an absolute like B&lt;&lt; -expires &gt;&gt;. This is intended to be
more secure since a clock could be changed to fake an absolute time. In
practice, as of 2011, C&lt;&lt; -max-age &gt;&gt; still does not enjoy the widespread support
that C&lt;&lt; -expires &gt;&gt; has. You can set both, and browsers that support
C&lt;&lt; -max-age &gt;&gt; should ignore the C&lt;&lt; Expires &gt;&gt; header. The drawback
to this approach is the bit of bandwidth for sending an extra header on each cookie.

B&lt;-domain&gt; points to a domain name or to a fully qualified host name.
If not specified, the cookie will be returned only to the Web server
that created it.

B&lt;-path&gt; points to a partial URL on the current server.  The cookie
will be returned to all URLs beginning with the specified path.  If
not specified, it defaults to '/', which returns the cookie to all
pages at your site.

B&lt;-secure&gt; if set to a true value instructs the browser to return the
cookie only when a cryptographic protocol is in use.

B&lt;-httponly&gt; if set to a true value, the cookie will not be accessible
via JavaScript.

For compatibility with Apache::Cookie, you may optionally pass in
a mod_perl request object as the first argument to C&lt;new()&gt;. It will
simply be ignored:

  my $c = CGI::Cookie-&gt;new($r,
                          -name    =&gt;  'foo',
                          -value   =&gt;  ['bar','baz']);

=head2 Sending the Cookie to the Browser

The simplest way to send a cookie to the browser is by calling the bake()
method:

  $c-&gt;bake;

This will print the Set-Cookie HTTP header to STDOUT using CGI.pm. CGI.pm
will be loaded for this purpose if it is not already. Otherwise CGI.pm is not
required or used by this module.

Under mod_perl, pass in an Apache request object:

  $c-&gt;bake($r);

If you want to set the cookie yourself, Within a CGI script you can send
a cookie to the browser by creating one or more Set-Cookie: fields in the
HTTP header.  Here is a typical sequence:

  my $c = CGI::Cookie-&gt;new(-name    =&gt;  'foo',
                          -value   =&gt;  ['bar','baz'],
                          -expires =&gt;  '+3M');

  print "Set-Cookie: $c\n";
  print "Content-Type: text/html\n\n";

To send more than one cookie, create several Set-Cookie: fields.

If you are using CGI.pm, you send cookies by providing a -cookie
argument to the header() method:

  print header(-cookie=&gt;$c);

Mod_perl users can set cookies using the request object's header_out()
method:

  $r-&gt;headers_out-&gt;set('Set-Cookie' =&gt; $c);

Internally, Cookie overloads the "" operator to call its as_string()
method when incorporated into the HTTP header.  as_string() turns the
Cookie's internal representation into an RFC-compliant text
representation.  You may call as_string() yourself if you prefer:

  print "Set-Cookie: ",$c-&gt;as_string,"\n";

=head2 Recovering Previous Cookies

	%cookies = CGI::Cookie-&gt;fetch;

B&lt;fetch&gt; returns an associative array consisting of all cookies
returned by the browser.  The keys of the array are the cookie names.  You
can iterate through the cookies this way:

	%cookies = CGI::Cookie-&gt;fetch;
	for (keys %cookies) {
	   do_something($cookies{$_});
        }

In a scalar context, fetch() returns a hash reference, which may be more
efficient if you are manipulating multiple cookies.

CGI.pm uses the URL escaping methods to save and restore reserved characters
in its cookies.  If you are trying to retrieve a cookie set by a foreign server,
this escaping method may trip you up.  Use raw_fetch() instead, which has the
same semantics as fetch(), but performs no unescaping.

You may also retrieve cookies that were stored in some external
form using the parse() class method:

       $COOKIES = `cat /usr/tmp/Cookie_stash`;
       %cookies = CGI::Cookie-&gt;parse($COOKIES);

If you are in a mod_perl environment, you can save some overhead by
passing the request object to fetch() like this:

   CGI::Cookie-&gt;fetch($r);

If the value passed to parse() is undefined, an empty array will returned in list
context, and an empty hashref will be returned in scalar context.

=head2 Manipulating Cookies

Cookie objects have a series of accessor methods to get and set cookie
attributes.  Each accessor has a similar syntax.  Called without
arguments, the accessor returns the current value of the attribute.
Called with an argument, the accessor changes the attribute and
returns its new value.

=over 4

=item B&lt;name()&gt;

Get or set the cookie's name.  Example:

	$name = $c-&gt;name;
	$new_name = $c-&gt;name('fred');

=item B&lt;value()&gt;

Get or set the cookie's value.  Example:

	$value = $c-&gt;value;
	@new_value = $c-&gt;value(['a','b','c','d']);

B&lt;value()&gt; is context sensitive.  In a list context it will return
the current value of the cookie as an array.  In a scalar context it
will return the B&lt;first&gt; value of a multivalued cookie.

=item B&lt;domain()&gt;

Get or set the cookie's domain.

=item B&lt;path()&gt;

Get or set the cookie's path.

=item B&lt;expires()&gt;

Get or set the cookie's expiration time.

=back


=head1 AUTHOR INFORMATION

Copyright 1997-1998, Lincoln D. Stein.  All rights reserved.  

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

Address bug reports and comments to: lstein@cshl.org

=head1 BUGS

This section intentionally left blank.

=head1 SEE ALSO

L&lt;CGI::Carp&gt;, L&lt;CGI&gt;

L&lt;RFC 2109|http://www.ietf.org/rfc/rfc2109.txt&gt;, L&lt;RFC 2695|http://www.ietf.org/rfc/rfc2965.txt&gt;

=cut
</pre></body></html>