Add fu->redirect, change $st->row behavior on 0 results, minor fixes

And with this, I have a working rewrite of the manned.org backend into
FU. \o/

The $st->row methods are very useful even for queries that may not
return anything, so their old behavior was unhelpful. Interestingly
enough, the error-on-multiple-rows did catch an actual bug in
Manned.org, so I'm keeping that behavior.
This commit is contained in:
Yorhel 2025-02-24 15:54:32 +01:00
parent fbbaa23842
commit 06e2f950fe
9 changed files with 62 additions and 34 deletions

32
FU.pm
View file

@ -26,7 +26,7 @@ sub fu() { $fu }
FU::Log::capture_warn(1);
FU::Log::set_fmt(sub($msg) {
FU::Log::default_fmt($msg,
fu->path && fu->method ? fu->method.' '.fu->path.(fu->query?'?'.fu->query:'') : '[init]',
fu->path && fu->method ? fu->method.' '.fu->path.(fu->query?'?'.fu->query:'') : '[global]',
);
});
@ -292,10 +292,12 @@ sub _log_err($e) {
return if !debug && ref $@ eq 'FU::err' && $@->[0] != 500;
if (!$REQ->{full_err} && (ref $@ ne 'FU::err' || $@->[0] == 500)) {
$REQ->{full_err}++;
$e =~ s/^\s+//;
$e =~ s/\s+$//;
log_write join "\n",
'IP: '.($REQ->{ip}||'-'),
'Headers:', (map " $_: $REQ->{hdr}{$_}", sort keys $REQ->{hdr}->%*),
'ERROR:', $e =~ s/(^|\n)/\n /rg;
'ERROR:'.($e =~ s/(^|\n)/\n /rg);
# TODO: decoded body, if we have that.
} else {
log_write $e;
@ -667,6 +669,7 @@ sub send_file($, $root, $path) {
return if $path =~ /\.\./;
my $fn = "$root/$path";
return if !-f $fn;
my $m = (stat $fn)[9];
return if !defined $m;
@ -690,6 +693,16 @@ sub send_file($, $root, $path) {
fu->done;
}
sub redirect($, $code, $location) {
state $alias = {qw/ perm 301 temp 302 tempget 303 tempsame 307 permsame 308 /};
fu->reset;
fu->status($alias->{$code} // $code);
fu->set_header(location => "$location");
fu->set_header('content-type', 'text/plain');
fu->set_body("Redirecting to $location\n");
fu->done;
}
sub _error_page($, $code, $title, $msg) {
fu->reset;
fu->status($code);
@ -1236,14 +1249,25 @@ C<application/octet-stream>.
This method sets an appropriate C<last-modified> header and supports
conditional requests with C<if-modified-since>.
=item fu->redirect($code, $location)
Generates a HTTP redirect response and calls C<< fu->done >>. C<$code> can be
one of the following status codes or an alias:
Status Alias Semantics
----------------------------------------
301 perm Permanent, method may or may not change to GET
302 temp Temporary, method may or may not change to GET
303 tempget Temporary to GET
307 tempsame Temporary without changing method
308 permsame Permanent without changing method
=back
I<TODO:> Setting cookies.
I<TODO:> JSON output.
I<TODO:> Redirection responses.
=head2 Running the Site