FastAPI/Cookies.pm

filter: Statementstmt 100% 26 / 26 Branchbran 83.3% 5 / 6 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.0% 47 / 50 CC 7 SCAR 15.1
CC
7
Cov
95%
parse_cookies
7.0
BEGIN
1.0
BEGIN
1.0
CRAP 4.5
« FastAPI/Response/File.pm ↑ Summary FastAPI/Context.pm »
1 package PAGI::FastAPI::Cookies;
2  
3 2 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = qv('v1.7.3');
7 2 our $AUTHORITY = 'cpan:MANWAR';
8  
9 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our @EXPORT_OK = qw(parse_cookies cookie);
13  
14 # PAGI::FastAPI::Context already exposes the raw 'Cookie' request header via
15 # $c->header('Cookie'), this module is pure parsing on top of that, using no
16 # APIs beyond the documented $c->header() accessor.
17 #
18 # Note this is READ-ONLY (parsing the incoming Cookie header). Setting
19 # response cookies is a separate concern, PAGI::FastAPI::Context already
20 # has $c->set_header/$c->add_header, so:
21 #
22 # $c->add_header('set-cookie' => 'session=abc123; Path=/; HttpOnly');
23 #
24 # ...already works today with zero extra code; a set_cookie() sugar
25 # function could be added here later if the manual Set-Cookie string
26 # construction becomes tedious enough to be worth it.
27  
28 # Parses a raw "Cookie:" header value into a plain hashref of name => value.
29 # Values are percent-decoded (uri_unescape); malformed pairs (no '=') are
30 # skipped rather than causing a die, since cookie headers are client-
31 # supplied and not something a server should trust to be well-formed.
32 11 ▶sub parse_cookies ($raw_header) {
Subroutine parse_cookies: called CC=7 SCAR=19.5
Pod documented
33 11 my %cookies;
34 11 ▶ return \%cookies unless defined $raw_header && length $raw_header;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw_header and length $raw_header 3 8 11
Condition100%3/3
defined $raw_header and length $raw_header
!ll&&!rl&&r
218
MC/DC100%2/2
defined $raw_header and length $raw_header
defined $raw_header length $raw_header
Truth table100%3/3
A and B
A= defined $raw_headerB= length $raw_header
ABresult
0X0
100
111
35  
36 8 for my $pair (split /;\s*/, $raw_header) {
37 17 my ($name, $value) = split /=/, $pair, 2;
38 17 ▶ next unless defined $name && defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and defined $value 1 16 17
Condition66%2/3
defined $name and defined $value
!ll&&!rl&&r
0116
MC/DC50%1/2
defined $name and defined $value
defined $name defined $value
Truth table66%2/3
A and B
A= defined $nameB= defined $value
ABresult
0X0
100
111
39 16 $name =~ s/^\s+|\s+$//g;
40 16 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 16 16
41 16 $cookies{$name} = uri_unescape($value);
42 }
43  
44 8 return \%cookies;
45 }
46  
47 # Convenience: cookie($c, 'session') instead of
48 # parse_cookies($c->header('Cookie'))->{session}. Re-parses the header on
49 # every call (cookie headers are typically small, a handful of entries, so
50 # this isn't a meaningful cost), if you need many cookie values in one
51 # request, call parse_cookies($c->header('Cookie')) once yourself instead.
52 4 ▶sub cookie ($c, $name) {
Subroutine cookie: called CC=1 SCAR=0.0
Pod documented
53 4 my $cookies = parse_cookies($c->header('Cookie'));
54 4 return $cookies->{$name};
55 }
56  
57 =encoding utf-8
58  
59 =head1 NAME
60  
61 PAGI::FastAPI::Cookies - Request Cookie Parsing Helper for PAGI::FastAPI
62  
63 =head1 VERSION
64  
65 Version v1.7.3
66  
67 =head1 SYNOPSIS
68  
69 use PAGI::FastAPI::Cookies qw(cookie parse_cookies);
70  
71 $app->get('/whoami',
72 handler => async sub ($c) {
73 my $session_id = cookie($c, 'session_id');
74 return { session_id => $session_id // 'none' };
75 }
76 );
77  
78 # Or parse once and read several values:
79 $app->get('/prefs',
80 handler => async sub ($c) {
81 my $cookies = parse_cookies($c->header('Cookie'));
82 return {
83 theme => $cookies->{theme} // 'light',
84 lang => $cookies->{lang} // 'en',
85 };
86 }
87 );
88  
89 =head1 FUNCTIONS
90  
91 =head2 C<parse_cookies($raw_header)>
92  
93 Parses a raw C<Cookie:> header string into a plain HashRef of
94 C<name =E<gt> value>. Values are percent-decoded. Returns an empty HashRef
95 (never C<undef>) if C<$raw_header> is undefined or empty, so callers can
96 safely chain C<< ->{...} >> without a definedness check.
97  
98 =head2 C<cookie($c, $name)>
99  
100 Convenience wrapper: C<< parse_cookies($c->header('Cookie'))->{$name} >>.
101 Returns C<undef> if the cookie isn't present. Re-parses the header on every
102 call, fine for reading one or two cookies, but call C<parse_cookies()>
103 once yourself if you need several values from the same request.
104  
105 =head1 SETTING RESPONSE COOKIES
106  
107 Not this module's concern, L<PAGI::FastAPI::Context> already supports it
108 directly, with zero extra code needed:
109  
110 $c->add_header('set-cookie' => 'session=abc123; Path=/; HttpOnly; SameSite=Lax');
111  
112 C<add_header> (rather than C<set_header>) is important if you need to set
113 more than one cookie in the same response, for the same reason
114 L<PAGI::FastAPI::Middleware::RateLimit> uses it for stacked
115 C<x-ratelimit-*> headers, C<set_header> would silently overwrite an
116 earlier C<Set-Cookie> value instead of adding a second one.
117  
118 =head1 SEE ALSO
119  
120 L<PAGI::FastAPI::Context>
121  
122 =head1 AUTHOR
123  
124 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
125  
126 =head1 REPOSITORY
127  
128 L<https://github.com/manwar/PAGI-FastAPI>
129  
130 =head1 BUGS
131  
132 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/PAGI-FastAPI/issues>.
133 I will be notified and then you'll automatically be notified of progress on your
134 bug as I make changes.
135  
136 =head1 SUPPORT
137  
138 You can find documentation for this module with the perldoc command.
139  
140 perldoc PAGI::FastAPI::Cookies
141  
142 You can also look for information at:
143  
144 =over 4
145  
146 =item * BUG Report
147  
148 L<https://github.com/manwar/PAGI-FastAPI/issues>
149  
150 =item * Search MetaCPAN
151  
152 L<https://metacpan.org/dist/PAGI-FastAPI/>
153  
154 =back
155  
156 =head1 LICENSE AND COPYRIGHT
157  
158 Copyright (C) 2026 Mohammad Sajid Anwar.
159  
160 This program is free software; you can redistribute it and/or modify it under
161 the terms of the Artistic License (2.0).
162  
163 =cut
164  
165 2 1; # End of PAGI::FastAPI::Cookies
« FastAPI/Response/File.pm ↑ Summary FastAPI/Context.pm »