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 /* ignore disconnect */
20 if ($ex->getMessage() !== "Empty message received from stream") {
21 fprintf(STDERR, "%s\n", $ex);
26 } while ($select !== false);
32 function server($handler, callable $cb) {
33 proc(PHP_BINARY, [__DIR__."/$handler"], $cb);
36 function nghttpd(callable $cb) {
37 $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
38 foreach (range(8000, 9000) as $port) {
39 $comm = "exec nghttpd -d html $port http2.key http2.crt";
40 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
46 $status = proc_get_status($proc);
48 if (!$status["running"]) {
53 $cb($port, $stdin, $stdout, $stderr);
54 } catch (Exception $e) {
58 proc_terminate($proc);
68 function proc($bin, $args, callable $cb) {
69 $spec = [["pipe","r"], ["pipe","w"], ["pipe","w"]];
70 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
71 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
77 $port = trim(fgets($stderr));
78 $R = [$stderr]; $W = []; $E = [];
79 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
81 if (is_numeric($port)) {
83 $cb($port, $stdin, $stdout, $stderr);
84 } catch (Exception $e) {
89 proc_terminate($proc);