From 3662931fc2dd45e287f9709ad62fee191a4b2fdc Mon Sep 17 00:00:00 2001 From: Yorhel Date: Thu, 27 Feb 2025 14:00:28 +0100 Subject: [PATCH] FU: Add "denied" and "notfound" convenience methods --- FU.pm | 14 ++++++++++++-- FU/DebugImpl.pm | 8 ++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/FU.pm b/FU.pm index d5ce879..55d8454 100644 --- a/FU.pm +++ b/FU.pm @@ -352,7 +352,7 @@ sub _do_req($c) { fu->done; } } - fu->error(404); + fu->notfound; } 1; }; @@ -671,6 +671,8 @@ sub formdata { sub done { die bless [200,'Done',FU::_caller_info], 'FU::err' } sub error($,$code,$msg=$code) { die bless [$code,$msg,FU::_caller_info], 'FU::err' } +sub denied { fu->error(403) } +sub notfound { fu->error(404) } sub status($, $code) { $FU::REQ->{status} = $code } sub set_body($, $data) { @@ -1137,7 +1139,7 @@ the following is a valid approach to handle user authentication: }; FU::get '/registered-users-only', sub { - fu->error(403) if !fu->{user}; + fu->denied if !fu->{user}; }; In addition to the request information and response generation methods @@ -1266,6 +1268,14 @@ elsewhere, this ends up in running the appropriate C handler. C<$message> is optional and currently only used for logging. +=item fu->denied + +Alias for C<< fu->error(403) >>. + +=item fu->notfound + +Alias for C<< fu->error(404) >>. + =item fu->reset Reset the response to an empty state, basically undoing all effects of the diff --git a/FU/DebugImpl.pm b/FU/DebugImpl.pm index 1c1875e..dddc7dc 100644 --- a/FU/DebugImpl.pm +++ b/FU/DebugImpl.pm @@ -79,7 +79,7 @@ my @tabs = ( tr_ sub { td_ $_; td_ $r->{reshdr}{$_}; - } for keys $r->{reshdr}->%*; + } for sort keys $r->{reshdr}->%*; }; ('Response') }, @@ -308,7 +308,7 @@ sub listing_ { } sub load($id) { - open my $fn, '<', "$FU::debug_info->{storage}/fu-$id.txt" or fu->error(404); + open my $fn, '<', "$FU::debug_info->{storage}/fu-$id.txt" or fu->notfound; scalar <$fn>; local $/=undef; fu->set_body(scalar <$fn>); @@ -322,12 +322,12 @@ sub render { fu->set_body(framework_ collect); } elsif ($q eq 'last') { my $lst = listing; - fu->error(404) if !@$lst; + fu->notfound if !@$lst; load $lst->[$#$lst]; } elsif ($FU::debug_info->{storage} && $q =~ /^[0-9a-f]{22}$/) { load $q; } else { - fu->error(404); + fu->notfound } }