diff --git a/FU.pm b/FU.pm index 23aac6c..4d7a28b 100644 --- a/FU.pm +++ b/FU.pm @@ -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, 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 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 for more information. =back -I Cookie parsing. - =head2 Generating Responses