yep, moved there. ugh
[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 while (!feof($client)) {
15 $cb($client);
16 }
17 }
18 } while ($select !== false);
19 return;
20 }
21 }
22 }
23
24 function server($handler, callable $cb) {
25 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
26 $comm = sprintf("%s %s/%s", PHP_BINARY, __DIR__, $handler);
27 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
28 $port = trim(fgets($pipes[2]));
29
30 try {
31 $cb($port, $stdin = $pipes[0], $stdout = $pipes[1], $stderr = $pipes[2]);
32 } catch (Exception $e) {
33 echo $e,"\n";
34 }
35
36 proc_terminate($proc);
37
38 fpassthru($stderr);
39 fpassthru($stdout);
40 }
41 }