reduce count of online tests
[m6w6/ext-http] / tests / 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 while (!feof($client)) {
15 $cb($client);
16 }
17 }
18 } while ($select !== false);
19 return;
20 }
21 }
22 }
23
24 function server($file, callable $cb) {
25 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
26 if (($proc = proc_open(PHP_BINARY . " $file", $spec, $pipes, __DIR__))) {
27 $port = trim(fgets($pipes[2]));
28
29 $cb($port, $stdin = $pipes[0], $stdout = $pipes[1], $stderr = $pipes[2]);
30
31 proc_terminate($proc);
32
33 fpassthru($stderr);
34 fpassthru($stdout);
35 }
36 }