home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _d17425d8c4caebb80528cb77a3282c56 < prev    next >
Encoding:
Text File  |  2004-06-01  |  20.8 KB  |  774 lines

  1. package HTTP::Cookies;
  2.  
  3. use strict;
  4. use HTTP::Date qw(str2time time2str);
  5. use HTTP::Headers::Util qw(split_header_words join_header_words);
  6. use LWP::Debug ();
  7.  
  8. use vars qw($VERSION $EPOCH_OFFSET);
  9. $VERSION = sprintf("%d.%02d", q$Revision: 1.38 $ =~ /(\d+)\.(\d+)/);
  10.  
  11. # Legacy: because "use "HTTP::Cookies" used be the ONLY way
  12. #  to load the class HTTP::Cookies::Netscape.
  13. require HTTP::Cookies::Netscape;
  14.  
  15. $EPOCH_OFFSET = 0;  # difference from Unix epoch
  16. if ($^O eq "MacOS") {
  17.     require Time::Local;
  18.     $EPOCH_OFFSET = Time::Local::timelocal(0,0,0,1,0,70);
  19. }
  20.  
  21. # A HTTP::Cookies object is a hash.  The main attribute is the
  22. # COOKIES 3 level hash:  $self->{COOKIES}{$domain}{$path}{$key}.
  23.  
  24. sub new
  25. {
  26.     my $class = shift;
  27.     my $self = bless {
  28.     COOKIES => {},
  29.     }, $class;
  30.     my %cnf = @_;
  31.     for (keys %cnf) {
  32.     $self->{lc($_)} = $cnf{$_};
  33.     }
  34.     $self->load;
  35.     $self;
  36. }
  37.  
  38.  
  39. sub add_cookie_header
  40. {
  41.     my $self = shift;
  42.     my $request = shift || return;
  43.     my $url = $request->url;
  44.     my $scheme = $url->scheme;
  45.     unless ($scheme =~ /^https?\z/) {
  46.     LWP::Debug::debug("Will not add cookies to non-HTTP requests");
  47.     return;
  48.     }
  49.  
  50.     my $domain = _host($request, $url);
  51.     $domain = "$domain.local" unless $domain =~ /\./;
  52.     my $secure_request = ($scheme eq "https");
  53.     my $req_path = _url_path($url);
  54.     my $req_port = $url->port;
  55.     my $now = time();
  56.     _normalize_path($req_path) if $req_path =~ /%/;
  57.  
  58.     my @cval;    # cookie values for the "Cookie" header
  59.     my $set_ver;
  60.     my $netscape_only = 0; # An exact domain match applies to any cookie
  61.  
  62.     while ($domain =~ /\./) {
  63.  
  64.         LWP::Debug::debug("Checking $domain for cookies");
  65.     my $cookies = $self->{COOKIES}{$domain};
  66.     next unless $cookies;
  67.     if ($self->{delayload} && defined($cookies->{'//+delayload'})) {
  68.         my $cookie_data = $cookies->{'//+delayload'}{'cookie'};
  69.         delete $self->{COOKIES}{$domain};
  70.         $self->load_cookie($cookie_data->[1]);
  71.         $cookies = $self->{COOKIES}{$domain};
  72.         next unless $cookies;  # should not really happen
  73.     }
  74.  
  75.     # Want to add cookies corresponding to the most specific paths
  76.     # first (i.e. longest path first)
  77.     my $path;
  78.     for $path (sort {length($b) <=> length($a) } keys %$cookies) {
  79.             LWP::Debug::debug("- checking cookie path=$path");
  80.         if (index($req_path, $path) != 0) {
  81.             LWP::Debug::debug("  path $path:$req_path does not fit");
  82.         next;
  83.         }
  84.  
  85.         my($key,$array);
  86.         while (($key,$array) = each %{$cookies->{$path}}) {
  87.         my($version,$val,$port,$path_spec,$secure,$expires) = @$array;
  88.             LWP::Debug::debug(" - checking cookie $key=$val");
  89.         if ($secure && !$secure_request) {
  90.             LWP::Debug::debug("   not a secure requests");
  91.             next;
  92.         }
  93.         if ($expires && $expires < $now) {
  94.             LWP::Debug::debug("   expired");
  95.             next;
  96.         }
  97.         if ($port) {
  98.             my $found;
  99.             if ($port =~ s/^_//) {
  100.             # The correponding Set-Cookie attribute was empty
  101.             $found++ if $port eq $req_port;
  102.             $port = "";
  103.             }
  104.             else {
  105.             my $p;
  106.             for $p (split(/,/, $port)) {
  107.                 $found++, last if $p eq $req_port;
  108.             }
  109.             }
  110.             unless ($found) {
  111.                 LWP::Debug::debug("   port $port:$req_port does not fit");
  112.             next;
  113.             }
  114.         }
  115.         if ($version > 0 && $netscape_only) {
  116.             LWP::Debug::debug("   domain $domain applies to " .
  117.                       "Netscape-style cookies only");
  118.             next;
  119.         }
  120.  
  121.             LWP::Debug::debug("   it's a match");
  122.  
  123.         # set version number of cookie header.
  124.             # XXX: What should it be if multiple matching
  125.                 #      Set-Cookie headers have different versions themselves
  126.         if (!$set_ver++) {
  127.             if ($version >= 1) {
  128.             push(@cval, "\$Version=$version");
  129.             }
  130.             elsif (!$self->{hide_cookie2}) {
  131.             $request->header(Cookie2 => '$Version="1"');
  132.             }
  133.         }
  134.  
  135.         # do we need to quote the value
  136.         if ($val =~ /\W/ && $version) {
  137.             $val =~ s/([\\\"])/\\$1/g;
  138.             $val = qq("$val");
  139.         }
  140.  
  141.         # and finally remember this cookie
  142.         push(@cval, "$key=$val");
  143.         if ($version >= 1) {
  144.             push(@cval, qq(\$Path="$path"))     if $path_spec;
  145.             push(@cval, qq(\$Domain="$domain")) if $domain =~ /^\./;
  146.             if (defined $port) {
  147.             my $p = '$Port';
  148.             $p .= qq(="$port") if length $port;
  149.             push(@cval, $p);
  150.             }
  151.         }
  152.  
  153.         }
  154.         }
  155.  
  156.     } continue {
  157.     # Try with a more general domain, alternately stripping
  158.     # leading name components and leading dots.  When this
  159.     # results in a domain with no leading dot, it is for
  160.     # Netscape cookie compatibility only:
  161.     #
  162.     # a.b.c.net    Any cookie
  163.     # .b.c.net    Any cookie
  164.     # b.c.net    Netscape cookie only
  165.     # .c.net    Any cookie
  166.  
  167.     if ($domain =~ s/^\.+//) {
  168.         $netscape_only = 1;
  169.     }
  170.     else {
  171.         $domain =~ s/[^.]*//;
  172.         $netscape_only = 0;
  173.     }
  174.     }
  175.  
  176.     $request->header(Cookie => join("; ", @cval)) if @cval;
  177.  
  178.     $request;
  179. }
  180.  
  181.  
  182. sub extract_cookies
  183. {
  184.     my $self = shift;
  185.     my $response = shift || return;
  186.  
  187.     my @set = split_header_words($response->_header("Set-Cookie2"));
  188.     my @ns_set = $response->_header("Set-Cookie");
  189.  
  190.     return $response unless @set || @ns_set;  # quick exit
  191.  
  192.     my $request = $response->request;
  193.     my $url = $request->url;
  194.     my $req_host = _host($request, $url);
  195.     $req_host = "$req_host.local" unless $req_host =~ /\./;
  196.     my $req_port = $url->port;
  197.     my $req_path = _url_path($url);
  198.     _normalize_path($req_path) if $req_path =~ /%/;
  199.  
  200.     if (@ns_set) {
  201.     # The old Netscape cookie format for Set-Cookie
  202.         # http://www.netscape.com/newsref/std/cookie_spec.html
  203.     # can for instance contain an unquoted "," in the expires
  204.     # field, so we have to use this ad-hoc parser.
  205.     my $now = time();
  206.  
  207.     # Build a hash of cookies that was present in Set-Cookie2
  208.     # headers.  We need to skip them if we also find them in a
  209.     # Set-Cookie header.
  210.     my %in_set2;
  211.     for (@set) {
  212.         $in_set2{$_->[0]}++;
  213.     }
  214.  
  215.     my $set;
  216.     for $set (@ns_set) {
  217.         my @cur;
  218.         my $param;
  219.         my $expires;
  220.         for $param (split(/;\s*/, $set)) {
  221.         my($k,$v) = split(/\s*=\s*/, $param, 2);
  222.         if (defined $v) {
  223.             $v =~ s/\s+$//;
  224.             #print "$k => $v\n";
  225.         }
  226.         else {
  227.             $k =~ s/\s+$//;
  228.             #print "$k => undef";
  229.         }
  230.         my $lc = lc($k);
  231.         if ($lc eq "expires") {
  232.             my $etime = str2time($v);
  233.             if ($etime) {
  234.             push(@cur, "Max-Age" => str2time($v) - $now);
  235.             $expires++;
  236.             }
  237.         }
  238.         else {
  239.             push(@cur, $k => $v);
  240.         }
  241.         }
  242.         next if $in_set2{$cur[0]};
  243.  
  244. #        push(@cur, "Port" => $req_port);
  245.         push(@cur, "Discard" => undef) unless $expires;
  246.         push(@cur, "Version" => 0);
  247.         push(@cur, "ns-cookie" => 1);
  248.         push(@set, \@cur);
  249.     }
  250.     }
  251.  
  252.   SET_COOKIE:
  253.     for my $set (@set) {
  254.     next unless @$set >= 2;
  255.  
  256.     my $key = shift @$set;
  257.     my $val = shift @$set;
  258.  
  259.         LWP::Debug::debug("Set cookie $key => $val");
  260.  
  261.     my %hash;
  262.     while (@$set) {
  263.         my $k = shift @$set;
  264.         my $v = shift @$set;
  265.         my $lc = lc($k);
  266.         # don't loose case distinction for unknown fields
  267.         $k = $lc if $lc =~ /^(?:discard|domain|max-age|
  268.                                     path|port|secure|version)$/x;
  269.         if ($k eq "discard" || $k eq "secure") {
  270.         $v = 1 unless defined $v;
  271.         }
  272.         next if exists $hash{$k};  # only first value is signigicant
  273.         $hash{$k} = $v;
  274.     };
  275.  
  276.     my %orig_hash = %hash;
  277.     my $version   = delete $hash{version};
  278.     $version = 1 unless defined($version);
  279.     my $discard   = delete $hash{discard};
  280.     my $secure    = delete $hash{secure};
  281.     my $maxage    = delete $hash{'max-age'};
  282.     my $ns_cookie = delete $hash{'ns-cookie'};
  283.  
  284.     # Check domain
  285.     my $domain  = delete $hash{domain};
  286.     $domain = lc($domain) if defined $domain;
  287.     if (defined($domain)
  288.         && $domain ne $req_host && $domain ne ".$req_host") {
  289.         if ($domain !~ /\./ && $domain ne "local") {
  290.             LWP::Debug::debug("Domain $domain contains no dot");
  291.         next SET_COOKIE;
  292.         }
  293.         $domain = ".$domain" unless $domain =~ /^\./;
  294.         if ($domain =~ /\.\d+$/) {
  295.             LWP::Debug::debug("IP-address $domain illeagal as domain");
  296.         next SET_COOKIE;
  297.         }
  298.         my $len = length($domain);
  299.         unless (substr($req_host, -$len) eq $domain) {
  300.             LWP::Debug::debug("Domain $domain does not match host $req_host");
  301.         next SET_COOKIE;
  302.         }
  303.         my $hostpre = substr($req_host, 0, length($req_host) - $len);
  304.         if ($hostpre =~ /\./ && !$ns_cookie) {
  305.             LWP::Debug::debug("Host prefix contain a dot: $hostpre => $domain");
  306.         next SET_COOKIE;
  307.         }
  308.     }
  309.     else {
  310.         $domain = $req_host;
  311.     }
  312.  
  313.     my $path = delete $hash{path};
  314.     my $path_spec;
  315.     if (defined $path && $path ne '') {
  316.         $path_spec++;
  317.         _normalize_path($path) if $path =~ /%/;
  318.         if (!$ns_cookie &&
  319.                 substr($req_path, 0, length($path)) ne $path) {
  320.             LWP::Debug::debug("Path $path is not a prefix of $req_path");
  321.         next SET_COOKIE;
  322.         }
  323.     }
  324.     else {
  325.         $path = $req_path;
  326.         $path =~ s,/[^/]*$,,;
  327.         $path = "/" unless length($path);
  328.     }
  329.  
  330.     my $port;
  331.     if (exists $hash{port}) {
  332.         $port = delete $hash{port};
  333.         if (defined $port) {
  334.         $port =~ s/\s+//g;
  335.         my $found;
  336.         for my $p (split(/,/, $port)) {
  337.             unless ($p =~ /^\d+$/) {
  338.               LWP::Debug::debug("Bad port $port (not numeric)");
  339.             next SET_COOKIE;
  340.             }
  341.             $found++ if $p eq $req_port;
  342.         }
  343.         unless ($found) {
  344.             LWP::Debug::debug("Request port ($req_port) not found in $port");
  345.             next SET_COOKIE;
  346.         }
  347.         }
  348.         else {
  349.         $port = "_$req_port";
  350.         }
  351.     }
  352.     $self->set_cookie($version,$key,$val,$path,$domain,$port,$path_spec,$secure,$maxage,$discard, \%hash)
  353.         if $self->set_cookie_ok(\%orig_hash);
  354.     }
  355.  
  356.     $response;
  357. }
  358.  
  359. sub set_cookie_ok
  360. {
  361.     1;
  362. }
  363.  
  364.  
  365. sub set_cookie
  366. {
  367.     my $self = shift;
  368.     my($version,
  369.        $key, $val, $path, $domain, $port,
  370.        $path_spec, $secure, $maxage, $discard, $rest) = @_;
  371.  
  372.     # path and key can not be empty (key can't start with '$')
  373.     return $self if !defined($path) || $path !~ m,^/, ||
  374.                 !defined($key)  || $key  =~ m,^\$,;
  375.  
  376.     # ensure legal port
  377.     if (defined $port) {
  378.     return $self unless $port =~ /^_?\d+(?:,\d+)*$/;
  379.     }
  380.  
  381.     my $expires;
  382.     if (defined $maxage) {
  383.     if ($maxage <= 0) {
  384.         delete $self->{COOKIES}{$domain}{$path}{$key};
  385.         return $self;
  386.     }
  387.     $expires = time() + $maxage;
  388.     }
  389.     $version = 0 unless defined $version;
  390.  
  391.     my @array = ($version, $val,$port,
  392.          $path_spec,
  393.          $secure, $expires, $discard);
  394.     push(@array, {%$rest}) if defined($rest) && %$rest;
  395.     # trim off undefined values at end
  396.     pop(@array) while !defined $array[-1];
  397.  
  398.     $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  399.     $self;
  400. }
  401.  
  402.  
  403. sub save
  404. {
  405.     my $self = shift;
  406.     my $file = shift || $self->{'file'} || return;
  407.     local(*FILE);
  408.     open(FILE, ">$file") or die "Can't open $file: $!";
  409.     print FILE "#LWP-Cookies-1.0\n";
  410.     print FILE $self->as_string(!$self->{ignore_discard});
  411.     close(FILE);
  412.     1;
  413. }
  414.  
  415.  
  416. sub load
  417. {
  418.     my $self = shift;
  419.     my $file = shift || $self->{'file'} || return;
  420.     local(*FILE, $_);
  421.     local $/ = "\n";  # make sure we got standard record separator
  422.     open(FILE, $file) or return;
  423.     my $magic = <FILE>;
  424.     unless ($magic =~ /^\#LWP-Cookies-(\d+\.\d+)/) {
  425.     warn "$file does not seem to contain cookies";
  426.     return;
  427.     }
  428.     while (<FILE>) {
  429.     next unless s/^Set-Cookie3:\s*//;
  430.     chomp;
  431.     my $cookie;
  432.     for $cookie (split_header_words($_)) {
  433.         my($key,$val) = splice(@$cookie, 0, 2);
  434.         my %hash;
  435.         while (@$cookie) {
  436.         my $k = shift @$cookie;
  437.         my $v = shift @$cookie;
  438.         $hash{$k} = $v;
  439.         }
  440.         my $version   = delete $hash{version};
  441.         my $path      = delete $hash{path};
  442.         my $domain    = delete $hash{domain};
  443.         my $port      = delete $hash{port};
  444.         my $expires   = str2time(delete $hash{expires});
  445.  
  446.         my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
  447.         my $secure    = exists $hash{secure};    delete $hash{secure};
  448.         my $discard   = exists $hash{discard};   delete $hash{discard};
  449.  
  450.         my @array =    ($version,$val,$port,
  451.              $path_spec,$secure,$expires,$discard);
  452.         push(@array, \%hash) if %hash;
  453.         $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  454.     }
  455.     }
  456.     close(FILE);
  457.     1;
  458. }
  459.  
  460.  
  461. sub revert
  462. {
  463.     my $self = shift;
  464.     $self->clear->load;
  465.     $self;
  466. }
  467.  
  468.  
  469. sub clear
  470. {
  471.     my $self = shift;
  472.     if (@_ == 0) {
  473.     $self->{COOKIES} = {};
  474.     }
  475.     elsif (@_ == 1) {
  476.     delete $self->{COOKIES}{$_[0]};
  477.     }
  478.     elsif (@_ == 2) {
  479.     delete $self->{COOKIES}{$_[0]}{$_[1]};
  480.     }
  481.     elsif (@_ == 3) {
  482.     delete $self->{COOKIES}{$_[0]}{$_[1]}{$_[2]};
  483.     }
  484.     else {
  485.     require Carp;
  486.         Carp::carp('Usage: $c->clear([domain [,path [,key]]])');
  487.     }
  488.     $self;
  489. }
  490.  
  491.  
  492. sub clear_temporary_cookies
  493. {
  494.     my($self) = @_;
  495.  
  496.     $self->scan(sub {
  497.         if($_[9] or        # "Discard" flag set
  498.            not $_[8]) {    # No expire field?
  499.             $_[8] = -1;            # Set the expire/max_age field
  500.             $self->set_cookie(@_); # Clear the cookie
  501.         }
  502.       });
  503. }
  504.  
  505.  
  506. sub DESTROY
  507. {
  508.     my $self = shift;
  509.     $self->save if $self->{'autosave'};
  510. }
  511.  
  512.  
  513. sub scan
  514. {
  515.     my($self, $cb) = @_;
  516.     my($domain,$path,$key);
  517.     for $domain (sort keys %{$self->{COOKIES}}) {
  518.     for $path (sort keys %{$self->{COOKIES}{$domain}}) {
  519.         for $key (sort keys %{$self->{COOKIES}{$domain}{$path}}) {
  520.         my($version,$val,$port,$path_spec,
  521.            $secure,$expires,$discard,$rest) =
  522.                @{$self->{COOKIES}{$domain}{$path}{$key}};
  523.         $rest = {} unless defined($rest);
  524.         &$cb($version,$key,$val,$path,$domain,$port,
  525.              $path_spec,$secure,$expires,$discard,$rest);
  526.         }
  527.     }
  528.     }
  529. }
  530.  
  531.  
  532. sub as_string
  533. {
  534.     my($self, $skip_discard) = @_;
  535.     my @res;
  536.     $self->scan(sub {
  537.     my($version,$key,$val,$path,$domain,$port,
  538.        $path_spec,$secure,$expires,$discard,$rest) = @_;
  539.     return if $discard && $skip_discard;
  540.     my @h = ($key, $val);
  541.     push(@h, "path", $path);
  542.     push(@h, "domain" => $domain);
  543.     push(@h, "port" => $port) if defined $port;
  544.     push(@h, "path_spec" => undef) if $path_spec;
  545.     push(@h, "secure" => undef) if $secure;
  546.     push(@h, "expires" => HTTP::Date::time2isoz($expires)) if $expires;
  547.     push(@h, "discard" => undef) if $discard;
  548.     my $k;
  549.     for $k (sort keys %$rest) {
  550.         push(@h, $k, $rest->{$k});
  551.     }
  552.     push(@h, "version" => $version);
  553.     push(@res, "Set-Cookie3: " . join_header_words(\@h));
  554.     });
  555.     join("\n", @res, "");
  556. }
  557.  
  558. sub _host
  559. {
  560.     my($request, $url) = @_;
  561.     if (my $h = $request->header("Host")) {
  562.     $h =~ s/:\d+$//;  # might have a port as well
  563.     return lc($h);
  564.     }
  565.     return lc($url->host);
  566. }
  567.  
  568. sub _url_path
  569. {
  570.     my $url = shift;
  571.     my $path;
  572.     if($url->can('epath')) {
  573.        $path = $url->epath;    # URI::URL method
  574.     }
  575.     else {
  576.        $path = $url->path;           # URI::_generic method
  577.     }
  578.     $path = "/" unless length $path;
  579.     $path;
  580. }
  581.  
  582. sub _normalize_path  # so that plain string compare can be used
  583. {
  584.     my $x;
  585.     $_[0] =~ s/%([0-9a-fA-F][0-9a-fA-F])/
  586.              $x = uc($1);
  587.                  $x eq "2F" || $x eq "25" ? "%$x" :
  588.                                             pack("C", hex($x));
  589.               /eg;
  590.     $_[0] =~ s/([\0-\x20\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
  591. }
  592.  
  593. 1;
  594.  
  595. __END__
  596.  
  597. =head1 NAME
  598.  
  599. HTTP::Cookies - HTTP cookie jars
  600.  
  601. =head1 SYNOPSIS
  602.  
  603.   use HTTP::Cookies;
  604.   $cookie_jar = HTTP::Cookies->new(
  605.     file => "$ENV{'HOME'}/lwp_cookies.dat',
  606.     autosave => 1,
  607.   );
  608.  
  609.   use LWP;
  610.   my $browser = LWP::UserAgent->new;
  611.   $browser->cookie_jar($cookie_jar);
  612.  
  613. Or for an empty and temporary cookie jar:
  614.  
  615.   use LWP;
  616.   my $browser = LWP::UserAgent->new;
  617.   $browser->cookie_jar( {} );
  618.  
  619. =head1 DESCRIPTION
  620.  
  621. This class is for objects that represent a "cookie jar" -- that is, a
  622. database of all the HTTP cookies that a given LWP::UserAgent object
  623. knows about.
  624.  
  625. Cookies are a general mechanism which server side connections can use
  626. to both store and retrieve information on the client side of the
  627. connection.  For more information about cookies refer to
  628. <URL:http://www.netscape.com/newsref/std/cookie_spec.html> and
  629. <URL:http://www.cookiecentral.com/>.  This module also implements the
  630. new style cookies described in I<RFC 2965>.
  631. The two variants of cookies are supposed to be able to coexist happily.
  632.  
  633. Instances of the class I<HTTP::Cookies> are able to store a collection
  634. of Set-Cookie2: and Set-Cookie: headers and are able to use this
  635. information to initialize Cookie-headers in I<HTTP::Request> objects.
  636. The state of a I<HTTP::Cookies> object can be saved in and restored from
  637. files.
  638.  
  639. =head1 METHODS
  640.  
  641. The following methods are provided:
  642.  
  643. =over 4
  644.  
  645. =item $cookie_jar = HTTP::Cookies->new
  646.  
  647. The constructor takes hash style parameters.  The following
  648. parameters are recognized:
  649.  
  650.   file:            name of the file to restore cookies from and save cookies to
  651.   autosave:        save during destruction (bool)
  652.   ignore_discard:  save even cookies that are requested to be discarded (bool)
  653.   hide_cookie2:    do not add Cookie2 header to requests
  654.  
  655. Future parameters might include (not yet implemented):
  656.  
  657.   max_cookies               300
  658.   max_cookies_per_domain    20
  659.   max_cookie_size           4096
  660.  
  661.   no_cookies   list of domain names that we never return cookies to
  662.  
  663. =item $cookie_jar->add_cookie_header( $request )
  664.  
  665. The add_cookie_header() method will set the appropriate Cookie:-header
  666. for the I<HTTP::Request> object given as argument.  The $request must
  667. have a valid url attribute before this method is called.
  668.  
  669. =item $cookie_jar->extract_cookies( $response )
  670.  
  671. The extract_cookies() method will look for Set-Cookie: and
  672. Set-Cookie2: headers in the I<HTTP::Response> object passed as
  673. argument.  Any of these headers that are found are used to update
  674. the state of the $cookie_jar.
  675.  
  676. =item $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
  677.  
  678. The set_cookie() method updates the state of the $cookie_jar.  The
  679. $key, $val, $domain, $port and $path arguments are strings.  The
  680. $path_spec, $secure, $discard arguments are boolean values. The $maxage
  681. value is a number indicating number of seconds that this cookie will
  682. live.  A value <= 0 will delete this cookie.  %rest defines
  683. various other attributes like "Comment" and "CommentURL".
  684.  
  685. =item $cookie_jar->save
  686.  
  687. =item $cookie_jar->save( $file )
  688.  
  689. This method file saves the state of the $cookie_jar to a file.
  690. The state can then be restored later using the load() method.  If a
  691. filename is not specified we will use the name specified during
  692. construction.  If the attribute I<ignore_discard> is set, then we
  693. will even save cookies that are marked to be discarded.
  694.  
  695. The default is to save a sequence of "Set-Cookie3" lines.
  696. "Set-Cookie3" is a proprietary LWP format, not known to be compatible
  697. with any browser.  The I<HTTP::Cookies::Netscape> sub-class can
  698. be used to save in a format compatible with Netscape.
  699.  
  700. =item $cookie_jar->load
  701.  
  702. =item $cookie_jar->load( $file )
  703.  
  704. This method reads the cookies from the file and adds them to the
  705. $cookie_jar.  The file must be in the format written by the save()
  706. method.
  707.  
  708. =item $cookie_jar->revert
  709.  
  710. This method empties the $cookie_jar and re-loads the $cookie_jar
  711. from the last save file.
  712.  
  713. =item $cookie_jar->clear
  714.  
  715. =item $cookie_jar->clear( $domain )
  716.  
  717. =item $cookie_jar->clear( $domain, $path )
  718.  
  719. =item $cookie_jar->clear( $domain, $path, $key )
  720.  
  721. Invoking this method without arguments will empty the whole
  722. $cookie_jar.  If given a single argument only cookies belonging to
  723. that domain will be removed.  If given two arguments, cookies
  724. belonging to the specified path within that domain are removed.  If
  725. given three arguments, then the cookie with the specified key, path
  726. and domain is removed.
  727.  
  728. =item $cookie_jar->clear_temporary_cookies
  729.  
  730. Discard all temporary cookies. Scans for all cookies in the jar
  731. with either no expire field or a true C<discard> flag. To be
  732. called when the user agent shuts down according to RFC 2965.
  733.  
  734. =item $cookie_jar->scan( \&callback )
  735.  
  736. The argument is a subroutine that will be invoked for each cookie
  737. stored in the $cookie_jar.  The subroutine will be invoked with
  738. the following arguments:
  739.  
  740.   0  version
  741.   1  key
  742.   2  val
  743.   3  path
  744.   4  domain
  745.   5  port
  746.   6  path_spec
  747.   7  secure
  748.   8  expires
  749.   9  discard
  750.  10  hash
  751.  
  752. =item $cookie_jar->as_string
  753.  
  754. =item $cookie_jar->as_string( $skip_discardables )
  755.  
  756. The as_string() method will return the state of the $cookie_jar
  757. represented as a sequence of "Set-Cookie3" header lines separated by
  758. "\n".  If $skip_discardables is TRUE, it will not return lines for
  759. cookies with the I<Discard> attribute.
  760.  
  761. =back
  762.  
  763. =head1 SEE ALSO
  764.  
  765. L<HTTP::Cookies::Netscape>, L<HTTP::Cookies::Microsoft>
  766.  
  767. =head1 COPYRIGHT
  768.  
  769. Copyright 1997-2002 Gisle Aas
  770.  
  771. This library is free software; you can redistribute it and/or
  772. modify it under the same terms as Perl itself.
  773.  
  774.