1HTTP::Cookies(3) User Contributed Perl Documentation HTTP::Cookies(3)
2
3
4
6 HTTP::Cookies - HTTP cookie jars
7
9 version 6.08
10
12 use HTTP::Cookies;
13 $cookie_jar = HTTP::Cookies->new(
14 file => "$ENV{'HOME'}/lwp_cookies.dat",
15 autosave => 1,
16 );
17
18 use LWP;
19 my $browser = LWP::UserAgent->new;
20 $browser->cookie_jar($cookie_jar);
21
22 Or for an empty and temporary cookie jar:
23
24 use LWP;
25 my $browser = LWP::UserAgent->new;
26 $browser->cookie_jar( {} );
27
29 This class is for objects that represent a "cookie jar" -- that is, a
30 database of all the HTTP cookies that a given LWP::UserAgent object
31 knows about.
32
33 Cookies are a general mechanism which server side connections can use
34 to both store and retrieve information on the client side of the
35 connection. For more information about cookies refer to Cookie Spec
36 <http://curl.haxx.se/rfc/cookie_spec.html> and Cookie Central
37 <http://www.cookiecentral.com>. This module also implements the new
38 style cookies described in RFC 2965
39 <https://tools.ietf.org/html/rfc2965>. The two variants of cookies are
40 supposed to be able to coexist happily.
41
42 Instances of the class HTTP::Cookies are able to store a collection of
43 Set-Cookie2: and Set-Cookie: headers and are able to use this
44 information to initialize Cookie-headers in HTTP::Request objects. The
45 state of a HTTP::Cookies object can be saved in and restored from
46 files.
47
49 This module does not support Public Suffix <https://publicsuffix.org/>
50 encouraged by a more recent standard, RFC 6265
51 <https://tools.ietf.org/html/rfc6265>.
52
53 This module's shortcomings mean that a malicious Web site can set
54 cookies to track your user agent across all sites under a top level
55 domain. See t/publicsuffix.t in this module's distribution for
56 details.
57
58 HTTP::CookieJar::LWP supports Public Suffix, but only provides a
59 limited subset of this module's functionality and does not support
60 standards older than RFC 6265.
61
63 The following methods are provided:
64
65 $cookie_jar = HTTP::Cookies->new
66 The constructor takes hash style parameters. The following
67 parameters are recognized:
68
69 file: name of the file to restore cookies from and save cookies to
70 autosave: save during destruction (bool)
71 ignore_discard: save even cookies that are requested to be discarded (bool)
72 hide_cookie2: do not add Cookie2 header to requests
73
74 Future parameters might include (not yet implemented):
75
76 max_cookies 300
77 max_cookies_per_domain 20
78 max_cookie_size 4096
79
80 no_cookies list of domain names that we never return cookies to
81
82 $cookie_jar->get_cookies( $url_or_domain )
83 $cookie_jar->get_cookies( $url_or_domain, $cookie_key,... )
84 Returns a hash of the cookies that applies to the given URL. If a
85 domainname is given as argument, then a prefix of "https://" is
86 assumed.
87
88 If one or more $cookie_key parameters are provided return the given
89 values, or "undef" if the cookie isn't available.
90
91 $cookie_jar->add_cookie_header( $request )
92 The add_cookie_header() method will set the appropriate
93 Cookie:-header for the HTTP::Request object given as argument. The
94 $request must have a valid url attribute before this method is
95 called.
96
97 $cookie_jar->extract_cookies( $response )
98 The extract_cookies() method will look for Set-Cookie: and
99 Set-Cookie2: headers in the HTTP::Response object passed as
100 argument. Any of these headers that are found are used to update
101 the state of the $cookie_jar.
102
103 $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port,
104 $path_spec, $secure, $maxage, $discard, \%rest )
105 The set_cookie() method updates the state of the $cookie_jar. The
106 $key, $val, $domain, $port and $path arguments are strings. The
107 $path_spec, $secure, $discard arguments are boolean values. The
108 $maxage value is a number indicating number of seconds that this
109 cookie will live. A value of $maxage <= 0 will delete this cookie.
110 The $version argument sets the version of the cookie; the default
111 value is 0 ( original Netscape spec ). Setting $version to another
112 value indicates the RFC to which the cookie conforms (e.g. version
113 1 for RFC 2109). %rest defines various other attributes like
114 "Comment" and "CommentURL".
115
116 $cookie_jar->save
117 $cookie_jar->save( $file )
118 $cookie_jar->save( file => $file, ignore_discard => $ignore_discard )
119 This method file saves the state of the $cookie_jar to a file. The
120 state can then be restored later using the load() method. If a
121 filename is not specified we will use the name specified during
122 construction. If the $ignore_discard value is true (or not
123 specified, but attribute ignore_discard was set at cookie jar
124 construction), then we will even save cookies that are marked to be
125 discarded.
126
127 The default is to save a sequence of "Set-Cookie3" lines.
128 "Set-Cookie3" is a proprietary LWP format, not known to be
129 compatible with any browser. The HTTP::Cookies::Netscape sub-class
130 can be used to save in a format compatible with Netscape.
131
132 $cookie_jar->load
133 $cookie_jar->load( $file )
134 This method reads the cookies from the file and adds them to the
135 $cookie_jar. The file must be in the format written by the save()
136 method.
137
138 $cookie_jar->revert
139 This method empties the $cookie_jar and re-loads the $cookie_jar
140 from the last save file.
141
142 $cookie_jar->clear
143 $cookie_jar->clear( $domain )
144 $cookie_jar->clear( $domain, $path )
145 $cookie_jar->clear( $domain, $path, $key )
146 Invoking this method without arguments will empty the whole
147 $cookie_jar. If given a single argument only cookies belonging to
148 that domain will be removed. If given two arguments, cookies
149 belonging to the specified path within that domain are removed. If
150 given three arguments, then the cookie with the specified key, path
151 and domain is removed.
152
153 $cookie_jar->clear_temporary_cookies
154 Discard all temporary cookies. Scans for all cookies in the jar
155 with either no expire field or a true "discard" flag. To be called
156 when the user agent shuts down according to RFC 2965.
157
158 $cookie_jar->scan( \&callback )
159 The argument is a subroutine that will be invoked for each cookie
160 stored in the $cookie_jar. The subroutine will be invoked with the
161 following arguments:
162
163 0 version
164 1 key
165 2 val
166 3 path
167 4 domain
168 5 port
169 6 path_spec
170 7 secure
171 8 expires
172 9 discard
173 10 hash
174
175 $cookie_jar->as_string
176 $cookie_jar->as_string( $skip_discardables )
177 The as_string() method will return the state of the $cookie_jar
178 represented as a sequence of "Set-Cookie3" header lines separated
179 by "\n". If $skip_discardables is TRUE, it will not return lines
180 for cookies with the Discard attribute.
181
183 HTTP::Cookies::Netscape, HTTP::Cookies::Microsoft
184
186 Gisle Aas <gisle@activestate.com>
187
189 This software is copyright (c) 2002-2019 by Gisle Aas.
190
191 This is free software; you can redistribute it and/or modify it under
192 the same terms as the Perl 5 programming language system itself.
193
194
195
196perl v5.30.1 2020-01-30 HTTP::Cookies(3)