mainthttpd.php 843 Bytes
Newer Older
cobra's avatar
cobra committed
1 2 3
#!/usr/bin/env php
<?php

4
$str = file_get_contents('503.html');
cobra's avatar
cobra committed
5 6 7 8 9 10 11 12 13 14 15
$port = 80;

echo 'MaintHTTPd starting up on port ' . $port . '...' . "\n";
echo 'Reponse string is: ' . $str . "\n";

$socket = stream_socket_server("tcp://0.0.0.0:$port", $errno, $errstr);
if (!$socket)
    die($errno . ': ' . $errstr . ' @' . __LINE__);
else
    echo 'Socket opened, listening for requests.' . "\n\n";
    
16
$strlen = strlen($str);
cobra's avatar
cobra committed
17 18 19 20 21 22
$start = time();
$i = 0;
while (true)
{
    if (is_resource($conn = @stream_socket_accept($socket)))
    {
23
        fwrite($conn, "HTTP/1.1 503 Service unavailable\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: " . $strlen . "\r\n\r\n" . $str . "\r\n\r\n");
cobra's avatar
cobra committed
24 25 26 27 28 29
        fclose($conn);
        echo "\r" . 'Served ' . ++$i . ' requests. (' . sprintf('%.2f', ($i/(time() - $start))) . '/s)   ';
    }
}

fclose($socket);