Mojo/Cookie/Response.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 34 / 34 Conditioncond 94.7% 18 / 19 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 98.2% 112 / 114 CC 26 SCAR 25.1
CC
26
Cov
99%
parse
14.0
to_string
13.0
BEGIN
1.0
CRAP 12.3
« Mojo/Server/Daemon.pm ↑ Summary Mojolicious/Command/routes.pm »
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 // ""
l!l
40160
MC/DC100%1/1
$str // ""
$str
Truth table100%2/2
A
A= $str
Aresult
11
00
16 200 ▶ while (my $pairs = shift @$tree) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pairs = shift @$tree) 57 200 257
17 57 my ($name, $value) = splice @$pairs, 0, 2;
18 57 ▶ push @cookies, $self->new(name => $name, value => $value // '');
Condition50%1/2
$value // ""
l!l
570
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
19  
20 57 ▶ while (my ($name, $value) = splice @$pairs, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$pairs, 0, 2)) 149 57 206
21 149 ▶ next unless $ATTRS{my $attr = lc $name};
Branch: 1 branch
BranchTrueFalseTotal
unless $ATTRS{my $attr = lc $name} 3 146 149
22 146 ▶ $value =~ s/^\.// if $attr eq 'domain' && defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "domain" and defined $value 36 110 146
Condition100%3/3
$attr eq "domain" and defined $value
!ll&&!rl&&r
109136
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
ABresult
0X0
100
111
23 146 ▶ $value = Mojo::Date->new($value // '')->epoch if $attr eq 'expires';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "expires" 27 119 146
Condition100%2/2
$value // ""
l!l
261
MC/DC100%1/1
$value // ""
$value
Truth table100%2/2
A
A= $value
Aresult
11
00
24 146 ▶ $value = 1 if $attr eq 'secure' || $attr eq 'httponly' || $attr eq 'partitioned';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "secure" or $attr eq "httponly" or $attr eq "partitioned" 20 126 146
Condition100%6/6
$attr eq "secure" or $attr eq "httponly" or $attr eq "partitioned"
l!l&&r!l&&!r
191126
$attr eq "secure" or $attr eq "httponly"
l!l&&r!l&&!r
181127
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"
ABCresult
1XX1
01X1
0011
0000
25 146 ▶ $cookies[-1]{$attr eq 'max-age' ? 'max_age' : $attr} = $value;
Branch: 1 branch
BranchTrueFalseTotal
$attr eq 'max-age' ? : 19 127 146
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
BranchTrueFalseTotal
unless length(my $name = $self->name // "") 1 41 42
Condition100%2/2
$self->name // ""
l!l
411
MC/DC100%1/1
$self->name // ""
$self->name
Truth table100%2/2
A
A= $self->name
Aresult
11
00
37 41 ▶ my $value = $self->value // '';
Condition100%2/2
$self->value // ""
l!l
401
MC/DC100%1/1
$self->value // ""
$self->value
Truth table100%2/2
A
A= $self->value
Aresult
11
00
38 41 ▶ my $cookie = join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /[,;" ]/u ? : 6 35 41
39  
40 # "expires"
41 41 my $expires = $self->expires;
42 41 ▶ $cookie .= '; expires=' . Mojo::Date->new($expires) if defined $expires;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expires 9 32 41
43  
44 # "domain"
45 41 ▶ if (my $domain = $self->domain) { $cookie .= "; domain=$domain" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $domain = $self->domain) 25 16 41
46  
47 # "path"
48 41 ▶ if (my $path = $self->path) { $cookie .= "; path=$path" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $path = $self->path) 25 16 41
49  
50 # "Partitioned"
51 41 ▶ $cookie .= "; Partitioned" if $self->partitioned;
Branch: 1 branch
BranchTrueFalseTotal
if $self->partitioned 1 40 41
52  
53 # "secure"
54 41 ▶ $cookie .= "; secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 7 34 41
55  
56 # "HttpOnly"
57 41 ▶ $cookie .= "; HttpOnly" if $self->httponly;
Branch: 1 branch
BranchTrueFalseTotal
if $self->httponly 2 39 41
58  
59 # "Same-Site"
60 41 ▶ if (my $samesite = $self->samesite) { $cookie .= "; SameSite=$samesite" }
Branch: 1 branch
BranchTrueFalseTotal
if (my $samesite = $self->samesite) 2 39 41
61  
62 # "Max-Age"
63 41 ▶ if (defined(my $max = $self->max_age)) { $cookie .= "; Max-Age=$max" }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $max = $self->max_age)) 10 31 41
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
« Mojo/Server/Daemon.pm ↑ Summary Mojolicious/Command/routes.pm »