Codice: Seleziona tutto
<?php
$documentroot="/var/www/htdocs";
$port=8080;
if(!$socket = @socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) die();
if(!@socket_bind($socket,'127.0.0.1',$port)) die();
if(!@socket_listen($socket))die();
$client = socket_accept($socket);
$get=null;
$i=0;
$headers=array();
while(true){
if(($stringa=@socket_read($client,1000000,PHP_NORMAL_READ))===FALSE)die();
if($stringa==="\n")continue;
$stringa=trim($stringa,"\n\r");
if(is_null($get)){
$get=substr($stringa,0,3);
if($get!="GET"){
socket_write($client,"501 Method Not Implemented\n");
die();
}else{
$stringa=explode(" ",$stringa);
$get=$stringa[1];
}
}else{
if($stringa === "") break;
$headers[]=$stringa;
}
}
$file="$documentroot/$get";
if(is_dir($file))$file.="/index.html";
if(is_file($file)){
socket_write($client,file_get_contents($file));
}else{
socket_write($client,"404 Not Found\n");
}
socket_shutdown($client);
socket_close($client);
socket_shutdown($socket);
socket_close($socket);
?>
edit:
limiti:
accetta una singola connessione. Alla connessione non effettua il fork del processo ma soddisfa la richiesta e poi muore
accetta solo metodo GET (no HEAD e POST)