3 function serve(callable $cb) {
4 foreach (range(8000, 9000) as $port) {
5 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
6 fprintf(STDERR, "%s\n", $port);
8 $R = [$server]; $W = []; $E = [];
9 $select = stream_select($R, $E, $E, 0, 10000);
10 if ($select && ($client = stream_socket_accept($server, 1))) {
11 if (getenv("PHP_HTTP_TEST_SSL")) {
12 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
15 while (!feof($client)) {
18 } catch (Exception $ex) {
19 fprintf(STDERR, "%s\n", $ex);
23 } while ($select !== false);
29 function server($handler, callable $cb) {
30 proc(PHP_BINARY, [__DIR__."/$handler"], $cb);
33 function nghttpd(callable $cb) {
34 $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
35 foreach (range(8000, 9000) as $port) {
36 $comm = "exec nghttpd -d html $port http2.key http2.crt";
37 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
43 $status = proc_get_status($proc);
45 if (!$status["running"]) {
50 $cb($port, $stdin, $stdout, $stderr);
51 } catch (Exception $e) {
55 proc_terminate($proc);
65 function proc($bin, $args, callable $cb) {
66 $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
67 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
68 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
74 $port = trim(fgets($stderr));
75 $R = [$stderr]; $W = []; $E = [];
76 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
78 if (is_numeric($port)) {
80 $cb($port, $stdin, $stdout, $stderr);
81 } catch (Exception $e) {
86 proc_terminate($proc);