Name

SPVM::Mojo::UserAgent::CookieJar - Cookie jar for HTTP user agents

Description

Mojo::UserAgent::CookieJar class in SPVM is a minimalistic and relaxed cookie jar used by Mojo::UserAgent, based on RFC 6265.

Usage

use Mojo::UserAgent::CookieJar;

# Add response cookies
my $jar = Mojo::UserAgent::CookieJar->new;
$jar->add(
  Mojo::Cookie::Response->new(
    name   => "foo",
    value  => "bar",
    domain => "localhost",
    path   => "/test"
  )
);

# Find request cookies
for my $cookie (@{$jar->find(Mojo::URL->new("http://localhost/test"))}) {
  say $cookie->name;
  say $cookie->value;
}

Interfaces

Fields

ignore

has ignore : rw Mojo::UserAgent::CookieJar::Callback::Ignore;

A callback used to decide if a cookie should be ignored by "collect".

Callback:

method : int ($cookie : Mojo::Cookie::Response);

Examples:

# Ignore all cookies
$jar->ignore(method : int ($cookie : Mojo::Cookie::Response) {
  return 1;
});

# Ignore cookies for domains "com", "net" and "org"
$jar->ignore(method : int ($cookie : Mojo::Cookie::Response) {
  
  unless (my $domain = $cookie->domain) {
    return 0;
  }
  
  return $domain eq "com" || $domain eq "net" || $domain eq "org";
});

has max_cookie_size : rw int;

Maximum cookie size in bytes, defaults to 4096 (4KiB).

Class Methods

new

static method new : Mojo::UserAgent::CookieJar ();

Create a new Mojo::UserAgent::CookieJar object and return it.

Instance Methods

add

method add : void ($cookie : object of Mojo::Cookie::Response|Mojo::Cookie::Response[]);

Add multiple Mojo::Cookie::Response objects to the jar.

all

method all : Mojo::Cookie::Response[] ();

Return all Mojo::Cookie::Response objects that are currently stored in the jar.

Examples:

# Names of all cookies
for my $_ (@{$jar->all}) {
  say $_->name;
}

collect

method collect : void ($tx : Mojo::Transaction::HTTP);

Collect response cookies from transaction.

empty

method empty : void ();

Empty the jar.

find

method find : Mojo::Cookie::Request[] ($url : Mojo::URL);

Find Mojo::Cookie::Request objects in the jar for Mojo::URL object.

Examples:

# Names of all cookies found
for my $_ (@{$jar->find(Mojo::URL->new("http://example.com/foo"))}) {
  say $_->name;
}

load

Not implemented.

prepare

method prepare : void ($tx : Mojo::Transaction::HTTP);

Prepare request cookies for transaction.

to_string

method to_string : string ();

save

Not implemented.

See Also

Copyright & License

Copyright (c) 2025 Yuki Kimoto

MIT License