#!/usr/bin/perl
# karakurt - pure perl httpd v0.01.13 (c) mahatma, GPLs

=head1 NAME

phttpd-0.01.13.pl - karakurt, small pure Perl httpd ([x]inetd or standalone).

=head1 DESCRIPTION

Small pure Perl httpd, only Perl CGI, faster Perl CGI execution.
Nice for configuration/single Perl CGI purposes.

=head1 README

karakurt, pure Perl httpd v0.01.13           (c) Dzianis Kahanovich, GPLs

This software are with NO WARRANTY!

I wrote it becouse I needs for small, fast, all-in-one httpd/perl, main -
in xinetd. There are my first server sockets programming (daemon/standalone),
then standalone mode are totally experemental, but caching modes are much more
experemental and unsecure and unsafe. Use it only for debugged, verifyed
scripts set. Also eXtreme mode must be not used with wildcard redirections:
every new URI will creating new cache entry. For real tasks even non-cached
mode are fast as perl "eval" method. But if you have commercial ;) heavy-loaded
perl-only site and if my daemon/forking model satisfy you (heh) - you may
trying eXtreme mode. But all software are with NO WARRANTY and all
PROBLEMS ARE YOUR OWN RISC!!

Configuration:
Look to %map variable and comments. There are regular expression/substitution.
Also try suexec file mode bits. I think, with mind you may build good security
for YOUR site (I not think that may be used in multi-user mode, but may be yes,
may be no - I trying to care for minimal security in non-cached mode, but not
believe in this).

Virtual hosts: use $ENV{'HTTP_HOST'} in %map target.

Do not put "tar" into %mime - you will get auto-ungzipping in your browser
(encoding). I experienced about "binary/unknown" are good content-type for
all binary downloads and real situations.

Also: all CGI scripts running via "eval", Perl commandline options are
emulated very relaxed.

May be easy added cool features in daemon (transparent compressing, etc), but
with price of unsimplifying code. Now it is minimal and functional and primary
will be used (by me) in LAN & localhost.

Run w/o options for help.

=head1 PREREQUISITES

Perl 5

=head1 COREQUISITES

Perl 5, no modules (xinetd). Sockets (for daemon mode only).

=pod OSNAMES

All

=pod SCRIPT CATEGORIES

Networking
Web

=cut

my (%OPTIONS,@RANGE);

my %rfc2068_status=(
100=>'Continue',
101=>'Switching Protocols',
200=>'OK',
201=>'Created',
202=>'Accepted',
203=>'Non-Authoritative Information',
204=>'No Content',
205=>'Reset Content',
206=>'Partial Content',
300=>'Multiple Choices',
301=>'Moved Permanently',
302=>'Moved Temporarily',
303=>'See Other',
304=>'Not Modified',
305=>'Use Proxy',
400=>'Bad Request',
401=>'Unauthorized',
402=>'Payment Required',
403=>'Forbidden',
404=>'Not Found',
405=>'Method Not Allowed',
406=>'Not Acceptable',
407=>'Proxy Authentication Required',
408=>'Request Time-out',
409=>'Conflict',
410=>'Gone',
411=>'Length Required',
412=>'Precondition Failed',
413=>'Request Entity Too Large',
414=>'Request-URI Too Large',
415=>'Unsupported Media Type',
500=>'Internal Server Error',
501=>'Not Implemented',
502=>'Bad Gateway',
503=>'Service Unavailable',
504=>'Gateway Time-out',
505=>'HTTP Version not supported'
);

sub httpd{
# 'id|options mask'=>sub{"[user:group]file"}
# 'id|options mask'=>sub{"file"}
my %map=(
 '00 \.\.'=>sub{"404"},
 '03n /html/(.*)'=>sub{"html/$1"},
# '22r /usr/portage/distfiles/(.*)'=>sub{"/usr/portage/distfiles/$1"},
# '22r /usr/portage/distfiles/.*'=>sub{"gcache-0.01.cgi"},
 '44 ..*'=>sub{"404"}
);

my %mime=(
'html'=>'text/html',
'htm'=>'text/html',
'js'=>'text/javascript',
'gif'=>'image/gif',
'jpg'=>'image/jpeg',
'gz'=>'application/x-gzip'
);

my %mime_enc=( # to prevent "content-encoding" just remove real type from mime
'gz'=>'gzip'
);

my %opt=(
 '0'=>sub{"\$\/=".oct($_[0])},
 'C'=>sub{'${^WIDE_SYSTEM_CALLS}=1'},
 'e'=>sub{'eval("'.$_[0].'")'},
 'i'=>sub{"\$\^I=$_[0]"},
 'I'=>sub{'push @INC,"'.$_[0].'"'},
 't'=>sub{'${^TAINT}=1'}, #warn
 'T'=>sub{'${^TAINT}=1'}, #fatal
 'W'=>sub{'$^W=1'},
 'X'=>sub{'$^W=0'}
);

my $version='0.01.13';
my $server="karakurt";
my $root=substr($0,0,rindex($0,'/'));
my ($content,%contents,%contents_uri,$file,@errors,$mainpid,@guid,$options);
my %methods=('GET'=>1,'POST'=>1,'HEAD'=>1);

$ENV{'SERVER_PROTOCOL'}='HTTP/1.1';
$ENV{'SERVER_SOFTWARE'}="$server/$version perl/$] $^O";
$ENV{'SERVER_SIGNATURE'}="<ADDRESS>$server/$version</ADDRESS>\n";

for(@_){
 if(substr($_,0,1) eq '-'){$OPTIONS{$content=substr($_,1)}=''}
 else{$OPTIONS{$content}.=$OPTIONS{$content}?" $_":$_}
}

END{
 print "Status: 500\n\nError(s): ".join("<br>",$!,$@,@errors[1,])."<br>While loading: $ENV{'SCRIPT_FILENAME'}<hr>$ENV{'SERVER_SIGNATURE'}" if(tied(*STDOUT)&&$$!=$mainpid)&&!$RANGE[4];
 if($mainpid){
  shutdown(STDIN,2);
  shutdown(SERVER,2) if($$==$mainpid)
 }
}
$SIG{__DIE__}=sub{push @errors,@_};

chdir($root);

if(exists($OPTIONS{'a'})){
my @a=split(/:/,$OPTIONS{'a'},2);
eval <<EOT
use Socket;
socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp'))&&
setsockopt(SERVER,SOL_SOCKET,SO_REUSEADDR,pack("l",1))&&
bind(SERVER, sockaddr_in(\$a[1]||591,\$a[0]?inet_aton(\$a[0]):INADDR_ANY))&&
listen(SERVER,SOMAXCONN)
EOT
||die "Socket open error: $!\n";
for(2..$OPTIONS{'m'}){fork||last}
$mainpid=$$;
DAEMON:
$SIG{CHLD}='IGNORE';
untie(*STDOUT);
close(STDOUT);
my $a;
while(1){
 close(STDIN);
 $a=accept(STDIN,SERVER) or die;
 last if(exists($OPTIONS{'x'})||exists($OPTIONS{'X'})||!fork)
}
($ENV{'REMOTE_PORT'},$a)=sockaddr_in($a);
$ENV{'REMOTE_ADDR'}=inet_ntoa($a);
($ENV{'SERVER_PORT'},$a)=sockaddr_in(getsockname(STDIN));
$ENV{'SERVER_ADDR'}=inet_ntoa($a);
open STDOUT,">&STDIN" or die
}elsif(exists($OPTIONS{'i'})){
 $ENV{'REMOTE_ADDR'}=$ENV{'REMOTE_HOST'}
}else{
 die "$server/$version pure Perl httpd, (c) Dzianis Kahanovich, 2005-2006, GPLs\n -i - [x]inetd mode (main goal, safe)\n -a [addr][:port] - bind to|\":591\"\n -H - break CGI (HEAD|range)\nStandalone (dangerous!):\n -m [n] - listeners|1\n -x - cache CGI (SCRIPT_FILENAME)\n -X - eXtreme cache CGI (REDIRECT_URI, no checking)\n -n - no caching\n(only Perl CGI supported)\n"
}

my $mode; undef my @err;
$options=$OPTIONS;
@guid=(-1,-1);
tie(*STDERR=*STDOUT,'phttpd');
seek(STDOUT,0,0);
@request=split(/[ \n\r]/,<STDIN>);
for(my $s=<STDIN>;defined($s) && $s ne "\n" && $s ne "\r\n";$s=<STDIN>){
 my $x;
 $s=~s/(.*?)\: (.*?)[\r\n]/$x=uc($1);$2/ge;
 if(defined($x)){
  $x=~s/-/_/g;
  $x="HTTP_$x";
  $x=~s/HTTP_CONTENT/CONTENT/g;
  $ENV{$x}=$s
 }
}
if(!$methods{$ENV{'REQUEST_METHOD'}=uc($request[0])}){
 @err=(405,$request[0]);
ERR:
 goto DAEMON if($$==$mainpid && fork);
 die defined(@err)?"Status: $err[0]\n\n$err[0] $rfc2068_status{$err[0]}: '$err[1]'\n":" \n"
}
$RANGE[3]=~s/bytes\=(.*?)-(.*?)\n/@RANGE[0,1]=($1,$2);1/gse if($RANGE[3]=$ENV{'HTTP_RANGE'});
($file,$ENV{'QUERY_STRING'})=split(/\?/,$ENV{'REQUEST_URI'}=$request[1],2);
$file="/$file" if(substr($file,0,1) ne '/');
$file.="\n";
$file=~s/(\/.*?)\/\.\.([\/\n])/$1$2/g;
chomp($file);
$ENV{'REDIRECT_URI'}=$ENV{'SCRIPT_NAME'}=$file||='/';
if(exists($contents_uri{$file})){
 $content=$contents_uri{$file};
 ($content,@guid[0,1])=@$content;
 goto EXEC_CGI
}
for(sort keys %map){
 my @mask=split(/ /,$_,2);
 $content=$file;
 $content=~s/$mask[1]//;
 if($content eq ''){
  undef $content;
  $options{$_}='' for(split(//,$mask[0]));
  $RANGE[5]=exists($options{'H'});
  my $e=$map{$_};
  $file=~s/$mask[1]/&$e/e;
  $file=~s/\[(.*?)\:(.*?)\]/my $g=($2+0 eq $2||$2 eq ''?$2:getgrnam($2));my $u=($1+0 eq $1||$1 eq ''?$1:getpwnam($1)); die "Invalid uid or gid\n" if(!(defined($u)&&defined($g)));@guid=($u,$g);''/e if(substr($file,0,1) eq '[');
  if($$!=$mainpid){
   $)="$guid[1] ".($(=$guid[1]) if($guid[1]!=-1);
   $<=$>=$guid[0] if($guid[0]!=-1)
  }
  $ENV{'SCRIPT_FILENAME'}=$file;
  undef my @stat;
  my $filetype=-d $file?1:-x$file?2:3;
  if($filetype==2){
   if(exists($contents{$file})){
    @stat=stat($file);
    if($stat[9]==$contents{$file}[12]){
     $content=$contents{$file};
     ($content,@guid[0,1])=@$content;
     goto EXEC_CGI
    }
    delete($contents{$file})
   }
   open FH,"<$file" or goto ERR;
   @stat=stat(FH) if(!defined(@stat));
   read FH,$content,$stat[7] or goto ERR;
   close FH;
   $content=~s/#\![ 	]{0,}(.*?)\n/my $x;for(split(\/[ 	]\/,$1)){if(substr($_,0,1) eq '-' && defined(my $o=$opt{substr($_,1,1)})){$x.=&$o(quotemeta(substr($_,2))).';'}};$x/gse if(substr($content,0,2) eq '#!');
   $guid[1]=$stat[5] if($stat[2]&02000);
   $guid[0]=$stat[4] if($stat[2]&04000);
   if($mainpid){
    if(!exists($options{'n'})){
     $content=eval "sub \{ $content \};";
     if(exists($options{'X'})){
      $contents_uri{$ENV{'REDIRECT_URI'}}=[$content,@guid]
     }elsif(exists($options{'x'})){
      $contents{$file}=[$content,@guid,@stat]
     }
   EXEC_CGI:
     $mode=1
    }
    goto DAEMON if($$==$mainpid && fork);
   }
   $)="$guid[1] ".($(=$guid[1]) if($guid[1]!=-1);
   $<=$>=$guid[0] if($guid[0]!=-1);
   if($mode){
    exit &$content
   }else{
    return $content
   }
  }
  goto DAEMON if($$==$mainpid && fork);
  @RANGE[2,5]=($RANGE[3],1);
  if($filetype==1){
   opendir DH,$file or goto ERR;
   @stat=stat(DH);
   my $s="Index of $ENV{'REQUEST_URI'}";
   $s="<html><head><title>$s</title></head><body><H1>$s</H1><pre><hr><pre>";
   for(readdir(DH)){
    my @st=stat("$file$_");
    $_=~s/([\x00-\x1f,:\"\'\\])/sprintf('%%%02X',ord($1))/eg;
    $_.='/' if(-d "$file$_");
    $s.="<a href='$_'>$_</a>		".localtime($st[9])."	$st[7]\n"
   }
   $s.="</pre><hr>$ENV{'SERVER_SIGNATURE'}</body></html>";
   close DH;
   print "Last-Modified: ".localtime($stat[9]),"\nContent-type: text/html\nContent-length: ",length($s),"\n\n",$s
  }else{
   open FH,"<$file" or goto ERR;
   @stat=stat(FH) if(!defined(@stat));
   my $l=$stat[7];
   my $h="Last-Modified: ".localtime($stat[9])."\nContent-Length: $l\n";
   my @x=(split(/\./,$file))[-1,-2];
   my ($t,$enc,$n);
   read(FH,$content,$n=$l<1024?$l:1024) or goto ERR;
   if(substr($content,0,14) eq '<!DOCTYPE html'){
    $t='text/html'
   }elsif(($enc=$mime_enc{$x[0]}) && substr(",$ENV{'HTTP_ACCEPT_ENCODING'},",",$enc,")>=0 && ($t=$mime{$x[1]})){
    $h.="Content-encoding: $enc\n"
   }
   if($RANGE[3]){
    substr($content,$n=$l)='' if($RANGE[1] ne ''&&($l=$RANGE[1]+1)<$n);
    if($RANGE[0]){
     $content='';
     seek(FH,$n=$RANGE[0],0) or goto ERR;
     seek(STDOUT,$n,0);
    }
   }
   $t||=$mime{$x[0]}||(-B $file?'binary/unknown':'text/plain');
   print $h,"Content-type: $t\n\n",$content;
   while($l-=$n){
    read(FH,$content,$n=$l<1024?$l:1024) or goto ERR;
    print $content
   }
   close FH
  }
  exit
 }
}
}


{
package phttpd;
my ($pos,$buf);
sub TIEHANDLE{bless({})}
sub SEEK{
 $pos=0 if(!$_[2]);
 $buf='' if(!($pos+=$_[1]))
}
sub PRINT{
shift;
my ($b,$l);
if($RANGE[4]){$b=join('',@_)}
else{
($buf,$b)=split(/[\r]{0,}\n[\r]{0,}\n[\r]{0,}/,join('',$buf,@_),2);
if(defined($b)){
 my %hh=('Content-type'=>'text/html','Status'=>200,'Server'=>$ENV{'SERVER_SOFTWARE'},'Date'=>''.localtime(time),'Connection'=>'close');
 $_=~s/(.*?)\:[ 	]{1,}(.*)[\r]{0,}/$hh{ucfirst(lc($1))}=$2/e for(split("\n",$buf));
 if($RANGE[3] && !exists($hh{'Content-range'})){
  $RANGE[4]=$RANGE[5]+1;
  my $l=$hh{'Content-length'};
  if($l ne ''){
   $hh{'Content-length'}=(($l<=$RANGE[1]||$RANGE[1] eq '')?$RANGE[1]=$l-1:$RANGE[1])+1-$RANGE[0]
  }else{$l='*'}
  $hh{'Content-range'}="bytes $RANGE[0]-$RANGE[1]/$l";
  $hh{'Status'}=206
 }
 $buf=delete($hh{'Status'});
 $buf="$ENV{'SERVER_PROTOCOL'} $buf $rfc2068_status{$buf}\n";
 while(my ($x,$y)=each %hh){$buf.="$x: $y\n"}
 untie *STDOUT;
 print "$buf\n";
 if($ENV{'REQUEST_METHOD'} eq 'HEAD'){
  exit if($RANGE[5]);
  $RANGE[4]=$RANGE[0]=$RANGE[1]=-1
 }elsif(!$RANGE[4]){return print $b}
}else{return 1}
}
$l=length($b);
if($RANGE[1] ne ''){
 if($pos>$RANGE[1]){
  exit if($RANGE[4]==2);
  goto RET
 }elsif((my $n=$pos+$l-1-$RANGE[1])>0){
  substr($b,-$n)='';
  $pos+=$n;
  $l-=$n
 }
}
if((my $n=$RANGE[0]-$pos)>0){
 if($n>$l){$n=$l;$b=''}
 else{substr($b,0,$n,'')}
 $pos+=$n;
 $l-=$n
}
if($l>0){
 untie *STDOUT;
 print $b;
 $pos+=$l
}
RET:
tie(*STDOUT,'phttpd') if($RANGE[4] && !tied(*STDOUT));
1
}
}

eval httpd(@ARGV)
