1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
|
<html>
<head>
<link rel="stylesheet" type="text/css" href="dox.css" />
<title>libapreq2-2.04-dev: libapreq2: Apache::Cookie</title>
</head>
<body>
<div id="page-header">
<p class="menu">
<a href="http://www.apache.org/">Apache Software Foundation</a> > <a href="http://httpd.apache.org">HTTP Server Project</a> >
<a href="http://httpd.apache.org/apreq/">Request Library Subproject</a></p>
<p class="apache">Apache HTTP Server Request Library</p>
<img alt="" src="feather.gif" /></div>
<!-- Generated by Doxygen 1.3.8 -->
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="pages.html">Related Pages</a></div>
<h1>Apache::Cookie<br>
<small>
[<a class="el" href="group__apreq__xs.html">Perl</a>]</small>
</h1><table border=0 cellpadding=0 cellspacing=0>
<tr><td></td></tr>
</table>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#apache__cookie__jar">Apache::Cookie::Jar</a></li>
<ul>
<li><a href="#new">new</a></li>
<li><a href="#cookies">cookies</a></li>
<li><a href="#status">status</a></li>
<li><a href="#env">env</a></li>
</ul>
<li><a href="#apache__cookie">Apache::Cookie</a></li>
<ul>
<li><a href="#new">new</a></li>
<li><a href="#freeze">freeze</a></li>
<li><a href="#thaw">thaw</a></li>
<li><a href="#as_string">as_string</a></li>
<li><a href="#name">name</a></li>
<li><a href="#value">value</a></li>
<li><a href="#raw_value">raw_value</a></li>
<li><a href="#bake">bake</a></li>
<li><a href="#bake2">bake2</a></li>
<li><a href="#domain">domain</a></li>
<li><a href="#path">path</a></li>
<li><a href="#version">version</a></li>
<li><a href="#expires">expires</a></li>
<li><a href="#secure">secure</a></li>
<li><a href="#comment">comment</a></li>
<li><a href="#commenturl">commentURL</a></li>
<li><a href="#fetch">fetch</a></li>
</ul>
<li><a href="#porting_from_1_x">PORTING from 1.X</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Apache::Cookie, Apache::Cookie::Jar - HTTP Cookies Class</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
use Apache::Cookie;</pre>
<pre>
$j = Apache::Cookie::Jar->new($r);
$c_in = $j->cookies("foo"); # get cookie from request headers</pre>
<pre>
$c_out = Apache::Cookie->new($r,
-name => "mycookie",
-value => $c_in->name );</pre>
<pre>
$c_out->path("/bar"); # set path to "/bar"
$c_out->bake; # send cookie in response headers</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The 2.X Apache::Cookie module is based on the original 1.X versions, which mimic
the CGI::Cookie API. The current version of this module includes several packages
and methods which are patterned after Apache::Request, yet remain largely
backwards-compatible with the original 1.X API (see the <a href="#porting_from_1_x">PORTING from 1.X</a> section
below for known issues).</p>
<p>This manpage documents the Apache::Cookie and Apache::Cookie::Jar packages.
Apache::Cookie::Error, Apache::Cookie::Jar::Error and Apache::Cookie::Table
are also provided by this module but documented elsewhere (related manpages
listed in <a href="#see_also">SEE ALSO</a>).</p>
<p>
</p>
<hr />
<h1><a name="apache__cookie__jar">Apache::Cookie::Jar</a></h1>
<p>This class collects Apache::Cookie objects into a lookup table. It plays
the same role for accessing the incoming cookies as Apache::Request does for
accessing the incoming params and file uploads.</p>
<p>
</p>
<h2><a name="new">new</a></h2>
<pre>
Apache::Cookie::Jar->new($env, %args)</pre>
<p>Class method that retrieves the parsed cookie jar from the current
environment. An optional VALUE_CLASS => $class argument instructs
the jar to bless any returned cookies into $class instead
of Apache::Cookie. This feature is meant to be useful in situations
where <code>Apache::Cookie::thaw()</code> is unable to correctly interpret an incoming
cookie's serialization. Users can simply override <code>thaw</code> in an
application-specific subclass and pass that subclass's name as the
VALUE_CLASS argument:</p>
<pre>
{
package FOO;
@ISA= 'Apache::Cookie';
}
my $jar = Apache::Cookie::Jar->new($r, VALUE_CLASS => "FOO");
ok $jar->cookies("foo")->isa("FOO");
ok $jar->cookies->{bar}->isa("FOO");</pre>
<p>
</p>
<h2><a name="cookies">cookies</a></h2>
<pre>
$jar->cookies()
$jar->cookies($key)</pre>
<p>Retrieve cookies named $key with from the jar object. In scalar
context the first such cookie is returned, and in list context the
full list of such cookies are returned.</p>
<p>If the $key argument is omitted, <code>scalar $jar->cookies()</code> will
return an Apache::Cookie::Table object containing all the cookies in
the jar. Modifications to the this object will affect the jar's
internal <em>cookies</em> table in <code>apreq_jar_t</code>, so their impact will
be noticed by all libapreq2 applications during this request.</p>
<p>In list context <code>$jar->cookies()</code> returns the list of names
for all the cookies in the jar. The order corresponds to the
order in which the cookies appeared in the incoming ``Cookie'' header.</p>
<p>This method will throw an Apache::Cookie::Jar::Error object into $@ if
the returned <code>value(s)</code> may be unreliable. In particular, note that
<code>scalar $jar->cookies("foo")</code> will not croak if it can locate
the a ``foo'' cookie within the jar's parsed cookie table, even if the
cookie parser has failed (the cookies are parsed in the same order
as they appeared in the ``Cookie'' header). In all other circumstances
<code>cookies</code> will croak if the parser failed to successfully parse the
``Cookie'' header.</p>
<pre>
$c = Apache::Cookie->new($r, name => "foo", value => 3);
$j->cookies->add($c);</pre>
<pre>
$cookie = $j->cookies("foo"); # first foo cookie
@cookies = $j->cookies("foo"); # all foo cookies
@names = $j->cookies(); # all cookie names</pre>
<p>
</p>
<h2><a name="status">status</a></h2>
<pre>
$jar->status()
$jar->status($set)</pre>
<p>Get or set the <em>APR</em> status code of the cookie parser:
APR_SUCCESS on success, error otherwise.</p>
<pre>
$j->status(-1);
ok $j->status == -1;
eval { @cookies = $j->cookies("foo") }; # croaks
ok $@->isa("Apache::Cookie::Jar::Error");
$j->status(0);</pre>
<p>
</p>
<h2><a name="env">env</a></h2>
<pre>
Apache::Cookie::Jar->env()
$jar->env()</pre>
<p>As a class method <code>Apache::Cookie::Jar->env</code> returns
the environment class associated with Apache::Cookie::Jar.
As an object method, <code>$jar->env</code> returns the environment
object which first created the $jar (via <code>new</code>).</p>
<pre>
ok $j->env->isa(Apache::Cookie::Jar->env);</pre>
<p>
</p>
<hr />
<h1><a name="apache__cookie">Apache::Cookie</a></h1>
<p>
</p>
<h2><a name="new">new</a></h2>
<pre>
Apache::Cookie->new($env, %args)</pre>
<p>Just like CGI::Cookie::new, but requires an additional environment argument:</p>
<pre>
$cookie = Apache::Cookie->new($r,
-name => 'foo',
-value => 'bar',
-expires => '+3M',
-domain => '.capricorn.com',
-path => '/cgi-bin/database',
-secure => 1
);</pre>
<p>The <code>-value</code> argument may be either an arrayref, a hashref, or
a string. <code>Apache::Cookie::freeze</code> encodes this argument into the
cookie's raw value.</p>
<p>
</p>
<h2><a name="freeze">freeze</a></h2>
<pre>
Apache::Cookie->freeze($value)</pre>
<p>Helper function (for <code>new</code>) that serializes a new cookie's value in a
manner compatible with CGI::Cookie (and Apache::Cookie 1.X). This class
method accepts an arrayref, hashref, or normal perl string in $value.</p>
<pre>
$value = Apache::Cookie->freeze(["2+2", "=4"]);</pre>
<p>
</p>
<h2><a name="thaw">thaw</a></h2>
<pre>
Apache::Cookie->thaw($value)
$cookie->thaw()</pre>
<p>This is the helper method (for <code>value</code>) responsible for decoding the
raw value of a cookie. An optional argument $value may be used in
place of the cookie's raw value. This method can also decode cookie
values created using CGI::Cookie or Apache::Cookie 1.X.</p>
<pre>
print $cookie->thaw; # prints "bar"
@values = Apache::Cookie->thaw($value); # ( "2+2", "=4" )</pre>
<p>
</p>
<h2><a name="as_string">as_string</a></h2>
<pre>
$cookie->as_string()</pre>
<p>Format the cookie object as a string. The quote-operator for Apache::Cookie
is overloaded to run this method whenever a cookie appears in quotes.</p>
<pre>
ok "$cookie" eq $cookie->as_string;</pre>
<p>
</p>
<h2><a name="name">name</a></h2>
<pre>
$cookie->name()</pre>
<p>Get the name of the cookie.</p>
<p>
</p>
<h2><a name="value">value</a></h2>
<pre>
$cookie->value()</pre>
<p>Get the (unswizzled) value of the cookie:</p>
<pre>
my $value = $cookie->value;
my @values = $cookie->value;</pre>
<p>Note: if the cookie's value was created using a <code>freeze</code> method,
one way to reconstitute the object is by subclassing
Apache::Cookie with a package that provides the associated <code>thaw</code> sub:</p>
<pre>
{
package My::COOKIE;
@ISA = 'Apache::Cookie';
sub thaw { my $val = shift->raw_value; $val =~ tr/a-z/A-Z/; $val }
}</pre>
<pre>
bless $cookie, "My::COOKIE";</pre>
<pre>
ok $cookie->value eq "BAR";</pre>
<p>
</p>
<h2><a name="raw_value">raw_value</a></h2>
<pre>
$cookie->raw_value()</pre>
<p>Gets the raw (opaque) value string as it appears in the incoming
``Cookie'' header.</p>
<pre>
ok $cookie->raw_value eq "bar";</pre>
<p>
</p>
<h2><a name="bake">bake</a></h2>
<pre>
$cookie->bake()</pre>
<p>Adds a <em>Set-Cookie</em> header to the outgoing headers table.</p>
<p>
</p>
<h2><a name="bake2">bake2</a></h2>
<pre>
$cookie->bake2()</pre>
<p>Adds a <em>Set-Cookie2</em> header to the outgoing headers table.</p>
<p>
</p>
<h2><a name="domain">domain</a></h2>
<pre>
$cookie->domain()
$cookie->domain($set)</pre>
<p>Get or set the domain for the cookie:</p>
<pre>
$domain = $cookie->domain;
$cookie->domain(".cp.net");</pre>
<p>
</p>
<h2><a name="path">path</a></h2>
<pre>
$cookie->path()
$cookie->path($set)</pre>
<p>Get or set the path for the cookie:</p>
<pre>
$path = $cookie->path;
$cookie->path("/");</pre>
<p>
</p>
<h2><a name="version">version</a></h2>
<pre>
$cookie->version()
$cookie->version($set)</pre>
<p>Get or set the cookie version for this cookie.
Netscape spec cookies have version = 0;
RFC-compliant cookies have version = 1.</p>
<pre>
ok $cookie->version == 0;
$cookie->version(1);
ok $cookie->version == 1;</pre>
<p>
</p>
<h2><a name="expires">expires</a></h2>
<pre>
$cookie->expires()
$cookie->expires($set)</pre>
<p>Get or set the future expire time for the cookie. When
assigning, the new value ($set) should match /^\+?(\d+)([YMDhms]?)$/
$2 qualifies the number in $1 as representing ``Y''ears, ``M''onths,
``D''ays, ``h''ours, ``m''inutes, or ``s''econds (if the qualifier is
omitted, the number is interpreted as representing seconds).
As a special case, $set = ``now'' is equivalent to $set = ``0''.</p>
<pre>
my $expires = $cookie->expires;
$cookie->expires("+3h"); # cookie is set to expire in 3 hours</pre>
<p>
</p>
<h2><a name="secure">secure</a></h2>
<pre>
$cookie->secure()
$cookie->secure($set)</pre>
<p>Get or set the secure flag for the cookie:</p>
<pre>
$cookie->secure(1);
$is_secure = $cookie->secure;
$cookie->secure(0);</pre>
<p>
</p>
<h2><a name="comment">comment</a></h2>
<pre>
$cookie->comment()
$cookie->comment($set)</pre>
<p>Get or set the comment field of an RFC (Version > 0) cookie.</p>
<pre>
$cookie->comment("Never eat yellow snow");
print $cookie->comment;</pre>
<p>
</p>
<h2><a name="commenturl">commentURL</a></h2>
<pre>
$cookie->commentURL()
$cookie->commentURL($set)</pre>
<p>Get or set the commentURL field of an RFC (Version > 0) cookie.</p>
<pre>
$cookie->commentURL("<a href="http://localhost/cookie.policy"">http://localhost/cookie.policy"</a>;);
print $cookie->commentURL;</pre>
<p>
</p>
<h2><a name="fetch">fetch</a></h2>
<pre>
Apache::Cookie->fetch($r)</pre>
<p>Fetch and parse the incoming <em>Cookie</em> header:</p>
<pre>
my $cookies = Apache::Cookie->fetch($r); # Apache::Cookie::Table ref</pre>
<pre>
my %cookies = Apache::Cookie->fetch($r);</pre>
<p>
</p>
<hr />
<h1><a name="porting_from_1_x">PORTING from 1.X</a></h1>
<p>Changes to the 1.X API:</p>
<ul>
<li><strong><a name="item_as"><code>Apache::Cookie::fetch</code> now expects an <code>$r</code> object as (second)
argument, although this isn't necessary in mod_perl 2 if
<code>Apache::RequestUtil</code> is loaded.</a></strong><br />
</li>
<li><strong><a name="item_apache_3a_3acookie_3a_3aparse_is_gone_2e"><code>Apache::Cookie::parse</code> is gone.</a></strong><br />
</li>
<li><strong><a name="item_apache_3a_3acookie_3a_3anew_no_longer_encodes_the_"><code>Apache::Cookie::new</code> no longer encodes the supplied cookie name.</a></strong><br />
</li>
<li><strong><a name="item_name"><code>name()</code> and <code>value()</code> no longer accept a ``set'' argument. In other words,
neither a cookie's name, nor its value, may be modified. A new cookie
should be made instead.</a></strong><br />
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="group__apreq__xs__table.html">the Apache::Cookie::Table manpage</a>, <a href="group__apreq__xs__error.html">the Apache::Cookie::Error manpage</a>,
<a href="group__apreq__xs__error.html">the Apache::Cookie::Jar::Error manpage</a>, <a href="group__apreq__xs__request.html">the Apache::Request manpage</a>,
CGI::Cookie(3)</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<pre>
Copyright 2003-2004 The Apache Software Foundation</pre>
<pre>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at</pre>
<pre>
<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a></pre>
<pre>
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.</pre>
<div id="footer">
<p class="apache">
Copyright © 2003-2004 <a href="http://www.apache.org">The Apache Software Foundation</a>.<br/>
See <a href="LICENSE.html">LICENSE</a>.</p>
<p class="menu">
<span style="color:#aaa">page generated by <a href="http://www.doxygen.org/"><code>doxygen</code></a>
version 1.3.8 on 30 Aug 2004</span>
</p>
</div>
</body>
</html>
|