blib/lib/PAGI/Middleware/Cookie.pm

filter: Statementstmt 91.0% 71 / 78 Branchbran 54.5% 12 / 22 Conditioncond 62.5% 10 / 16 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 94.4% 17 / 18 Podpod 100% 1 / 1 totaltotal 79.8% 115 / 144 CC 21 SCAR 15.9
CC
21
Cov
80%
delete
12.0
_format_set_cookie
9.8
__ANON__
5.0
CRAP 4.9
« blib/lib/PAGI/Middleware/Session.pm ↑ Summary lib/PAGI/Test/WebSocket.pm »
1 package PAGI::Middleware::Cookie;
2 1 $PAGI::Middleware::Cookie::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Cookie::Baker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PAGI::Middleware::Cookie - Cookie parsing middleware
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Middleware::Builder;
16  
17 my $app = builder {
18 enable 'Cookie';
19 $my_app;
20 };
21  
22 # In your app:
23 async sub app {
24 my ($scope, $receive, $send) = @_;
25  
26 my $cookies = $scope->{'pagi.cookies'};
27 my $session_id = $cookies->{session_id};
28 }
29  
30 =head1 DESCRIPTION
31  
32 PAGI::Middleware::Cookie parses the Cookie header and makes the parsed
33 cookies available in C<< $scope->{'pagi.cookies'} >> as a hashref.
34  
35 It also provides a helper for setting response cookies.
36  
37 =head1 CONFIGURATION
38  
39 =over 4
40  
41 =item * secret (optional)
42  
43 Secret key for signed cookies. Required for C<get_signed>/C<set_signed>.
44  
45 =back
46  
47 =cut
48  
49 2 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
50 2 my ($self, $config) = @_;
51  
52 2 $self->{secret} = $config->{secret};
53 }
54  
55 2 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
56 2 my ($self, $app) = @_;
57  
58 2 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
59 2 my ($scope, $receive, $send) = @_;
60 2 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 2 2
61 0 await $app->($scope, $receive, $send);
62 0 return;
63 }
64  
65 # Parse cookies from Cookie header
66 2 ▶ my $cookie_header = $self->_get_header($scope, 'cookie') // '';
Condition100%2/2
$self->_get_header($scope, "cookie") // ""
l!l
11
MC/DC100%1/1
$self->_get_header($scope, "cookie") // ""
$self->_get_header($scope, "cookie")
Truth table100%2/2
A
A= $self->_get_header($scope, "cookie")
Aresult
11
00
67 2 my $cookies = $self->_parse_cookies($cookie_header);
68  
69 # Create cookie jar for setting response cookies
70 2 my @response_cookies;
71 my $cookie_jar = PAGI::Middleware::Cookie::Jar->new(
72 \@response_cookies,
73 1 ▶ sub { $self->_format_set_cookie(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 2 );
75  
76 # Add cookies and jar to scope
77 2 my $new_scope = $self->modify_scope($scope, {
78 'pagi.cookies' => $cookies,
79 'pagi.cookie_jar' => $cookie_jar,
80 });
81  
82 # Wrap send to add Set-Cookie headers
83 4 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
84 4 my ($event) = @_;
85 4 ▶ if ($event->{type} eq 'http.response.start' && @response_cookies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{'type'} eq 'http.response.start' and @response_cookies) { } 1 3 4
Condition100%3/3
$event->{"type"} eq "http.response.start" and @response_cookies
!ll&&!rl&&r
211
MC/DC100%2/2
$event->{"type"} eq "http.response.start" and @response_cookies
$event->{"type"} eq "http.response.start" @response_cookies
Truth table100%3/3
A and B
A= $event->{"type"} eq "http.response.start"B= @response_cookies
ABresult
0X0
100
111
86 1 ▶ my @headers = @{$event->{headers} // []};
Condition50%1/2
$event->{"headers"} // []
l!l
10
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
Aresult
11
00
87 1 for my $cookie (@response_cookies) {
88 1 push @headers, ['Set-Cookie', $cookie];
89 }
90 await $send->({
91 %$event,
92 headers => \@headers,
93 1 });
94 } else {
95 3 await $send->($event);
96 }
97 2 };
98  
99 2 await $app->($new_scope, $receive, $wrapped_send);
100 2 };
101 }
102  
103 2 ▶sub _parse_cookies {
Subroutine _parse_cookies: called CC=3 SCAR=11.0
104 2 my ($self, $header) = @_;
105  
106 2 ▶ return {} unless defined $header && length $header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header and length $header 1 1 2
Condition66%2/3
defined $header and length $header
!ll&&!rl&&r
011
MC/DC50%1/2
defined $header and length $header
defined $header length $header
Truth table66%2/3
A and B
A= defined $headerB= length $header
ABresult
0X0
100
111
107 1 return Cookie::Baker::crush_cookie($header);
108 }
109  
110 1 ▶sub _format_set_cookie {
Subroutine _format_set_cookie: called CC=8 SCAR=22.8
111 1 my ($self, $name, $value, %opts) = @_;
112  
113 my %cookie_opts = (
114 value => $value,
115 1 ▶ path => $opts{path} // '/',
Condition50%1/2
$opts{"path"} // "/"
l!l
01
MC/DC0%0/1
$opts{"path"} // "/"
$opts{"path"}
Truth table50%1/2
A
A= $opts{"path"}
Aresult
11
00
116 );
117 1 ▶ $cookie_opts{domain} = $opts{domain} if defined $opts{domain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"domain"} 0 1 1
118 1 ▶ $cookie_opts{expires} = $opts{expires} if defined $opts{expires};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"expires"} 0 1 1
119 1 ▶ $cookie_opts{'max-age'} = $opts{max_age} if defined $opts{max_age};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"max_age"} 0 1 1
120 1 ▶ $cookie_opts{secure} = $opts{secure} if $opts{secure};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"secure"} 1 0 1
121 1 ▶ $cookie_opts{httponly} = $opts{httponly} if $opts{httponly};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"httponly"} 1 0 1
122 1 ▶ $cookie_opts{samesite} = $opts{samesite} if defined $opts{samesite};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"samesite"} 0 1 1
123 1 return Cookie::Baker::bake_cookie($name, \%cookie_opts);
124 }
125  
126 2 ▶sub _get_header {
Subroutine _get_header: called CC=4 SCAR=14.2
127 2 my ($self, $scope, $name) = @_;
128  
129 2 $name = lc($name);
130 2 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
20
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
131 1 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 1 0 1
132 }
133 1 return;
134 }
135  
136 # Simple cookie jar class for method-style access
137 package PAGI::Middleware::Cookie::Jar;
138 1 $PAGI::Middleware::Cookie::Jar::VERSION = '0.002002';
139 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
143 2 my ($class, $cookies_ref, $formatter) = @_;
144  
145 2 return bless {
146 cookies => $cookies_ref,
147 formatter => $formatter,
148 }, $class;
149 }
150  
151 1 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
152 1 my ($self, $name, $value, %opts) = @_;
153  
154 1 push @{$self->{cookies}}, $self->{formatter}->($name, $value, %opts);
155 }
156  
157 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
158 0 my ($self, $name, %opts) = @_;
159  
160 my %cookie_opts = (
161 value => '',
162 expires => 0, # Epoch 0 = expired
163 0 ▶ path => $opts{path} // '/',
Condition0%0/2
$opts{"path"} // "/"
l!l
00
MC/DC0%0/1
$opts{"path"} // "/"
$opts{"path"}
Truth table0%0/2
A
A= $opts{"path"}
Aresult
11
00
164 );
165 0 ▶ $cookie_opts{domain} = $opts{domain} if defined $opts{domain};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"domain"} 0 0 0
166 0 push @{$self->{cookies}}, Cookie::Baker::bake_cookie($name, \%cookie_opts);
167 }
168  
169 package PAGI::Middleware::Cookie;
170  
171 1 1;
172  
173 __END__
« blib/lib/PAGI/Middleware/Session.pm ↑ Summary lib/PAGI/Test/WebSocket.pm »