App-Phoebe
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/App/Phoebe.pm view on Meta::CPAN
}
} elsif (@{$server->{wiki_space}}) {
@spaces = @{$server->{wiki_space}};
}
return join("|", map { quotemeta } @spaces);
}
# A regular expression matching parts of reserved paths in URLs. When looking at
# gemini://localhost:1965/page/test or gemini://localhost:1965/do/index and
# using a client that has an "up" command, you'd end up at
# gemini://localhost:1965/page â but what should happen in this case? We should
# redirect these requests to gemini://localhost:1965/, I think.
sub reserved_regex {
return join("|", qw(do page raw file html history diff));
}
sub success {
my $stream = shift;
my $type = shift || 'text/gemini; charset=UTF-8';
my $lang = shift;
if ($lang) {
result($stream, "20", "$type; lang=$lang");
} else {
result($stream, "20", "$type");
}
}
sub result {
my $stream = shift;
my $code = shift;
my $meta = shift;
my $data = shift||"";
$stream->write("$code $meta\r\n$data");
}
sub handle_titan {
my $stream = shift;
my $data = shift;
# extra processing of the request if we didn't do that, yet
$data->{upload} ||= is_upload($stream, $data->{request}) or return;
my $size = $data->{upload}->{params}->{size};
my $actual = length($data->{buffer});
if ($actual == $size) {
$log->debug("Handle Titan request");
process_titan($stream, $data->{request}, $data->{upload}, $data->{buffer}, $size);
# do not close in case we're waiting for the lock
return;
} elsif ($actual > $size) {
$log->debug("Received more than the promised $size bytes");
result($stream, "59", "Received more than the promised $size bytes");
$stream->close_gracefully();
return;
}
$log->debug("Waiting for " . ($size - $actual) . " more bytes");
}
sub process_titan {
my ($stream, $request, $upload, $buffer, $size) = @_;
eval {
local $SIG{'ALRM'} = sub { $log->error("Timeout processing upload $request") };
alarm(10); # timeout
if (run_extensions($stream, $request, $upload, $buffer, $size)) {
# config file goes first
} else {
save_page($stream, $upload->{host}, $upload->{space}, $upload->{id},
$upload->{params}->{mime}, $buffer, $size);
}
alarm(0);
};
# save page might still be waiting for the lock so we must not close the
# stream: save_page will close the stream
return unless $@;
$log->error("Error: $@");
$stream->close_gracefully();
}
sub save_page {
my $stream = shift;
my $host = shift;
my $space = shift;
my $id = shift;
my $type = shift || "text/plain";
my $data = shift;
my $length = shift;
# If the operation succeeds, we can close the stream; if the operation fails,
# we can close the stream; but if the operation was rescheduled, we must not
# close the stream!
if ($type ne "text/plain" and $type ne "text/gemini") {
if ($length == 0) {
with_lock($stream, $host, $space,
sub {
delete_file($stream, $host, $space, $id);
$stream->close_gracefully();
});
} else {
with_lock($stream, $host, $space,
sub {
write_file($stream, $host, $space, $id, $data, $type);
$stream->close_gracefully();
});
}
} elsif ($length == 0) {
with_lock($stream, $host, $space,
sub {
delete_page($stream, $host, $space, $id);
$stream->close_gracefully();
});
} elsif (utf8::decode($data)) { # decodes in-place and returns success
with_lock($stream, $host, $space,
sub {
write_page($stream, $host, $space, $id, $data);
$stream->close_gracefully();
});
} else {
$log->debug("The text is invalid UTF-8");
result($stream, "59", "The text is invalid UTF-8");
$stream->close_gracefully();
}
}
view all matches for this distributionview release on metacpan - search on metacpan
( run in 0.527 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )