4 if (!defined("PHP_BINARY")) {
5 define("PHP_BINARY", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
9 foreach (range(8000, 9000) as $port) {
10 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
11 fprintf(STDERR, "%s\n", $port);
13 $R = array($server); $W = array(); $E = array();
14 $select = stream_select($R, $E, $E, 0, 10000);
15 if ($select && ($client = stream_socket_accept($server, 1))) {
16 if (getenv("PHP_HTTP_TEST_SSL")) {
17 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
20 while (!feof($client)) {
23 } catch (Exception $ex) {
24 /* ignore disconnect */
25 if ($ex->getMessage() !== "Empty message received from stream") {
26 fprintf(STDERR, "%s\n", $ex);
31 } while ($select !== false);
37 function server($handler, $cb) {
38 proc(PHP_BINARY, array(__DIR__."/$handler"), $cb);
41 function nghttpd($cb) {
42 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
43 foreach (range(8000, 9000) as $port) {
44 $comm = "exec nghttpd -d html $port http2.key http2.crt";
45 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
51 $status = proc_get_status($proc);
53 if (!$status["running"]) {
58 $cb($port, $stdin, $stdout, $stderr);
59 } catch (Exception $e) {
63 proc_terminate($proc);
73 function proc($bin, $args, $cb) {
74 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
75 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
76 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
82 $port = trim(fgets($stderr));
83 $R = array($stderr); $W = array(); $E = array();
84 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
86 if (is_numeric($port)) {
88 $cb($port, $stdin, $stdout, $stderr);
89 } catch (Exception $e) {
94 proc_terminate($proc);