1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
<?php declare(strict_types=1);
function http_server_skipif() {
if (!function_exists('pcntl_fork')) die('skip pcntl_fork() not available');
if (!function_exists('posix_kill')) die('skip posix_kill() not available');
if (!stream_socket_server('tcp://localhost:0')) die('skip stream_socket_server() failed');
}
function http_server_init(&$output = null) {
pcntl_alarm(60);
$server = stream_socket_server('tcp://localhost:0', $errno, $errstr);
if (!$server) {
return false;
}
if ($output === null) {
$output = tmpfile();
if ($output === false) {
return false;
}
}
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
return [
'pid' => $pid,
'uri' => 'http://' . stream_socket_get_name($server, false),
];
}
return $server;
}
/* Minimal HTTP server with predefined responses.
*
* $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
* $files is an iterable of files or callable generator yielding files.
* containing N responses for N expected requests. Server dies after N requests.
* $output is a stream on which everything sent by clients is written to
*/
function http_server($files, &$output = null) {
if (!is_resource($server = http_server_init($output))) {
return $server;
}
if (is_callable($files)) {
$files = $files($server);
}
foreach($files as $file) {
$sock = stream_socket_accept($server);
if (!$sock) {
exit(1);
}
// read headers
$content_length = 0;
stream_set_blocking($sock, false);
while (!feof($sock)) {
list($r, $w, $e) = array(array($sock), null, null);
if (!stream_select($r, $w, $e, 1)) continue;
$line = stream_get_line($sock, 8192, "\r\n");
if ($line === '') {
fwrite($output, "\r\n");
break;
} else if ($line !== false) {
fwrite($output, "$line\r\n");
if (preg_match('#^Content-Length\s*:\s*([[:digit:]]+)\s*$#i', $line, $matches)) {
$content_length = (int) $matches[1];
}
}
}
stream_set_blocking($sock, true);
// read content
if ($content_length > 0) {
stream_copy_to_stream($sock, $output, $content_length);
}
// send response
$fd = fopen($file, 'rb');
stream_copy_to_stream($fd, $sock);
fclose($sock);
}
exit(0);
}
function http_server_sleep($micro_seconds = 500000)
{
if (!is_resource($server = http_server_init($output))) {
return $server;
}
$sock = stream_socket_accept($server);
if (!$sock) {
exit(1);
}
usleep($micro_seconds);
fclose($sock);
exit(0);
}
function http_server_kill(int $pid) {
posix_kill($pid, SIGTERM);
pcntl_waitpid($pid, $status);
}
|