Back to index

php5  5.3.10
Functions
server.inc File Reference

Go to the source code of this file.

Functions

 http_server_skipif ($socket_string)
 http_server ($socket_string, array $files, &$output=null)
 http_server_kill ($pid)

Function Documentation

http_server ( socket_string,
array $  files,
&$  output = null 
)

Definition at line 16 of file server.inc.

                                                                    {

       pcntl_alarm(60);

       $server = stream_socket_server($socket_string, $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;
       }

       foreach($files as $file) {

              $sock = stream_socket_accept($server);
              if (!$sock) {
                     exit(1);
              }

              // read headers

              $content_length = 0;

              stream_set_blocking($sock, 0);
              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 === b'') {
                            fwrite($output, b"\r\n");
                            break;
                     } else if ($line !== false) {
                            fwrite($output, b"$line\r\n");

                            if (preg_match(b'#^Content-Length\s*:\s*([[:digit:]]+)\s*$#i', $line, $matches)) {
                                   $content_length = (int) $matches[1];
                            }
                     }
              }
              stream_set_blocking($sock, 1);

              // 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);
}
http_server_kill ( pid)

Definition at line 87 of file server.inc.

                                {
       posix_kill($pid, SIGTERM);
       pcntl_waitpid($pid, $status);
}
http_server_skipif ( socket_string)

Definition at line 3 of file server.inc.

                                            {

       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');
}