home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / HTTP / Cookies.pm < prev    next >
Encoding:
Perl POD Document  |  2008-10-20  |  20.2 KB  |  781 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 = "5.817";
  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://wp.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.             $set =~ s/^\s+//;
  218.         my @cur;
  219.         my $param;
  220.         my $expires;
  221.         my $first_param = 1;
  222.         for $param (split(/;\s*/, $set)) {
  223.                 next unless length($param);
  224.         my($k,$v) = split(/\s*=\s*/, $param, 2);
  225.         if (defined $v) {
  226.             $v =~ s/\s+$//;
  227.             #print "$k => $v\n";
  228.         }
  229.         else {
  230.             $k =~ s/\s+$//;
  231.             #print "$k => undef";
  232.         }
  233.         if (!$first_param && lc($k) eq "expires") {
  234.             my $etime = str2time($v);
  235.             if ($etime) {
  236.             push(@cur, "Max-Age" => str2time($v) - $now);
  237.             $expires++;
  238.             }
  239.         }
  240.                 elsif (!$first_param && lc($k) =~ /^(?:version|discard|ns-cookie)/) {
  241.                     # ignore
  242.                 }
  243.         else {
  244.             push(@cur, $k => $v);
  245.         }
  246.         $first_param = 0;
  247.         }
  248.             next unless @cur;
  249.         next if $in_set2{$cur[0]};
  250.  
  251. #        push(@cur, "Port" => $req_port);
  252.         push(@cur, "Discard" => undef) unless $expires;
  253.         push(@cur, "Version" => 0);
  254.         push(@cur, "ns-cookie" => 1);
  255.         push(@set, \@cur);
  256.     }
  257.     }
  258.  
  259.   SET_COOKIE:
  260.     for my $set (@set) {
  261.     next unless @$set >= 2;
  262.  
  263.     my $key = shift @$set;
  264.     my $val = shift @$set;
  265.  
  266.         LWP::Debug::debug("Set cookie $key => $val");
  267.  
  268.     my %hash;
  269.     while (@$set) {
  270.         my $k = shift @$set;
  271.         my $v = shift @$set;
  272.         my $lc = lc($k);
  273.         # don't loose case distinction for unknown fields
  274.         $k = $lc if $lc =~ /^(?:discard|domain|max-age|
  275.                                     path|port|secure|version)$/x;
  276.         if ($k eq "discard" || $k eq "secure") {
  277.         $v = 1 unless defined $v;
  278.         }
  279.         next if exists $hash{$k};  # only first value is signigicant
  280.         $hash{$k} = $v;
  281.     };
  282.  
  283.     my %orig_hash = %hash;
  284.     my $version   = delete $hash{version};
  285.     $version = 1 unless defined($version);
  286.     my $discard   = delete $hash{discard};
  287.     my $secure    = delete $hash{secure};
  288.     my $maxage    = delete $hash{'max-age'};
  289.     my $ns_cookie = delete $hash{'ns-cookie'};
  290.  
  291.     # Check domain
  292.     my $domain  = delete $hash{domain};
  293.     $domain = lc($domain) if defined $domain;
  294.     if (defined($domain)
  295.         && $domain ne $req_host && $domain ne ".$req_host") {
  296.         if ($domain !~ /\./ && $domain ne "local") {
  297.             LWP::Debug::debug("Domain $domain contains no dot");
  298.         next SET_COOKIE;
  299.         }
  300.         $domain = ".$domain" unless $domain =~ /^\./;
  301.         if ($domain =~ /\.\d+$/) {
  302.             LWP::Debug::debug("IP-address $domain illeagal as domain");
  303.         next SET_COOKIE;
  304.         }
  305.         my $len = length($domain);
  306.         unless (substr($req_host, -$len) eq $domain) {
  307.             LWP::Debug::debug("Domain $domain does not match host $req_host");
  308.         next SET_COOKIE;
  309.         }
  310.         my $hostpre = substr($req_host, 0, length($req_host) - $len);
  311.         if ($hostpre =~ /\./ && !$ns_cookie) {
  312.             LWP::Debug::debug("Host prefix contain a dot: $hostpre => $domain");
  313.         next SET_COOKIE;
  314.         }
  315.     }
  316.     else {
  317.         $domain = $req_host;
  318.     }
  319.  
  320.     my $path = delete $hash{path};
  321.     my $path_spec;
  322.     if (defined $path && $path ne '') {
  323.         $path_spec++;
  324.         _normalize_path($path) if $path =~ /%/;
  325.         if (!$ns_cookie &&
  326.                 substr($req_path, 0, length($path)) ne $path) {
  327.             LWP::Debug::debug("Path $path is not a prefix of $req_path");
  328.         next SET_COOKIE;
  329.         }
  330.     }
  331.     else {
  332.         $path = $req_path;
  333.         $path =~ s,/[^/]*$,,;
  334.         $path = "/" unless length($path);
  335.     }
  336.  
  337.     my $port;
  338.     if (exists $hash{port}) {
  339.         $port = delete $hash{port};
  340.         if (defined $port) {
  341.         $port =~ s/\s+//g;
  342.         my $found;
  343.         for my $p (split(/,/, $port)) {
  344.             unless ($p =~ /^\d+$/) {
  345.               LWP::Debug::debug("Bad port $port (not numeric)");
  346.             next SET_COOKIE;
  347.             }
  348.             $found++ if $p eq $req_port;
  349.         }
  350.         unless ($found) {
  351.             LWP::Debug::debug("Request port ($req_port) not found in $port");
  352.             next SET_COOKIE;
  353.         }
  354.         }
  355.         else {
  356.         $port = "_$req_port";
  357.         }
  358.     }
  359.     $self->set_cookie($version,$key,$val,$path,$domain,$port,$path_spec,$secure,$maxage,$discard, \%hash)
  360.         if $self->set_cookie_ok(\%orig_hash);
  361.     }
  362.  
  363.     $response;
  364. }
  365.  
  366. sub set_cookie_ok
  367. {
  368.     1;
  369. }
  370.  
  371.  
  372. sub set_cookie
  373. {
  374.     my $self = shift;
  375.     my($version,
  376.        $key, $val, $path, $domain, $port,
  377.        $path_spec, $secure, $maxage, $discard, $rest) = @_;
  378.  
  379.     # path and key can not be empty (key can't start with '$')
  380.     return $self if !defined($path) || $path !~ m,^/, ||
  381.                 !defined($key)  || $key  =~ m,^\$,;
  382.  
  383.     # ensure legal port
  384.     if (defined $port) {
  385.     return $self unless $port =~ /^_?\d+(?:,\d+)*$/;
  386.     }
  387.  
  388.     my $expires;
  389.     if (defined $maxage) {
  390.     if ($maxage <= 0) {
  391.         delete $self->{COOKIES}{$domain}{$path}{$key};
  392.         return $self;
  393.     }
  394.     $expires = time() + $maxage;
  395.     }
  396.     $version = 0 unless defined $version;
  397.  
  398.     my @array = ($version, $val,$port,
  399.          $path_spec,
  400.          $secure, $expires, $discard);
  401.     push(@array, {%$rest}) if defined($rest) && %$rest;
  402.     # trim off undefined values at end
  403.     pop(@array) while !defined $array[-1];
  404.  
  405.     $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  406.     $self;
  407. }
  408.  
  409.  
  410. sub save
  411. {
  412.     my $self = shift;
  413.     my $file = shift || $self->{'file'} || return;
  414.     local(*FILE);
  415.     open(FILE, ">$file") or die "Can't open $file: $!";
  416.     print FILE "#LWP-Cookies-1.0\n";
  417.     print FILE $self->as_string(!$self->{ignore_discard});
  418.     close(FILE);
  419.     1;
  420. }
  421.  
  422.  
  423. sub load
  424. {
  425.     my $self = shift;
  426.     my $file = shift || $self->{'file'} || return;
  427.     local(*FILE, $_);
  428.     local $/ = "\n";  # make sure we got standard record separator
  429.     open(FILE, $file) or return;
  430.     my $magic = <FILE>;
  431.     unless ($magic =~ /^\#LWP-Cookies-(\d+\.\d+)/) {
  432.     warn "$file does not seem to contain cookies";
  433.     return;
  434.     }
  435.     while (<FILE>) {
  436.     next unless s/^Set-Cookie3:\s*//;
  437.     chomp;
  438.     my $cookie;
  439.     for $cookie (_split_header_words($_)) {
  440.         my($key,$val) = splice(@$cookie, 0, 2);
  441.         my %hash;
  442.         while (@$cookie) {
  443.         my $k = shift @$cookie;
  444.         my $v = shift @$cookie;
  445.         $hash{$k} = $v;
  446.         }
  447.         my $version   = delete $hash{version};
  448.         my $path      = delete $hash{path};
  449.         my $domain    = delete $hash{domain};
  450.         my $port      = delete $hash{port};
  451.         my $expires   = str2time(delete $hash{expires});
  452.  
  453.         my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
  454.         my $secure    = exists $hash{secure};    delete $hash{secure};
  455.         my $discard   = exists $hash{discard};   delete $hash{discard};
  456.  
  457.         my @array =    ($version,$val,$port,
  458.              $path_spec,$secure,$expires,$discard);
  459.         push(@array, \%hash) if %hash;
  460.         $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  461.     }
  462.     }
  463.     close(FILE);
  464.     1;
  465. }
  466.  
  467.  
  468. sub revert
  469. {
  470.     my $self = shift;
  471.     $self->clear->load;
  472.     $self;
  473. }
  474.  
  475.  
  476. sub clear
  477. {
  478.     my $self = shift;
  479.     if (@_ == 0) {
  480.     $self->{COOKIES} = {};
  481.     }
  482.     elsif (@_ == 1) {
  483.     delete $self->{COOKIES}{$_[0]};
  484.     }
  485.     elsif (@_ == 2) {
  486.     delete $self->{COOKIES}{$_[0]}{$_[1]};
  487.     }
  488.     elsif (@_ == 3) {
  489.     delete $self->{COOKIES}{$_[0]}{$_[1]}{$_[2]};
  490.     }
  491.     else {
  492.     require Carp;
  493.         Carp::carp('Usage: $c->clear([domain [,path [,key]]])');
  494.     }
  495.     $self;
  496. }
  497.  
  498.  
  499. sub clear_temporary_cookies
  500. {
  501.     my($self) = @_;
  502.  
  503.     $self->scan(sub {
  504.         if($_[9] or        # "Discard" flag set
  505.            not $_[8]) {    # No expire field?
  506.             $_[8] = -1;            # Set the expire/max_age field
  507.             $self->set_cookie(@_); # Clear the cookie
  508.         }
  509.       });
  510. }
  511.  
  512.  
  513. sub DESTROY
  514. {
  515.     my $self = shift;
  516.     $self->save if $self->{'autosave'};
  517. }
  518.  
  519.  
  520. sub scan
  521. {
  522.     my($self, $cb) = @_;
  523.     my($domain,$path,$key);
  524.     for $domain (sort keys %{$self->{COOKIES}}) {
  525.     for $path (sort keys %{$self->{COOKIES}{$domain}}) {
  526.         for $key (sort keys %{$self->{COOKIES}{$domain}{$path}}) {
  527.         my($version,$val,$port,$path_spec,
  528.            $secure,$expires,$discard,$rest) =
  529.                @{$self->{COOKIES}{$domain}{$path}{$key}};
  530.         $rest = {} unless defined($rest);
  531.         &$cb($version,$key,$val,$path,$domain,$port,
  532.              $path_spec,$secure,$expires,$discard,$rest);
  533.         }
  534.     }
  535.     }
  536. }
  537.  
  538.  
  539. sub as_string
  540. {
  541.     my($self, $skip_discard) = @_;
  542.     my @res;
  543.     $self->scan(sub {
  544.     my($version,$key,$val,$path,$domain,$port,
  545.        $path_spec,$secure,$expires,$discard,$rest) = @_;
  546.     return if $discard && $skip_discard;
  547.     my @h = ($key, $val);
  548.     push(@h, "path", $path);
  549.     push(@h, "domain" => $domain);
  550.     push(@h, "port" => $port) if defined $port;
  551.     push(@h, "path_spec" => undef) if $path_spec;
  552.     push(@h, "secure" => undef) if $secure;
  553.     push(@h, "expires" => HTTP::Date::time2isoz($expires)) if $expires;
  554.     push(@h, "discard" => undef) if $discard;
  555.     my $k;
  556.     for $k (sort keys %$rest) {
  557.         push(@h, $k, $rest->{$k});
  558.     }
  559.     push(@h, "version" => $version);
  560.     push(@res, "Set-Cookie3: " . join_header_words(\@h));
  561.     });
  562.     join("\n", @res, "");
  563. }
  564.  
  565. sub _host
  566. {
  567.     my($request, $url) = @_;
  568.     if (my $h = $request->header("Host")) {
  569.     $h =~ s/:\d+$//;  # might have a port as well
  570.     return lc($h);
  571.     }
  572.     return lc($url->host);
  573. }
  574.  
  575. sub _url_path
  576. {
  577.     my $url = shift;
  578.     my $path;
  579.     if($url->can('epath')) {
  580.        $path = $url->epath;    # URI::URL method
  581.     }
  582.     else {
  583.        $path = $url->path;           # URI::_generic method
  584.     }
  585.     $path = "/" unless length $path;
  586.     $path;
  587. }
  588.  
  589. sub _normalize_path  # so that plain string compare can be used
  590. {
  591.     my $x;
  592.     $_[0] =~ s/%([0-9a-fA-F][0-9a-fA-F])/
  593.              $x = uc($1);
  594.                  $x eq "2F" || $x eq "25" ? "%$x" :
  595.                                             pack("C", hex($x));
  596.               /eg;
  597.     $_[0] =~ s/([\0-\x20\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
  598. }
  599.  
  600. 1;
  601.  
  602. __END__
  603.  
  604. =head1 NAME
  605.  
  606. HTTP::Cookies - HTTP cookie jars
  607.  
  608. =head1 SYNOPSIS
  609.  
  610.   use HTTP::Cookies;
  611.   $cookie_jar = HTTP::Cookies->new(
  612.     file => "$ENV{'HOME'}/lwp_cookies.dat',
  613.     autosave => 1,
  614.   );
  615.  
  616.   use LWP;
  617.   my $browser = LWP::UserAgent->new;
  618.   $browser->cookie_jar($cookie_jar);
  619.  
  620. Or for an empty and temporary cookie jar:
  621.  
  622.   use LWP;
  623.   my $browser = LWP::UserAgent->new;
  624.   $browser->cookie_jar( {} );
  625.  
  626. =head1 DESCRIPTION
  627.  
  628. This class is for objects that represent a "cookie jar" -- that is, a
  629. database of all the HTTP cookies that a given LWP::UserAgent object
  630. knows about.
  631.  
  632. Cookies are a general mechanism which server side connections can use
  633. to both store and retrieve information on the client side of the
  634. connection.  For more information about cookies refer to
  635. <URL:http://wp.netscape.com/newsref/std/cookie_spec.html> and
  636. <URL:http://www.cookiecentral.com/>.  This module also implements the
  637. new style cookies described in I<RFC 2965>.
  638. The two variants of cookies are supposed to be able to coexist happily.
  639.  
  640. Instances of the class I<HTTP::Cookies> are able to store a collection
  641. of Set-Cookie2: and Set-Cookie: headers and are able to use this
  642. information to initialize Cookie-headers in I<HTTP::Request> objects.
  643. The state of a I<HTTP::Cookies> object can be saved in and restored from
  644. files.
  645.  
  646. =head1 METHODS
  647.  
  648. The following methods are provided:
  649.  
  650. =over 4
  651.  
  652. =item $cookie_jar = HTTP::Cookies->new
  653.  
  654. The constructor takes hash style parameters.  The following
  655. parameters are recognized:
  656.  
  657.   file:            name of the file to restore cookies from and save cookies to
  658.   autosave:        save during destruction (bool)
  659.   ignore_discard:  save even cookies that are requested to be discarded (bool)
  660.   hide_cookie2:    do not add Cookie2 header to requests
  661.  
  662. Future parameters might include (not yet implemented):
  663.  
  664.   max_cookies               300
  665.   max_cookies_per_domain    20
  666.   max_cookie_size           4096
  667.  
  668.   no_cookies   list of domain names that we never return cookies to
  669.  
  670. =item $cookie_jar->add_cookie_header( $request )
  671.  
  672. The add_cookie_header() method will set the appropriate Cookie:-header
  673. for the I<HTTP::Request> object given as argument.  The $request must
  674. have a valid url attribute before this method is called.
  675.  
  676. =item $cookie_jar->extract_cookies( $response )
  677.  
  678. The extract_cookies() method will look for Set-Cookie: and
  679. Set-Cookie2: headers in the I<HTTP::Response> object passed as
  680. argument.  Any of these headers that are found are used to update
  681. the state of the $cookie_jar.
  682.  
  683. =item $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
  684.  
  685. The set_cookie() method updates the state of the $cookie_jar.  The
  686. $key, $val, $domain, $port and $path arguments are strings.  The
  687. $path_spec, $secure, $discard arguments are boolean values. The $maxage
  688. value is a number indicating number of seconds that this cookie will
  689. live.  A value <= 0 will delete this cookie.  %rest defines
  690. various other attributes like "Comment" and "CommentURL".
  691.  
  692. =item $cookie_jar->save
  693.  
  694. =item $cookie_jar->save( $file )
  695.  
  696. This method file saves the state of the $cookie_jar to a file.
  697. The state can then be restored later using the load() method.  If a
  698. filename is not specified we will use the name specified during
  699. construction.  If the attribute I<ignore_discard> is set, then we
  700. will even save cookies that are marked to be discarded.
  701.  
  702. The default is to save a sequence of "Set-Cookie3" lines.
  703. "Set-Cookie3" is a proprietary LWP format, not known to be compatible
  704. with any browser.  The I<HTTP::Cookies::Netscape> sub-class can
  705. be used to save in a format compatible with Netscape.
  706.  
  707. =item $cookie_jar->load
  708.  
  709. =item $cookie_jar->load( $file )
  710.  
  711. This method reads the cookies from the file and adds them to the
  712. $cookie_jar.  The file must be in the format written by the save()
  713. method.
  714.  
  715. =item $cookie_jar->revert
  716.  
  717. This method empties the $cookie_jar and re-loads the $cookie_jar
  718. from the last save file.
  719.  
  720. =item $cookie_jar->clear
  721.  
  722. =item $cookie_jar->clear( $domain )
  723.  
  724. =item $cookie_jar->clear( $domain, $path )
  725.  
  726. =item $cookie_jar->clear( $domain, $path, $key )
  727.  
  728. Invoking this method without arguments will empty the whole
  729. $cookie_jar.  If given a single argument only cookies belonging to
  730. that domain will be removed.  If given two arguments, cookies
  731. belonging to the specified path within that domain are removed.  If
  732. given three arguments, then the cookie with the specified key, path
  733. and domain is removed.
  734.  
  735. =item $cookie_jar->clear_temporary_cookies
  736.  
  737. Discard all temporary cookies. Scans for all cookies in the jar
  738. with either no expire field or a true C<discard> flag. To be
  739. called when the user agent shuts down according to RFC 2965.
  740.  
  741. =item $cookie_jar->scan( \&callback )
  742.  
  743. The argument is a subroutine that will be invoked for each cookie
  744. stored in the $cookie_jar.  The subroutine will be invoked with
  745. the following arguments:
  746.  
  747.   0  version
  748.   1  key
  749.   2  val
  750.   3  path
  751.   4  domain
  752.   5  port
  753.   6  path_spec
  754.   7  secure
  755.   8  expires
  756.   9  discard
  757.  10  hash
  758.  
  759. =item $cookie_jar->as_string
  760.  
  761. =item $cookie_jar->as_string( $skip_discardables )
  762.  
  763. The as_string() method will return the state of the $cookie_jar
  764. represented as a sequence of "Set-Cookie3" header lines separated by
  765. "\n".  If $skip_discardables is TRUE, it will not return lines for
  766. cookies with the I<Discard> attribute.
  767.  
  768. =back
  769.  
  770. =head1 SEE ALSO
  771.  
  772. L<HTTP::Cookies::Netscape>, L<HTTP::Cookies::Microsoft>
  773.  
  774. =head1 COPYRIGHT
  775.  
  776. Copyright 1997-2002 Gisle Aas
  777.  
  778. This library is free software; you can redistribute it and/or
  779. modify it under the same terms as Perl itself.
  780.  
  781.