Skip to content

Instantly share code, notes, and snippets.

@hanhan1978
Created June 7, 2017 16:12
Show Gist options
  • Save hanhan1978/243524900e90206ed214f13e092f9136 to your computer and use it in GitHub Desktop.
Save hanhan1978/243524900e90206ed214f13e092f9136 to your computer and use it in GitHub Desktop.
<?php
$sock = @socket_create_listen(8091);
if(!$sock){die("port in use\n");}
$pids=[];
for($i=0; $i < 10 ; $i++){
$pid = pcntl_fork();
if($pid){
$pids[] = $pid;
}else{
response($sock);
}
}
foreach ($pids as $pid) {
pcntl_waitpid($pid, $status);
}
function response($sock)
{
$client_sock = socket_accept($sock);
while (true) {
$buf = socket_read($client_sock, 1024);
if (preg_match('/GET ([^ ]+)/', $buf, $m) && is_file('./htdocs/' . $m[1])) {
$content = file_get_contents('./htdocs/' . $m[1]);
$res = "HTTP/1.1 200 OK\nContent-Type: text/html\nCache-Control: max-age=8640000\nAccept-Ranges: bytes\nContent-Length: 197\nServer: HTTPServer/0.1\n\n";
socket_write($client_sock, $res . $content);
}
socket_close($client_sock);
$client_sock = socket_accept($sock);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment