#!/usr/bin/perl # karakurt - pure perl httpd v0.01.21 (c) mahatma, GPLs =head1 NAME phttpd-0.01.21.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.21 (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. Keep-alive are off by default. Use it with "-t" option only. Mode 2&3 may cause problems with pipelining, but I am not sure. Mode 3 may be violated by bad CGI/Content-length. Mode 4 are slow and experemental (unknown Content-length are buffered). Mode 5 using internally (totally buffered). Commandline examples: Good server: "--a :80 --m 2 --x --k 3 --t 1" Secure & light: "--a :80 --m -1 --y -t 3" Run w/o options for help. Changes: 0.01.18 - bugfixes, caching now work, keep-alive. 0.01.19 - bugfixes, keep-alive off by default. 0.01.21 - bugfixes... changed command format. =head1 PREREQUISITES Perl 5 =head1 COREQUISITES Perl 5, no modules. =pod OSNAMES All =pod SCRIPT CATEGORIES Networking Web =cut { package phttpd; # 'id{[|options]} mask'=>sub{"[user:group]file"} # 'id{[|options]} mask'=>sub{"file"} my %map=( '00 \.\.'=>sub{"404"}, '03|n /html/(.*)'=>sub{"html/$1"}, # '22 /usr/portage/(.*)'=>sub{"/usr/portage/$1"}, # '22 /usr/portage/distfiles/.*'=>sub{"gcache-0.01.cgi"}, '44 ..*'=>sub{"404"} ); # 'Host' => sub{}, may be used for clustering my %domain=( # 'www.domain.com'=>sub{cluster('.domain.com')}, ''=>sub{1,"Status: 400\n\nerror"} # rfc ); # single-host cluster (no vhosting) # remember first used IP in cookie and redirect here later sub cluster{ my $cookie="karakurtCluster"; my $ip=$ENV{'SERVER_ADDR'}; my $l=length($cookie)+1; my $h; for(split(/;[ ]*/,$ENV{'HTTP_COOKIE'})){$h=substr($_,$l) if(substr($_,0,$l) eq "$cookie=")} return if($ip eq $h); return 1,"Status: 300\nLocation: http://$h$ENV{REQUEST_URI}\n\n" if($h); 0,$l="Set-cookie: $cookie=$ip".($_[0]?"; domain=$_[0]":'')."\n" } my (@RANGE,@errors,$mainpid); # RANGE: 0-1 2=keep-alive 3=range requested 4=range proceed('2'=break) 5=break # 6-done 7-timeout 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' ); my ($pos,$buf,$done); sub httpd{ 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.21'; my $server="karakurt"; my $root=substr($0,0,rindex($0,'/')); my (%OPTIONS,$content,%contents,%URIs,@guid,%options,$conf); my @saveguid=($>,$)); my %methods=('GET'=>1,'POST'=>1,'HEAD'=>1); %ENV=( 'PATH'=>$ENV{'PATH'}, 'REMOTE_ADDR'=>$ENV{'REMOTE_HOST'}, 'DOCUMENT_ROOT'=>$root, 'GATEWAY_INTERFACE'=>'CGI/1.1', 'SERVER_SOFTWARE'=>"$server/$version perl/$] $^O", 'SERVER_SIGNATURE'=>"
$server/$version
\n" ); $|=1; for(@ARGV){ if(substr($_,0,2) eq '--'){$OPTIONS{$content=substr($_,2)}=''} else{$OPTIONS{$content}.=$OPTIONS{$content}?" $_":$_} } chdir($root); do $OPTIONS{'c'} if($conf=-M $OPTIONS{'c'}); if(exists($OPTIONS{'a'})){ my @a=split(/:/,$OPTIONS{'a'},3); if(exists($OPTIONS{'l'})){ eval q( use Socket; socket(SERVER,PF_INET,SOCK_STREAM,getprotobyname('tcp'))&& setsockopt(SERVER,SOL_SOCKET,SO_REUSEADDR,pack("l",1))&& bind(SERVER, sockaddr_in($ENV{'SERVER_PORT'}=$a[1]||591,$a[0]?inet_aton($a[0]):INADDR_ANY))&& listen(SERVER,$a[2]||SOMAXCONN)||die "Socket open error: $!\n"; ) }else{ *sockaddr_in=sub{pack 'Sna4x8',2,@_}; *inet_aton=sub{pack 'C4',split(/\./,$_[0])}; *inet_ntoa=sub{join('.',unpack('C4',$_[0]))}; socket(SERVER,2,1,6)&& setsockopt(SERVER,1,2,pack("l",1))&& bind(SERVER,sockaddr_in($ENV{'SERVER_PORT'}=$a[1]||591,inet_aton($a[0]||'0.0.0.0')))&& listen(SERVER,$a[2]||512)||die "Socket open error: $!\n" } $OPTIONS{'m'}<0&&fork&&exit; fork||last for(2..abs($OPTIONS{'m'})); $mainpid=$$; DAEMON: ($>,$))=@saveguid; $SIG{CHLD}='IGNORE'; close(STDOUT); my $a; while(1){ close(STDIN); $a=accept(STDIN,SERVER) or die; if($conf!=(my $x=-M $OPTIONS{'c'})){$conf=$x;do $OPTIONS{'c'}} last if(exists($OPTIONS{'x'})||exists($OPTIONS{'X'})||(exists($OPTIONS{'y'})?$mainpid=fork:!fork)) } $mainpid||=$$; ($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'})){ exit print qq($server/$version pure Perl httpd, (c) Dzianis Kahanovich, 2005-2006, GPLs --i - [x]inetd mode (main goal, safe) --a [addr][:port[:queue]] - bind to|":591:512" --H - break CGI (HEAD|range) --c [file] - load config Standalone (dangerous!): --l large code (may be safe) --m [-][n] - [detach]listeners|1 --t [n] - request timeout --x - cache CGI (SCRIPT_FILENAME -> precompile) --X - eXtreme cache CGI (SCRIPT_NAME -> SCRIPT_FILENAME) --n - no caching --k [n] - keep-alive level 0-none, 1-HTTP/1.0, 2-smart, 3-smart+CGI, 4-full/buff --y - inversed fork (non-cached) (only Perl CGI supported) ) } ALIVE: my ($mode,$file,@stat,@err)=(0); %options=%OPTIONS; @RANGE=('','',$OPTIONS{'k'},0,0,0,0,$OPTIONS{'t'}); tie(*STDERR=*STDOUT,'phttpd'); ($pos,$buf)=(0,''); if($RANGE[7]){ my $t=time; while(1){ $_=''; recv(STDIN,$_,32000,2); last if/\n\r?\n/gc; if((time-$t)>$RANGE[7]){ OPEN FL ">log"; print FL "timeout: $!\n"; close FL; goto DAEMON if($$==$mainpid); exit } } } my @request=split(/[ \n\r]+/,,4); $ENV{'SERVER_PROTOCOL'}=substr($content=$request[2],5,5,'')<1.1||$RANGE[2]==1?'HTTP/1.0':'HTTP/1.1'; if($content ne 'HTTP/'){@err=(400,$request[2]);goto ERR} if(!$methods{$ENV{'REQUEST_METHOD'}=$request[0]}){@err=(405,$request[0]);goto ERR} ($file,$ENV{'QUERY_STRING'})=(split(/\?/,$file=$ENV{'REQUEST_URI'}=$request[1],2),''); for(my $s=;defined($s) && $s ne "\n" && $s ne "\r\n";$s=){$s=~s/(.*?)\: (.*?)[\r\n]/my $x=$1;$x=~tr\/-a-z\/_A-Z\/;$ENV{substr($x,0,7) eq 'CONTENT'?$x:"HTTP_$x"}=$2/ge} if($_=$domain{"$ENV{'HTTP_HOST'}"}){ @err=&$_; $content=$err[1]; goto STD if($err[0]); print $content } $RANGE[3]=~s/bytes\=([0-9]*)-([0-9]*)/@RANGE[0,1]=($1,$2);1/gse if($RANGE[3]=$ENV{'HTTP_RANGE'}); $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||='/'; goto (MAP_OK,EXEC_CGI)[(($content,@stat)=@{$contents{$file=$content}})!=0] if(($content,@guid)=@{$URIs{$file}}); @guid=(-1,-1); for(sort keys %map){ my @mask=split(/ /,$_,2); $content=$file; $content=~s/$mask[1]//; if($content eq ''){ $options{$_}='' for(split(/\|/,$mask[0])); $file=~s/$mask[1]/&{$map{$_}}/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 '['); goto MAP_OK } } goto E404; MAP_OK: if($guid[1]!=-1){ $)="$guid[1] $guid[1]"; if($) ne "$guid[1] $guid[1]"){ ESEC: $RANGE[2]=0; @err=(500,'Security error'); goto ERR } } if($guid[0]!=-1){ $>=$guid[0]; goto ESEC if($> ne $guid[0]); } if(-d $file){ goto DAEMON if($$==$mainpid && fork); my $s=$ENV{'REQUEST_URI'}; if(substr($s,-1) ne '/'){ print "Status: 301\nLocation: $s/\n\n"; exit } opendir FH,$file or goto ERR; @stat=stat(FH); $s="Index of $s"; $s="Last-Modified: ".gmtime($stat[9])."\nContent-type: text/html\n\n$s

$s


";
  for(readdir(FH)){
   my @st=stat("$file$_");
   $_=~s/([\x00-\x1f,:\"\'\\])/sprintf('%%%02X',ord($1))/eg;
   $_.='/' if(-d "$file$_");
   $s.="$_		".localtime($st[9])."	$st[7]\n";
   if(length($s)>1024){print $s;$s=''}
  }
  print "$s

$ENV{'SERVER_SIGNATURE'}"; close FH; }elsif(-x $file){ if(($content,my @s)=@{$contents{$file}}){ @stat=stat($file); goto EXEC_CGI if($stat[9]==$s[9]); delete($contents{$file}) } open FH,"<$file" or goto ERR; @stat=stat(FH) if(!@stat); read(FH,$content,$stat[7]) or goto ERR; close FH; $content=~s/#\![ ]*(.*?)\n/my $x;for(split(\/[ ]\/,$1)){if(substr($_,0,1) eq '-' && defined(my $o=$opt{substr($_,1,1)})){$x.=&$o(quotemeta(substr($_,2))).';'}};"$x\n"/gse if(substr($content,0,2) eq '#!'); if($$==$mainpid&&!exists($options{'n'})){ $content=eval "sub \{ $content \};"; $URIs{$ENV{'SCRIPT_NAME'}}=[$file,@guid] if(exists($options{'X'})); $contents{$file}=[$content,@stat] if(exists($options{'x'})); EXEC_CGI: $mode=1 } goto DAEMON if($$==$mainpid && fork); $RANGE[5]=(exists($options{'H'})); $RANGE[2]=0 if($RANGE[2]<3); $ENV{'SCRIPT_FILENAME'}=$file; $guid[1]=$stat[5] if($stat[2]&02000); $guid[0]=$stat[4] if($stat[2]&04000); $)="$guid[1] ".($(=$guid[1]) if($guid[1]!=-1); $<=$>=$guid[0] if($guid[0]!=-1); return $mode,$content if($RANGE[2]<4); $mode?&$content:eval $content }elsif(-e $file){ goto DAEMON if($$==$mainpid && fork); open FH,"<$file" or goto ERR; $RANGE[2]=3 if($RANGE[2]>3); @stat=stat(FH); my $l=$stat[7]; my $h="Last-Modified: ".gmtime($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 '=0 && ($t=$mime{$x[1]})){ $h.="Content-encoding: $enc\n" } if($RANGE[3]){ $content=$n>($l=$RANGE[1]+1)?substr($content,0,$n=$l):($n='') if($RANGE[1] ne ''); if($RANGE[0]){ $content=''; seek(FH,$pos=$n=$RANGE[0],0) or goto ERR; } } $t||=$mime{$x[0]}||(-B $file?'binary/unknown':'text/plain'); print $h,"Content-type: $t\n\n",$content; untie *STDOUT; while($l-=$n){ read(FH,$content,$n=$l<1024?$l:1024) or goto ERR; print $content } close FH }else{ E404: @err=(404,$file); ERR: if(!$err[1]){ goto DAEMON if($$==$mainpid && fork); die " \n" } $content="Status: $err[0]\n\n$err[0] $rfc2068_status{$err[0]}: '$err[1]'"; STD: goto DAEMON if($$==$mainpid && fork); $RANGE[6]=1; print $content } if($RANGE[2]==5){ $RANGE[6]=1; print '' } exit if(!$RANGE[2]); $OPTIONS{'t'}=abs($RANGE[7]) if($OPTIONS{'t'}>abs($RANGE[7])); ($>,$))=@saveguid; goto (ALIVE,ESEC)[$> ne $saveguid[0] && $) ne $saveguid[1]]; } sub TIEHANDLE{bless({})} sub SEEK{ $pos=0 if(!$_[2]); $buf='' if(!($pos+=$_[1])) } sub PRINT{ shift; my $out=*STDOUT; my ($b,$l); if($RANGE[2]==5&&!$RANGE[6]){$buf.=join('',@_);return 1} if($RANGE[4]){$b=join('',@_);untie $out} else{ ($buf,$b)=split(/[\r]*\n[\r]*\n[\r]*/,join('',$buf,@_),2); if(defined($b)){ if($RANGE[2]){ $l=lc($ENV{'HTTP_CONNECTION'}||''); $RANGE[2]=0 if($l eq 'close'||($l ne 'keep-alive'&&$ENV{'SERVER_PROTOCOL'} lt 'HTTP/1.1')); } my %hh=('Content-type'=>'text/html','Status'=>200,'Server'=>$ENV{'SERVER_SOFTWARE'},'Date'=>''.gmtime(time)); $_=~s/(.*?)\:[ ]+(.*)[\r]*/$hh{ucfirst(lc($1))}=$2/e for(split(/\n/,$buf)); if(exists($hh{'Content-length'})){ $l=$hh{'Content-length'} }elsif($RANGE[6]){ $hh{'Content-length'}=$l=length($b) }elsif($RANGE[2]==4){ $buf.="\n\n$b"; $RANGE[2]=5; return 1 }else{ $RANGE[2]=0; $l='*' } if(lc($hh{'Connection'}||=$RANGE[2]?'keep-alive':'close') eq 'close'){ $RANGE[2]=0 }elsif($RANGE[7] && (my $x=$ENV{'HTTP_KEEP_ALIVE'})){ if($RANGE[7]>$x){$RANGE[7]=$x} elsif($RANGE[7]!=$x){$hh{'Keep-alive'}||="timeout=$RANGE[7]"} } if($RANGE[3] && !exists($hh{'Content-range'})){ $RANGE[4]=$RANGE[5]+1; $hh{'Content-length'}=(($l<=$RANGE[1]||$RANGE[1] eq '')?$RANGE[1]=$l-1:$RANGE[1])+1-$RANGE[0] if($l ne '*'); $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"} $buf.="\n"; untie $out; if($ENV{'REQUEST_METHOD'} eq 'HEAD'){ print $out $buf; exit if($RANGE[5]); $RANGE[4]=$RANGE[0]=$RANGE[1]=-1 }elsif(!$RANGE[4]){return print $out $buf.$b}else{print $out $buf}; }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){ print $out $b; $pos+=$l } RET: tie($out,'phttpd') if($RANGE[4] && !tied($out)); 1 } sub OPEN{my $f=shift;open(tied($f),@_)} sub WRITE{PRINT($_[0],substr($_[1],0,$_[2]))} sub PRINTF{PRINT(shift,sprintf(shift,@_))} sub CLOSE{untie *STDOUT;close(STDOUT)} $SIG{__DIE__}=sub{push @errors,@_}; END{ print "Status: 500\n\nError(s): ".join("
",$!,$@,@errors[1,])."
While loading: $ENV{'SCRIPT_NAME'}
$ENV{'SERVER_SIGNATURE'}" if(tied(*STDOUT)&&$$!=$mainpid)&&!$RANGE[4]; if($mainpid){ close(STDOUT); close(STDIN); shutdown(SERVER,2) if($$==$mainpid) } } } my ($mode,$content)=phttpd::httpd(); $mode?&$content:eval $content