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