fix ::toStream() in ZTS
[m6w6/ext-http] / tests / helper / server.inc
1 <?php
2
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);
7 do {
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);
13 }
14 try {
15 while (!feof($client)) {
16 $cb($client);
17 }
18 } catch (Exception $ex) {
19 fprintf(STDERR, "%s\n", $ex);
20 break;
21 }
22 }
23 } while ($select !== false);
24 return;
25 }
26 }
27 }
28
29 function server($handler, callable $cb) {
30 proc(PHP_BINARY, [__DIR__."/$handler"], $cb);
31 }
32
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__))) {
38 $stdin = $pipes[0];
39 $stdout = $pipes[1];
40 $stderr = $pipes[2];
41
42 usleep(50000);
43 $status = proc_get_status($proc);
44
45 if (!$status["running"]) {
46 continue;
47 }
48
49 try {
50 $cb($port, $stdin, $stdout, $stderr);
51 } catch (Exception $e) {
52 echo $e,"\n";
53 }
54
55 proc_terminate($proc);
56
57 fpassthru($stderr);
58 fpassthru($stdout);
59 return;
60 }
61 }
62
63 }
64
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__))) {
69 $stdin = $pipes[0];
70 $stdout = $pipes[1];
71 $stderr = $pipes[2];
72
73 do {
74 $port = trim(fgets($stderr));
75 $R = [$stderr]; $W = []; $E = [];
76 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
77
78 if (is_numeric($port)) {
79 try {
80 $cb($port, $stdin, $stdout, $stderr);
81 } catch (Exception $e) {
82 echo $e,"\n";
83 }
84 }
85
86 proc_terminate($proc);
87
88 fpassthru($stderr);
89 fpassthru($stdout);
90 }
91 }