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
|
<?php
function http_server_skipif($socket_string) {
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($socket_string)) die('skip stream_socket_server() failed');
}
/* 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 array of 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($socket_string, array $files, &$output = null)
{
ini_set('default_socket_timeout', 5);
pcntl_alarm(10);
$server = stream_socket_server($socket_string, $errno, $errstr);
if (!$server) {
return false;
}
if ($output === null) {
if (false===($output = tmpfile())) {
return false;
}
}
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
return $pid;
}
foreach($files as $file) {
$sock = stream_socket_accept($server);
if (!$sock) {
exit(1);
}
// read headers
$content_length = 0;
while (false!==($line=trim(fgets($sock)))) {
fwrite($output, "$line\r\n");
if (''===$line) {
break;
} else {
if (preg_match('#^Content-Length:\s*([[:digit:]]+)\s*$#i', $line, $matches)) {
$content_length = (int) $matches[1];
}
}
}
// read content
if ($content_length > 0) {
$line = fread($sock, $content_length);
fwrite($output, "$line\r\n");
}
// send response
fputs($sock, $file);
fclose($sock);
}
exit(0);
}
function http_server_kill($pid) {
posix_kill($pid, SIGTERM);
pcntl_waitpid($pid, $status);
}
?>
|