| 1 | package Mojo::Cookie::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 28 | ▶ | use Mojo::Base 'Mojo::Cookie'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use Mojo::Date; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use Mojo::Util qw(quote split_cookie_header); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 28 | has [qw(domain expires host_only httponly max_age partitioned path samesite secure)]; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 28 | my %ATTRS = map { $_ => 1 } qw(domain expires httponly max-age partitioned path samesite secure); | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 200 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 200 | my ($self, $str) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 200 | my @cookies; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 200 | ▶ | my $tree = split_cookie_header $str // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$str // ""
MC/DC100%1/1
$str // ""
$str
Truth table100%2/2
A
A= $str
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 200 | ▶ | while (my $pairs = shift @$tree) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 57 | my ($name, $value) = splice @$pairs, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 57 | ▶ | push @cookies, $self->new(name => $name, value => $value // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$value // ""
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 57 | ▶ | while (my ($name, $value) = splice @$pairs, 0, 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 149 | ▶ | next unless $ATTRS{my $attr = lc $name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 146 | ▶ | $value =~ s/^\.// if $attr eq 'domain' && defined $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$attr eq "domain" and defined $value
MC/DC100%2/2
$attr eq "domain" and defined $value
$attr eq "domain"
defined $value
Truth table100%3/3
A and B
A= $attr eq "domain"B= defined $value
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 146 | ▶ | $value = Mojo::Date->new($value // '')->epoch if $attr eq 'expires'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$value // ""
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 146 | ▶ | $value = 1 if $attr eq 'secure' || $attr eq 'httponly' || $attr eq 'partitioned'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$attr eq "secure" or $attr eq "httponly" or $attr eq "partitioned"
$attr eq "secure" or $attr eq "httponly"
MC/DC100%3/3
$attr eq "secure" or $attr eq "httponly" or $attr eq "partitioned"
$attr eq "secure"
$attr eq "httponly"
$attr eq "partitioned"
Truth table100%4/4
A or B or C
A= $attr eq "secure"B= $attr eq "httponly"C= $attr eq "partitioned"
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 146 | ▶ | $cookies[-1]{$attr eq 'max-age' ? 'max_age' : $attr} = $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 200 | return \@cookies; | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 42 | ▶ | sub to_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 42 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # Name and value | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 42 | ▶ | return '' unless length(my $name = $self->name // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->name // ""
MC/DC100%1/1
$self->name // ""
$self->name
Truth table100%2/2
A
A= $self->name
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 41 | ▶ | my $value = $self->value // ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->value // ""
MC/DC100%1/1
$self->value // ""
$self->value
Truth table100%2/2
A
A= $self->value
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 41 | ▶ | my $cookie = join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # "expires" | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 41 | my $expires = $self->expires; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 41 | ▶ | $cookie .= '; expires=' . Mojo::Date->new($expires) if defined $expires; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | # "domain" | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 41 | ▶ | if (my $domain = $self->domain) { $cookie .= "; domain=$domain" } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # "path" | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 41 | ▶ | if (my $path = $self->path) { $cookie .= "; path=$path" } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # "Partitioned" | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 41 | ▶ | $cookie .= "; Partitioned" if $self->partitioned; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # "secure" | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 41 | ▶ | $cookie .= "; secure" if $self->secure; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | # "HttpOnly" | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 41 | ▶ | $cookie .= "; HttpOnly" if $self->httponly; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | # "Same-Site" | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 41 | ▶ | if (my $samesite = $self->samesite) { $cookie .= "; SameSite=$samesite" } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # "Max-Age" | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 41 | ▶ | if (defined(my $max = $self->max_age)) { $cookie .= "; Max-Age=$max" } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 41 | return $cookie; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 28 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | Mojo::Cookie::Response - HTTP response cookie | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | use Mojo::Cookie::Response; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | my $cookie = Mojo::Cookie::Response->new; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | $cookie->name('foo'); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $cookie->value('bar'); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | say "$cookie"; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | L<Mojo::Cookie::Response> is a container for HTTP response cookies, based on L<RFC | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 6265|https://tools.ietf.org/html/rfc6265>. | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | L<Mojo::Cookie::Response> inherits all attributes from L<Mojo::Cookie> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head2 domain | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | my $domain = $cookie->domain; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | $cookie = $cookie->domain('localhost'); | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | Cookie domain. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head2 expires | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | my $expires = $cookie->expires; | ||||||||||||||||||||||||||||||||||||||||||
| 104 | $cookie = $cookie->expires(time + 60); | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | Expiration for cookie. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head2 host_only | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | my $bool = $cookie->host_only; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | $cookie = $cookie->host_only($bool); | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | Host-only flag, indicating that the canonicalized request-host is identical to the cookie's L</"domain">. | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | =head2 httponly | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | my $bool = $cookie->httponly; | ||||||||||||||||||||||||||||||||||||||||||
| 118 | $cookie = $cookie->httponly($bool); | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | HttpOnly flag, which can prevent client-side scripts from accessing this cookie. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =head2 max_age | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | my $max_age = $cookie->max_age; | ||||||||||||||||||||||||||||||||||||||||||
| 125 | $cookie = $cookie->max_age(60); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Max age for cookie. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =head2 partitioned | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | my $partitioned = $cookie->partitioned; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | $cookie = $cookie->partitioned(1); | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | Partitioned flag, this is to be used in accordance to the CHIPS amendment to RFC 6265. | ||||||||||||||||||||||||||||||||||||||||||
| 135 | Note that this attribute is B<EXPERIMENTAL> because even though most commonly used browsers support the | ||||||||||||||||||||||||||||||||||||||||||
| 136 | feature, there is no specification yet besides L<this | ||||||||||||||||||||||||||||||||||||||||||
| 137 | draft|https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-00.html>. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head2 path | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | my $path = $cookie->path; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | $cookie = $cookie->path('/test'); | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | Cookie path. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =head2 samesite | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | my $samesite = $cookie->samesite; | ||||||||||||||||||||||||||||||||||||||||||
| 149 | $cookie = $cookie->samesite('Lax'); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | SameSite value. | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =head2 secure | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | my $bool = $cookie->secure; | ||||||||||||||||||||||||||||||||||||||||||
| 156 | $cookie = $cookie->secure($bool); | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | Secure flag, which instructs browsers to only send this cookie over HTTPS connections. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | L<Mojo::Cookie::Response> inherits all methods from L<Mojo::Cookie> and implements the following new ones. | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | =head2 parse | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | my $cookies = Mojo::Cookie::Response->parse('f=b; path=/'); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | Parse cookies. | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | =head2 to_string | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | my $str = $cookie->to_string; | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | Render cookie. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =cut | ||||||||||||||||||||||||||||||||||||||||||