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