Mojo::Cookie - HTTP 1.1 Cookie Base Class


Mojolicious documentation Contained in the Mojolicious distribution.

Index


Code Index:

NAME

Top

Mojo::Cookie - HTTP 1.1 Cookie Base Class

SYNOPSIS

Top

  use Mojo::Base 'Mojo::Cookie';

DESCRIPTION

Top

Mojo::Cookie is an abstract base class for HTTP 1.1 cookies as described in RFC 2965.

ATTRIBUTES

Top

Mojo::Cookie implements the following attributes.

name

  my $name = $cookie->name;
  $cookie  = $cookie->name('foo');

Cookie name.

path

  my $path = $cookie->path;
  $cookie  = $cookie->path('/test');

Cookie path.

value

  my $value = $cookie->value;
  $cookie   = $cookie->value('/test');

Cookie value.

version

  my $version = $cookie->version;
  $cookie     = $cookie->version(1);

Cookie version.

METHODS

Top

Mojo::Cookie inherits all methods from Mojo::Base and implements the following new ones.

to_string

  my $string = $cookie->to_string;

Render cookie.

SEE ALSO

Top

Mojolicious, Mojolicious::Guides, http://mojolicio.us.


Mojolicious documentation Contained in the Mojolicious distribution.

package Mojo::Cookie;
use Mojo::Base -base;
use overload
  'bool'   => sub {1},
  '""'     => sub { shift->to_string },
  fallback => 1;

use Carp 'croak';
use Mojo::Util 'unquote';

has [qw/name path value version/];

# Regex
my $COOKIE_SEPARATOR_RE = qr/^\s*\,\s*/;
my $EXPIRES_RE          = qr/^([^\;\,]+\,?[^\;\,]+)\s*/;
my $NAME_RE             = qr/
    ^\s*
    ([^\=\;\,]+)   # Relaxed Netscape token, allowing whitespace
    \s*
    \=?            # '=' (optional)
    \s*
/x;
my $SEPARATOR_RE = qr/^\s*\;\s*/;
my $VALUE_RE     = qr/^([^\;\,]+)\s*/;

# "My Homer is not a communist.
#  He may be a liar, a pig, an idiot, a communist,
#  but he is not a porn star."
sub to_string { croak 'Method "to_string" not implemented by subclass' }

sub _tokenize {
  my ($self, $string) = @_;

  # Nibbling parser
  my (@tree, @token);
  while ($string) {

    # Name
    if ($string =~ s/$NAME_RE//o) {
      my $name = $1;
      my $value;

      # "expires" is a special case, thank you Netscape...
      if ($name =~ /expires/i && $string =~ s/$EXPIRES_RE//o) { $value = $1 }

      # Value
      elsif ($string =~ s/$VALUE_RE//o) { $value = $1 }

      # Token
      push @token, [$name, $value];

      # Separator
      $string =~ s/$SEPARATOR_RE//o;

      # Cookie separator
      if ($string =~ s/$COOKIE_SEPARATOR_RE//o) {
        push @tree, [@token];
        @token = ();
      }
    }

    # Bad format
    else {last}

  }

  # No separator
  push @tree, [@token] if @token;

  @tree;
}

1;
__END__