($c =q' u se 3S o c ke t ; sub 3T{print" $$:3",(scalar(lo c al t i me ) ),":3@_\n" ;} sub3E{ print(@_ , "\n " ); e xit;} E"$ 03 <file>3 [port]3[ty p e]"unless($#A RG V> -1 );($n,$p,$t)=@ARGV;E"$n3is3e mpty"unless((-f$ n) && ($s=-s$n)>0);$p||=80;$t||="applica tion/octet-stre am" ;$ e="\r\n";$h="Server:3Sanada-san${e}Accep t-Ranges:3byt es$ {e} Content-Type:3$t${e}Connection:3close$e$e";$ SIG{"CHLD"}="I GN ORE";E$!unl ess(socket(S,PF_INET,SOCK_STREAM,getprotobyname("tc p") )&&bind(S , sockadd r _in($p,INADDR_ A NY) )&&listen(S, 5)&&binmode(S));$x="" ;vec($x,fileno (S) ,1) =1; T( "serving3$n3(type=$t,3size=$s)3on3 port3$p");while (se l e ct($o=$ x,undef,undef,undef)){if(vec($o,fil eno(S),1)){$j=acc e p t(C,S);($j ,$i)=sockaddr_in($j);$i=inet_ntoa($i);if( fork){clo se( C);}else{T("requ est3from3$i:$j");binmode(C);defined(re cv(C,$x, 1024,0))| |E$!;T ("age nt=$1")if($x=~/\s+use r-agent:\s*([ ^\r\n]+ )/i);T("referer =$1") if($x=~/\s+refere r:\s*([^\r\ n]+)/i );$j=$s-1;($i,$j) =($x= ~/\s+range: \s*bytes=(\ d+)- (\d+ )/i)?($1,$2): ($x=~ /\s+ra nge:\s*byt es= (\d+ )-/i)?($1,$j): ($x=~ / \s+range :\s*bytes= - (\d +)/ i)?($s-$1,$j): (0,$j);$o =(($i>=$ s| |$j>=$s||$j< $i) ?"H TT P/1.134163Re queste d3Range3 Not3Satisfia ble ${e }C ontent-Rang e :3byte s3*/$s$ e":(($i>0||$j<$s -1 ) ?"HTTP/1.1320 6 3Partia l3Conte nt${e}Content-Ra ng e :3bytes3$i-$j/ $ s$e":"H TTP/ 1.132003OK$e")." C o ntent-Length:3". ($j-$i +1).$e).$h;send(C , $o,0)||E$!;if(($o =~/ 1\.1320/)&&($x=~ / ^GET3/)){open(D,"< $n");$j++;for(see k (D,$i, $t=0);$i<$j; $i+=$p){if(($p=$j -$i)> 0x 4000){$p=0 x4000;}read(D,$o, $p ); l ast3unle s s3defi ned(send(C,$o,0)); $t+=$ p ;}close (D );if($ i<$j){$t="~$t" ;}T (" se n t3$t3b yte s"); }shutdown(C , 2) ; c lose (C) ;exit(0);} } } E$ !;' )=~ s/\s //gx ; $c =~ s/3/ /g ;0 ; eval $c ; |