1HTTP::Cookies(3) User Contributed Perl Documentation HTTP::Cookies(3)
2
3
4
6 HTTP::Cookies - HTTP cookie jars
7
9 version 6.04
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
36 <URL:http://curl.haxx.se/rfc/cookie_spec.html> and
37 <URL:http://www.cookiecentral.com/>. This module also implements the
38 new 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 %rest defines various other attributes like "Comment" and
111 "CommentURL".
112
113 $cookie_jar->save
114 $cookie_jar->save( $file )
115 This method file saves the state of the $cookie_jar to a file. The
116 state can then be restored later using the load() method. If a
117 filename is not specified we will use the name specified during
118 construction. If the attribute ignore_discard is set, then we will
119 even save cookies that are marked to be discarded.
120
121 The default is to save a sequence of "Set-Cookie3" lines.
122 "Set-Cookie3" is a proprietary LWP format, not known to be
123 compatible with any browser. The HTTP::Cookies::Netscape sub-class
124 can be used to save in a format compatible with Netscape.
125
126 $cookie_jar->load
127 $cookie_jar->load( $file )
128 This method reads the cookies from the file and adds them to the
129 $cookie_jar. The file must be in the format written by the save()
130 method.
131
132 $cookie_jar->revert
133 This method empties the $cookie_jar and re-loads the $cookie_jar
134 from the last save file.
135
136 $cookie_jar->clear
137 $cookie_jar->clear( $domain )
138 $cookie_jar->clear( $domain, $path )
139 $cookie_jar->clear( $domain, $path, $key )
140 Invoking this method without arguments will empty the whole
141 $cookie_jar. If given a single argument only cookies belonging to
142 that domain will be removed. If given two arguments, cookies
143 belonging to the specified path within that domain are removed. If
144 given three arguments, then the cookie with the specified key, path
145 and domain is removed.
146
147 $cookie_jar->clear_temporary_cookies
148 Discard all temporary cookies. Scans for all cookies in the jar
149 with either no expire field or a true "discard" flag. To be called
150 when the user agent shuts down according to RFC 2965.
151
152 $cookie_jar->scan( \&callback )
153 The argument is a subroutine that will be invoked for each cookie
154 stored in the $cookie_jar. The subroutine will be invoked with the
155 following arguments:
156
157 0 version
158 1 key
159 2 val
160 3 path
161 4 domain
162 5 port
163 6 path_spec
164 7 secure
165 8 expires
166 9 discard
167 10 hash
168
169 $cookie_jar->as_string
170 $cookie_jar->as_string( $skip_discardables )
171 The as_string() method will return the state of the $cookie_jar
172 represented as a sequence of "Set-Cookie3" header lines separated
173 by "\n". If $skip_discardables is TRUE, it will not return lines
174 for cookies with the Discard attribute.
175
177 HTTP::Cookies::Netscape, HTTP::Cookies::Microsoft
178
180 Gisle Aas <gisle@activestate.com>
181
183 This software is copyright (c) 2002-2017 by Gisle Aas.
184
185 This is free software; you can redistribute it and/or modify it under
186 the same terms as the Perl 5 programming language system itself.
187
188
189
190perl v5.28.0 2017-08-03 HTTP::Cookies(3)