lingering TSRM macros
[m6w6/ext-http] / tests / helper / server.inc
1 <?php
2
3 $php = getenv('TEST_PHP_EXECUTABLE');
4 if ($php) {
5 define('PHP_BIN', $php);
6 } else if (defined('PHP_BINARY')) {
7 define('PHP_BIN', PHP_BINARY);
8 } else {
9 // PHP-5.3
10 define("PHP_BIN", PHP_BINDIR.DIRECTORY_SEPARATOR."php");
11 }
12
13 foreach (array("raphf", "propro", "http") as $ext) {
14 if (!extension_loaded($ext)) {
15 switch (PHP_SHLIB_SUFFIX) {
16 case "dll":
17 dl("php_$ext.dll");
18 break;
19 default:
20 dl($ext .".". PHP_SHLIB_SUFFIX);
21 }
22 }
23 }
24
25 function serve($cb) {
26 /* stream_socket_server() automatically sets SO_REUSEADDR,
27 * which is, well, bad if the tests are run in parallel
28 */
29 $offset = rand(0,2000);
30 foreach (range(8000+$offset, 9000+$offset) as $port) {
31 if (($server = @stream_socket_server("tcp://localhost:$port"))) {
32 fprintf(STDERR, "%s\n", $port);
33 do {
34 $R = array($server); $W = array(); $E = array();
35 $select = stream_select($R, $E, $E, 0, 10000);
36 if ($select && ($client = stream_socket_accept($server, 1))) {
37 if (getenv("PHP_HTTP_TEST_SSL")) {
38 stream_socket_enable_crypto($client, true, STREAM_CRYPTO_METHOD_SSLv23_SERVER);
39 }
40 try {
41 while (!feof($client)) {
42 $cb($client);
43 }
44 } catch (Exception $ex) {
45 /* ignore disconnect */
46 if ($ex->getMessage() !== "Empty message received from stream") {
47 fprintf(STDERR, "%s\n", $ex);
48 }
49 break;
50 }
51 }
52 } while ($select !== false);
53 return;
54 }
55 }
56 }
57
58 function server($handler, $cb) {
59 $args = explode(' ', getenv('TEST_PHP_ARGS'));
60 $args[] = __DIR__."/$handler";
61 foreach ($args as $k => $v) {
62 if (!$v) unset($args[$k]);
63 }
64 proc(PHP_BIN, $args, $cb);
65 }
66
67 function nghttpd($cb) {
68 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
69 $offset = rand(0,2000);
70 foreach (range(8000+$offset, 9000+$offset) as $port) {
71 $comm = "exec nghttpd -d html $port http2.key http2.crt";
72 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
73 $stdin = $pipes[0];
74 $stdout = $pipes[1];
75 $stderr = $pipes[2];
76
77 usleep(50000);
78 $status = proc_get_status($proc);
79
80 if (!$status["running"]) {
81 continue;
82 }
83
84 try {
85 $cb($port, $stdin, $stdout, $stderr);
86 } catch (Exception $e) {
87 echo $e,"\n";
88 }
89
90 proc_terminate($proc);
91
92 fpassthru($stderr);
93 fpassthru($stdout);
94 return;
95 }
96 }
97
98 }
99
100 function proc($bin, $args, $cb) {
101 $spec = array(array("pipe","r"), array("pipe","w"), array("pipe","w"));
102 $comm = escapeshellcmd($bin) . " ". implode(" ", array_map("escapeshellarg", $args));
103 if (($proc = proc_open($comm, $spec, $pipes, __DIR__))) {
104 $stdin = $pipes[0];
105 $stdout = $pipes[1];
106 $stderr = $pipes[2];
107
108 do {
109 $port = trim(fgets($stderr));
110 $R = array($stderr); $W = array(); $E = array();
111 } while (is_numeric($port) && stream_select($R, $W, $E, 0, 10000));
112
113 if (is_numeric($port)) {
114 try {
115 $cb($port, $stdin, $stdout, $stderr);
116 } catch (Exception $e) {
117 echo $e,"\n";
118 }
119 }
120
121 proc_terminate($proc);
122
123 fpassthru($stderr);
124 fpassthru($stdout);
125 }
126 }