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