Back to index

php5  5.3.10
server.inc
Go to the documentation of this file.
00001 <?php
00002 
00003 function http_server_skipif($socket_string) {
00004 
00005        if (!function_exists('pcntl_fork')) die('skip pcntl_fork() not available');
00006        if (!function_exists('posix_kill')) die('skip posix_kill() not available');
00007        if (!stream_socket_server($socket_string)) die('skip stream_socket_server() failed');
00008 }
00009 
00010 /* Minimal HTTP server with predefined responses.
00011  *
00012  * $socket_string is the socket to create and listen on (e.g. tcp://127.0.0.1:1234)
00013  * $files is an array of files containing N responses for N expected requests. Server dies after N requests.
00014  * $output is a stream on which everything sent by clients is written to
00015  */
00016 function http_server($socket_string, array $files, &$output = null) {
00017 
00018        pcntl_alarm(60);
00019 
00020        $server = stream_socket_server($socket_string, $errno, $errstr);
00021        if (!$server) {
00022               return false;
00023        }
00024 
00025        if ($output === null) {
00026               $output = tmpfile();
00027               if ($output === false) {
00028                      return false;
00029               }
00030        }
00031 
00032        $pid = pcntl_fork();
00033        if ($pid == -1) {
00034               die('could not fork');
00035        } else if ($pid) {
00036               return $pid;
00037        }
00038 
00039        foreach($files as $file) {
00040 
00041               $sock = stream_socket_accept($server);
00042               if (!$sock) {
00043                      exit(1);
00044               }
00045 
00046               // read headers
00047 
00048               $content_length = 0;
00049 
00050               stream_set_blocking($sock, 0);
00051               while (!feof($sock)) {
00052 
00053                      list($r, $w, $e) = array(array($sock), null, null);
00054                      if (!stream_select($r, $w, $e, 1)) continue;
00055 
00056                      $line = stream_get_line($sock, 8192, "\r\n");
00057                      if ($line === b'') {
00058                             fwrite($output, b"\r\n");
00059                             break;
00060                      } else if ($line !== false) {
00061                             fwrite($output, b"$line\r\n");
00062 
00063                             if (preg_match(b'#^Content-Length\s*:\s*([[:digit:]]+)\s*$#i', $line, $matches)) {
00064                                    $content_length = (int) $matches[1];
00065                             }
00066                      }
00067               }
00068               stream_set_blocking($sock, 1);
00069 
00070               // read content
00071 
00072               if ($content_length > 0) {
00073                      stream_copy_to_stream($sock, $output, $content_length);
00074               }
00075 
00076               // send response
00077 
00078               $fd = fopen($file, 'rb');
00079               stream_copy_to_stream($fd, $sock);
00080 
00081               fclose($sock);
00082        }
00083 
00084        exit(0);
00085 }
00086 
00087 function http_server_kill($pid) {
00088        posix_kill($pid, SIGTERM);
00089        pcntl_waitpid($pid, $status);
00090 }
00091 
00092 ?>