1HTTP::Cookies(3)      User Contributed Perl Documentation     HTTP::Cookies(3)
2
3
4

NAME

6       HTTP::Cookies - HTTP cookie jars
7

SYNOPSIS

9         use HTTP::Cookies;
10         $cookie_jar = HTTP::Cookies->new(
11           file => "$ENV{'HOME'}/lwp_cookies.dat',
12           autosave => 1,
13         );
14
15         use LWP;
16         my $browser = LWP::UserAgent->new;
17         $browser->cookie_jar($cookie_jar);
18
19       Or for an empty and temporary cookie jar:
20
21         use LWP;
22         my $browser = LWP::UserAgent->new;
23         $browser->cookie_jar( {} );
24

DESCRIPTION

26       This class is for objects that represent a "cookie jar" -- that is, a
27       database of all the HTTP cookies that a given LWP::UserAgent object
28       knows about.
29
30       Cookies are a general mechanism which server side connections can use
31       to both store and retrieve information on the client side of the con‐
32       nection.  For more information about cookies refer to
33       <URL:http://www.netscape.com/newsref/std/cookie_spec.html> and
34       <URL:http://www.cookiecentral.com/>.  This module also implements the
35       new style cookies described in RFC 2965.  The two variants of cookies
36       are supposed to be able to coexist happily.
37
38       Instances of the class HTTP::Cookies are able to store a collection of
39       Set-Cookie2: and Set-Cookie: headers and are able to use this informa‐
40       tion to initialize Cookie-headers in HTTP::Request objects.  The state
41       of a HTTP::Cookies object can be saved in and restored from files.
42

METHODS

44       The following methods are provided:
45
46       $cookie_jar = HTTP::Cookies->new
47           The constructor takes hash style parameters.  The following parame‐
48           ters are recognized:
49
50             file:            name of the file to restore cookies from and save cookies to
51             autosave:        save during destruction (bool)
52             ignore_discard:  save even cookies that are requested to be discarded (bool)
53             hide_cookie2:    do not add Cookie2 header to requests
54
55           Future parameters might include (not yet implemented):
56
57             max_cookies               300
58             max_cookies_per_domain    20
59             max_cookie_size           4096
60
61             no_cookies   list of domain names that we never return cookies to
62
63       $cookie_jar->add_cookie_header( $request )
64           The add_cookie_header() method will set the appropriate
65           Cookie:-header for the HTTP::Request object given as argument.  The
66           $request must have a valid url attribute before this method is
67           called.
68
69       $cookie_jar->extract_cookies( $response )
70           The extract_cookies() method will look for Set-Cookie: and
71           Set-Cookie2: headers in the HTTP::Response object passed as argu‐
72           ment.  Any of these headers that are found are used to update the
73           state of the $cookie_jar.
74
75       $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port,
76       $path_spec, $secure, $maxage, $discard, \%rest )
77           The set_cookie() method updates the state of the $cookie_jar.  The
78           $key, $val, $domain, $port and $path arguments are strings.  The
79           $path_spec, $secure, $discard arguments are boolean values. The
80           $maxage value is a number indicating number of seconds that this
81           cookie will live.  A value <= 0 will delete this cookie.  %rest
82           defines various other attributes like "Comment" and "CommentURL".
83
84       $cookie_jar->save
85       $cookie_jar->save( $file )
86           This method file saves the state of the $cookie_jar to a file.  The
87           state can then be restored later using the load() method.  If a
88           filename is not specified we will use the name specified during
89           construction.  If the attribute ignore_discard is set, then we will
90           even save cookies that are marked to be discarded.
91
92           The default is to save a sequence of "Set-Cookie3" lines.
93           "Set-Cookie3" is a proprietary LWP format, not known to be compati‐
94           ble with any browser.  The HTTP::Cookies::Netscape sub-class can be
95           used to save in a format compatible with Netscape.
96
97       $cookie_jar->load
98       $cookie_jar->load( $file )
99           This method reads the cookies from the file and adds them to the
100           $cookie_jar.  The file must be in the format written by the save()
101           method.
102
103       $cookie_jar->revert
104           This method empties the $cookie_jar and re-loads the $cookie_jar
105           from the last save file.
106
107       $cookie_jar->clear
108       $cookie_jar->clear( $domain )
109       $cookie_jar->clear( $domain, $path )
110       $cookie_jar->clear( $domain, $path, $key )
111           Invoking this method without arguments will empty the whole
112           $cookie_jar.  If given a single argument only cookies belonging to
113           that domain will be removed.  If given two arguments, cookies
114           belonging to the specified path within that domain are removed.  If
115           given three arguments, then the cookie with the specified key, path
116           and domain is removed.
117
118       $cookie_jar->clear_temporary_cookies
119           Discard all temporary cookies. Scans for all cookies in the jar
120           with either no expire field or a true "discard" flag. To be called
121           when the user agent shuts down according to RFC 2965.
122
123       $cookie_jar->scan( \&callback )
124           The argument is a subroutine that will be invoked for each cookie
125           stored in the $cookie_jar.  The subroutine will be invoked with the
126           following arguments:
127
128             0  version
129             1  key
130             2  val
131             3  path
132             4  domain
133             5  port
134             6  path_spec
135             7  secure
136             8  expires
137             9  discard
138            10  hash
139
140       $cookie_jar->as_string
141       $cookie_jar->as_string( $skip_discardables )
142           The as_string() method will return the state of the $cookie_jar
143           represented as a sequence of "Set-Cookie3" header lines separated
144           by "\n".  If $skip_discardables is TRUE, it will not return lines
145           for cookies with the Discard attribute.
146

SEE ALSO

148       HTTP::Cookies::Netscape, HTTP::Cookies::Microsoft
149
151       Copyright 1997-2002 Gisle Aas
152
153       This library is free software; you can redistribute it and/or modify it
154       under the same terms as Perl itself.
155
156
157
158perl v5.8.8                       2004-04-06                  HTTP::Cookies(3)
Impressum