App-zen
view release on metacpan or search on metacpan
} else {
$ret = $section;
}
}
err("No section found for `$s'") unless ($ignore || $ret);
return $ret;
}
sub load_zen_file
{
my $filename = shift;
my $st = stat($filename);
my $mtime = $st->mtime;
my $filesize = $st->size;
open(my $fh, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
flock($fh, LOCK_EX) or die "Could not lock file '$filename' $!";
my $ln = 0;
my @lines = ();
} else {
print "HTTP/1.0 400 Bad Request\r\n\r\nUnsupported Request\n";
}
}
# Should return the data since last fetch offset.
sub on_append_file {
my ($cgi, $path) = @_;
my $data = $cgi->param('POSTDATA');
append_to_file($path, $data);
my @stat_info = stat($path);
if (@stat_info) {
# Extract the last modification time
my $mtime = $stat_info[9];
print "HTTP/1.0 200 OK\r\n";
print "Last-Modified: $mtime\r\n";
print "Content-Length: 0\r\n\r\n";
} else {
print "HTTP/1.0 500 Server error\r\n";
}
}
sub on_put_file {
my ($cgi, $path) = @_;
my $l = $cgi->url_param('l') || 0;
my $n = $cgi->url_param('n') || 0;
my $last_mtime = $cgi->url_param('last_modified');
if (-f $path) {
# override require last_mtime
my $stat = stat($path);
my $mtime = $stat->mtime;
if (!$last_mtime) {
write_error(409, "Conflict", "Missing query parameter 'last_modified' l=$l");
print Dumper $cgi;
return;
}
if ($last_mtime != $mtime) {
write_error(409, "Conflict", "last modified doesn't match: $mtime");
return;
}
$n--;
}
}
}
close $fh;
write_file($path, $x.$data.$y);
} else {
print "HTTP/1.0 400 Bad request\r\n";
return;
}
my $mtime = stat($path)->mtime;
if ($mtime) {
print "HTTP/1.0 200 OK\r\n";
print "Last-Modified: $mtime\r\n";
print "Content-Length: 0\r\n\r\n";
} else {
print "HTTP/1.0 500 Server error\r\n";
}
}
sub on_get_file {
} else {
print "Content-Type: text/plain\r\n\r\n";
open FH, "$zenCommand --section \"$section\" $path|";
}
}
while (<FH>) {
print $_;
}
close FH;
} elsif ($raw eq '1' && $l > 0) {
my $mtime = stat($path)->mtime;
print "HTTP/1.0 200 OK\r\n";
print "Last-Modified: $mtime\r\n";
print "Content-Type: text/plain\r\n\r\n";
open my $fh, '<', $path or die "Can not open file: $!";
while (<$fh>) {
if ($l == 1) {
last if $n == 0;
print $_;
$n--;
} else {
$l--;
}
}
close $fh;
} else {
my ($ext) = $path =~ /\.([^.]+)$/;
my $mime_type = $mimeTypes{lc($ext)} || 'application/octet-stream';
open my $fh, '<:raw', $path or die "Cannot open file: $!";
my $mtime = stat($path)->mtime;
my $filesize = -s $path;
print "HTTP/1.1 200 OK\r\n";
print "Content-Type: $mime_type\r\n";
print "Last-Modified: $mtime\r\n";
print "Content-Length: $filesize\r\n";
print "\r\n";
# Print the binary content of the file to the CGI output
binmode STDOUT;
while (read $fh, my $buffer, 4096) {
write_error(404, "Not found");
return;
}
my @contents = ();
opendir(my $dh, $path) or die "Cannot open directory: $!";
while (my $file = readdir $dh) {
next if $file =~ /^\./;
next if $file =~ /~$/;
my $filepath = "$path/$file";
my $mtime = stat($filepath)->mtime;
push @contents, {
name => -d $filepath ? "$file/" : $file,
size => -s "$path/$file",
mtime => $mtime,
};
}
@contents = sort { $a->{name} cmp $b->{name} } @contents;
my $fmt = $cgi->param('fmt');
if ($fmt) {
( run in 1.131 second using v1.01-cache-2.11-cpan-49f99fa48dc )