#!/usr/bin/perl # karakurt - pure perl httpd v0.01.01 (c) mahatma, GPLs eval httpd(@ARGV); exit $?; sub httpd{ # 'id mask'=>sub{"[user:group]file"} # 'id mask'=>sub{"file"} my %map=( '00 \.\.'=>sub{"404"}, '03 /html/(.*)'=>sub{"html/$1"}, # '22 /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 %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 $version='0.01.01'; my $server="karakurt"; my $root=substr($0,0,rindex($0,'/')); my (@OPTIONS,$content,%contents,%contents_uri,$file,@errors,$mainpid,@guid); $ENV{'REMOTE_ADDR'}=$ENV{'REMOTE_HOST'}; $ENV{'SERVER_PROTOCOL'}='HTTP/1.0'; $ENV{'SERVER_SOFTWARE'}="$server/$version"; $ENV{'SERVER_SIGNATURE'}="
$server/$version\n"; for(@_){ if(substr($_,0,1) eq '-'){$OPTIONS{$content=substr($_,1)}=''} else{$OPTIONS{$content}.=$OPTIONS{$content}?" $_":$_} } END{print "Status: 500\n\nError(s): ".join(""; for(readdir(DH)){ my @st=stat("$file$_"); $_=~s/([\x00-\x1f,:\"\'\\])/sprintf('%%%02X',ord($1))/eg; $_.='/' if(-d "$file$_"); $s.="$_ ".localtime($st[9])." $st[7]\n"; } $s.="
$ENV{'SERVER_SIGNATURE'}"; close DH; print "Last-Modified: ".localtime($stat[9]),"\nContent-type: text/html\nContent-length: ",length($s),"\n\n",$s; }else{ open FH,"<$file" or die " \n"; @stat=stat(FH) if(!defined(@stat)); my $h="Last-Modified: ".localtime($stat[9])."\n"; my @x=(split(/\./,$file))[-1,-2]; my ($t,$enc); read(FH,$content,my $cnt=1024>$stat[7]?$stat[7]:1024) or die " \n"; if(substr($content,0,14) eq '=0 && ($t=$mime{$x[1]})){ $h.="Content-encoding: $enc\n" } $t||=$mime{$x[0]}||(-B $file?'binary/unknown':'text/plain'); print $h,"Content-Length: ",$stat[7],"\nContent-type: $t\n\n",$content; while((my $n=$stat[7]-$cnt)>0){ read(FH,$content,$cnt=+($n>1024?1024:$n)) or die " \n"; print $content } close FH } exit $? } } } { package phttpd; my $buf; sub TIEHANDLE{bless({})} sub PRINT{ shift; ($buf,my $b)=split(/[\r]{0,}\n[\r]{0,}\n[\r]{0,}/,join('',$buf,@_),2); if(defined($b)){ untie *STDOUT; untie *STDERR; my %hh=('Content-type'=>'text/html','Status'=>200,'Server'=>$ENV{'SERVER_SOFTWARE'},'Date'=>''.localtime(time)); $_=~s/(.*?)\:[ ]{1,}(.*)[\r]{0,}/$hh{ucfirst(lc($1))}=$2/e for(split("\n",$buf)); $buf=delete($hh{'Status'}); $buf="$ENV{'SERVER_PROTOCOL'} $buf $rfc2068_status{$buf}\n"; while(my ($x,$y)=each %hh){$buf.="$x: $y\n"} print $buf,"\n",$b; undef $buf; } 1 } } __END__ =head1 NAME phttpd-0.01.01.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.01 (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: while all CGI scripts running via "eval" - all Perl comman dline options in scripts will be ignored. 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. =head1 PREREQUISITES Perl 5 (tested with 5.8.6 only) =head1 COREQUISITES Perl 5, no modules (xinetd). Sockets (for daemon mode only). =pod OSNAMES All =pod SCRIPT CATEGORIES Web =cut