Browse Source

Add some CGI::Lite bits

master
Lucas 10 months ago
parent
commit
35d6457478
  1. 60
      Pastry.pl

60
Pastry.pl

@ -20,20 +20,48 @@ use warnings;
use FCGI;
use FCGI::ProcManager;
use IO::Handle;
use File::Temp qw(tempfile);
use File::Spec;
use CGI::Lite;
sub handle_request ($)
my $UPLOAD_DIR = "./u";
my $DOMAIN = "example.com";
sub random_filename ($)
{
my $file = shift;
my $suffix;
$file =~ m/.*\.(.*)/ and $suffix = $1;
# We're interested in the filename and the file itself being created.
# We don't care about the file handle.
my (undef, $tfile) = tempfile(
TEMPLATE => "XXXXXXXX",
SUFFIX => "." . ($suffix || "bin"),
DIR => $UPLOAD_DIR,
UNLINK => 0,
);
return $tfile;
}
sub handle_request ($$)
{
my $req = shift;
my ($req, $cgi) = @_;
my %env = %{$req->GetEnvironment()};
my ($inh, $outh, $errh) = $req->GetHandles();
my $params = do {
local %ENV = %env;
local *STDIN = *$inh;
$cgi->parse_new_form_data();
};
print $outh "Content-Type: text/plain\n\n";
foreach my $k (sort keys %env) {
print $outh "$k=$env{$k}\n";
foreach my $k (sort keys %$params) {
print $outh "$k=$params->{$k}\n";
}
$req->Finish();
}
sub main ()
@ -50,11 +78,29 @@ sub main ()
my $req = FCGI::Request(IO::Handle->new(), IO::Handle->new(),
IO::Handle->new(), \%env, $sock, FCGI::FAIL_ACCEPT_ON_INTR);
my $cgi = CGI::Lite->new();
# 10 MB max upload
#$cgi->set_size_limit(10 * 1024 * 1024);
# Actually, let the HTTP server handle this.
# handle filenames with File::Temp
$cgi->add_timestamp(0);
$cgi->filter_filename(\&random_filename);
# temporary filename already includes directory component
$cgi->set_directory(".") or die "Can't set directory";
# Don't manipulate any uploaded file
$cgi->remove_mime_type($_) for $cgi->get_mime_types();
while ($req->Accept() >= 0) {
$pm->pm_pre_dispatch();
handle_request($req);
handle_request($req, $cgi);
$req->Finish();
$pm->pm_post_dispatch();
}
FCGI::CloseSocket($sock);
}
main();