Apache2::Cookie(3pm)

okay so you opened Apache2::Cookie(3pm), the User Contributed Perl Documentation page, and honestly? slay. it's the HTTP Cookies Class, and it also comes with its bestie Apache2::Cookie::Jar. let's get into it 🫢


✨ SYNOPSIS ✨

use Apache2::Cookie;

$j = Apache2::Cookie::Jar->new($r);
$c_in = $j->cookies("foo");         # get cookie from request headers

$c_out = Apache2::Cookie->new($r,
                              -name  => "mycookie",
                              -value => $c_in->name );

$c_out->path("/bar");               # set path to "/bar"
$c_out->bake($r);                   # send cookie in response headers

like… look at her go πŸ’…


🌸 DESCRIPTION 🌸

so Apache2::Cookie is built on the original 1.X versions, which were doing their little CGI::Cookie API cosplay. the current version of this module ships several packages and methods that are patterned after Apache2::Request, but it stays largely backwards-compatible with the original 1.X API. (heads up: there's a "PORTING from 1.X" section below with the known issues, don't skip it, iykyk.)

this manpage documents the Apache2::Cookie and Apache2::Cookie::Jar packages. both of them. together. iconic.


this class collects Apache2::Cookie objects into a lookup table. it does the same job for getting at your incoming cookies that Apache2::Request does for getting at your incoming params and file uploads. no cap.

✨ new ✨

Apache2::Cookie::Jar->new($env)

class method that retrieves the parsed cookie jar from the current environment. that's it, that's the whole thing πŸ’–

πŸ“ cookies πŸ“

$jar->cookies()
$jar->cookies($key)

retrieve cookies named $key from the jar object. in scalar context the first such cookie is returned, and in list context the full list of such cookies comes back at you.

if the $key argument is omitted, scalar $jar->cookies() will return an APR::Request::Cookie::Table object containing all the cookies in the jar. ⚠️ and listen: modifications to this object will affect the jar's internal cookies table in apreq_jar_t, so their impact will be noticed by all libapreq2 applications during this request. it's giving shared state, bestie.

in list context $jar->cookies() 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.

this method will throw an APR::Request::Error object into $@ if the returned value(s) could be unreliable. in particular, note that scalar $jar->cookies("foo") 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 cookies will croak if the parser failed to successfully parse the Cookie header. 😭

$c = Apache2::Cookie->new($r, name => "foo", value => 3);
$j->cookies->add($c);

$cookie = $j->cookies("foo");  # first foo cookie
@cookies = $j->cookies("foo"); # all foo cookies
@names = $j->cookies();        # all cookie names

πŸ’œ status πŸ’œ

$jar->status()

get the APR status code of the cookie parser: APR_SUCCESS on success, error otherwise. short king of a method fr fr.


✨ new ✨

Apache2::Cookie->new($env, %args)

just like CGI::Cookie::new, but it requires an additional environment argument. main character energy:

$cookie = Apache2::Cookie->new($r,
                         -name    =>  'foo',
                         -value   =>  'bar',
                         -expires =>  '+3M',
                         -domain  =>  '.capricorn.com',
                         -path    =>  '/cgi-bin/database',
                         -secure  =>  1
                        );

the -value argument may be either an arrayref, a hashref, or a string. Apache2::Cookie::freeze encodes this argument into the cookie's raw value.

🧊 freeze 🧊

Apache2::Cookie->freeze($value)

helper function (for new) that serializes a new cookie's value in a manner compatible with CGI::Cookie (and Apache2::Cookie 1.X). this class method accepts an arrayref, hashref, or normal perl string in $value.

$value = Apache2::Cookie->freeze(["2+2", "=4"]);

πŸ”₯ thaw πŸ”₯

Apache2::Cookie->thaw($value)
$cookie->thaw()

this is the helper method (for value) 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 Apache2::Cookie 1.X.

print $cookie->thaw;                    # prints "bar"
@values = Apache2::Cookie->thaw($value); # ( "2+2", "=4" )

πŸ¦‹ as_string πŸ¦‹

$cookie->as_string()

format the cookie object as a string. the quote-operator for Apache2::Cookie is overloaded to run this method whenever a cookie appears in quotes. so fancy.

ok "$cookie" eq $cookie->as_string;

🌈 name 🌈

$cookie->name()

get the name of the cookie. just the name. that's her whole job πŸ’…

πŸ’– value πŸ’–

$cookie->value()

get the (unswizzled) value of the cookie:

my $value = $cookie->value;
my @values = $cookie->value;
note: if the cookie's value was created using a freeze method, one way to reconstitute the object is by subclassing Apache2::Cookie with a package that provides the associated thaw sub:
{
    package My::COOKIE;
    @ISA = 'Apache2::Cookie';
    sub thaw { my $val = shift->raw_value; $val =~ tr/a-z/A-Z/; $val }
}

bless $cookie, "My::COOKIE";

ok $cookie->value eq "BAR";

πŸ‘‘ raw_value πŸ‘‘

$cookie->raw_value()

gets the raw (opaque) value string as it appears in the incoming Cookie header. no decoding, no thoughts, just vibes.

ok $cookie->raw_value eq "bar";

πŸͺ bake πŸͺ

$cookie->bake($r)

adds a Set-Cookie header to the outgoing headers table. go off sis πŸ”₯

πŸͺ bake2 πŸͺ

$cookie->bake2($r)

adds a Set-Cookie2 header to the outgoing headers table. the sequel. the encore.

🌟 domain 🌟

$cookie->domain()
$cookie->domain($set)

get or set the domain for the cookie:

$domain = $cookie->domain;
$cookie->domain(".cp.net");

πŸ›€οΈ path πŸ›€οΈ

$cookie->path()
$cookie->path($set)

get or set the path for the cookie:

$path = $cookie->path;
$cookie->path("/");

🏷️ version 🏷️

$cookie->version()
$cookie->version($set)

get or set the cookie version for this cookie. Netscape spec cookies have version = 0; RFC-compliant cookies have version = 1.

ok $cookie->version == 0;
$cookie->version(1);
ok $cookie->version == 1;

⏰ expires ⏰

$cookie->expires()
$cookie->expires($set)

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 Years, Months, Days, hours, minutes, or seconds (if the qualifier is omitted, the number is interpreted as representing seconds). as a special case, $set = "now" is equivalent to $set = "0".

my $expires = $cookie->expires;
$cookie->expires("+3h"); # cookie is set to expire in 3 hours

okay but the "now" = "0" thing is so real of her 😭

πŸ”’ secure πŸ”’

$cookie->secure()
$cookie->secure($set)

get or set the secure flag for the cookie:

$cookie->secure(1);
$is_secure = $cookie->secure;
$cookie->secure(0);

πŸ›‘οΈ httponly πŸ›‘οΈ

$cookie->httponly()
$cookie->httponly($set)

get or set the HttpOnly flag for the cookie:

$cookie->httponly(1);
$is_HttpOnly = $cookie->httponly;
$cookie->httponly(0);

πŸ’¬ comment πŸ’¬

$cookie->comment()
$cookie->comment($set)

get or set the comment field of an RFC (Version > 0) cookie.

$cookie->comment("Never eat yellow snow");
print $cookie->comment;

(and honestly? correct advice, never eat yellow snow πŸ’€)

πŸ’¬ commentURL πŸ’¬

$cookie->commentURL()
$cookie->commentURL($set)

get or set the commentURL field of an RFC (Version > 0) cookie.

$cookie->commentURL("http://localhost/cookie.policy");
print $cookie->commentURL;

🧚 fetch 🧚

Apache2::Cookie->fetch($r)

fetch and parse the incoming Cookie header:

my $cookies = Apache2::Cookie->fetch($r); # APR::Request::Cookie::Table ref

it should be noted, that with perl 5.8+ Iterator magic, table is able to handle multiple cookies of the same name.

my %cookies = Apache2::Cookie->fetch($r);

🚚 PORTING from 1.X 🚚

the changes to the 1.X API, bestie. read them, love them, adjust your code accordingly:

  • Apache2::Cookie::fetch now expects an $r object as (second) argument, although this isn't necessary in mod_perl 2 if Apache2::RequestUtil is loaded and 'PerlOptions +GlobalRequest' is in effect.
  • Apache2::Cookie::parse is gone. πŸͺ¦ rest in peace.
  • Apache2::Cookie::new no longer encodes the supplied cookie name.
  • Apache2::Cookie::new() returns undef if -value is not specified or -value => undef.
  • name() and value() 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. no takebacks, no edits, periodt.

πŸ‘€ SEE ALSO πŸ‘€

Apache2::Request, APR::Request::Cookie, APR::Request::Error, CGI::Cookie(3)

go read those too, the lore is deep πŸ’–


 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You 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

     http://www.apache.org/licenses/LICENSE-2.0

 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.

and that's the whole page, babe! this was Apache2::Cookie(3pm) from perl v5.42.3, dated 2026-08-21. go forth and set some cookies responsibly πŸͺβœ¨πŸ’…

πŸ“¦ where this page comes from