Skip to content

Instantly share code, notes, and snippets.

@DivinityArcane
Created February 5, 2013 07:01
Show Gist options
  • Save DivinityArcane/4712758 to your computer and use it in GitHub Desktop.
Save DivinityArcane/4712758 to your computer and use it in GitHub Desktop.
For lulz and testing out crap. :D
<?php
class httpd
{
private $sock = null;
private function http_response($client, $code, $err)
{
socket_write($client, "HTTP/1.1 $code $err\r\nContent-type: text/plain\r\n\r\n");
}
private function OK($c){$this->http_response($c,200,'OK');}
private function NO($c){$this->http_response($c,403,'FORBIDDEN');}
public function __construct($addr, $port)
{
$this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($this->sock, $addr, $port) or die('Could not bind to address');
socket_listen($this->sock);
for(;;)
{
$client = socket_accept($this->sock);
$input = socket_read($client, 1024);
if (!strstr($input, "\r\n")) $this->NO($client);
else
{
$data = explode("\r\n", $input);
$head = $data[0];
if (!stristr($head, ' ')) $this->NO($client);
else
{
$req = explode(' ', $head);
if (count($req) != 3) $this->NO($client);
else
{
if (strtoupper($req[0]) != 'GET' || strtoupper($req[2]) != 'HTTP/1.1') $this->NO($client);
else
{
list($host, $port) = array('', 0);
socket_getpeername($client, $host, $port);
printf('%s:%d requested page: %s'.chr(10), $host, $port, $req[1]);
// Do shit
$this->OK($client);
}
}
}
}
socket_close($client);
}
socket_close($this->sock);
}
}
new httpd('0.0.0.0', 80);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment