From 59065ff6e02b605cddb55c15436b294f553c8619 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Mon, 7 Oct 2019 12:13:12 +0200 Subject: Various improvements * mansrv: Interpret material appearing between {% and %} in template files as Perl expression. Avoid using hardcoded URLs. --- mansrv | 70 +++++++++++++++++++++++++++++++++++++++++++++++----------------- top.html | 4 ++-- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/mansrv b/mansrv index f05fedb..5e99ec1 100755 --- a/mansrv +++ b/mansrv @@ -19,9 +19,10 @@ use strict; use File::Basename; use sigtrap; use Sys::Syslog; +use Safe; -my $server = "mansrv"; -my $version = "1.0"; +my $package_name = "mansrv"; +our $VERSION = "1.1"; my $cf = "/etc/mansrv.conf"; our $docdir; @@ -110,17 +111,34 @@ sub build_manpath() { $ENV{'MANPATH'} = "$dirs:$ENV{'MANPATH'}" if ($dirs); } +sub expand_template { + my ($comp, $code, $file, $line) = @_; + my $r = $comp->reval($code); + unless (defined($r)) { + syslog("LOG_ERR", "%s:%d: error expanding template expression %s", + $file, $line, $code); + $r = ''; + } + return $r; +} + sub interpret_file($$) { my ($ofd, $file) = @_; my $ifd; open($ifd, $file) or syserror("cannot open $file: $!"); + my $s = new Safe 'Root' ; + %{$s->varglob('ENV')} = %ENV; + ${$s->varglob('TITLE')} = $ARGV[1]; + ${$s->varglob('SECTION')} = $ARGV[0]; + ${$s->varglob('PACKAGE')} = $package_name; + ${$s->varglob('VERSION')} = $VERSION; + ${$s->varglob('SERVER')} = "$proto://$ENV{SERVER_NAME}"; + while (<$ifd>) { - s/\@TITLE\@/$ARGV[1]/g; - s/\@SECTION\@/$ARGV[0]/g; - s/\@SERVER\@/$server/g; - s/\@VERSION\@/$version/g; - print $ofd $_; + chomp; + s/\{%(.*)%\}/expand_template($s, $1, $file, $.)/ex; + print $ofd "$_\n"; } close($ifd); } @@ -159,9 +177,7 @@ sub checkdeps($) { # ############################################################################# -my $script; # This script name. -($script = $0) =~ s/.*\///; -openlog($script, "ndelay,pid", "daemon"); +openlog(basename($0), "ndelay,pid", "daemon"); if ($ENV{'MANSRV_CONF'}) { $cf = $ENV{'MANSRV_CONF'}; @@ -178,11 +194,21 @@ if ($manref =~ /\?$/) { } # Set up environment -&build_manpath; +build_manpath(); + +my $proto; +if (exists($ENV{HTTP_X_FORWARDED_PROTO})) { + $proto = $ENV{HTTP_X_FORWARDED_PROTO}; +} elsif ($ENV{HTTPS} eq 'on') { + $proto = 'https'; +} else { + $proto = 'http'; +} +$ENV{REQUEST_SCHEME} = $proto; $ENV{'MANCGI'}='WEBDOC'; if ($#ARGV != 1) { - print "Location: http://man.gnu.org.ua\n"; + print "Location: $ENV{REQUEST_SCHEME}://$ENV{SERVER_NAME}\n"; print "\n"; exit 0; } @@ -457,24 +483,32 @@ Include path for B (list of directories separated with semicolons). =head1 TEMPLATE SUBSTITUTIONS While interpreting the contents of the files B, B and -B, the following character sequences are removed and replaced with -the corresponding expansions: +B, the material between B<{%> and B<%}> is evaluated as a Perl +expression. It can make references to the following variables: =over 4 -=item B<@SERVER@> +=item B<$PACKAGE> Canonical name of the program (B). -=item B<@VERSION@> +=item B<$VERSION> Version of B. -=item B<@TITLE@> +=item B<%ENV> + +Trimmed environment from the master process. + +=item B<$SERVER> + +Base server URL. + +=item B<$TITLE> Manpage title. -=item B<@SECTION@> +=item B<$SECTION> Requested manpage section. diff --git a/top.html b/top.html index 768cefe..c53dc92 100644 --- a/top.html +++ b/top.html @@ -2,9 +2,9 @@ "http://www.w3.org/TR/html4/loose.dtd"> - + -@TITLE@ +{% $TITLE %}
-- cgit v1.2.1