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