FU: Add fu->cookie()

This commit is contained in:
Yorhel 2025-03-17 16:18:12 +01:00
parent d8ecc71abb
commit c2e0f158ac

34
FU.pm
View file

@ -655,6 +655,22 @@ sub query {
_getfield $FU::REQ->{qs_parsed}, @_;
}
sub cookie {
shift;
return fu->header('cookie') if !@_;
$FU::REQ->{cookie} ||= do {
my %c;
for my $c (split /; /, fu->header('cookie')||'') {
my($n, $v) = split /=/, $c, 2;
if (!exists $c{$n}) { $c{$n} = $v }
elsif (ref $c{$n}) { push $c{$n}->@*, $v }
else { $c{$n} = [ $c{$n}, $v ] }
}
\%c
};
_getfield $FU::REQ->{cookie}, @_;
}
sub json {
shift;
fu->error(400, "Invalid content type for json") if (fu->header('content-type')||'') ne 'application/json';
@ -1301,6 +1317,22 @@ To fetch all query paramaters as decoded by C<query_decode()>, use:
my $data = fu->query({type=>'any'});
To fetch a query parameter that may have multiple values, use:
my $arrayref = fu->query(q => {accept_scalar => 1});
# OR:
my $first_value = fu->query(q => {accept_array => 'first'});
# OR:
my $last_value = fu->query(q => {accept_array => 'last'});
=item fu->cookie(...)
Like C<< fu->query() >> but parses the C<Cookie> request header. Beware that,
exactly like with query parameters, it's possible for a cookie to have multiple
values and thus get represented as an array.
=item fu->json(...)
Like C<< fu->query() >> but parses the request body as JSON. Returns the raw
@ -1323,8 +1355,6 @@ objects. Refer to L<FU::MultipartFormData> for more information.
=back
I<TODO:> Cookie parsing.
=head2 Generating Responses