Skip to content

Instantly share code, notes, and snippets.

@hanhan1978
Created June 7, 2017 15:55
Show Gist options
  • Save hanhan1978/a6d86f4ce45a0da787ed7be4f1c6337f to your computer and use it in GitHub Desktop.
Save hanhan1978/a6d86f4ce45a0da787ed7be4f1c6337f to your computer and use it in GitHub Desktop.
<?php
$sock = @socket_create_listen(8080);
if(!$sock){die("port in use\n");}
$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