.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTTP::Cookies 3" .TH HTTP::Cookies 3 2023-12-14 "perl v5.38.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME HTTP::Cookies \- HTTP cookie jars .SH VERSION .IX Header "VERSION" version 6.11 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 5 \& use HTTP::Cookies; \& $cookie_jar = HTTP::Cookies\->new( \& file => "$ENV{\*(AqHOME\*(Aq}/lwp_cookies.dat", \& autosave => 1, \& ); \& \& use LWP; \& my $browser = LWP::UserAgent\->new; \& $browser\->cookie_jar($cookie_jar); .Ve .PP Or for an empty and temporary cookie jar: .PP .Vb 3 \& use LWP; \& my $browser = LWP::UserAgent\->new; \& $browser\->cookie_jar( {} ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This class is for objects that represent a "cookie jar" \-\- that is, a database of all the HTTP cookies that a given LWP::UserAgent object knows about. .PP Cookies are a general mechanism which server side connections can use to both store and retrieve information on the client side of the connection. For more information about cookies refer to Cookie Spec and Cookie Central . This module also implements the new style cookies described in RFC 2965 . The two variants of cookies are supposed to be able to coexist happily. .PP Instances of the class \fIHTTP::Cookies\fR are able to store a collection of Set\-Cookie2: and Set-Cookie: headers and are able to use this information to initialize Cookie-headers in \fIHTTP::Request\fR objects. The state of a \fIHTTP::Cookies\fR object can be saved in and restored from files. .SH LIMITATIONS .IX Header "LIMITATIONS" This module does not support Public Suffix encouraged by a more recent standard, RFC 6265 . .PP This module's shortcomings mean that a malicious Web site can set cookies to track your user agent across all sites under a top level domain. See \fIt/publicsuffix.t\fR in this module's distribution for details. .PP HTTP::CookieJar::LWP supports Public Suffix, but only provides a limited subset of this module's functionality and does not support standards older than \&\fIRFC 6265\fR. .SH METHODS .IX Header "METHODS" The following methods are provided: .ie n .IP "$cookie_jar = HTTP::Cookies\->new" 4 .el .IP "\f(CW$cookie_jar\fR = HTTP::Cookies\->new" 4 .IX Item "$cookie_jar = HTTP::Cookies->new" The constructor takes hash style parameters. The following parameters are recognized: .Sp .Vb 4 \& file: name of the file to restore cookies from and save cookies to \& autosave: save during destruction (bool) \& ignore_discard: save even cookies that are requested to be discarded (bool) \& hide_cookie2: do not add Cookie2 header to requests .Ve .Sp Future parameters might include (not yet implemented): .Sp .Vb 3 \& max_cookies 300 \& max_cookies_per_domain 20 \& max_cookie_size 4096 \& \& no_cookies list of domain names that we never return cookies to .Ve .ie n .IP "$cookie_jar\->get_cookies( $url_or_domain )" 4 .el .IP "\f(CW$cookie_jar\fR\->get_cookies( \f(CW$url_or_domain\fR )" 4 .IX Item "$cookie_jar->get_cookies( $url_or_domain )" .PD 0 .ie n .IP "$cookie_jar\->get_cookies( $url_or_domain, $cookie_key,... )" 4 .el .IP "\f(CW$cookie_jar\fR\->get_cookies( \f(CW$url_or_domain\fR, \f(CW$cookie_key\fR,... )" 4 .IX Item "$cookie_jar->get_cookies( $url_or_domain, $cookie_key,... )" .PD Returns a hash of the cookies that applies to the given URL. If a domainname is given as argument, then a prefix of "https://" is assumed. .Sp If one or more \f(CW$cookie_key\fR parameters are provided return the given values, or \f(CW\*(C`undef\*(C'\fR if the cookie isn't available. .ie n .IP "$cookie_jar\->add_cookie_header( $request )" 4 .el .IP "\f(CW$cookie_jar\fR\->add_cookie_header( \f(CW$request\fR )" 4 .IX Item "$cookie_jar->add_cookie_header( $request )" The \fBadd_cookie_header()\fR method will set the appropriate Cookie:\-header for the \fIHTTP::Request\fR object given as argument. The \f(CW$request\fR must have a valid url attribute before this method is called. .ie n .IP "$cookie_jar\->extract_cookies( $response )" 4 .el .IP "\f(CW$cookie_jar\fR\->extract_cookies( \f(CW$response\fR )" 4 .IX Item "$cookie_jar->extract_cookies( $response )" The \fBextract_cookies()\fR method will look for Set-Cookie: and Set\-Cookie2: headers in the \fIHTTP::Response\fR object passed as argument. Any of these headers that are found are used to update the state of the \f(CW$cookie_jar\fR. .ie n .IP "$cookie_jar\->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \e%rest )" 4 .el .IP "\f(CW$cookie_jar\fR\->set_cookie( \f(CW$version\fR, \f(CW$key\fR, \f(CW$val\fR, \f(CW$path\fR, \f(CW$domain\fR, \f(CW$port\fR, \f(CW$path_spec\fR, \f(CW$secure\fR, \f(CW$maxage\fR, \f(CW$discard\fR, \e%rest )" 4 .IX Item "$cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, %rest )" The \fBset_cookie()\fR method updates the state of the \f(CW$cookie_jar\fR. The \&\f(CW$key\fR, \f(CW$val\fR, \f(CW$domain\fR, \f(CW$port\fR and \f(CW$path\fR arguments are strings. The \&\f(CW$path_spec\fR, \f(CW$secure\fR, \f(CW$discard\fR arguments are boolean values. The \f(CW$maxage\fR value is a number indicating number of seconds that this cookie will live. A value of \f(CW$maxage\fR <= 0 will delete this cookie. The \f(CW$version\fR argument sets the version of the cookie; the default value is 0 ( original Netscape spec ). Setting \f(CW$version\fR to another value indicates the RFC to which the cookie conforms (e.g. version 1 for RFC 2109). \f(CW%rest\fR defines various other attributes like "Comment" and "CommentURL". .ie n .IP $cookie_jar\->save 4 .el .IP \f(CW$cookie_jar\fR\->save 4 .IX Item "$cookie_jar->save" .PD 0 .ie n .IP "$cookie_jar\->save( $file )" 4 .el .IP "\f(CW$cookie_jar\fR\->save( \f(CW$file\fR )" 4 .IX Item "$cookie_jar->save( $file )" .ie n .IP "$cookie_jar\->save( file => $file, ignore_discard => $ignore_discard )" 4 .el .IP "\f(CW$cookie_jar\fR\->save( file => \f(CW$file\fR, ignore_discard => \f(CW$ignore_discard\fR )" 4 .IX Item "$cookie_jar->save( file => $file, ignore_discard => $ignore_discard )" .PD This method saves the state of the \f(CW$cookie_jar\fR to a file. The state can then be restored later using the \fBload()\fR method. If a filename is not specified we will use the name specified during construction. If the \f(CW$ignore_discard\fR value is true (or not specified, but attribute \fIignore_discard\fR was set at cookie jar construction), then we will even save cookies that are marked to be discarded. .Sp The default is to save a sequence of "Set\-Cookie3" lines. "Set\-Cookie3" is a proprietary LWP format, not known to be compatible with any browser. The \fIHTTP::Cookies::Netscape\fR sub-class can be used to save in a format compatible with Netscape. .ie n .IP $cookie_jar\->load 4 .el .IP \f(CW$cookie_jar\fR\->load 4 .IX Item "$cookie_jar->load" .PD 0 .ie n .IP "$cookie_jar\->load( $file )" 4 .el .IP "\f(CW$cookie_jar\fR\->load( \f(CW$file\fR )" 4 .IX Item "$cookie_jar->load( $file )" .PD This method reads the cookies from the file and adds them to the \&\f(CW$cookie_jar\fR. The file must be in the format written by the \fBsave()\fR method. .ie n .IP $cookie_jar\->revert 4 .el .IP \f(CW$cookie_jar\fR\->revert 4 .IX Item "$cookie_jar->revert" This method empties the \f(CW$cookie_jar\fR and re-loads the \f(CW$cookie_jar\fR from the last save file. .ie n .IP $cookie_jar\->clear 4 .el .IP \f(CW$cookie_jar\fR\->clear 4 .IX Item "$cookie_jar->clear" .PD 0 .ie n .IP "$cookie_jar\->clear( $domain )" 4 .el .IP "\f(CW$cookie_jar\fR\->clear( \f(CW$domain\fR )" 4 .IX Item "$cookie_jar->clear( $domain )" .ie n .IP "$cookie_jar\->clear( $domain, $path )" 4 .el .IP "\f(CW$cookie_jar\fR\->clear( \f(CW$domain\fR, \f(CW$path\fR )" 4 .IX Item "$cookie_jar->clear( $domain, $path )" .ie n .IP "$cookie_jar\->clear( $domain, $path, $key )" 4 .el .IP "\f(CW$cookie_jar\fR\->clear( \f(CW$domain\fR, \f(CW$path\fR, \f(CW$key\fR )" 4 .IX Item "$cookie_jar->clear( $domain, $path, $key )" .PD Invoking this method without arguments will empty the whole \&\f(CW$cookie_jar\fR. If given a single argument only cookies belonging to that domain will be removed. If given two arguments, cookies belonging to the specified path within that domain are removed. If given three arguments, then the cookie with the specified key, path and domain is removed. .ie n .IP $cookie_jar\->clear_temporary_cookies 4 .el .IP \f(CW$cookie_jar\fR\->clear_temporary_cookies 4 .IX Item "$cookie_jar->clear_temporary_cookies" Discard all temporary cookies. Scans for all cookies in the jar with either no expire field or a true \f(CW\*(C`discard\*(C'\fR flag. To be called when the user agent shuts down according to RFC 2965. .ie n .IP "$cookie_jar\->scan( \e&callback )" 4 .el .IP "\f(CW$cookie_jar\fR\->scan( \e&callback )" 4 .IX Item "$cookie_jar->scan( &callback )" The argument is a subroutine that will be invoked for each cookie stored in the \f(CW$cookie_jar\fR. The subroutine will be invoked with the following arguments: .Sp .Vb 11 \& 0 version \& 1 key \& 2 val \& 3 path \& 4 domain \& 5 port \& 6 path_spec \& 7 secure \& 8 expires \& 9 discard \& 10 hash .Ve .ie n .IP $cookie_jar\->as_string 4 .el .IP \f(CW$cookie_jar\fR\->as_string 4 .IX Item "$cookie_jar->as_string" .PD 0 .ie n .IP "$cookie_jar\->as_string( $skip_discardables )" 4 .el .IP "\f(CW$cookie_jar\fR\->as_string( \f(CW$skip_discardables\fR )" 4 .IX Item "$cookie_jar->as_string( $skip_discardables )" .PD The \fBas_string()\fR method will return the state of the \f(CW$cookie_jar\fR represented as a sequence of "Set\-Cookie3" header lines separated by "\en". If \f(CW$skip_discardables\fR is TRUE, it will not return lines for cookies with the \fIDiscard\fR attribute. .SH "SEE ALSO" .IX Header "SEE ALSO" HTTP::Cookies::Netscape, HTTP::Cookies::Microsoft .SH AUTHOR .IX Header "AUTHOR" Gisle Aas .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2002 by Gisle Aas. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.