<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl

use strict;
use warnings;
use IO::Socket::INET; # The server
use File::Spec; # For finding files
# use File::FlockDir qw(open close flock $Assume_LockDir_Zombie_Minutes); # For locking log file remove on OSs serporting Flock
use Date::Format;

our $VERSION="0.2.392";

=head1 NAME

POD Server.

=cut

=head2 Config

The config settings you can use these are set in the script file for now.

=over 4
=cut

my $config; # Holds all config info.
$config-&gt;{Port} = "8080";

=item Port

The port that the server should listen on.

=cut

$config-&gt;{Index}-&gt;{File} = "index.pod"; # Where to save the index file # And get it some of the time
$config-&gt;{Index}-&gt;{On} = 1;
$config-&gt;{Index}-&gt;{Url} = "index";
$config-&gt;{Index}-&gt;{PerSlaves} = 16;
$config-&gt;{Index}-&gt;{GroupSize} = 2;

=item Index

Contains info on the index file

=item Index-&gt;File

The file to use for generating and returning an index.

=item Index-&gt;On

Wether to make an index or not.

=item Index-&gt;Url

The file name of the index as seen by the world.

=item Index-&gt;PerlSlaves

Make a new index arfter this many slaves die.
An index is made when the server starts

=item Index-&gt;GroupeSize

This is the number of letters which you whant to use from the beging of the page name I recomend 1 or 2.

=cut

# $config-&gt;{PodDirs}-&gt;[0] = '/usr/lib/perl5';
$config-&gt;{UseINC} = 1;

=item PodDirs

Contains the dirs that have pod files

=item PodDirs-&gt;@

An array of dirs to look for pod files in.
POD Server will look for them in the order of the array

=item UseINC

If set to true POD Server will append the @inc array to the PodDirs array.

=cut

$config-&gt;{PodExt}-&gt;[0] = ".pod";
$config-&gt;{PodExt}-&gt;[1] = ".pm";
$config-&gt;{PodExt}-&gt;[2] = ".pl";
$config-&gt;{PodExt}-&gt;[3] = ".plx";
$config-&gt;{PodExt}-&gt;[4] = ".al";
$config-&gt;{PodExt}-&gt;[5] = ".cgi";

=item PodExt

An array of pod extenshions to use in the order of the array.

=cut

$config-&gt;{Style}-&gt;{Norm}-&gt;{mime} = "text/plain";

$config-&gt;{Style}-&gt;{raw}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{raw}-&gt;{on} = 1;

$config-&gt;{Style}-&gt;{pod}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{pod}-&gt;{on} = 1;

$config-&gt;{Style}-&gt;{html}-&gt;{mine} = "text/html";
$config-&gt;{Style}-&gt;{html}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{html}-&gt;{Script} = "pod2html";
$config-&gt;{Style}-&gt;{html}-&gt;{PodRoot} = '';

$config-&gt;{Style}-&gt;{text}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{text}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{text}-&gt;{Script} = "pod2text";

$config-&gt;{Style}-&gt;{xml}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{xml}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{xml}-&gt;{Script} = "pod2xml";

$config-&gt;{Style}-&gt;{man}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{man}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{man}-&gt;{Script} = "pod2man";

$config-&gt;{Style}-&gt;{pdf}-&gt;{mine} = "application/pdf";
$config-&gt;{Style}-&gt;{pdf}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{pdf}-&gt;{Script} = "pod2pdf";

$config-&gt;{Style}-&gt;{textc}-&gt;{mine} = "text/plain";
$config-&gt;{Style}-&gt;{textc}-&gt;{on} = 1;
$config-&gt;{Style}-&gt;{textc}-&gt;{Script} = "pod2text";

=item Style

This contains all the style information.

The data is split into gropes one for each style and a B&lt;Norm&gt; for defolts.

Each group can have these settings and possible others.
=over 8

=item * mime

The mime of the file type eg C&lt;text/html&gt; or C&lt;text/plain&gt;.

=item * on

A true, false value stating whether to use this option or return an empty file

=item * Script

The file name of the script which does the converting.

=back

The following are curently serported formats.

=over 8

=item * raw

All of the file like a web server.
This is a sercurity risk

=item * pod

Just the pod bits of the file.

=item * html

The file converted to html

=item * text

Plain text

=item * xml

The file converted to xml

=item * man

The file is converted to man page format

=item * pdf

Pdf vershion of the file.

=item * textc

A Colour text vershion of the file

=back

=cut

$config-&gt;{DStyle} = "html";

=item DStyle

The normal style to use when nune has been spesified.

=cut

$config-&gt;{Slaves} = 2;

=item Slaves

The number of slaves to fork to handle requests.
This is not the number of proses that will be running as most formats run external programs and the server may fork another proses to make the index.

=cut

$config-&gt;{ConsPerSlave} = 8;

=item ConsPerSlave

The number of connections each slave should run before dieing and being repliced

=cut

$config-&gt;{Fork} = 0;

=item Fork

A true false value on wethere to us C&lt;fork&gt; function.
I added this because I keep getting invalid page folts when one of the proses dye on windows
If you can help me with this let me know what I am doing wong.

=cut

$config-&gt;{PerlExe} = '/usr/bin/perl';

=item PerlExe

This is used by some of the formats to run perl scripts.
It should be the full parth to your perl exe.

=cut

$config-&gt;{TempDir} = '/tmp';

=item TempDir

The temp directory that holds all temp files.
They should be removed but this is may not happen so you should have it some where where files will be deleated arter a shourt time.

=cut

$config-&gt;{log}-&gt;{File} = "pod.log";
$config-&gt;{log}-&gt;{ToFile} = 1;
$config-&gt;{log}-&gt;{ToStderr} = 1;
$config-&gt;{log}-&gt;{ToStdout} = 0;

=item log

Contains login information

=item log-&gt;File

The file to log to

=item log-&gt;ToFile

A true false value stating whether to send loging info to the log file

=item log-&gt;ToStderr

A true false value stating whether to send loging info to STDERR

=item log-&gt;ToStdout

A true false value stating whether to send loging info to STDOUT

=back

=cut

my $nl = "\015\012";
my %server; # Holds pids of servers
$| = 1;

# $Assume_LockDir_Zombie_Minutes = 1; # Stop log from getting stuck. # Remove on Mershens with flock

# LOCK_SH = 1; # Note to self
# LOCK_EX = 2;
# LOCK_NB = 4;
# LOCK_UN = 8;

my %log = (
	server_no =&gt; 0,
	con_no =&gt; 0,
);

my $server = IO::Socket::INET-&gt;new(
	LocalPort =&gt; $config-&gt;{Port},
	Listen =&gt; SOMAXCONN,
) or &amp;logthis(
	message =&gt; "Could not start server $!",
	fatal =&gt;  1,
	%log
);

&amp;logthis(
	message =&gt; "Server started",
	%log,
);

if ($config-&gt;{UseINC}){
	push(@{$config-&gt;{PodDirs}}, @INC);
}

if ($config-&gt;{Fork}){
	foreach (1..$config-&gt;{Slaves}){ # Pree Fork
		&amp;forkit($_);
	}
	while (1){
		if ($config-&gt;{Index}-&gt;{On}){ # Make index
			if (my $pid = fork){}
			else {
				&amp;mkindex();
				exit;
			}
		}
		foreach (1..$config-&gt;{Index}-&gt;{PerSlaves}){ # Refork slaves
			my $pid = wait;
			my $server_no = $server{$pid};
			&amp;forkit($server_no);
		}
	}
}
else{
	my $server_no; # Count servers
	while (1){
		if ($config-&gt;{Index}-&gt;{On}){&amp;mkindex();} # Make index
		foreach (1..$config-&gt;{Index}-&gt;{PerSlaves}){ # Parse connections
			$server_no++;
			&amp;connection($server_no);
		}
	}
}

sub logthis{ # I do not want to hide log.
	# 0 Logging failed
	# 1 All OK
	# 2 Not logging all ok
	my @arg = @_;
	if ($config-&gt;{log}-&gt;{ToFile} || $config-&gt;{log}-&gt;{ToStderr}){
		foreach (@arg){
			s/\012|\015//g;
		}
		my %set = @arg;
		my $ip = join(".", unpack("CCCC", $set{ip} || "\0\0\0\0"));
		my $ident = $set{ident} || "-";
		my $user = $set{user} || "-";
		my $time = time2str("%d/%b/%Y:%T %z", time);
		my $request = $set{request} || "-";
		my $status = $set{status} || "-";
		my $size = $set{size} || "-";
		# Extras
		my $file = $set{file} || "-";
		my $format = $set{format} || "-";
		my $message = $set{message} || "-";
		my $server_no = $set{server_no} || "-";
		my $con_no = $set{con_no} || "-";
		my $logline = "$ip $ident $user [$time] \"$request\" $status $size $file \"$message\" $server_no $con_no\n";
		if ($config-&gt;{log}-&gt;{ToFile}){
			open(LOG, "&gt;&gt;$config-&gt;{log}-&gt;{File}") or return 0;
			flock(LOG, 2) or return 0;
			my $oldfh = select LOG;
			$| = 1;
			select $oldfh;
			seek(LOG, 0, 2);
			print LOG $logline;
			close LOG;
		}
		if ($config-&gt;{log}-&gt;{ToStderr}){
			my $oldfh = select STDERR;
			$| = 1;
			select $oldfh;
			print STDERR $logline;
		}
		if ($config-&gt;{log}-&gt;{ToStdout}){
			my $oldfh = select STDOUT;
			$| = 1;
			select $oldfh;
			print STDOUT $logline;
		}
		if ($set{fatal}){
			exit;
		}
		return 1;
	}
	else {
		return 2; # A true value but not one because no log was made
	}
}

sub forkit{
	my $server_no = shift;
	if (my $pid = fork){
		$server{$pid} = $server_no;
		sleep(1);
		return 1;
	}
	else{
		&amp;connection($server_no);
		sleep(1);
		exit;
	}
}

sub connection{
	my $server_no = shift;
	SERVER:foreach my $con_no(1..$config-&gt;{ConsPerSlave}){
		my $client = $server-&gt;accept(); # Items needed for the connection
		my %log;
		CONNECTION:{
			$log{server_no} = $server_no;
			$log{con_no} = $con_no;
			my $con;
			$log{ip} = $client-&gt;peeraddr();
			my $request = &lt;$client&gt;;
			$log{request} = $request;
			($con-&gt;{method}, $con-&gt;{url}, $con-&gt;{proto}) = $request =~ m/^(\S+)\s+(\S+)\s+(\S+)\s*$/ or eval{
				# No bad request lines
				$log{message} .= "Request error";
				print $client "HTTP/1.1 400 Bad Request", $nl;
				print $client "Date: ", gmtime(), $nl;
				print $client $nl;
				$log{status} = 400;
				last CONNECTION;
			};
			unless ($con-&gt;{method} =~ m/get/i){ # Only alow get requests
				print $client "HTTP/1.1 501 Not Implemented", $nl;
				print $client "Date: ", gmtime(), $nl;
				print $client $nl;
				$log{status} = 501;
				last CONNECTION;
			}
			($con-&gt;{url}, $con-&gt;{query}) = split(/\?/, $con-&gt;{url});
			while ($con-&gt;{url} =~ s/\.\.//g){1} # remove double dots to prevent hacks all of them
			$con-&gt;{format} = $con-&gt;{query} || $config-&gt;{DStyle}; # Find out what style to use
			if ($con-&gt;{url} eq "/"){ # Did they not state a file then return the index.
				$con-&gt;{url} = $config-&gt;{Index}-&gt;{Url};
			}
			WHICH_FILE:{
				# Witch file
				my $file;
				if ($con-&gt;{url} =~ m/^(.*)\./){ # Nead big (.*) so only extenshion removed and not file name
					$file = $1;
				}
				else{
					$file = $con-&gt;{url};
				}
				$file =~ s/^\\\///g; # Remove leading slashes no route file hear
				unless ($file){
					$log{message} .= "No file";
					print $client "HTTP/1.1 500 Internal Server Error", $nl;
					print $client "Date: ", gmtime(), $nl;
					print $client $nl;
					$log{status} = 501;
					last CONNECTION;
				}
				foreach my $t_dir(@{$config-&gt;{PodDirs}}){
					foreach my $t_ext(@{$config-&gt;{PodExt}}){
						my $t_file = $t_dir . File::Spec-&gt;catfile(split(/\/+|:+/, $file . $t_ext));
						if (-e $t_file &amp;&amp; -r _){
							$con-&gt;{podfile} = $t_file;
							$log{file} .= $t_file;
							last WHICH_FILE;
						}
					}
				}
				unless ($con-&gt;{podfile}){
					print $client "HTTP/1.1 404 Not Found", $nl;
					print $client "Date: ", gmtime(), $nl;
					my $mime = $config-&gt;{Style}-&gt;{$con-&gt;{format}}-&gt;{mine} || $config-&gt;{Style}-&gt;{Norm}-&gt;{mime};
					print $client "Content-Type: ", $mime, $nl;
					print $client $nl;
					if (-e $config-&gt;{Index}-&gt;{File} &amp;&amp; -r _){
						$log{status} = 404;
						$log{file} .= $config-&gt;{Index}-&gt;{File};
						$con-&gt;{podfile} = $config-&gt;{Index}-&gt;{File};
						goto FORMAT; # Go and get the index know to show with 404 error.
					}
					else {
						last CONNECTION; # Nothing else possible so give up.
					}
				}
			}
			print $client "HTTP/1.1 200 OK", $nl;
			print $client "Date: ", gmtime(), $nl;
			my $mime = $config-&gt;{Style}-&gt;{$con-&gt;{format}}-&gt;{mine} || $config-&gt;{Style}-&gt;{Norm}-&gt;{mime};
			print $client "Content-Type: ", $mime, $nl;
			print $client "Sever: PodServer/2.0 (perl/$^O)", $nl;
			print $client $nl;
			$log{status} = 200;
			FORMAT:foreach my $t_format(1..2){
				#  \/ Use these vars should not change much
				my $format = $con-&gt;{format};
				my $file = $con-&gt;{podfile};
				my $fh = $client;
				if ($format =~ m/html/i){
					if ($config-&gt;{Style}-&gt;{html}-&gt;{on}){
						# Used open instead of qx so that data is returned to the user quicker to start.
						my $command = "$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{html}-&gt;{Script} --infile=$file --htmlroot=/ --quiet";
						open(PIPE, "$command|");
						while ($_ = &lt;PIPE&gt;){
							print $fh $_;
						}
						close PIPE;
						$log{format} = "HTML";
					}
					last FORMAT;
				}
				elsif ($format =~ m/text/i){
					if ($config-&gt;{Style}-&gt;{html}-&gt;{on}){
						my $command = "$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{text}-&gt;{Script} $file";
						open(PIPE, "$command|");
						while ($_ = &lt;PIPE&gt;){
							print $fh $_;
						}
						close PIPE;
						$log{format} = "TEXT";
					}
					last FORMAT;
				}
				elsif ($format =~ m/textc/i){
					if ($config-&gt;{Style}-&gt;{textc}-&gt;{on}){
						my $command = "$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{textc}-&gt;{Script} -c $file";
						open(PIPE, "$command|");
						while ($_ = &lt;PIPE&gt;){
							print $fh $_;
						}
						close PIPE;
						$log{format} = "TEXTC";
					}
					last FORMAT;
				}
				elsif ($format =~ m/pdf/i){
					if ($config-&gt;{Style}-&gt;{pdf}-&gt;{on}){
						`$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{pdf}-&gt;{Script} $file`;
						my $pdf = $file . ".pdf";
						open(TMP, "&lt;$pdf") or last FORMAT;
						seek(TMP, 0, 0); # No errors please use TMP more than once
						while (&lt;TMP&gt;){
							print $fh $_;
						}
						close TMP;
						unlink $pdf;
						$log{format} = "PDF";
					}
					last FORMAT;
				}
				elsif ($format =~ m/xml/i){
					if ($config-&gt;{Style}-&gt;{xml}-&gt;{on}){
						my $command = "$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{xml}-&gt;{Script} $file";
						open(PIPE, "$command|");
						while ($_ = &lt;PIPE&gt;){
							print $fh $_;
						}
						close PIPE;
						$log{format} = "XML";
					}
					last FORMAT;
				}
				elsif ($format =~ m/man/i){
					if ($config-&gt;{Style}-&gt;{man}-&gt;{on}){
						my $command = "$config-&gt;{PerlExe} $config-&gt;{Style}-&gt;{man}-&gt;{Script} $file";
						open(PIPE, "$command|");
						while ($_ = &lt;PIPE&gt;){
							print $fh $_;
						}
						close PIPE;
						$log{format} = "MAN";
					}
					last FORMAT;
				}
				elsif ($format =~ m/pod/i){
					if ($config-&gt;{Style}-&gt;{pod}-&gt;{on}){
						open(POD, "&lt;$file");
						flock(POD, 1);
						while (&lt;POD&gt;){
							unless (/^=/){next}
							if (/^=/){
								print $fh $_;
								while (&lt;POD&gt;){
									if (/^=cut/){last}
									print $fh $_;
								}
							}
						}
						$log{format} = "POD";
					}
					last FORMAT;
				}
				elsif ($format =~ m/raw/i){
					if ($config-&gt;{Style}-&gt;{raw}-&gt;{on}){
						open(POD, "&lt;$file");
						flock(POD, 1);
						while (&lt;POD&gt;){
							print $fh $_;
						}
						close POD;
						$log{format} = "RAW";
					}
					last FORMAT;
				}
				# Template
				#elsif ($format =~ m/raw/i){
				#	if ($config-&gt;{Style}-&gt;{raw}-&gt;{on}){
				#		# Put code hear
				#		# Get settings from $config
				#		$log{format} = "HTML";
				#	}
				#	last FORMAT;
				#}

				else {
					if ($t_format == 1){
						$format = $config-&gt;{DStyle};
					}
					else {
						$log{message} .= "Bad format $format";
						last CONNECTION;
					}
				}
			}
		}
		close $client;
		&amp;logthis(%log) or warn "Logging error\n";
	}
	return 256;
}
sub mkindex{
	our @index;
	my %log = (
		server_no =&gt; 0,
		con_no =&gt; 1,
		message =&gt; "New Index",
	);
	foreach (@{$config-&gt;{PodDirs}}){
		my $indent = 0;
		&amp;parsedir($_);
	}
	sub parsedir{
		my $dir = shift;
		my @stack = @_;
		opendir(my $dh, $dir) or warn "can not open $dir: $!";
		FILE:while (my $file = readdir($dh)){
			if ($file =~ m/^\./){next FILE}
			my $file_long = File::Spec-&gt;catfile($dir, $file);
			if (-f $file_long){
				foreach (@{$config-&gt;{PodExt}}){
					if ($file =~ m/(.*)$_/){
						push(@index, join("::", @stack, $1));
						next FILE;
					}
				}
				next FILE;
			}
			if (-d $file_long){
				$file_long =~ s/\\/\\/go;
				&amp;parsedir($file_long, @stack, $file);
			}
		}
		close $dh;
	}
	@index = sort{lc($a) cmp lc($b)} @index;
	unless (open(INDEX, "&gt;$config-&gt;{Index}-&gt;{File}")){
		$log{message} = "Can not open Log file $!";
		return 0;
	}
	flock(INDEX, 2);
	my $oldfh = select INDEX;
	$| = 1;
	select $oldfh;
	print INDEX "=head1 Index\n\n";
	print INDEX "=over 4\n\n";
	my @ol;
	my $indexlets = $config-&gt;{Index}-&gt;{GroupSize}-1;
	foreach (@index){
		if ($_){
			my @nl = split("", $_);
			my $changed = 0;
			foreach (0..$indexlets){
				unless (lc($nl[$_]) eq lc($ol[$_])){
					$ol[$_] = uc($nl[$_]);
					$changed++;
				}
			}
			if ($changed){
					print INDEX "\n\n=head2 " . join("", @ol[0..$indexlets]) . "\n\n";
				}
			print INDEX "L&lt;$_|$_&gt;, ";
		}
	}
	print INDEX "\n\n=back\n\n";
	print INDEX "=cut\n\n";
	close INDEX;
	&amp;logthis(%log);
	return 1;
}

=head1 SYNOPSIS

The POD Server acts as a HTTP server returning pod pages from your libery and converts them to the format that you want.
Curently it serports

=over 4

=item *

html

=item *

text

=item *

textc

=item *

xml

=item *

pdf

=item *

raw

=item *

pod

=item *

man

=head1 DESCRIPTION

POD Server requires that you a just the config setings and then it is ready to go.
Just Start it up and then type in the address of the server and pod page in your brouser eg.

F&lt;http://localhost:8181/pod::html&gt;, or
F&lt;http://localhost:8181/IO/Socket::INET?html&gt;.

The format is http://B&lt;Server Address&gt;:B&lt;Server Port&gt;/B&lt;Pod file separated by / or ::&gt;?B&lt;Format&gt;

=head1 AUTHOR

CAJ 020326 The origenal script and pod.

=head1 BUGS

There is know return errors to the client just 200 and a blank file.

=head1 SEE ALSO

I&lt;pod2html(1)&gt;, I&lt;pod2text(1)&gt;, I&lt;pod2pdf(1)&gt;, I&lt;pod2man(1)&gt;, I&lt;pod2xml(1)&gt;

=head1 COPYRIGHT

This program is free software.  You may copy or redistribute it under the same terms as Perl itself.
Future versions may have a different copyright.

=head1 SCRIPT CATEGORIES

Networking

=head1 PREREQUISITES

strict
warnings
IO::Socket::INET
File::Spec
Date::Format

=head1 COREQUISITES

File::FlockDir
Pod::XML
Pod::Pdf
Pod::Text
Pod::Html
Pod::Man

=head1 README

POD Server is a simple http server whitch converts pod to many diferant formats on the fly when they are requested.

=cut
</pre></body></html>